gutenberg.org admin documentation

(For a while, this will probably be a hodge-podge)

On this page:


per-ebook "landing pages"

  1. Someone requests (e.g.) https://www.gutenberg.org/ebooks/12

  2. _VG/html/.htaccess has a RewriteRule that proxies the latter URL to http://gutenberg2.ibiblio.org:8000/ebooks/12

  3. (In what follows, _AC = /home/marcello/prj/autocat3)

  4. On gutenberg2.ibiblio.org, the app server _AC/CherryPyApp.py routes the /ebooks/12 path to a BibrecPage instance.

  5. The BibrecPage class is defined in _AC/BibrecPage.py. Its index method constructs a BaseSearcher.OpenSearch instance os, setting various properties. In particular:

    The index method then passes os to Page.Page.format for rendering in HTML.

  6. _AC/templates/bibrec.html mostly controls what HTML to generate.

  7. The database's files table reflects the contents of the filesystem.


RDF files/catalog

Every day at 1am, Marcello's crontab invokes _VG/private/bin/cron-rebuild-files, which invokes ebookconverter --build=all on a range of 2000 ebooks, designed to cover the whole collection once a month.

(So, once we get to #56000, some books will be processed only in longer months, and once we get to #62000, some books will never be processed. As of 2017-03-26, we're at #54433 and growing by maybe 200 per month, so we should change the scheme before the end of 2017.)

Anyhow, ebookconverter (_VG/local/bin/ebookconverter, source mostly in _VG/local/lib/python3.4/site-packages/ebookconverter/) is a Python program that invokes ebookmaker, that creates (among other things) one RDF file per ebook: _VG/html/cache/epub/$num/pg$num.rdf

Then, every day at 5am, Marcello's crontab invokes _VG/private/bin/cron-rdf-catalog. On Thursdays, this first invokes ebookconverter --build=rdf ... But every day, it finds all the RDF files under _VG/html/cache/epub, makes a tar file of them (_VG/private/tmp/rdf/rdf-files.tar), compresses that using zip and bzip2, and puts the results at _VG/html/cache/epub/feeds/rdf-files.tar.{bz2,zip}


wiki

See the Special:Version page for lots of useful information.

Currently, _VG/html/w is a symlink to _VG/html/mediawiki-1.23.

skins

_VG/html/w/LocalSettings.php has a section entitled "Gutenberg Customization". Among other things, it says:

    require_once ("$IP/skins/GutenBook/GutenBook.php");
    require_once ("$IP/skins/GutenVector/GutenVector.php");

    $wgDefaultSkin = 'gutenvector';

which pulls in 2 skins, GutenBook and GutenVector (defined in their respective subdirectories of _VG/html/w/skins), and then sets GutenVector as the default.

GutenVector is Marcello's extension of MediaWiki's Vector skin.

tag extensions

_VG/html/w/LocalSettings.php also says:

    require_once ("$IP/extensions/EmailAddressImage/EmailAddressImage.php");
    require_once ("$IP/extensions/Cite/Cite.php");
    require_once ("extensions/QRCode.php");
    require_once ("extensions/QRCodeInline.php");
    require_once ("extensions/IFrame.php");

which defines 5 tag extensions:

database

The wiki uses a MariaDB database. For database connection details (including password), look in _VG/html/w/LocalSettings.php for variables $wgDBserver etc

To connect to the server from login1, invoke:

    source /opt/rh/rh-mariadb100/enable

(Better, put it in your .bashrc or .bash_profile or .profile.) This adds mysql-related stuff to your environment. Then:

    mysql --host=mysql.ibiblio.org --database=gutenberg_wiki --user=gutenberg -p

and enter the password when prompted. This starts the mysql/MariaDB client.

backup

Every day at 2am, Marcello's crontab invokes _VG/private/bin/cron, which (among other things) invokes _VG/private/bin/wikibackup and _VG/private/bin/wikidump.

_VG/private/bin/wikibackup:

  1. Runs mysqldump on the gutenberg_wiki database, which generates a file of SQL commands that could reconstruct the whole database from scratch. This is run through bzip2 and put in _VG/private/sqlbackups/$DATE-wikibackup.sql.bz2.
  2. Tries (but currently fails) to scp the file into pglaf's backups/sql/ directory.
  3. Copies any first-of-the-month wikibackup files into _VG/private/sqlbackups/monthly/.
  4. Deletes any files directly in _VG/private/sqlbackups/ that are older than 2 months.

_VG/private/bin/wikidump:

  1. Runs _VG/html/w/maintenance/dumpBackup.php, which generates an XML document that records the current revision of each page in the wiki. This is run through bzip2 and put in _VG/private/sqlbackups/$DATE-wikidump.bz2.
  2. Tries (but currently fails) to scp the file into pglaf's backups/sql/ directory.
  3. Copies any first-of-the-month wikidump files into _VG/private/sqlbackups/monthly/.
  4. Deletes any files directly in _VG/private/sqlbackups/ that are older than 2 months.

I'm not sure why we need wikidump.bz2, since it appears to contain just a subset of the info in wikibackup.sql.bz2.

Before compression, wikibackup.sql is about 50 times bigger than wikidump. After compression, it's about 30 times bigger.

upgrading

MediaWiki's wiki describes the upgrade process here. Note that some of that page assumes you're doing an in-place upgrade (i.e., the new files overwrite the old), which is not what we do. For a not-in-place upgrade (where the new files are in a separate directory from the old), the preferred procedure is somewhat buried here. Here's my procedure:

  1. Check the system requirements. Read the release notes.
  2. Download + unpack the tarball to a new dir.
  3. Copy customizations from the old dir to new dir. If necessary, upgrade extensions in the new dir.
                cd /public/vhost/g/gutenberg/html
                old=mediawiki-1.23
                new=mediawiki-1.28.2
    
                cp -p $old/LocalSettings.php $new/
    
                cp -p -r $old/images/[a-z0-9]*  $new/images/
                    # restrict to [a-z0-9]* to avoid overwriting images/{.htaccess,README}
    
                cp -p -r $old/skins/Guten* $new/skins
    
                # extensions:
                # Cite is bundled.
    
                cp -p -r $old/extensions/EmailAddressImage/ $new/extensions/
    
                cp -p $old/extensions/{IFrame,QRCode,QRCodeInline}.php $new/extensions/
    
                # No custom logo-image.
                # No top-level .htaccess
            
  4. In the new LocalSettings.php, make any necessary changes.
                Delete:
                    $wgEnableSorbs = true;           (dropped in 1.24)
                    $wgDisableCounters = true;       (dropped in 1.25)
                    $wgSessionsInObjectCache = true; (deprecated + always-true since 1.26)
            
  5. At this point, you might be able to test the new wiki via explicit URLs (e.g., via http://www.gutenberg.org/mediawiki-1.28.2/). But note that, at this point, following links will generally send you to the old code.
  6. Clear pending jobs. (There probably won't be any.)
                source scl_source enable rh-php56  # This adds 'php' to your path.
                php $old/maintenance/runJobs.php   # no output, exits after about 3 seconds
                  # Have to do this before you put the wiki in read-only mode.
            
  7. In the old LocalSettings.php, put the wiki into read-only mode. (This will only be visible to users if they try to edit a page.)
                $wgReadOnly = '2017-06-26: Upgrading to MediaWiki 1.28.2!';
            
  8. Back up the database. (Or confirm that nothing has happened since the last successful auto-backup.)
  9. Run the update script.
                php $new/maintenance/update.php | tee $new/update_output.jmdyck.txt
                # ... a few pages of output, then:
                # Done in 23 s.
            
    At this point, users may get "A database query error has occurred. This may indicate a bug in the software.", so proceed quickly to the next step.
  10. Shift the 'w' symlink to the new dir. (Note that this take the wiki out of read-only mode.)
                rm w && ln -s mediawiki-1.28.2 w
            
  11. Test. Make sure the extensions work. Make sure you can edit a page.

Later, ...

  1. _VG/private/lib/python/autocat/BookshelfScraper.py stopped working properly. It uses the MediaWiki 'action API' to get "bookshelf" pages and their content, and after the upgrade, the API queries started to receive only partial results. See here for some discussion of limits, and here for how to get the whole result-set when it is truncated due to limits. I modified the BookshelfScraper.py accordingly.
  2. Many pages (e.g., Animal_(Bookshelf)) would show the text "Favorite-icon-16x16.png" rather than rendering the icon itself. Ultimately, this was because every request for the image https://www.gutenberg.org/w/images/8/86/Favorite-icon-16x16.png would result in a 302-redirect to http://www.gutenberg.org/files/86/Favorite-icon-16x16.png, which doesn't exist.

    The bizarre rewriting of w/images/8 to files was eventually traced to mediawiki-1.28.2/images/.htaccess: when its RewriteRule was commented out, the redirect stopped happening.

    MediaWiki task T30235 describes why the Rewrite stuff is there, and it seems reasonable enough (modulo IE6). But why it caused the redirect for us makes no sense. Apparently it's an Apache bug: see MW task T64289, where people complain of similarly odd (though not identical) behaviour caused by the rewrite. However, the fix for that bug (adding Options +FollowSymLinks to the .htaccess file) doesn't work for us (and it's not clear to me why it worked for anyone).

    Anyhow, in the end, our solution was to take the .htaccess file out of the picture:

                mv .htaccess .htaccess.orig.moved_aside
            

captcha

What is the mechanism by which CAPTCHAs are triggered?

  1. _VG/html/.htaccess has a couple of RewriteRules that rewrite (some) requests for dirs/.* and cache/epub/.* into ratelimiter.php/....
  2. _VG/html/ratelimiter.php is a symbolic link to _VG/html/ratelimiter2.php
  3. (There's also a _VG/html/ratelimiter1.php, which appears to be an older, simpler version of _VG/html/ratelimiter2.php. I don't think it's used any more.)
  4. Normally, the ultimate effect of _VG/html/ratelimiter2.php is that it performs an Apache sub-request for the original URL. However, it first runs various checks that might cause something else to happen. The most notable of these are redirect_deep_links() and rate_limit().
  5. (In addition to the above chain of events, most of _VG/html/catalog/world/*.php start with a call to rate_limit().)
  6. Function rate_limit() is defined in _VG/private/lib/php/ratelimiter.phh. It sends a HEAD request to http://gutenberg2.ibiblio.org:8000/iplimit/ Back in function rate_limit(), we receive the response to the HEAD request and examine it. If the response contains the header X-Ratelimiter-Denied, then the function 307-redirects the original request to http://www.gutenberg.org/w/captcha/question/
  7. _VG/html/.htaccess has a RewriteRule that rewrites the latter URL to http://gutenberg2.ibiblio.org:8000/w/captcha/question/ with the 'P' flag for "Proxy"...
  8. ...?
Also:
  1. _VG/html/.htaccess has a few RewriteRules that proxy certain gutenberg.org URLs to http://gutenberg2.ibiblio.org:8000/... (I think this mainly applies to URLs whose path-part starts with /ebooks that haven't already been rewritten to /cache/epub URLs.)
  2. On gutenberg2.ibiblio.org, /etc/init/autocat3.conf ensures that there's always a python3 process running /home/marcello/prj/autocat3/CherryPyApp.py. It's an app server that listens on port 8000.
  3. The autocat3 app handles incoming requests...
  4. ... generates logs in /var/log/autocat/

apache logs

  1. Web requests for gutenberg.org are handled by a cluster of 5 web-servers: gutenweb{1-5}.int.ibiblio.org (AKA gutenweb{1-5}.lb.ibiblio.org) These are virtual hosts. (Currently, 1-3 are running CentOS 6.8 on oVirt, and 4+5 are running CentOS 7.0 on VMware. Eventually, 1-3 will migrate to the latter.)
  2. We don't have shell access to those servers, so we can't see the "live" apache logs.
  3. Every day at 1:15am, on each of the web-servers, some process extracts the logs for the web requests handled the previous day, and creates files in /public/scratch/logs/hosted_sites/www.gutenberg.org/, which we can see.
  4. ibiblio keeps these files for about a week.
  5. Every day at 06:00am, a cron job in marcello's crontab invokes _VG/private/bin/cron-analog, which:

If, for whatever reason, there are logs in /public/scratch/logs/hosted_sites/www.gutenberg.org/ that didn't get processed, you can catch up with commands such as:

    cd /public/vhost/g/gutenberg/private/bin
    . ./env
    export PGPORT=5432
    ./cron-topten 2016-09-01
    ./analog/cron.pl --copy --daily --date=2016-09-01

(Note that when you invoke cron-topten with a specific date argument like that, it skips the last 2 steps, i.e. it doesn't call make-topten.php or email a report. Expect it to take about 13 minutes to run.)

(Hm, it looks like cron.pl only works if you're marcello, due to directory permissions.)


database

Postgres

psql --host gutenberg-pg1.int.ibiblio.org --username gutenberg --dbname gutenberg

    gutenberg=> \dt
                     List of relations
     Schema |         Name         | Type  |   Owner   
    --------+----------------------+-------+-----------
     public | aliases              | table | gutenberg
     public | attriblist           | table | gutenberg
     public | attributes           | table | gutenberg
     public | author_urls          | table | gutenberg
     public | authors              | table | gutenberg
     public | books                | table | gutenberg
     public | bookshelves          | table | gutenberg
     public | categories           | table | gutenberg
     public | changelog            | table | gutenberg
     public | compressions         | table | gutenberg
     public | dcmitypes            | table | gutenberg
     public | dpid                 | table | gutenberg
     public | encodings            | table | gutenberg
     public | filecount            | table | gutenberg
     public | files                | table | gutenberg
     public | filetypes            | table | gutenberg
     public | fts                  | table | gutenberg
     public | langs                | table | gutenberg
     public | loccs                | table | gutenberg
     public | mirrors              | table | gutenberg
     public | mn_books_authors     | table | gutenberg
     public | mn_books_bookshelves | table | gutenberg
     public | mn_books_categories  | table | gutenberg
     public | mn_books_langs       | table | gutenberg
     public | mn_books_loccs       | table | gutenberg
     public | mn_books_subjects    | table | gutenberg
     public | mn_users_permissions | table | gutenberg
     public | permissions          | table | gutenberg
     public | roles                | table | gutenberg
     public | subjects             | table | gutenberg
     public | terms                | table | gutenberg
     public | tweets               | table | gutenberg
     public | users                | table | gutenberg
    (33 rows)

'push' system

  1. When a whitewasher is finished with a book, they deposit a properly-formed .zip file (named, e.g. 52678.zip) in /home/push on pglaf.org.
  2. On pglaf.org, a cron job (in gbnewby's crontab) (which runs hourly at 20 past) invokes /home/gbnewby/.bin/dopush.sh, which goes to /home/push, and for each zip file it finds there, does the following:
    1. Copies it (via scp) into ~gbnewby/.tmp/pg-upload/ on login.ibiblio.org.
    2. Shells to login.ibiblio.org to unzip it to ~gbnewby/ftp/$numpath/ and then discard it.
      • ~gbnewby/ftp is a symlink to _FG/
      • $numpath denotes a path within the PG post-10k hierarchy. E.g., for $num = 12345, $numpath = 1/2/3/4/12345
    3. Shells to login.ibiblio.org to create an empty 'trigger' file _VG/private/logs/dopush/$num.zip.trig
    4. Repeats the above steps (except for creating the trigger file) on readingroo.ms and aleph.gutenberg.org.
    Output is appended to /home/gbnewby/logs/dopush-log.txt
  3. On login.ibiblio.org, every 20 minutes, a cron job (in marcello's crontab) invokes _VG/private/bin/cron-dopush, which:
    1. invokes _VG/private/lib/python/autocat/FileInfo.py, which looks in _VG/private/logs/dopush for $num.zip.trig files, and for each, calls scan_directory, which:
      1. scans _VG/html/files/$num/ (which is a symlink to _FG/$numpath) for files, and for each:
        1. calls scan_file or scan_zip, which ... prints file metadata
        2. spawns _VG/private/bin/bitcollider-0.6.0 in a subprocess and then prints out various hashes
      2. moves the $num.zip.trig file to _VG/private/logs/dopush/backup/
      (stdout of FileInfo.py is piped to autocat.php)
    2. invokes _VG/private/lib/python/autocat/autocat.php, which takes in the output of FileInfo.py (echoing it to stdout), breaks it into sections (one per file in _VG/html/files/$num/), extracts info from the lines, and updates the database (adding a few lines per file to stdout).
    3. invokes _VG/private/lib/python/autocat/AutoDelete.py, which extracts filenames from the database, checks which files still exist in the filesystem, and updates the database for any which don't.
    4. invokes _VG/local/bin/ebookconverter -v --range=1- --goback=24 twice: once with --make=all and once with --build=facebook --build=twitter
    5. fetches http://gutenberg2:8000/covers/small/latest/10, saves it in _VG/html/cache/latest-covers.html.utf8, and zips that up into _VG/html/cache/latest-covers.html.utf8.gzip.

If an ebook's auto-generated content is not appearing in the catalog,

  1. On pglaf.org, check /home/gbnewby/logs/dopush-log.txt. Scan for the ebook number to find the record(s) of it being pushed. You might see something like:
            checkdir error:  cannot create 52674/52674-h/images
            File exists
            unable to process 52674/52674-h/images/img-cover.jpg.
            
    I don't know why the images dir would already exist, or why its pre-existence would cause unzip to fail to put an image file in it, but the absence of a cover-page image will cause the auto-generation of epub and kindle versions to fail. You should be able to fix it (at least partly) by re-pushing the file:
            sudo mv /home/DONE/$num.zip /home/push
            
    and wait for the next run of pglaf's dopush.
  2. After that, on ibiblio.org, the next run of cron-dopush should handle the FileInfo and autocat processing for the re-push. However, when it invokes ebookconverter, that might or might not take care of rebuilding the missing files. (Check _VG/private/logs/epub-maker/$timestamp.log.) If it's been less than 24 hours since the problematic push, the files will probably be generated. If not, you should be able to do it yourself (on ibiblio.org):
            source /public/vhost/g/gutenberg/private/bin/env
            $LOCAL/bin/ebookconverter -v --make=all --range=$num
            
    You should get a single line of output of the form:
            Logging to: /public/vhost/g/gutenberg/private/logs/epub-maker/$timestamp.log
            
    Look at that file, and also _VG/html/cache/epub/$num/pg$num.converter.log, but note that the latter may start with earlier output.

file-system

~gbnewby