WikiPage: Allow replaceSection on an nonexistent page
authorAdam Roses Wight <awight@wikimedia.org>
Thu, 5 Jun 2014 00:34:42 +0000 (17:34 -0700)
committerTheDJ <hartman.wiki@gmail.com>
Thu, 5 Jun 2014 20:31:17 +0000 (20:31 +0000)
This corrects a regression, where replaceSection would require that the
page have at least one revision in the database.  If the page does not
exist, continue with the replacement, using a null baseRevId.

Also: clean up a few comments in the replaceSection functions.

Bug: 66141
Change-Id: Ieb2cb05aee07a1d8bbdc3bdfe9536dfa8bee529e

includes/WikiPage.php

index 676d8d5..1c66ef5 100644 (file)
@@ -1479,7 +1479,7 @@ class WikiPage implements Page, IDBAccessObject {
         * @throws MWException
         * @return string New complete article text, or null if error.
         *
-        * @deprecated since 1.21, use replaceSectionContent() instead
+        * @deprecated since 1.21, use replaceSectionAtRev() instead
         */
        public function replaceSection( $section, $text, $sectionTitle = '',
                $edittime = null
@@ -1540,13 +1540,9 @@ class WikiPage implements Page, IDBAccessObject {
                if ( $edittime && $section !== 'new' ) {
                        $dbw = wfGetDB( DB_MASTER );
                        $rev = Revision::loadFromTimestamp( $dbw, $this->mTitle, $edittime );
-                       if ( !$rev ) {
-                               wfDebug( __METHOD__ . " given bad revision time for page " .
-                                       $this->getId() . "; edittime: $edittime)\n" );
-                               wfProfileOut( __METHOD__ );
-                               return null;
+                       if ( $rev ) {
+                               $baseRevId = $rev->getId();
                        }
-                       $baseRevId = $rev->getId();
                }
 
                wfProfileOut( __METHOD__ );
@@ -1583,12 +1579,12 @@ class WikiPage implements Page, IDBAccessObject {
                        if ( is_null( $baseRevId ) || $section == 'new' ) {
                                $oldContent = $this->getContent();
                        } else {
-                               // TODO: try DB_READ first
+                               // TODO: try DB_SLAVE first
                                $dbw = wfGetDB( DB_MASTER );
                                $rev = Revision::loadFromId( $dbw, $baseRevId );
 
                                if ( !$rev ) {
-                                       wfDebug( "WikiPage::replaceSection asked for bogus section (page: " .
+                                       wfDebug( __METHOD__ . " asked for bogus section (page: " .
                                                $this->getId() . "; section: $section; edittime: $edittime)\n" );
                                        wfProfileOut( __METHOD__ );
                                        return null;
@@ -1603,7 +1599,6 @@ class WikiPage implements Page, IDBAccessObject {
                                return null;
                        }
 
-                       // FIXME: $oldContent might be null?
                        $newContent = $oldContent->replaceSection( $section, $sectionContent, $sectionTitle );
                }