mirror of
https://github.com/standardebooks/web.git
synced 2025-07-07 23:30:35 -04:00
Only write the OPDS feed and updated timestamp if the feeds actually changed
This commit is contained in:
parent
1f6170d23a
commit
6252cff1e5
2 changed files with 30 additions and 10 deletions
|
@ -19,6 +19,20 @@ class OpdsFeed{
|
|||
$this->IsCrawlable = $isCrawlable;
|
||||
}
|
||||
|
||||
private function Sha1Entries(string $xmlString): string{
|
||||
$xml = new SimpleXMLElement(str_replace('xmlns=', 'ns=', $xmlString));
|
||||
$xml->registerXPathNamespace('dc', 'http://purl.org/dc/elements/1.1/');
|
||||
$xml->registerXPathNamespace('schema', 'http://schema.org/');
|
||||
$entries = $xml->xpath('/feed/entry') ?? [];
|
||||
|
||||
$output = '';
|
||||
foreach($entries as $entry){
|
||||
$output .= $entry->asXml();
|
||||
}
|
||||
|
||||
return sha1(preg_replace('/\s/ius', '', $output));
|
||||
}
|
||||
|
||||
public function Save(string $filename): void{
|
||||
$updatedTimestamp = gmdate('Y-m-d\TH:i:s\Z');
|
||||
|
||||
|
@ -28,16 +42,22 @@ class OpdsFeed{
|
|||
file_put_contents($tempFilename, $feed);
|
||||
exec('se clean ' . escapeshellarg($tempFilename));
|
||||
|
||||
rename($tempFilename, $filename);
|
||||
// Did we actually update the feed? If so, write to file and update the index
|
||||
if(!is_file($filename)){
|
||||
// File doesn't exist, write it out
|
||||
rename($tempFilename, $filename);
|
||||
}
|
||||
elseif($this->Sha1Entries($feed) != $this->Sha1Entries(file_get_contents($filename))){
|
||||
// Files don't match, save the file and update the index feed with the last updated timestamp
|
||||
$xml = new SimpleXMLElement(str_replace('xmlns=', 'ns=', file_get_contents(WEB_ROOT . '/opds/index.xml')));
|
||||
$xml->registerXPathNamespace('dc', 'http://purl.org/dc/elements/1.1/');
|
||||
$xml->registerXPathNamespace('schema', 'http://schema.org/');
|
||||
|
||||
// Update the index feed with the last updated timestamp
|
||||
$xml = new SimpleXMLElement(str_replace('xmlns=', 'ns=', file_get_contents(WEB_ROOT . '/opds/index.xml')));
|
||||
$xml->registerXPathNamespace('dc', 'http://purl.org/dc/elements/1.1/');
|
||||
$xml->registerXPathNamespace('schema', 'http://schema.org/');
|
||||
$feedEntry = ($xml->xpath('/feed/entry[id="' . $this->Id . '"]/updated') ?? [])[0];
|
||||
$feedEntry[0] = $updatedTimestamp;
|
||||
file_put_contents(WEB_ROOT . '/opds/index.xml', str_replace(" ns=", " xmlns=", $xml->asXml() ?? ''));
|
||||
|
||||
$feedEntry = ($xml->xpath('/feed/entry[id="' . $this->Id . '"]/updated') ?? [])[0];
|
||||
$feedEntry[0] = $updatedTimestamp;
|
||||
file_put_contents(WEB_ROOT . '/opds/index.xml', str_replace(" ns=", " xmlns=", $xml->asXml() ?? ''));
|
||||
exec('se clean ' . WEB_ROOT . '/opds/index.xml');
|
||||
rename($tempFilename, $filename);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -227,7 +227,7 @@ if [ "${verbose}" = "true" ]; then
|
|||
printf "Rebuilding OPDS catalog ... "
|
||||
fi
|
||||
|
||||
bash -c "php \"${scriptsDir}\"/generate-opds.php --webroot \"${webRoot}\" --weburl \"${webUrl}\" > \"${webRoot}\"/www/opds/all.xml"
|
||||
bash -c "php \"${scriptsDir}\"/generate-opds.php --webroot \"${webRoot}\" --weburl \"${webUrl}\""
|
||||
|
||||
if [ "${verbose}" = "true" ]; then
|
||||
printf "Done.\n"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue