* Make Revision::newFromTitle() get the page_latest value from the master instead...
authorTim Starling <tstarling@users.mediawiki.org>
Fri, 26 Sep 2008 02:42:30 +0000 (02:42 +0000)
committerTim Starling <tstarling@users.mediawiki.org>
Fri, 26 Sep 2008 02:42:30 +0000 (02:42 +0000)
* Use inefficient replication-aware fetches only if replication is in use
* Update docs/database.txt on how to check for replication

docs/database.txt
includes/Revision.php

index 60e268c..e80a494 100644 (file)
@@ -74,7 +74,7 @@ want to write code destined for Wikipedia.
 It's often the case that the best algorithm to use for a given task
 depends on whether or not replication is in use. Due to our unabashed
 Wikipedia-centrism, we often just use the replication-friendly version,
-but if you like, you can use $wgLoadBalancer->getServerCount() > 1 to
+but if you like, you can use wfGetLB()->getServerCount() > 1 to
 check to see if replication is in use.
 
 === Lag ===
@@ -110,7 +110,7 @@ in the session, and then at the start of each request, waiting for the
 slave to catch up to that position before doing any reads from it. If
 this wait times out, reads are allowed anyway, but the request is
 considered to be in "lagged slave mode". Lagged slave mode can be
-checked by calling $wgLoadBalancer->getLaggedSlaveMode(). The only
+checked by calling wfGetLB()->getLaggedSlaveMode(). The only
 practical consequence at present is a warning displayed in the page
 footer.
 
index c369571..aa77bf9 100644 (file)
@@ -42,16 +42,24 @@ class Revision {
         * @return Revision
         */
        public static function newFromTitle( $title, $id = 0 ) {
-               if( $id ) {
-                       $matchId = intval( $id );
+               $conds = array( 
+                       'page_namespace' => $title->getNamespace(), 
+                       'page_title' => $title->getDBkey()
+               );
+               if ( $id ) {
+                       // Use the specified ID
+                       $conds['rev_id'] = $id;
+               } elseif ( wfGetLB()->getServerCount() > 1 ) {
+                       // Get the latest revision ID from the master
+                       $dbw = wfGetDB( DB_MASTER );
+                       $latest = $dbw->selectField( 'page', 'page_latest', $conds, __METHOD__ );
+                       $conds['rev_id'] = $latest;
                } else {
-                       $matchId = 'page_latest';
+                       // Use a join to get the latest revision
+                       $conds[] = 'rev_id=page_latest';
                }
-               return Revision::newFromConds(
-                       array( "rev_id=$matchId",
-                              'page_id=rev_page',
-                              'page_namespace' => $title->getNamespace(),
-                              'page_title'     => $title->getDBkey() ) );
+               $conds[] = 'page_id=rev_page';
+               return Revision::newFromConds( $conds );
        }
 
        /**
@@ -149,7 +157,7 @@ class Revision {
        private static function newFromConds( $conditions ) {
                $db = wfGetDB( DB_SLAVE );
                $row = Revision::loadFromConds( $db, $conditions );
-               if( is_null( $row ) ) {
+               if( is_null( $row ) && wfGetLB()->getServerCount() > 1 ) {
                        $dbw = wfGetDB( DB_MASTER );
                        $row = Revision::loadFromConds( $dbw, $conditions );
                }
@@ -237,7 +245,7 @@ class Revision {
                        array( 'page', 'revision' ),
                        $fields,
                        $conditions,
-                       'Revision::fetchRow',
+                       __METHOD__,
                        array( 'LIMIT' => 1 ) );
                $ret = $db->resultObject( $res );
                return $ret;
@@ -848,7 +856,7 @@ class Revision {
                                __METHOD__ );
                }
 
-               if( !$row ) {
+               if( !$row && wfGetLB()->getServerCount() > 1 ) {
                        // Possible slave lag!
                        $dbw = wfGetDB( DB_MASTER );
                        $row = $dbw->selectRow( 'text',
@@ -945,7 +953,7 @@ class Revision {
                        $conds['rev_page'] = $pageId;
                }
                $timestamp = $dbr->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__ );
-               if ( $timestamp === false ) {
+               if ( $timestamp === false && wfGetLB()->getServerCount() > 1 ) {
                        # Not in slave, try master
                        $dbw = wfGetDB( DB_MASTER );
                        $timestamp = $dbw->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__ );