mirror of
https://github.com/standardebooks/web.git
synced 2025-07-05 06:10:36 -04:00
85 lines
2.9 KiB
PHP
Executable file
85 lines
2.9 KiB
PHP
Executable file
#!/usr/bin/php
|
|
<?
|
|
require_once('/standardebooks.org/web/lib/Core.php');
|
|
|
|
$longopts = ['webroot:'];
|
|
$options = getopt('', $longopts);
|
|
$webRoot = $options['webroot'] ?? WEB_ROOT;
|
|
|
|
$ebooksByMonth = [];
|
|
$lastUpdatedTimestamps = [];
|
|
|
|
// Iterate over all ebooks and arrange them by publication month
|
|
foreach(Library::GetEbooksFromFilesystem($webRoot) as $ebook){
|
|
$timestamp = $ebook->Created->format('Y-m');
|
|
$updatedTimestamp = $ebook->Updated->getTimestamp();
|
|
|
|
if(!isset($ebooksByMonth[$timestamp])){
|
|
$ebooksByMonth[$timestamp] = [];
|
|
$lastUpdatedTimestamps[$timestamp] = $updatedTimestamp;
|
|
}
|
|
|
|
$ebooksByMonth[$timestamp][] = $ebook;
|
|
if($updatedTimestamp > $lastUpdatedTimestamps[$timestamp]){
|
|
$lastUpdatedTimestamps[$timestamp] = $updatedTimestamp;
|
|
}
|
|
}
|
|
|
|
foreach($ebooksByMonth as $month => $ebooks){
|
|
$filename = 'se-ebooks-' . $month . '.zip';
|
|
$filePath = $webRoot . '/patrons-circle/downloads/' . $filename;
|
|
|
|
// If the file doesn't exist, or if the content.opf last updated time is newer than the file modification time
|
|
if(!file_exists($filePath) || filemtime($filePath) < $lastUpdatedTimestamps[$month]){
|
|
print('Creating ' . $filePath . "\n");
|
|
|
|
$tempFilename = tempnam(sys_get_temp_dir(), "se-ebooks");
|
|
|
|
$zip = new ZipArchive();
|
|
|
|
if($zip->open($tempFilename, ZipArchive::CREATE) !== true){
|
|
print('Can\'t open file: ' . $tempFilename . "\n");
|
|
continue;
|
|
}
|
|
|
|
foreach($ebooks as $ebook){
|
|
if($ebook->EpubUrl !== null){
|
|
$ebookFilePath = $webRoot . '/' . $ebook->EpubUrl;
|
|
$zip->addFile($ebookFilePath, $ebook->UrlSafeIdentifier . '/' . basename($ebookFilePath));
|
|
}
|
|
|
|
if($ebook->Azw3Url !== null){
|
|
$ebookFilePath = $webRoot . '/' . $ebook->Azw3Url;
|
|
$zip->addFile($ebookFilePath, $ebook->UrlSafeIdentifier . '/' . basename($ebookFilePath));
|
|
}
|
|
|
|
if($ebook->KepubUrl !== null){
|
|
$ebookFilePath = $webRoot . '/' . $ebook->KepubUrl;
|
|
$zip->addFile($ebookFilePath, $ebook->UrlSafeIdentifier . '/' . basename($ebookFilePath));
|
|
}
|
|
|
|
if($ebook->AdvancedEpubUrl !== null){
|
|
$ebookFilePath = $webRoot . '/' . $ebook->AdvancedEpubUrl;
|
|
$zip->addFile($ebookFilePath, $ebook->UrlSafeIdentifier . '/' . basename($ebookFilePath));
|
|
}
|
|
|
|
if($ebook->TextSinglePageUrl !== null){
|
|
$ebookFilePath = $webRoot . '/' . $ebook->TextSinglePageUrl . '.xhtml';
|
|
|
|
// Strip the navigation header that was added as part of the deploy process
|
|
$xhtml = file_get_contents($ebookFilePath);
|
|
$xhtml = preg_replace('|<body><header><nav>.+?</nav></header>|ius', '<body>', $xhtml);
|
|
|
|
$zip->addFromString($ebook->UrlSafeIdentifier . '/' . str_replace('single-page', $ebook->UrlSafeIdentifier, basename($ebookFilePath)), $xhtml);
|
|
}
|
|
}
|
|
|
|
$zip->close();
|
|
|
|
rename($tempFilename, $filePath);
|
|
|
|
// Set a filesystem attribute for the number of ebooks in the file. This will be used
|
|
// to display that number on the downloads page.
|
|
exec('attr -q -s ebook-count -V ' . escapeshellarg(sizeof($ebooks)) . ' ' . escapeshellarg($filePath));
|
|
}
|
|
}
|