Avoid excess master queries in getTimestampFromId
authorAaron Schulz <aschulz@wikimedia.org>
Wed, 11 Mar 2015 03:33:52 +0000 (20:33 -0700)
committerAaron Schulz <aschulz@wikimedia.org>
Wed, 11 Mar 2015 21:37:14 +0000 (14:37 -0700)
Bug: T92357
Change-Id: Id5cf7a5620daeee12abfa778f9be19435f3e5e84

includes/Revision.php
includes/api/ApiSetNotificationTimestamp.php

index d535028..cc00f9f 100644 (file)
@@ -1684,23 +1684,21 @@ class Revision implements IDBAccessObject {
         *
         * @param Title $title
         * @param int $id
-        * @return string
+        * @return string|bool False if not found
         */
-       static function getTimestampFromId( $title, $id ) {
-               $dbr = wfGetDB( DB_SLAVE );
+       static function getTimestampFromId( $title, $id, $flags = 0 ) {
+               $db = ( $flags & self::READ_LATEST )
+                       ? wfGetDB( DB_MASTER )
+                       : wfGetDB( DB_SLAVE );
                // Casting fix for databases that can't take '' for rev_id
                if ( $id == '' ) {
                        $id = 0;
                }
                $conds = array( 'rev_id' => $id );
                $conds['rev_page'] = $title->getArticleID();
-               $timestamp = $dbr->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__ );
-               if ( $timestamp === false && wfGetLB()->getServerCount() > 1 ) {
-                       # Not in slave, try master
-                       $dbw = wfGetDB( DB_MASTER );
-                       $timestamp = $dbw->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__ );
-               }
-               return wfTimestamp( TS_MW, $timestamp );
+               $timestamp = $db->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__ );
+
+               return ( $timestamp !== false ) ? wfTimestamp( TS_MW, $timestamp ) : false;
        }
 
        /**
index 5d37e20..dec64cc 100644 (file)
@@ -73,7 +73,8 @@ class ApiSetNotificationTimestamp extends ApiBase {
                        }
                        $title = reset( $pageSet->getGoodTitles() );
                        if ( $title ) {
-                               $timestamp = Revision::getTimestampFromId( $title, $params['torevid'] );
+                               $timestamp = Revision::getTimestampFromId(
+                                       $title, $params['torevid'], Revision::READ_LATEST );
                                if ( $timestamp ) {
                                        $timestamp = $dbw->timestamp( $timestamp );
                                } else {
@@ -86,7 +87,8 @@ class ApiSetNotificationTimestamp extends ApiBase {
                        }
                        $title = reset( $pageSet->getGoodTitles() );
                        if ( $title ) {
-                               $revid = $title->getNextRevisionID( $params['newerthanrevid'] );
+                               $revid = $title->getNextRevisionID(
+                                       $params['newerthanrevid'], Title::GAID_FOR_UPDATE );
                                if ( $revid ) {
                                        $timestamp = $dbw->timestamp( Revision::getTimestampFromId( $title, $revid ) );
                                } else {