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 ===
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.
* @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 );
}
/**
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 );
}
array( 'page', 'revision' ),
$fields,
$conditions,
- 'Revision::fetchRow',
+ __METHOD__,
array( 'LIMIT' => 1 ) );
$ret = $db->resultObject( $res );
return $ret;
__METHOD__ );
}
- if( !$row ) {
+ if( !$row && wfGetLB()->getServerCount() > 1 ) {
// Possible slave lag!
$dbw = wfGetDB( DB_MASTER );
$row = $dbw->selectRow( 'text',
$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__ );