From 99f1c1537a3ab9e724a081be40dc96cd448c819c Mon Sep 17 00:00:00 2001 From: Alex Cabal Date: Sat, 26 Dec 2020 14:55:13 -0600 Subject: [PATCH] Switch from JSON-LD to RDFa for internal metadata --- config/phpstan/phpstan.neon | 4 +++ lib/Constants.php | 4 ++- lib/Ebook.php | 57 ++++++++++++++++++++++++++------ templates/EbookGrid.php | 18 ++++++---- templates/Header.php | 7 +--- www/ebooks/ebook.php | 66 ++++++++++++++++++++++++++++++------- www/ebooks/index.php | 5 +-- 7 files changed, 125 insertions(+), 36 deletions(-) diff --git a/config/phpstan/phpstan.neon b/config/phpstan/phpstan.neon index bef367ab..d74ac90b 100644 --- a/config/phpstan/phpstan.neon +++ b/config/phpstan/phpstan.neon @@ -25,3 +25,7 @@ parameters: - %rootDir%/../../../lib - %rootDir%/../../../www - %rootDir%/../../../scripts + dynamicConstantNames: + - DONATION_ALERT_ON + - DONATION_ALERT_ALWAYS_ON + - DONATION_ALERT_ON_DURING_HOLIDAYS diff --git a/lib/Constants.php b/lib/Constants.php index c3bb08eb..bb189e73 100644 --- a/lib/Constants.php +++ b/lib/Constants.php @@ -1,5 +1,7 @@ attributes()->id; } - $refines = null; - $refinesElement = $xml->xpath('/package/metadata/meta[@property="file-as"][@refines="#' . $id . '"]'); - if($refinesElement !== false && sizeof($refinesElement) > 0){ - $refines = (string)$refinesElement[0]; + $fileAs = null; + $fileAsElement = $xml->xpath('/package/metadata/meta[@property="file-as"][@refines="#' . $id . '"]'); + if($fileAsElement !== false && sizeof($fileAsElement) > 0){ + $fileAs = (string)$fileAsElement[0]; } - $this->Authors[] = new Contributor( (string)$author, - $refines, + $this->Authors[] = new Contributor( + (string)$author, + $fileAs, $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 . '"]')), + 'aut', + $this->NullIfEmpty($xml->xpath('/package/metadata/meta[@property="se:url.authority.nacoaf"][@refines="#' . $id . '"]')) ); } @@ -256,7 +259,7 @@ class Ebook{ throw new EbookParsingException('Invalid element.'); } - $this->AuthorsUrl = preg_replace('|url:https://standardebooks.org/ebooks/([^/]+)/.*|ius', '/ebooks/\1/', $this->Identifier); + $this->AuthorsUrl = preg_replace('|url:https://standardebooks.org/ebooks/([^/]+)/.*|ius', '/ebooks/\1', $this->Identifier); foreach($xml->xpath('/package/metadata/dc:contributor') ?: [] as $contributor){ $id = ''; @@ -270,6 +273,7 @@ class Ebook{ $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 . '"]')), + $role, $this->NullIfEmpty($xml->xpath('/package/metadata/meta[@property="se:url.authority.nacoaf"][@refines="#' . $id . '"]')) ); @@ -437,6 +441,16 @@ class Ebook{ $this->TitleWithCreditsHtml = Formatter::ToPlainText($this->Title) . ', by ' . str_replace('&', '&', $this->AuthorsHtml . $titleContributors); } + public function GetCollectionPosition(Collection $collection): ?int{ + foreach($this->Collections as $c){ + if($c->Name == $collection->Name){ + return $c->SequenceNumber; + } + } + + return null; + } + public function Contains(string $query): bool{ // When searching an ebook, we search the title, alternate title, author(s), SE tags, series data, and LoC tags. // Also, if the ebook is shorts or poetry, search the ToC as well. @@ -587,11 +601,34 @@ class Ebook{ $string = ''; $i = 0; foreach($contributors as $contributor){ + + $role = 'schema:contributor'; + switch($contributor->MarcRole){ + case 'trl': + $role = 'schema:translator'; + break; + case 'ill': + $role = 'schema:illustrator'; + break; + } + if($contributor->WikipediaUrl){ - $string .= '' . Formatter::ToPlainText($contributor->Name) . ''; + $string .= '' . Formatter::ToPlainText($contributor->Name) . ''; + + if($contributor->NacoafUrl){ + $string .= ''; + } + + $string .= ''; } else{ - $string .= Formatter::ToPlainText($contributor->Name); + $string .= '' . Formatter::ToPlainText($contributor->Name) . ''; + + if($contributor->NacoafUrl){ + $string .= ''; + } + + $string .= ''; } if($i == sizeof($contributors) - 2 && sizeof($contributors) > 2){ diff --git a/templates/EbookGrid.php b/templates/EbookGrid.php index 68f84d12..8ff8644b 100644 --- a/templates/EbookGrid.php +++ b/templates/EbookGrid.php @@ -8,20 +8,26 @@ if(!isset($ebooks)){ $ebooks = []; } ?> - class="list"> + class="list" typeof="schema:BookSeries" about="Url ?>"> + + + -
  • - +
  • resource="Url ?>" property="schema:hasPart" value="GetCollectionPosition($collection) ?>" about="Url ?>"> + + + + CoverImage2xAvifUrl !== null){ ?> - The cover for the Standard Ebooks edition of <?= Formatter::ToPlainText(strip_tags($ebook->TitleWithCreditsHtml)) ?> + The cover for the Standard Ebooks edition of <?= Formatter::ToPlainText(strip_tags($ebook->TitleWithCreditsHtml)) ?> -

    Title) ?>

    +

    Title) ?>

    Authors as $author){ ?> -

    Name != 'Anonymous'){ ?>Name) ?>

    +

    Name != 'Anonymous'){ ?>Name) ?>

    diff --git a/templates/Header.php b/templates/Header.php index e5935bf9..765b3fe1 100644 --- a/templates/Header.php +++ b/templates/Header.php @@ -23,7 +23,7 @@ print(''); print("\n"); ?> - + <? if($title != ''){ ?><?= Formatter::ToPlainText($title) ?> - <? } ?>Standard Ebooks: Free and liberated ebooks, carefully produced for the true book lover. @@ -49,11 +49,6 @@ print("\n"); - - -
    diff --git a/www/ebooks/ebook.php b/www/ebooks/ebook.php index e1294257..cd654262 100644 --- a/www/ebooks/ebook.php +++ b/www/ebooks/ebook.php @@ -68,22 +68,45 @@ catch(\Exception $ex){ include(WEB_ROOT . '/404.php'); exit(); } -?> strip_tags($ebook->TitleWithCreditsHtml), 'ogType' => 'book', 'coverUrl' => $ebook->DistCoverUrl, 'highlight' => 'ebooks', 'description' => 'The Standard Ebooks edition of ' . $ebook->Title . ': ' . $ebook->Description, 'jsonld' => htmlentities($ebook->GenerateJsonLd(), ENT_NOQUOTES)]) ?> +?> strip_tags($ebook->TitleWithCreditsHtml), 'ogType' => 'book', 'coverUrl' => $ebook->DistCoverUrl, 'highlight' => 'ebooks', 'description' => 'The Standard Ebooks edition of ' . $ebook->Title . ': ' . $ebook->Description]) ?>
    -
    +
    + + + + + + + + WikipediaUrl){ ?> + + +
    + + + +
    -

    Title) ?>

    +

    Title) ?>

    Authors as $author){ ?> + Name != 'Anonymous'){ ?> -

    Name) ?>

    +

    + Name) ?> + + NacoafUrl){ ?> + +

    HeroImage2xAvifUrl !== null){ ?> - The cover for the Standard Ebooks edition of <?= Formatter::ToPlainText(strip_tags($ebook->TitleWithCreditsHtml)) ?> +
    @@ -94,7 +117,7 @@ catch(\Exception $ex){ Collections) > 0){ ?> Collections as $collection){ ?> -

    SequenceNumber !== null){ ?>№ SequenceNumber) ?> in thePart of the Name) ?? '') ?> +

    SequenceNumber !== null){ ?>№ SequenceNumber) ?> in thePart of the Name) ?? '') ?> Type !== null){ ?> Name), mb_strtolower($collection->Type), -strlen(mb_strtolower($collection->Type))) !== 0){ ?> Type ?>. @@ -128,22 +151,38 @@ catch(\Exception $ex){

    Download for ereaders

      EpubUrl !== null){ ?> -
    • Compatible epub All devices and apps except Kindles and Kobos.

      +
    • + + Compatible epub + + All devices and apps except Kindles and Kobos.

    • Azw3Url !== null){ ?> -
    • azw3 Kindle devices and apps.KindleCoverUrl !== null){ ?> Also download the Kindle cover thumbnail to see the cover in your Kindle’s library.

      +
    • + + azw3 + + Kindle devices and apps.KindleCoverUrl !== null){ ?> Also download the Kindle cover thumbnail to see the cover in your Kindle’s library.

    • KepubUrl !== null){ ?> -
    • kepub Kobo devices and apps.

      +
    • + + kepub + + Kobo devices and apps.

    • AdvancedEpubUrl !== null){ ?> -
    • Advanced epub An advanced format not yet fully compatible with most ereaders.

      +
    • + + Advanced epub + + An advanced format not yet fully compatible with most ereaders.

    @@ -159,7 +198,12 @@ catch(\Exception $ex){
  • Start from the table of contents

  • TextSinglePageUrl !== null){ ?> -
  • Read on one page

  • +
  • + + Read on one page + + +

  • diff --git a/www/ebooks/index.php b/www/ebooks/index.php index f0dedf10..3bc0fbd8 100644 --- a/www/ebooks/index.php +++ b/www/ebooks/index.php @@ -13,6 +13,7 @@ try{ $sort = HttpInput::GetString('sort', false); $pages = 0; $totalEbooks = 0; + $collectionObject = null; if($page <= 0){ $page = 1; @@ -51,7 +52,7 @@ try{ // Are we looking at a collection? if($collection !== null){ $ebooks = Library::GetEbooksByCollection($collection); - $collectionObject = null; + // Get the *actual* name of the collection, in case there are accent marks (like "Arsène Lupin") if(sizeof($ebooks) > 0){ foreach($ebooks[0]->Collections as $c){ @@ -138,7 +139,7 @@ catch(\Exception $ex){

    No ebooks matched your filters. You can try different filters, or browse all of our ebooks.

    - $ebooks, 'view' => $view]) ?> + $ebooks, 'view' => $view, 'collection' => $collectionObject]) ?> 0 && $collection === null){ ?>