From cae117951b35f4ef6511342208c4082e34566db1 Mon Sep 17 00:00:00 2001 From: Alex Cabal Date: Wed, 24 Jun 2020 19:12:38 -0500 Subject: [PATCH] Refactor generate-opds script and create a new 'newest 30' OPDS feed --- lib/OpdsFeed.php | 42 +++++++++++++++++++++++++++++ scripts/generate-opds.php | 57 +++++++++++---------------------------- 2 files changed, 57 insertions(+), 42 deletions(-) create mode 100644 lib/OpdsFeed.php diff --git a/lib/OpdsFeed.php b/lib/OpdsFeed.php new file mode 100644 index 00000000..0862085e --- /dev/null +++ b/lib/OpdsFeed.php @@ -0,0 +1,42 @@ +Url = $url; + $this->Id = $url; + $this->Title = $title; + $this->Ebooks = $ebooks; + $this->IsCrawlable = $isCrawlable; + } + + public function Save(string $filename): void{ + $updatedTimestamp = gmdate('Y-m-d\TH:i:s\Z'); + + $feed = Template::OpdsFeed(['id' => $this->Url, 'url' => $this->Url, 'title' => $this->Title, 'updatedTimestamp' => $updatedTimestamp, 'isCrawlable' => $this->IsCrawlable, 'entries' => $this->Ebooks]); + + $tempFilename = tempnam('/tmp/', 'se-opds-'); + file_put_contents($tempFilename, $feed); + exec('se clean ' . escapeshellarg($tempFilename)); + + rename($tempFilename, $filename); + + // 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())); + exec('se clean ' . WEB_ROOT . '/opds/index.xml'); + } +} diff --git a/scripts/generate-opds.php b/scripts/generate-opds.php index 0e9cdac4..b7e1a914 100755 --- a/scripts/generate-opds.php +++ b/scripts/generate-opds.php @@ -1,4 +1,8 @@ ModifiedTimestamp->format('Y-m-dTH:i:sZ') . ' ' . $ebook->Identifier] = $ebook; + $allEbooks[$ebook->ModifiedTimestamp->format('Y-m-d\TH:i:s\Z') . ' ' . $ebook->Identifier] = $ebook; + $newestEbooks[$ebook->Timestamp->format('Y-m-d\TH:i:s\Z') . ' ' . $ebook->Identifier] = $ebook; } -krsort($sortedContentFiles); +krsort($allEbooks); +$allFeed = new OpdsFeed(SITE_URL . '/opds/all', 'All Standard Ebooks', $allEbooks, true); +$allFeed->Save(WEB_ROOT . '/opds/all.xml'); -$url = SITE_URL . '/opds/all'; - -$feed = Template::OpdsFeed(['id' => $url, 'url' => $url, 'title' => 'All Standard Ebooks', 'updatedTimestamp' => $updatedTimestamp, 'isCrawlable' => true, 'entries' => $sortedContentFiles]); - -$tempFilename = tempnam('/tmp/', 'se-opds-'); - -file_put_contents($tempFilename, $feed); -exec('se clean ' . escapeshellarg($tempFilename)); - -// If the feed has changed compared to the version currently on disk, copy our new version over -// and update the updated timestamp in the master opds index. -try{ - if(filesize($webRoot . '/www/opds/all.xml') !== filesize($tempFilename)){ - $oldFeed = file_get_contents($webRoot . '/www/opds/all.xml'); - $newFeed = file_get_contents($tempFilename); - if($oldFeed != $newFeed){ - file_put_contents($webRoot . '/www/opds/all.xml', $newFeed); - - // Update the index feed with the last updated timestamp - $xml = new SimpleXMLElement(str_replace('xmlns=', 'ns=', file_get_contents($webRoot . '/www/opds/index.xml'))); - $xml->registerXPathNamespace('dc', 'http://purl.org/dc/elements/1.1/'); - $xml->registerXPathNamespace('schema', 'http://schema.org/'); - - $allUpdated = $xml->xpath('/feed/entry[id="https://standardebooks.org/opds/all"]/updated')[0]; - $allUpdated[0] = $updatedTimestamp; - file_put_contents($webRoot . '/www/opds/index.xml', str_replace(" ns=", " xmlns=", $xml->asXml())); - exec('se clean ' . escapeshellarg($webRoot) . '/www/opds/index.xml'); - } - } -} -catch(Exception $ex){ - rename($tempFilename, $webRoot . '/www/opds/all.xml'); -} - -unlink($tempFilename); +krsort($newestEbooks); +$newestEbooks = array_slice($newestEbooks, 0, 30); +$newestFeed = new OpdsFeed(SITE_URL . '/opds/newest', 'Newest 30 Standard Ebooks', $newestEbooks); +$newestFeed->Save(WEB_ROOT . '/opds/newest.xml'); ?>