diff --git a/config/apache/standardebooks.org.conf b/config/apache/standardebooks.org.conf index ea12b2e4..b8872ac7 100644 --- a/config/apache/standardebooks.org.conf +++ b/config/apache/standardebooks.org.conf @@ -281,13 +281,13 @@ Define webroot /standardebooks.org/web RewriteCond %{QUERY_STRING} \bquery= RewriteRule ^/feeds/(opds|atom|rss)/all.xml$ /feeds/$1/search.php [QSA] - RewriteRule ^/feeds/(atom|rss)/([^/\.]+)$ /feeds/collection.php?type=$1&name=$2 + RewriteRule ^/feeds/(atom|rss)/([^/\.]+)$ /feeds/collection.php?type=$1&class=$2 RewriteRule ^/feeds/(.+\.xml)$ /feeds/download.php?path=$1 # Rewrite rules for bulk downloads RewriteRule ^/bulk-downloads/(.+\.zip)$ /bulk-downloads/download.php?path=$1 - RewriteRule ^/bulk-downloads/([^/\.]+)$ /bulk-downloads/collection.php?name=$1 + RewriteRule ^/bulk-downloads/([^/\.]+)$ /bulk-downloads/collection.php?class=$1 # Specific config for /bulk-downloads diff --git a/config/apache/standardebooks.test.conf b/config/apache/standardebooks.test.conf index 56f8125d..6339917c 100644 --- a/config/apache/standardebooks.test.conf +++ b/config/apache/standardebooks.test.conf @@ -263,13 +263,13 @@ Define webroot /standardebooks.org/web RewriteCond %{QUERY_STRING} \bquery= RewriteRule ^/feeds/(opds|atom|rss)/all.xml$ /feeds/$1/search.php [QSA] - RewriteRule ^/feeds/(atom|rss)/([^/\.]+)$ /feeds/collection.php?type=$1&name=$2 + RewriteRule ^/feeds/(atom|rss)/([^/\.]+)$ /feeds/collection.php?type=$1&class=$2 RewriteRule ^/feeds/(.+\.xml)$ /feeds/download.php?path=$1 # Rewrite rules for bulk downloads RewriteRule ^/bulk-downloads/(.+\.zip)$ /bulk-downloads/download.php?path=$1 - RewriteRule ^/bulk-downloads/([^/\.]+)$ /bulk-downloads/collection.php?name=$1 + RewriteRule ^/bulk-downloads/([^/\.]+)$ /bulk-downloads/collection.php?class=$1 # Specific config for /bulk-downloads diff --git a/lib/Library.php b/lib/Library.php index eb11e8f8..0f4ab00e 100644 --- a/lib/Library.php +++ b/lib/Library.php @@ -380,6 +380,54 @@ class Library{ return ['months' => $months, 'subjects' => $subjects, 'collections' => $collections, 'authors' => $authors]; } + /** + * @return array>> + */ + public static function RebuildFeedsCache(?string $returnType = null, ?string $returnClass = null): ?array{ + $feedTypes = ['opds', 'atom', 'rss']; + $feedClasses = ['authors', 'collections', 'subjects']; + $retval = null; + $collator = Collator::create('en_US'); // Used for sorting letters with diacritics like in author names + if($collator === null){ + throw new Exceptions\SeException('Couldn\'t create collator object when rebuilding feeds cache.'); + } + + foreach($feedTypes as $type){ + foreach($feedClasses as $class){ + $files = glob(WEB_ROOT . '/feeds/' . $type . '/' . $class . '/*.xml'); + + $feeds = []; + + foreach($files as $file){ + $obj = new stdClass(); + $obj->Url = '/feeds/' . $type . '/' . $class . '/' . basename($file, '.xml'); + + $obj->Label = exec('attr -g se-label ' . escapeshellarg($file)) ?: null; + if($obj->Label == null){ + $obj->Label = basename($file, '.xml'); + } + + $obj->LabelSort = exec('attr -g se-label-sort ' . escapeshellarg($file)) ?: null; + if($obj->LabelSort == null){ + $obj->LabelSort = basename($file, '.xml'); + } + + $feeds[] = $obj; + } + + usort($feeds, function($a, $b) use($collator){ return $collator->compare($a->LabelSort, $b->LabelSort); }); + + if($type == $returnType && $class == $returnClass){ + $retval = $feeds; + } + + apcu_store('feeds-index-' . $type . '-' . $class, $feeds); + } + } + + return $retval; + } + public static function RebuildCache(): void{ // We check a lockfile because this can be a long-running command. // We don't want to queue up a bunch of these in case someone is refreshing the index constantly. diff --git a/lib/Session.php b/lib/Session.php index 705fe54b..79a72e1f 100644 --- a/lib/Session.php +++ b/lib/Session.php @@ -67,7 +67,7 @@ class Session extends PropertiesBase{ return null; } - public static function SetSessionCookie($sessionId): void{ + public static function SetSessionCookie(string $sessionId): void{ setcookie('sessionid', $sessionId, time() + 60 * 60 * 24 * 14 * 1, '/', SITE_DOMAIN, true, false); // Expires in two weeks } diff --git a/scripts/deploy-ebook-to-www b/scripts/deploy-ebook-to-www index 1f0ea7df..4cfb7a63 100755 --- a/scripts/deploy-ebook-to-www +++ b/scripts/deploy-ebook-to-www @@ -143,6 +143,10 @@ if ! [ -f "${scriptsDir}"/generate-feeds ]; then die "\"${scriptsDir}\"/generate-feeds\" is not a file or could not be found." fi +if ! [ -f "${scriptsDir}"/generate-bulk-downloads ]; then + die "\"${scriptsDir}\"/generate-bulk-downloads\" is not a file or could not be found." +fi + mkdir -p "${webRoot}"/images/covers/ for dir in "$@" @@ -391,7 +395,7 @@ if [ "${queuedTasks}" = "false" ]; then printf "Rebuilding web library cache ... " fi - "${scriptsDir}"/rebuild-library-cache + "${scriptsDir}"/rebuild-cache library if [ "${verbose}" = "true" ]; then printf "Done.\n" @@ -410,6 +414,7 @@ if [ "${feeds}" = "true" ]; then fi "${scriptsDir}/generate-feeds" --webroot "${webRoot}" --weburl "${webUrl}" + "${scriptsDir}"/rebuild-cache feeds if [ "${verbose}" = "true" ]; then printf "Done.\n" @@ -429,6 +434,7 @@ if [ "${bulkDownloads}" = "true" ]; then fi "${scriptsDir}/generate-bulk-downloads" --webroot "${webRoot}" + "${scriptsDir}"/rebuild-cache bulk-downloads if [ "${verbose}" = "true" ]; then printf "Done.\n" diff --git a/scripts/rebuild-cache b/scripts/rebuild-cache new file mode 100755 index 00000000..54bcdd19 --- /dev/null +++ b/scripts/rebuild-cache @@ -0,0 +1,43 @@ +#!/bin/bash + +usage(){ + echo -n + fmt <" > /tmp/rebuild-cache.php +fi + +if [ "${type}" = "bulk-downloads" ]; then + echo "" > /tmp/rebuild-cache.php +fi + +if [ "${type}" = "feeds" ]; then + echo "" > /tmp/rebuild-cache.php +fi + +sudo -u www-data env SCRIPT_FILENAME=/tmp/rebuild-cache.php REQUEST_METHOD=GET cgi-fcgi -bind -connect "/run/php/standardebooks.org.sock" &> /dev/null +rm /tmp/rebuild-cache.php diff --git a/scripts/rebuild-library-cache b/scripts/rebuild-library-cache deleted file mode 100755 index 2b6f609c..00000000 --- a/scripts/rebuild-library-cache +++ /dev/null @@ -1,26 +0,0 @@ -#!/bin/bash - -usage(){ - echo -n - fmt <" > /tmp/rebuild-library-cache.php -sudo -u www-data env SCRIPT_FILENAME=/tmp/rebuild-library-cache.php REQUEST_METHOD=GET cgi-fcgi -bind -connect "/run/php/standardebooks.org.sock" &> /dev/null -rm /tmp/rebuild-library-cache.php diff --git a/www/bulk-downloads/collection.php b/www/bulk-downloads/collection.php index 3661ee02..8a79e3a7 100644 --- a/www/bulk-downloads/collection.php +++ b/www/bulk-downloads/collection.php @@ -5,9 +5,9 @@ use function Safe\apcu_fetch; use function Safe\preg_replace; $canDownload = false; -$name = HttpInput::Str(GET, 'name', false) ?? ''; +$class = HttpInput::Str(GET, 'class', false) ?? ''; -if($name != 'authors' && $name != 'collections' && $name != 'subjects' && $name != 'months'){ +if($class != 'authors' && $class != 'collections' && $class != 'subjects' && $class != 'months'){ Template::Emit404(); } @@ -18,14 +18,14 @@ if($GLOBALS['User'] !== null && $GLOBALS['User']->Benefits->CanBulkDownload){ $collection = []; try{ - $collection = apcu_fetch('bulk-downloads-' . $name); + $collection = apcu_fetch('bulk-downloads-' . $class); } catch(Safe\Exceptions\ApcuException $ex){ $result = Library::RebuildBulkDownloadsCache(); - $collection = $result[$name]; + $collection = $result[$class]; } -$title = preg_replace('/s$/', '', ucfirst($name)); +$title = preg_replace('/s$/', '', ucfirst($class)); ?> 'Downloads by ' . $title, 'highlight' => '', 'description' => 'Download zip files containing all of the Standard Ebooks in a given collection.']) ?>
@@ -34,8 +34,8 @@ $title = preg_replace('/s$/', '', ucfirst($name));

Patrons circle members get convenient access to zip files containing collections of different categories of ebooks. You can join the Patrons Circle with a small donation in support of our continuing mission to create free, beautiful digital literature, and download these collections files too.

-

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 which file format to download.

- +

These zip files contain each ebook in every format we offer, and are kept updated with the latest versions of each ebook. Read about which file format to download.

+ diff --git a/www/feeds/collection.php b/www/feeds/collection.php index 87e9c55f..9494a82f 100644 --- a/www/feeds/collection.php +++ b/www/feeds/collection.php @@ -6,10 +6,10 @@ use function Safe\glob; use function Safe\preg_replace; use function Safe\usort; -$name = HttpInput::Str(GET, 'name', false) ?? ''; +$class = HttpInput::Str(GET, 'class', false) ?? ''; $type = HttpInput::Str(GET, 'type', false) ?? ''; -if($name != 'authors' && $name != 'collections' && $name != 'subjects'){ +if($class != 'authors' && $class != 'collections' && $class != 'subjects'){ Template::Emit404(); } @@ -19,7 +19,7 @@ if($type != 'rss' && $type != 'atom'){ $feeds = []; -$lcTitle = preg_replace('/s$/', '', $name); +$lcTitle = preg_replace('/s$/', '', $class); $ucTitle = ucfirst($lcTitle); $ucType = 'RSS 2.0'; if($type === 'atom'){ @@ -27,36 +27,10 @@ if($type === 'atom'){ } try{ - $feeds = apcu_fetch('feeds-index-' . $type . '-' . $name); + $feeds = apcu_fetch('feeds-index-' . $type . '-' . $class); } catch(Safe\Exceptions\ApcuException $ex){ - $files = glob(WEB_ROOT . '/feeds/' . $type . '/' . $name . '/*.xml'); - - $feeds = []; - - foreach($files as $file){ - $obj = new stdClass(); - $obj->Url = '/feeds/' . $type . '/' . $name . '/' . basename($file, '.xml'); - - $obj->Label = exec('attr -g se-label ' . escapeshellarg($file)) ?: null; - if($obj->Label == null){ - $obj->Label = basename($file, '.xml'); - } - - $obj->LabelSort = exec('attr -g se-label-sort ' . escapeshellarg($file)) ?: null; - if($obj->LabelSort == null){ - $obj->LabelSort = basename($file, '.xml'); - } - - $feeds[] = $obj; - } - - $collator = Collator::create('en_US'); // Used for sorting letters with diacritics like in author names - if($collator !== null){ - usort($feeds, function($a, $b) use($collator){ return $collator->compare($a->LabelSort, $b->LabelSort); }); - } - - apcu_store('feeds-index-' . $type . '-' . $name, $feeds, 43200); // 12 hours + $feeds = Library::RebuildFeedsCache($type, $class); } ?> $ucType . ' Ebook Feeds by ' . $ucTitle, 'description' => 'A list of available ' . $ucType . ' feeds of Standard Ebooks ebooks by ' . $lcTitle . '.']) ?>
Scroll right →