From 6252cff1e5f674dc86fa231e92beec9a3d589c5f Mon Sep 17 00:00:00 2001 From: Alex Cabal Date: Wed, 24 Jun 2020 21:28:15 -0500 Subject: [PATCH] Only write the OPDS feed and updated timestamp if the feeds actually changed --- lib/OpdsFeed.php | 38 ++++++++++++++++++++++++++++--------- scripts/deploy-ebook-to-www | 2 +- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/lib/OpdsFeed.php b/lib/OpdsFeed.php index df1954a1..05193dce 100644 --- a/lib/OpdsFeed.php +++ b/lib/OpdsFeed.php @@ -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); + } } } diff --git a/scripts/deploy-ebook-to-www b/scripts/deploy-ebook-to-www index 1178af4b..9cba8e56 100755 --- a/scripts/deploy-ebook-to-www +++ b/scripts/deploy-ebook-to-www @@ -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"