mirror of
https://github.com/standardebooks/web.git
synced 2025-07-07 07:10:29 -04:00
Replace GetEbooksFromFilesystem with a DB implementation
This commit is contained in:
parent
364a2ca78f
commit
f4a19a2d12
3 changed files with 6 additions and 75 deletions
|
@ -78,12 +78,13 @@ class Library{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array<Ebook>
|
* @return array<Ebook>
|
||||||
* @throws Exceptions\AppException
|
|
||||||
*/
|
*/
|
||||||
public static function GetEbooks(): array{
|
public static function GetEbooks(): array{
|
||||||
// Get all ebooks, unsorted.
|
// Get all ebooks, unsorted.
|
||||||
/** @var array<Ebook> */
|
return Db::Query('
|
||||||
return self::GetFromApcu('ebooks');
|
SELECT *
|
||||||
|
from Ebooks
|
||||||
|
', [], Ebook::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -386,76 +387,6 @@ class Library{
|
||||||
return $artworks;
|
return $artworks;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return array<mixed>
|
|
||||||
* @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<Ebook>
|
|
||||||
*/
|
|
||||||
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{
|
private static function FillBulkDownloadObject(string $dir, string $downloadType, string $urlRoot): stdClass{
|
||||||
$obj = new stdClass();
|
$obj = new stdClass();
|
||||||
|
|
||||||
|
|
|
@ -104,7 +104,7 @@ function CreateZip(string $filePath, array $ebooks, string $type, string $webRoo
|
||||||
}
|
}
|
||||||
|
|
||||||
// Iterate over all ebooks and arrange them by publication month
|
// 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');
|
$timestamp = $ebook->EbookCreated->format('Y-m');
|
||||||
$updatedTimestamp = $ebook->EbookUpdated->getTimestamp();
|
$updatedTimestamp = $ebook->EbookUpdated->getTimestamp();
|
||||||
|
|
||||||
|
|
|
@ -89,7 +89,7 @@ foreach($dirs as $dir){
|
||||||
}
|
}
|
||||||
|
|
||||||
// Iterate over all ebooks to build the various feeds.
|
// Iterate over all ebooks to build the various feeds.
|
||||||
foreach(Library::GetEbooksFromFilesystem($webRoot) as $ebook){
|
foreach(Library::GetEbooks() as $ebook){
|
||||||
$allEbooks[] = $ebook;
|
$allEbooks[] = $ebook;
|
||||||
$newestEbooks[] = $ebook;
|
$newestEbooks[] = $ebook;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue