Only write the OPDS feed and updated timestamp if the feeds actually changed

This commit is contained in:
Alex Cabal 2020-06-24 21:28:15 -05:00
parent 1f6170d23a
commit 6252cff1e5
2 changed files with 30 additions and 10 deletions

View file

@ -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,9 +42,13 @@ class OpdsFeed{
file_put_contents($tempFilename, $feed);
exec('se clean ' . escapeshellarg($tempFilename));
// 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);
// Update the index feed with the last updated timestamp
}
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/');
@ -38,6 +56,8 @@ class OpdsFeed{
$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);
}
}
}

View file

@ -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"