const DELETED_USER = 4;
const DELETED_RESTRICTED = 8;
+ // Audience options for Revision::getText()
+ const FOR_PUBLIC = 1;
+ const FOR_THIS_USER = 2;
+ const RAW = 3;
+
/**
* Load a page revision from a given revision ID number.
* Returns null if no such revision can be found.
* @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;
}
/**
- * Fetch revision's user id if it's available to all users
+ * Fetch revision's user id if it's available to the specified audience.
+ * If the specified audience does not have access to it, zero will be
+ * returned.
+ *
+ * @param integer $audience One of:
+ * Revision::FOR_PUBLIC to be displayed to all users
+ * Revision::FOR_THIS_USER to be displayed to $wgUser
+ * Revision::RAW get the ID regardless of permissions
+ *
+ *
* @return int
*/
- public function getUser() {
- if( $this->isDeleted( self::DELETED_USER ) ) {
+ public function getUser( $audience = self::FOR_PUBLIC ) {
+ if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_USER ) ) {
+ return 0;
+ } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_USER ) ) {
return 0;
} else {
return $this->mUser;
}
/**
- * Fetch revision's username if it's available to all users
+ * Fetch revision's username if it's available to the specified audience.
+ * If the specified audience does not have access to the username, an
+ * empty string will be returned.
+ *
+ * @param integer $audience One of:
+ * Revision::FOR_PUBLIC to be displayed to all users
+ * Revision::FOR_THIS_USER to be displayed to $wgUser
+ * Revision::RAW get the text regardless of permissions
+ *
* @return string
*/
- public function getUserText() {
- if( $this->isDeleted( self::DELETED_USER ) ) {
+ public function getUserText( $audience = self::FOR_PUBLIC ) {
+ if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_USER ) ) {
+ return "";
+ } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_USER ) ) {
return "";
} else {
return $this->mUserText;
}
/**
- * Fetch revision comment if it's available to all users
+ * Fetch revision comment if it's available to the specified audience.
+ * If the specified audience does not have access to the comment, an
+ * empty string will be returned.
+ *
+ * @param integer $audience One of:
+ * Revision::FOR_PUBLIC to be displayed to all users
+ * Revision::FOR_THIS_USER to be displayed to $wgUser
+ * Revision::RAW get the text regardless of permissions
+ *
* @return string
*/
- function getComment() {
- if( $this->isDeleted( self::DELETED_COMMENT ) ) {
+ function getComment( $audience = self::FOR_PUBLIC ) {
+ if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_COMMENT ) ) {
+ return "";
+ } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_COMMENT ) ) {
return "";
} else {
return $this->mComment;
}
/**
- * Fetch revision text if it's available to all users
+ * Fetch revision text if it's available to the specified audience.
+ * If the specified audience does not have the ability to view this
+ * revision, an empty string will be returned.
+ *
+ * @param integer $audience One of:
+ * Revision::FOR_PUBLIC to be displayed to all users
+ * Revision::FOR_THIS_USER to be displayed to $wgUser
+ * Revision::RAW get the text regardless of permissions
+ *
+ *
* @return string
*/
- public function getText( $isPublic = true ) {
- if( $isPublic && $this->isDeleted( self::DELETED_TEXT ) ) {
+ public function getText( $audience = self::FOR_PUBLIC ) {
+ if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_TEXT ) ) {
return "";
- } else if( !$this->userCan( self::DELETED_TEXT ) ) {
+ } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_TEXT ) ) {
return "";
} else {
return $this->getRawText();
}
}
+ /**
+ * Alias for getText(Revision::FOR_THIS_USER)
+ */
+ public function revText() {
+ return $this->getText( self::FOR_THIS_USER );
+ }
+
/**
* Fetch revision text without regard for view restrictions
* @return string
# Write to external storage if required
if( $wgDefaultExternalStore ) {
// Store and get the URL
- $data = ExternalStore::randomInsert( $data );
+ $data = ExternalStore::insertToDefault( $data );
if( !$data ) {
throw new MWException( "Unable to store text to external storage" );
}
__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__ );