From 7480b9250cbb561f1f3e9b65fe048f4a96afc88c Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Tue, 10 Mar 2015 20:33:52 -0700 Subject: [PATCH] Avoid excess master queries in getTimestampFromId Bug: T92357 Change-Id: Id5cf7a5620daeee12abfa778f9be19435f3e5e84 --- includes/Revision.php | 18 ++++++++---------- includes/api/ApiSetNotificationTimestamp.php | 6 ++++-- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/includes/Revision.php b/includes/Revision.php index d535028a92..cc00f9f152 100644 --- a/includes/Revision.php +++ b/includes/Revision.php @@ -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; } /** diff --git a/includes/api/ApiSetNotificationTimestamp.php b/includes/api/ApiSetNotificationTimestamp.php index 5d37e20dc5..dec64cc746 100644 --- a/includes/api/ApiSetNotificationTimestamp.php +++ b/includes/api/ApiSetNotificationTimestamp.php @@ -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 { -- 2.20.1