Handle empty revision table in populateArchiveRevId.php
authorJames Montalvo <edwin.j.montalvo@nasa.gov>
Mon, 10 Sep 2018 19:45:37 +0000 (14:45 -0500)
committerJames D. Forrester <jforrester@wikimedia.org>
Fri, 28 Sep 2018 00:18:14 +0000 (17:18 -0700)
Running update.php for new wikis without any revisions yet fails when
update.php attempts to run populateArchiveRevId.php. This problem
does not exist if using the web installer or running
maintenance/install.php, since both of these generate a Main Page
revision. Manually generating a MediaWiki database by sourcing
maintenance/tables.sql does not generate any revisions, and thus is
susceptible to this problem.

Bug: T203982

Change-Id: Ifd78c50fb1e11f82340cd83a10fa903b7c5fc1e7

maintenance/populateArchiveRevId.php

index 60f5e8a..c03eb24 100644 (file)
@@ -220,7 +220,43 @@ class PopulateArchiveRevId extends LoggedUpdateMaintenance {
                        );
                }
                if ( !$rev ) {
-                       throw new UnexpectedValueException( 'No revisions are available to copy' );
+                       // Since no revisions are available to copy, generate a dummy
+                       // revision to a dummy page, then rollback the commit
+                       wfDebug( __METHOD__ . ": No revisions are available to copy\n" );
+
+                       $dbw->begin();
+
+                       // Make a title and revision and insert them
+                       $title = Title::newFromText( "PopulateArchiveRevId_4b05b46a81e29" );
+                       $page = WikiPage::factory( $title );
+                       $updater = $page->newPageUpdater(
+                               User::newSystemUser( 'Maintenance script', [ 'steal' => true ] )
+                       );
+                       $updater->setContent(
+                               'main',
+                               ContentHandler::makeContent( "Content for dummy rev", $title )
+                       );
+                       $updater->saveRevision(
+                               CommentStoreComment::newUnsavedComment( 'dummy rev summary' ),
+                               EDIT_NEW | EDIT_SUPPRESS_RC
+                       );
+
+                       // get the revision row just inserted
+                       $rev = $dbw->selectRow(
+                               'revision',
+                               '*',
+                               [],
+                               __METHOD__,
+                               [ 'ORDER BY' => 'rev_timestamp ASC' ]
+                       );
+
+                       $dbw->rollback();
+               }
+               if ( !$rev ) {
+                       // This should never happen.
+                       throw new UnexpectedValueException(
+                               'No revisions are available to copy, and one couldn\'t be created'
+                       );
                }
 
                unset( $rev->rev_id );