From 44dc65005dc678d00e43999f8500f58c59cd26f1 Mon Sep 17 00:00:00 2001 From: Mike Colagrosso Date: Sun, 30 Jun 2024 21:50:52 -0600 Subject: [PATCH] Query Contributors table for GetEbooksByAuthor() --- lib/Library.php | 35 +++++++++++++++++++++++++++++++---- www/ebooks/author.php | 2 +- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/lib/Library.php b/lib/Library.php index 5f732987..d8188e3b 100644 --- a/lib/Library.php +++ b/lib/Library.php @@ -119,11 +119,38 @@ class Library{ /** * @return array - * @throws Exceptions\AppException */ - public static function GetEbooksByAuthor(string $wwwFilesystemPath): array{ - /** @var array */ - return self::GetFromApcu('author-' . $wwwFilesystemPath); + public static function GetEbooksByAuthor(string $urlPath): array{ + if(mb_strpos($urlPath, '_') === false){ + // Single author + return Db::Query(' + SELECT e.* + from Ebooks e + inner join Contributors con using (EbookId) + where con.MarcRole = "aut" + and con.UrlName = ? + order by e.EbookCreated desc + ', [$urlPath], Ebook::class); + } + else{ + // Multiple authors, e.g., karl-marx_friedrich-engels + $authors = explode('_', $urlPath); + $queryPlaceholder = '(' . implode(', ', array_fill(0, count($authors), '?')) . ')'; // For example, (?, ?) for two authors + + $params = $authors; + $params[] = sizeof($authors); // The number of authors in the URL must match the number of Contributor records. + + return Db::Query(' + SELECT e.* + from Ebooks e + inner join Contributors con using (EbookId) + where con.MarcRole = "aut" + and con.UrlName in ' . $queryPlaceholder . ' + group by e.EbookId + having count(distinct con.UrlName) = ? + order by e.EbookCreated desc + ', $params, Ebook::class); + } } /** diff --git a/www/ebooks/author.php b/www/ebooks/author.php index c3ac4fdb..2ef636d7 100644 --- a/www/ebooks/author.php +++ b/www/ebooks/author.php @@ -12,7 +12,7 @@ try{ throw new Exceptions\AuthorNotFoundException(); } - $ebooks = Library::GetEbooksByAuthor($wwwFilesystemPath); + $ebooks = Library::GetEbooksByAuthor($urlPath); if(sizeof($ebooks) == 0){ throw new Exceptions\AuthorNotFoundException();