From 86f3adca36f0a0d5b86b9ada198b10fa0976cd55 Mon Sep 17 00:00:00 2001 From: Alex Cabal Date: Fri, 19 Jun 2020 16:20:40 -0500 Subject: [PATCH] Add search ability to OPDS feed --- config/apache/standardebooks.org.conf | 8 ++++-- config/apache/standardebooks.test.conf | 8 ++++-- lib/Contributor.php | 4 ++- lib/Ebook.php | 9 +++++- scripts/generate-opds.php | 1 + templates/OpdsEntry.php | 31 ++++++++++++++++++++ www/opds/search.php | 39 ++++++++++++++++++++++++++ 7 files changed, 94 insertions(+), 6 deletions(-) create mode 100644 templates/OpdsEntry.php create mode 100644 www/opds/search.php diff --git a/config/apache/standardebooks.org.conf b/config/apache/standardebooks.org.conf index 5f25ac53..76670114 100644 --- a/config/apache/standardebooks.org.conf +++ b/config/apache/standardebooks.org.conf @@ -159,9 +159,9 @@ Define domain standardebooks.org RewriteRule ^(.+)$ $1.php [QSA] # End PHP-FPM configuration - # Received: /filename and /filename.xml exists in filesystem; Result: rewrite to /filename.xml and end request + # Received: /filename and /filename.xml exists in filesystem; Result: rewrite to /filename.xml RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME}.xml -f - RewriteRule (.*) $1.xml [L] + RewriteRule (.*) $1.xml # Remove trailing slashes RewriteRule ^/(.+?)/$ /$1 [R=301,L] @@ -219,6 +219,10 @@ Define domain standardebooks.org RewriteCond %{REQUEST_FILENAME} !^/ebooks/.+?/dist/.+$ RewriteCond %{REQUEST_FILENAME} !^/ebooks/.+?/src/.+$ RewriteRule ^/ebooks/([^\.]+?)$ /ebooks/ebook.php?url-path=$1 + + # If we ask for /opds/all?query=xyz, rewrite that to the search page. + RewriteCond %{QUERY_STRING} ^query= + RewriteRule ^/opds/all.xml$ /opds/search.php [QSA] diff --git a/config/apache/standardebooks.test.conf b/config/apache/standardebooks.test.conf index 084d2601..69a3f364 100644 --- a/config/apache/standardebooks.test.conf +++ b/config/apache/standardebooks.test.conf @@ -158,9 +158,9 @@ Define domain standardebooks.test RewriteRule ^(.+)$ $1.php [QSA] # End PHP-FPM configuration - # Received: /filename and /filename.xml exists in filesystem; Result: rewrite to /filename.xml and end request + # Received: /filename and /filename.xml exists in filesystem; Result: rewrite to /filename.xml RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME}.xml -f - RewriteRule (.*) $1.xml [L] + RewriteRule (.*) $1.xml # Remove trailing slashes RewriteRule ^/(.+?)/$ /$1 [R=301,L] @@ -218,4 +218,8 @@ Define domain standardebooks.test RewriteCond %{REQUEST_FILENAME} !^/ebooks/.+?/dist/.+$ RewriteCond %{REQUEST_FILENAME} !^/ebooks/.+?/src/.+$ RewriteRule ^/ebooks/([^\.]+?)$ /ebooks/ebook.php?url-path=$1 + + # If we ask for /opds/all?query=xyz, rewrite that to the search page. + RewriteCond %{QUERY_STRING} ^query= + RewriteRule ^/opds/all.xml$ /opds/search.php [QSA] diff --git a/lib/Contributor.php b/lib/Contributor.php index 7f182b1d..3cd85215 100644 --- a/lib/Contributor.php +++ b/lib/Contributor.php @@ -6,13 +6,15 @@ class Contributor{ public $WikipediaUrl; public $MarcRole; public $FullName; + public $NacoafUrl; - public function __construct(string $name, string $sortName = null, string $fullName = null, string $wikipediaUrl = null, string $marcRole = null){ + public function __construct(string $name, string $sortName = null, string $fullName = null, string $wikipediaUrl = null, string $marcRole = null, string $nacoafUrl = null){ $this->Name = str_replace('\'', '’', $name); $this->UrlName = Formatter::MakeUrlSafe($name); $this->SortName = $sortName; $this->FullName = $fullName; $this->WikipediaUrl = $wikipediaUrl; $this->MarcRole = $marcRole; + $this->NacoafUrl = $nacoafUrl; } } diff --git a/lib/Ebook.php b/lib/Ebook.php index c34d43e1..ab8c2528 100644 --- a/lib/Ebook.php +++ b/lib/Ebook.php @@ -50,6 +50,7 @@ class Ebook{ public $ContributorsHtml; public $TitleWithCreditsHtml = ''; public $Timestamp; + public $ModifiedTimestamp; public function __construct(string $wwwFilesystemPath){ // First, construct a source repo path from our WWW filesystem path. @@ -162,6 +163,11 @@ class Ebook{ $this->Timestamp = new DateTime((string)$date[0]); } + $modifiedDate = $xml->xpath('/package/metadata/meta[@property="dcterms:modified"]'); + if($modifiedDate !== false && sizeof($modifiedDate) > 0){ + $this->ModifiedTimestamp = new DateTime((string)$modifiedDate[0]); + } + // Get SE tags foreach($xml->xpath('/package/metadata/meta[@property="se:subject"]') ?: [] as $tag){ $this->Tags[] = new Tag($tag); @@ -222,7 +228,8 @@ class Ebook{ (string)$contributor, $this->NullIfEmpty($xml->xpath('/package/metadata/meta[@property="file-as"][@refines="#' . $id . '"]')), $this->NullIfEmpty($xml->xpath('/package/metadata/meta[@property="se:name.person.full-name"][@refines="#' . $id . '"]')), - $this->NullIfEmpty($xml->xpath('/package/metadata/meta[@property="se:url.encyclopedia.wikipedia"][@refines="#' . $id . '"]')) + $this->NullIfEmpty($xml->xpath('/package/metadata/meta[@property="se:url.encyclopedia.wikipedia"][@refines="#' . $id . '"]')), + $this->NullIfEmpty($xml->xpath('/package/metadata/meta[@property="se:url.authority.nacoaf"][@refines="#' . $id . '"]')) ); // A display-sequence of 0 indicates that we don't want to process this contributor diff --git a/scripts/generate-opds.php b/scripts/generate-opds.php index 1dde4c7b..81198676 100755 --- a/scripts/generate-opds.php +++ b/scripts/generate-opds.php @@ -42,6 +42,7 @@ print("\n"); + All Standard Ebooks Free and liberated ebooks, carefully produced for the true book lover. /images/logo.png diff --git a/templates/OpdsEntry.php b/templates/OpdsEntry.php new file mode 100644 index 00000000..bc4bf47a --- /dev/null +++ b/templates/OpdsEntry.php @@ -0,0 +1,31 @@ + + Url ?> + <?= $ebook->Title ?> + Authors as $author){ ?> + + Name ?> + WikipediaUrl !== null){ ?>WikipediaUrl ?> + FullName !== null){ ?>FullName ?> + NacoafUrl !== null){ ?>NacoafUrl ?> + + + Timestamp->format('Y-m-d\TH:i:s\Z') ?> + Timestamp->format('Y-m-d\TH:i:s\Z') ?> + Language ?> + Standard Ebooks + Sources as $source){ ?> + Url ?> + + Public domain in the United States; original content released to the public domain via the Creative Commons CC0 1.0 Universal Public Domain Dedication + Description, ENT_QUOTES, 'UTF-8') ?> + LongDescription ?> + LocTags as $subject){ ?> + + + + + + + + + diff --git a/www/opds/search.php b/www/opds/search.php new file mode 100644 index 00000000..824cc1db --- /dev/null +++ b/www/opds/search.php @@ -0,0 +1,39 @@ +\n"); +?> + + https://standardebooks.org/opds/all + + + + + + Standard Ebooks OPDS Search Results + Free and liberated ebooks, carefully produced for the true book lover. + https://standardebooks.org/images/logo.png + Format('Y-m-d\TH:i:s\Z') ?> + + Standard Ebooks + https://standardebooks.org + + + $ebook]) ?> + +