1
0
Fork 0
mirror of https://github.com/Yetangitu/owncloud-apps.git synced 2025-10-02 14:49:17 +02:00

Fixed excessive rescans due to faulty time calculation, improved ISBN regexes (which can be improved further still)

This commit is contained in:
frankdelange 2014-12-21 23:22:44 +01:00
parent 5e385a7756
commit 84b7c25638
4 changed files with 81 additions and 55 deletions

BIN
dist/files_opds-0.5.1.tar.gz vendored Normal file

Binary file not shown.

View file

@ -4,7 +4,7 @@
<name>OPDS catalog</name>
<description>Personal OPDS catalog</description>
<licence>AGPL</licence>
<version>0.5</version>
<version>0.5.1</version>
<author>Frank de Lange</author>
<requiremin>7.0</requiremin>
<shipped>true</shipped>

View file

@ -27,41 +27,51 @@ class Isbn
/**
* @brief try to find a valid ISBN in the given text, using a cascade of
* regexps. Can be optimized.
* regexps.
*
* @param string $text text to search through
* @return string $hit ISBN on success, false otherwise
*/
public static function scan($text) {
if ($hits = preg_grep('/isbn/i',$text)) {
foreach ($hits as $hit) {
$hit = preg_replace('/.*ISBN(?:[ -]?1[03]:)?\s*([xX0-9-]{10,17}).*/i','$1',$hit,1);
//$hit = preg_replace('/isbn([ -]\(?1[03]\)?)?/i','',$hit);
$hit = preg_replace('/[^0-9X]/i','',$hit);
if(self::validate($hit)) {
return $hit;
$match = array();
foreach($text as $line) {
/* generic ISBN 10/13 pattern */
if(preg_match_all('/ISBN(?:[ -]?[1[03]]?)?:?\s*((97[89])?[X0-9-]{10,14})/i',$line,$match)) {
foreach($match[1] as $hit) {
$hit = preg_replace('/[^0-9X]/i','',$hit);
if(self::validate($hit)) {
return $hit;
}
}
}
}
/* single ISBN-13 targeted pattern */
if ($hits = preg_grep('/\d{3}[ -]?\d[ -]?\d{4}[ -]?\d{4}[ -]?\d/', $text)) {
foreach ($hits as $hit) {
$hit = preg_replace('/.*(\d{3}[ -]?\d[ -]?\d{4}[ -]?\d{4}[ -]?\d).*/','$1',$hit,1);
$hit = preg_replace('/[^0-9]/i','',$hit);
if(self::validate($hit)) {
return $hit;
/* single ISBN-13 targeted pattern (canonical format) */
if(preg_match_all('/(97[89][ -]?\d[ -]?\d{4}[ -]?\d{4}[ -]?\d)/',$line,$match)) {
foreach($match[1] as $hit) {
$hit = preg_replace('/[^0-9]/','',$hit);
if(self::validate($hit)) {
return $hit;
}
}
}
/* single ISBN-13 targeted pattern (free format) */
if(preg_match_all('/(9[\d -]{11,15}\d)/',$line,$match)) {
foreach($match[1] as $hit) {
$hit = preg_replace('/[^0-9]/','',$hit);
if(self::validate($hit)) {
return $hit;
}
}
}
}
/* single ISBN-10 targeted pattern */
if ($hits = preg_grep('/\d[\d -]{8,11}[\dX]/i', $text)) {
foreach ($hits as $hit) {
$hit = preg_replace('/.*(\d[\d -]{8,11}[\dX]).*/','$1',$hit,1);
$hit = preg_replace('/[^0-9X]/i','',$hit);
if(self::validate($hit)) {
return $hit;
/* single ISBN-10 targeted pattern */
if(preg_match_all('/(\d[\d -]{8,11}[\dX])/i',$line,$match)) {
foreach($match[1] as $hit) {
$hit = preg_replace('/[^0-9X]/i','',$hit);
if(self::validate($hit)) {
return $hit;
}
}
}
}
@ -115,22 +125,28 @@ class Isbn
* @return bool true if valid ISBN-10
*/
static function isIsbn10 ($isbn) {
$checksum = 0;
for ($i = 0; $i < 10; ++$i) {
if (!isset($isbn{$i})) {
return false;
}
if ('X' === $isbn{$i}) {
$digit = 10;
} elseif (ctype_digit($isbn{$i})) {
$digit = $isbn{$i};
} else {
return false;
}
$checksum += $digit * intval(10 - $i);
}
$len = strlen($isbn);
if ($len == 10) {
//Calculate check digit
$check = 0;
for ($i = 0; $i < 9; $i++) {
if ($isbn[$i] === "X") {
$check += 10 * intval(10 - $i);
} else {
$check += intval($isbn[$i]) * intval(10 - $i);
}
}
$check = 11 - $check % 11;
if ($check === 10) {
$check = 'X';
} elseif ($check === 11) {
$check = 0;
}
return 0 === $checkSum % 11 ? true : false;
return ($check == $isbn{$len - 1});
}
return false;
}
/**
@ -139,15 +155,25 @@ class Isbn
* @return bool true if valid ISBN-13
*/
static function isIsbn13 ($isbn) {
$checksum = 0;
for ($i = 0; $i < 13; $i +=2) {
$checksum += $isbn{$i};
}
for ($i = 1; $i < 12; $i +=2) {
$checksum += $isbn{$i} * 3;
}
$len = strlen($isbn);
if ($len == 13) {
//Calculate check digit
$check = 0;
for ($i = 0; $i < 12; $i += 2) {
$check += substr($isbn, $i, 1);
}
for ($i = 1; $i < 12; $i += 2) {
$check += 3 * substr($isbn, $i, 1);
}
$check = 10 - $check % 10;
if ($check === 10) {
$check = 0;
}
return 0 === $checkSum % 10 ? true : false;
return ($check == $isbn{$len - 1});
}
return false;
}
}

View file

@ -119,7 +119,7 @@ class Meta
* @return array of metadata
*/
public static function get($id) {
if (!($meta = self::load($id)) || (isset($meta['rescan']) && time() > $meta['rescan'])) {
if (!($meta = self::load($id)) || (isset($meta['rescan']) && time() > strtotime($meta['rescan']))) {
if(isset($meta['rescan'])) {
$meta['rescan'] = null;
}