Rearrange bulk download index pages

This commit is contained in:
Alex Cabal 2022-07-10 21:15:10 -05:00
parent 169a74a3cb
commit 9a72b27233
10 changed files with 89 additions and 194 deletions

2
.gitignore vendored
View file

@ -14,4 +14,4 @@ composer.lock
www/manual/*
!www/manual/index.php
config/php/fpm/standardebooks.org-secrets.ini
www/bulk-downloads/**/*.zip
www/bulk-downloads/*/

View file

@ -281,6 +281,7 @@ Define webroot /standardebooks.org/web
# Rewrite rules for bulk downloads
RewriteRule ^/bulk-downloads/(.+\.zip)$ /bulk-downloads/download.php?path=$1
RewriteRule ^/bulk-downloads/([^/\.]+)$ /bulk-downloads/collection.php?name=$1
# Enable mod_authn_dbd
DBDriver mysql

View file

@ -263,6 +263,7 @@ Define webroot /standardebooks.org/web
# Rewrite rules for bulk downloads
RewriteRule ^/bulk-downloads/(.+\.zip)$ /bulk-downloads/download.php?path=$1
RewriteRule ^/bulk-downloads/([^/\.]+)$ /bulk-downloads/collection.php?name=$1
# Enable mod_authn_dbd
DBDriver mysql

View file

@ -322,7 +322,8 @@ class Library{
* @return array<string, array<int|string, array<int|string, mixed>>>
*/
public static function RebuildBulkDownloadsCache(): array{
$years = [];
$collator = collator_create( 'en_US' ); // Used for sorting letters with diacritics like in author names
$months = [];
$subjects = [];
$collections = [];
$authors = [];
@ -340,14 +341,14 @@ class Library{
$year = $date->format('Y');
$month = $date->format('F');
if(!isset($years[$year])){
$years[$year] = [];
if(!isset($months[$year])){
$months[$year] = [];
}
$years[$year][$month] = $obj;
$months[$year][$month] = $obj;
}
apcu_store('bulk-downloads-years', $years, 43200); // 12 hours
apcu_store('bulk-downloads-months', $months, 43200); // 12 hours
// Generate bulk downloads by subject
foreach(glob(WEB_ROOT . '/bulk-downloads/subjects/*/', GLOB_NOSORT) as $dir){
@ -361,7 +362,7 @@ class Library{
foreach(glob(WEB_ROOT . '/bulk-downloads/collections/*/', GLOB_NOSORT) as $dir){
$collections[] = self::FillBulkDownloadObject($dir, 'collections', '/collections');
}
usort($collections, function($a, $b){ return $a->LabelSort <=> $b->LabelSort; });
usort($authors, function($a, $b) use($collator){ return $collator->compare($a->LabelSort, $b->LabelSort); });
apcu_store('bulk-downloads-collections', $collections, 43200); // 12 hours
@ -369,11 +370,11 @@ class Library{
foreach(glob(WEB_ROOT . '/bulk-downloads/authors/*/', GLOB_NOSORT) as $dir){
$authors[] = self::FillBulkDownloadObject($dir, 'authors', '/ebooks');
}
usort($authors, function($a, $b){ return $a->LabelSort <=> $b->LabelSort; });
usort($authors, function($a, $b) use($collator){ return $collator->compare($a->LabelSort, $b->LabelSort); });
apcu_store('bulk-downloads-authors', $authors, 43200); // 12 hours
return ['years' => $years, 'subjects' => $subjects, 'collections' => $collections, 'authors' => $authors];
return ['months' => $months, 'subjects' => $subjects, 'collections' => $collections, 'authors' => $authors];
}
public static function RebuildCache(): void{

View file

@ -1,33 +0,0 @@
<?
require_once('Core.php');
use function Safe\apcu_fetch;
$canDownload = false;
if($GLOBALS['User'] !== null && $GLOBALS['User']->Benefits->CanBulkDownload){
$canDownload = true;
}
$authors = [];
try{
$authors = apcu_fetch('bulk-downloads-authors');
}
catch(Safe\Exceptions\ApcuException $ex){
$result = Library::RebuildBulkDownloadsCache();
$authors = $result['authors'];
}
?><?= Template::Header(['title' => 'Downloads by Author', 'highlight' => '', 'description' => 'Download zip files containing all of the Standard Ebooks by a given author.']) ?>
<main>
<section class="bulk-downloads">
<h1>Downloads by Author</h1>
<? if(!$canDownload){ ?>
<p><a href="/about#patrons-circle">Patrons circle members</a> can download zip files containing all of the ebooks that were released in a given month of Standard Ebooks history. You can <a href="/donate#patrons-circle">join the Patrons Circle</a> with a small donation in support of our continuing mission to create free, beautiful digital literature.</p>
<? } ?>
<p>These zip files contain each ebook in every format we offer, and are updated once daily with the latest versions of each ebook. Read about <a href="/help/how-to-use-our-ebooks#which-file-to-download">which file format to download</a>.</p>
<?= Template::BulkDownloadTable(['label' => 'Author', 'collections' => $authors]); ?>
</section>
</main>
<?= Template::Footer() ?>

View file

@ -0,0 +1,77 @@
<?
require_once('Core.php');
use function Safe\apcu_fetch;
use function Safe\preg_replace;
$canDownload = false;
$name = HttpInput::Str(GET, 'name', false) ?? '';
if($name != 'authors' && $name != 'collections' && $name != 'subjects' && $name != 'months'){
$name = 'subjects';
}
if($GLOBALS['User'] !== null && $GLOBALS['User']->Benefits->CanBulkDownload){
$canDownload = true;
}
$collection = [];
try{
$collection = apcu_fetch('bulk-downloads-' . $name);
}
catch(Safe\Exceptions\ApcuException $ex){
$result = Library::RebuildBulkDownloadsCache();
$collection = $result[$name];
}
$title = preg_replace('/s$/', '', ucfirst($name));
?><?= Template::Header(['title' => 'Downloads by ' . $title, 'highlight' => '', 'description' => 'Download zip files containing all of the Standard Ebooks in a given collection.']) ?>
<main>
<section class="bulk-downloads">
<h1>Downloads by <?= $title ?></h1>
<? if(!$canDownload){ ?>
<p><a href="/about#patrons-circle">Patrons circle members</a> get convenient access to zip files containing collections of different categories of ebooks. You can <a href="/donate#patrons-circle">join the Patrons Circle</a> with a small donation in support of our continuing mission to create free, beautiful digital literature, and download these collections files too.</p>
<? } ?>
<p>These zip files contain each ebook in every format we offer, and are updated once daily with the latest versions of each ebook. Read about <a href="/help/how-to-use-our-ebooks#which-file-to-download">which file format to download</a>.</p>
<? if($name == 'months'){ ?>
<table class="download-list">
<tbody>
<? foreach($collection as $year => $months){
$yearHeader = Formatter::ToPlainText($year);
?>
<tr class="year-header">
<th colspan="13" scope="colgroup" id="<?= $yearHeader ?>"><?= Formatter::ToPlainText((string)$year) ?></th>
</tr>
<tr class="mid-header">
<th id="<?= $yearHeader?>-type" scope="col">Month</th>
<th id="<?= $yearHeader ?>-ebooks" scope="col">Ebooks</th>
<th id="<?= $yearHeader ?>-updated" scope="col">Updated</th>
<th id="<?= $yearHeader ?>-download" colspan="10" scope="col">Ebook format</th>
</tr>
<? foreach($months as $month => $collection){
$monthHeader = Formatter::ToPlainText($month);
?>
<tr>
<th class="row-header" headers="<?= $yearHeader ?> <?= $monthHeader ?> <?= $yearHeader ?>-type" id="<?= $monthHeader ?>"><?= Formatter::ToPlainText($month) ?></th>
<td class="number" headers="<?= $yearHeader ?> <?= $monthHeader ?> <?= $yearHeader ?>-ebooks"><?= Formatter::ToPlainText(number_format($collection->EbookCount)) ?></td>
<td class="number" headers="<?= $yearHeader ?> <?= $monthHeader ?> <?= $yearHeader ?>-updated"><?= Formatter::ToPlainText($collection->UpdatedString) ?></td>
<? foreach($collection->ZipFiles as $item){ ?>
<td headers="<?= $yearHeader ?> <?= $monthHeader ?> <?= $yearHeader ?>-download" class="download"><a href="<?= $item->Url ?>"><?= $item->Type ?></a></td>
<td headers="<?= $yearHeader ?> <?= $monthHeader ?> <?= $yearHeader ?>-download">(<?= Formatter::ToPlainText($item->Size) ?>)</td>
<? } ?>
</tr>
<? } ?>
<? } ?>
</tbody>
</table>
<? }else{ ?>
<?= Template::BulkDownloadTable(['label' => $title, 'collections' => $collection]); ?>
<? } ?>
</section>
</main>
<?= Template::Footer() ?>

View file

@ -1,33 +0,0 @@
<?
require_once('Core.php');
use function Safe\apcu_fetch;
$canDownload = false;
if($GLOBALS['User'] !== null && $GLOBALS['User']->Benefits->CanBulkDownload){
$canDownload = true;
}
$collections = [];
try{
$collections = apcu_fetch('bulk-downloads-collections');
}
catch(Safe\Exceptions\ApcuException $ex){
$result = Library::RebuildBulkDownloadsCache();
$collections = $result['collections'];
}
?><?= Template::Header(['title' => 'Downloads by Collection', 'highlight' => '', 'description' => 'Download zip files containing all of the Standard Ebooks in a given collection.']) ?>
<main>
<section class="bulk-downloads">
<h1>Downloads by Collection</h1>
<? if(!$canDownload){ ?>
<p><a href="/about#patrons-circle">Patrons circle members</a> can download zip files containing all of the ebooks that were released in a given month of Standard Ebooks history. You can <a href="/donate#patrons-circle">join the Patrons Circle</a> with a small donation in support of our continuing mission to create free, beautiful digital literature.</p>
<? } ?>
<p>These zip files contain each ebook in every format we offer, and are updated once daily with the latest versions of each ebook. Read about <a href="/help/how-to-use-our-ebooks#which-file-to-download">which file format to download</a>.</p>
<?= Template::BulkDownloadTable(['label' => 'Collection', 'collections' => $collections]); ?>
</section>
</main>
<?= Template::Footer() ?>

View file

@ -1,32 +1,11 @@
<?
require_once('Core.php');
use function Safe\apcu_fetch;
$canDownload = false;
if($GLOBALS['User'] !== null && $GLOBALS['User']->Benefits->CanBulkDownload){
$canDownload = true;
}
$years = [];
$subjects = [];
$collections = [];
$authors = [];
try{
$years = apcu_fetch('bulk-downloads-years');
$subjects = apcu_fetch('bulk-downloads-subjects');
$collections = apcu_fetch('bulk-downloads-collections');
$authors = apcu_fetch('bulk-downloads-authors');
}
catch(Safe\Exceptions\ApcuException $ex){
$result = Library::RebuildBulkDownloadsCache();
$years = $result['years'];
$subjects = $result['subjects'];
$collections = $result['collections'];
$authors = $result['authors'];
}
?><?= Template::Header(['title' => 'Bulk Ebook Downloads', 'highlight' => '', 'description' => 'Download zip files containing all of the Standard Ebooks released in a given month.']) ?>
<main>
<section class="narrow has-hero">

View file

@ -1,65 +0,0 @@
<?
require_once('Core.php');
use function Safe\apcu_fetch;
$canDownload = false;
if($GLOBALS['User'] !== null && $GLOBALS['User']->Benefits->CanBulkDownload){
$canDownload = true;
}
$years = [];
try{
$years = apcu_fetch('bulk-downloads-years');
}
catch(Safe\Exceptions\ApcuException $ex){
$result = Library::RebuildBulkDownloadsCache();
$years = $result['years'];
}
?><?= Template::Header(['title' => 'Downloads by Month', 'highlight' => '', 'description' => 'Download zip files containing all of the Standard Ebooks released in a given month.']) ?>
<main>
<section class="bulk-downloads">
<h1>Downloads by Month</h1>
<? if(!$canDownload){ ?>
<p><a href="/about#patrons-circle">Patrons circle members</a> can download zip files containing all of the ebooks that were released in a given month of Standard Ebooks history. You can <a href="/donate#patrons-circle">join the Patrons Circle</a> with a small donation in support of our continuing mission to create free, beautiful digital literature.</p>
<? } ?>
<p>These zip files contain each ebook in every format we offer, and are updated once daily with the latest versions of each ebook. Read about <a href="/help/how-to-use-our-ebooks#which-file-to-download">which file format to download</a>.</p>
<table class="download-list">
<tbody>
<? foreach($years as $year => $months){
$yearHeader = Formatter::ToPlainText($year);
?>
<tr class="year-header">
<th colspan="13" scope="colgroup" id="<?= $yearHeader ?>"><?= Formatter::ToPlainText((string)$year) ?></th>
</tr>
<tr class="mid-header">
<th id="<?= $yearHeader?>-type" scope="col">Month</th>
<th id="<?= $yearHeader ?>-ebooks" scope="col">Ebooks</th>
<th id="<?= $yearHeader ?>-updated" scope="col">Updated</th>
<th id="<?= $yearHeader ?>-download" colspan="10" scope="col">Ebook format</th>
</tr>
<? foreach($months as $month => $collection){
$monthHeader = Formatter::ToPlainText($month);
?>
<tr>
<th class="row-header" headers="<?= $yearHeader ?> <?= $monthHeader ?> <?= $yearHeader ?>-type" id="<?= $monthHeader ?>"><?= Formatter::ToPlainText($month) ?></th>
<td class="number" headers="<?= $yearHeader ?> <?= $monthHeader ?> <?= $yearHeader ?>-ebooks"><?= Formatter::ToPlainText(number_format($collection->EbookCount)) ?></td>
<td class="number" headers="<?= $yearHeader ?> <?= $monthHeader ?> <?= $yearHeader ?>-updated"><?= Formatter::ToPlainText($collection->UpdatedString) ?></td>
<? foreach($collection->ZipFiles as $item){ ?>
<td headers="<?= $yearHeader ?> <?= $monthHeader ?> <?= $yearHeader ?>-download" class="download"><a href="<?= $item->Url ?>"><?= $item->Type ?></a></td>
<td headers="<?= $yearHeader ?> <?= $monthHeader ?> <?= $yearHeader ?>-download">(<?= Formatter::ToPlainText($item->Size) ?>)</td>
<? } ?>
</tr>
<? } ?>
<? } ?>
</tbody>
</table>
</section>
</main>
<?= Template::Footer() ?>

View file

@ -1,33 +0,0 @@
<?
require_once('Core.php');
use function Safe\apcu_fetch;
$canDownload = false;
if($GLOBALS['User'] !== null && $GLOBALS['User']->Benefits->CanBulkDownload){
$canDownload = true;
}
$subjects = [];
try{
$subjects = apcu_fetch('bulk-downloads-subjects');
}
catch(Safe\Exceptions\ApcuException $ex){
$result = Library::RebuildBulkDownloadsCache();
$subjects = $result['subjects'];
}
?><?= Template::Header(['title' => 'Downloads by Subject', 'highlight' => '', 'description' => 'Download zip files containing all of the Standard Ebooks by a given subject.']) ?>
<main>
<section class="bulk-downloads">
<h1>Downloads by Subject</h1>
<? if(!$canDownload){ ?>
<p><a href="/about#patrons-circle">Patrons circle members</a> can download zip files containing all of the ebooks that were released in a given month of Standard Ebooks history. You can <a href="/donate#patrons-circle">join the Patrons Circle</a> with a small donation in support of our continuing mission to create free, beautiful digital literature.</p>
<? } ?>
<p>These zip files contain each ebook in every format we offer, and are updated once daily with the latest versions of each ebook. Read about <a href="/help/how-to-use-our-ebooks#which-file-to-download">which file format to download</a>.</p>
<?= Template::BulkDownloadTable(['label' => 'Subject', 'collections' => $subjects]); ?>
</section>
</main>
<?= Template::Footer() ?>