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> <name>OPDS catalog</name>
<description>Personal OPDS catalog</description> <description>Personal OPDS catalog</description>
<licence>AGPL</licence> <licence>AGPL</licence>
<version>0.5</version> <version>0.5.1</version>
<author>Frank de Lange</author> <author>Frank de Lange</author>
<requiremin>7.0</requiremin> <requiremin>7.0</requiremin>
<shipped>true</shipped> <shipped>true</shipped>

View file

@ -27,45 +27,55 @@ class Isbn
/** /**
* @brief try to find a valid ISBN in the given text, using a cascade of * @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 * @param string $text text to search through
* @return string $hit ISBN on success, false otherwise * @return string $hit ISBN on success, false otherwise
*/ */
public static function scan($text) { public static function scan($text) {
if ($hits = preg_grep('/isbn/i',$text)) { $match = array();
foreach ($hits as $hit) { foreach($text as $line) {
$hit = preg_replace('/.*ISBN(?:[ -]?1[03]:)?\s*([xX0-9-]{10,17}).*/i','$1',$hit,1); /* generic ISBN 10/13 pattern */
//$hit = preg_replace('/isbn([ -]\(?1[03]\)?)?/i','',$hit); if(preg_match_all('/ISBN(?:[ -]?[1[03]]?)?:?\s*((97[89])?[X0-9-]{10,14})/i',$line,$match)) {
$hit = preg_replace('/[^0-9X]/i','',$hit); foreach($match[1] as $hit) {
if(self::validate($hit)) { $hit = preg_replace('/[^0-9X]/i','',$hit);
return $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(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;
}
} }
} }
} }
/* 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-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;
}
}
}
return false; return false;
} }
@ -115,22 +125,28 @@ class Isbn
* @return bool true if valid ISBN-10 * @return bool true if valid ISBN-10
*/ */
static function isIsbn10 ($isbn) { static function isIsbn10 ($isbn) {
$checksum = 0; $len = strlen($isbn);
for ($i = 0; $i < 10; ++$i) { if ($len == 10) {
if (!isset($isbn{$i})) { //Calculate check digit
return false; $check = 0;
} for ($i = 0; $i < 9; $i++) {
if ('X' === $isbn{$i}) { if ($isbn[$i] === "X") {
$digit = 10; $check += 10 * intval(10 - $i);
} elseif (ctype_digit($isbn{$i})) { } else {
$digit = $isbn{$i}; $check += intval($isbn[$i]) * intval(10 - $i);
} else { }
return false; }
} $check = 11 - $check % 11;
$checksum += $digit * intval(10 - $i); 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 * @return bool true if valid ISBN-13
*/ */
static function isIsbn13 ($isbn) { static function isIsbn13 ($isbn) {
$checksum = 0; $len = strlen($isbn);
for ($i = 0; $i < 13; $i +=2) { if ($len == 13) {
$checksum += $isbn{$i}; //Calculate check digit
} $check = 0;
for ($i = 1; $i < 12; $i +=2) { for ($i = 0; $i < 12; $i += 2) {
$checksum += $isbn{$i} * 3; $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 * @return array of metadata
*/ */
public static function get($id) { 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'])) { if(isset($meta['rescan'])) {
$meta['rescan'] = null; $meta['rescan'] = null;
} }