diff --git a/lib/Library.php b/lib/Library.php index ea970348..20527b45 100644 --- a/lib/Library.php +++ b/lib/Library.php @@ -78,12 +78,13 @@ class Library{ /** * @return array - * @throws Exceptions\AppException */ public static function GetEbooks(): array{ // Get all ebooks, unsorted. - /** @var array */ - return self::GetFromApcu('ebooks'); + return Db::Query(' + SELECT * + from Ebooks + ', [], Ebook::class); } /** @@ -386,76 +387,6 @@ class Library{ return $artworks; } - - /** - * @return array - * @throws Exceptions\AppException - */ - private static function GetFromApcu(string $variable): array{ - $results = []; - - try{ - $results = apcu_fetch($variable); - } - catch(Safe\Exceptions\ApcuException $ex){ - try{ - // If we can't fetch this variable, rebuild the whole cache. - apcu_fetch('is-cache-fresh'); - } - catch(Safe\Exceptions\ApcuException $ex){ - Library::RebuildCache(); - try{ - $results = apcu_fetch($variable); - } - catch(Safe\Exceptions\ApcuException){ - // We can get here if the cache is currently rebuilding from a different process. - // Nothing we can do but wait, so wait 20 seconds before retrying - sleep(20); - - try{ - $results = apcu_fetch($variable); - } - catch(Safe\Exceptions\ApcuException){ - // Cache STILL rebuilding... give up silently for now - } - } - } - } - - if(!is_array($results)){ - $results = [$results]; - } - - return $results; - } - - /** - * @return array - */ - public static function GetEbooksFromFilesystem(?string $webRoot = WEB_ROOT): array{ - $ebooks = []; - - $contentFiles = explode("\n", trim(shell_exec('find ' . escapeshellarg($webRoot . '/ebooks/') . ' -name "content.opf" | sort'))); - - foreach($contentFiles as $path){ - if($path == '') - continue; - - $ebookWwwFilesystemPath = ''; - - try{ - $ebookWwwFilesystemPath = preg_replace('|/content\.opf|ius', '', $path); - - $ebooks[] = Ebook::FromFilesystem($ebookWwwFilesystemPath); - } - catch(\Exception){ - // An error in a book isn't fatal; just carry on. - } - } - - return $ebooks; - } - private static function FillBulkDownloadObject(string $dir, string $downloadType, string $urlRoot): stdClass{ $obj = new stdClass(); diff --git a/scripts/generate-bulk-downloads b/scripts/generate-bulk-downloads index c9d53d1e..fee61e5c 100755 --- a/scripts/generate-bulk-downloads +++ b/scripts/generate-bulk-downloads @@ -104,7 +104,7 @@ function CreateZip(string $filePath, array $ebooks, string $type, string $webRoo } // Iterate over all ebooks and arrange them by publication month -foreach(Library::GetEbooksFromFilesystem($webRoot) as $ebook){ +foreach(Library::GetEbooks() as $ebook){ $timestamp = $ebook->EbookCreated->format('Y-m'); $updatedTimestamp = $ebook->EbookUpdated->getTimestamp(); diff --git a/scripts/generate-feeds b/scripts/generate-feeds index 0c678c29..b331bf8f 100755 --- a/scripts/generate-feeds +++ b/scripts/generate-feeds @@ -89,7 +89,7 @@ foreach($dirs as $dir){ } // Iterate over all ebooks to build the various feeds. -foreach(Library::GetEbooksFromFilesystem($webRoot) as $ebook){ +foreach(Library::GetEbooks() as $ebook){ $allEbooks[] = $ebook; $newestEbooks[] = $ebook;