From 2d5e66f2f2a93613cfb5fa0613f9f823c4c5d66b Mon Sep 17 00:00:00 2001 From: Mike Colagrosso Date: Sat, 29 Jun 2024 11:41:56 -0600 Subject: [PATCH] Get carousel books from the DB --- lib/Library.php | 41 ++++++++++++++++++++++++++++------------- www/ebooks/ebook.php | 29 ++--------------------------- 2 files changed, 30 insertions(+), 40 deletions(-) diff --git a/lib/Library.php b/lib/Library.php index bfbbcafd..5f732987 100644 --- a/lib/Library.php +++ b/lib/Library.php @@ -126,19 +126,6 @@ class Library{ return self::GetFromApcu('author-' . $wwwFilesystemPath); } - /** - * @return array - */ - public static function GetEbooksByTag(string $tag): array{ - try{ - /** @var array */ - return apcu_fetch('tag-' . $tag) ?? []; - } - catch(Safe\Exceptions\ApcuException){ - return []; - } - } - /** * @return array * @throws Exceptions\AppException @@ -173,6 +160,34 @@ class Library{ return $ebooks; } + /** + * @return array + */ + public static function GetRelatedEbooks(Ebook $ebook, int $count, ?EbookTag $relatedTag): array{ + if($relatedTag !== null){ + $relatedEbooks = Db::Query(' + SELECT e.* + from Ebooks e + inner join EbookTags et using (EbookId) + where et.TagId = ? + and et.EbookId != ? + order by RAND() + limit ? + ', [$relatedTag->TagId, $ebook->EbookId, $count], Ebook::class); + } + else{ + $relatedEbooks = Db::Query(' + SELECT * + from Ebooks + where EbookId != ? + order by RAND() + limit ? + ', [$ebook->EbookId, $count], Ebook::class); + } + + return $relatedEbooks; + } + /** * @return array */ diff --git a/www/ebooks/ebook.php b/www/ebooks/ebook.php index 766c3668..af489a2e 100644 --- a/www/ebooks/ebook.php +++ b/www/ebooks/ebook.php @@ -12,6 +12,7 @@ $scanSources = []; $otherSources = []; $carousel = []; $carouselTag = null; +$targetCarouselSize = 5; try{ $urlPath = trim(str_replace('.', '', HttpInput::Str(GET, 'url-path') ?? ''), '/'); // Contains the portion of the URL (without query string) that comes after https://standardebooks.org/ebooks/ @@ -64,36 +65,10 @@ try{ } } - // Generate the bottom carousel. - // Pick a random tag from this ebook, and get ebooks in the same tag - $ebooks = []; if(sizeof($ebook->Tags) > 0){ $carouselTag = $ebook->Tags[rand(0, sizeof($ebook->Tags) - 1)]; - $ebooks = Library::GetEbooksByTag(strtolower($carouselTag->Name)); - } - else{ - $ebooks = Library::GetEbooks(); - } - - shuffle($ebooks); - - $targetCarouselSize = 5; - // Use <= here because we want to exclude the ebook we're looking at from the carousel. - // One of the matching ebooks will always be the current ebook. - if(sizeof($ebooks) <= $targetCarouselSize){ - $targetCarouselSize = sizeof($ebooks) - 1; - } - - if($targetCarouselSize > 0){ - $i = 0; - while(sizeof($carousel) < $targetCarouselSize){ - if(isset($ebooks[$i]) && $ebooks[$i]->Url !== $ebook->Url){ - $carousel[] = $ebooks[$i]; - } - - $i++; - } } + $carousel = Library::GetRelatedEbooks($ebook, $targetCarouselSize, $carouselTag); } catch(Exceptions\SeeOtherEbookException $ex){ http_response_code(301);