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:
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
BIN
dist/files_opds-0.5.1.tar.gz
vendored
Normal file
Binary file not shown.
|
@ -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>
|
||||
|
|
|
@ -27,16 +27,17 @@ 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);
|
||||
$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;
|
||||
|
@ -44,11 +45,20 @@ class Isbn
|
|||
}
|
||||
}
|
||||
|
||||
/* 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);
|
||||
/* 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;
|
||||
}
|
||||
|
@ -56,15 +66,15 @@ class Isbn
|
|||
}
|
||||
|
||||
/* 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);
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
@ -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};
|
||||
$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 {
|
||||
return false;
|
||||
$check += intval($isbn[$i]) * intval(10 - $i);
|
||||
}
|
||||
$checksum += $digit * 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};
|
||||
$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) {
|
||||
$checksum += $isbn{$i} * 3;
|
||||
$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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue