Merge "Removed "page-lastedit" cache key trick from WikiPage"
[lhc/web/wiklou.git] / includes / page / WikiPage.php
index f2a3c79..5caffab 100644 (file)
@@ -1246,6 +1246,13 @@ class WikiPage implements Page, IDBAccessObject {
        ) {
                global $wgContentHandlerUseDB;
 
+               // Assertion to try to catch T92046
+               if ( (int)$revision->getId() === 0 ) {
+                       throw new InvalidArgumentException(
+                               __METHOD__ . ': Revision has ID ' . var_export( $revision->getId(), 1 )
+                       );
+               }
+
                $content = $revision->getContent();
                $len = $content ? $content->getSize() : 0;
                $rt = $content ? $content->getUltimateRedirectTarget() : null;
@@ -1480,8 +1487,18 @@ class WikiPage implements Page, IDBAccessObject {
 
                $baseRevId = null;
                if ( $edittime && $sectionId !== 'new' ) {
-                       $dbw = wfGetDB( DB_MASTER );
-                       $rev = Revision::loadFromTimestamp( $dbw, $this->mTitle, $edittime );
+                       $dbr = wfGetDB( DB_SLAVE );
+                       $rev = Revision::loadFromTimestamp( $dbr, $this->mTitle, $edittime );
+                       // Try the master if this thread may have just added it.
+                       // This could be abstracted into a Revision method, but we don't want
+                       // to encourage loading of revisions by timestamp.
+                       if ( !$rev
+                               && wfGetLB()->getServerCount() > 1
+                               && wfGetLB()->hasOrMadeRecentMasterChanges()
+                       ) {
+                               $dbw = wfGetDB( DB_MASTER );
+                               $rev = Revision::loadFromTimestamp( $dbw, $this->mTitle, $edittime );
+                       }
                        if ( $rev ) {
                                $baseRevId = $rev->getId();
                        }
@@ -1520,10 +1537,7 @@ class WikiPage implements Page, IDBAccessObject {
                        if ( is_null( $baseRevId ) || $sectionId === 'new' ) {
                                $oldContent = $this->getContent();
                        } else {
-                               // TODO: try DB_SLAVE first
-                               $dbw = wfGetDB( DB_MASTER );
-                               $rev = Revision::loadFromId( $dbw, $baseRevId );
-
+                               $rev = Revision::newFromId( $baseRevId );
                                if ( !$rev ) {
                                        wfDebug( __METHOD__ . " asked for bogus section (page: " .
                                                $this->getId() . "; section: $sectionId)\n" );