From 5dfe3d644471ea4cc3391099d1f23951300addd3 Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Fri, 21 Oct 2011 23:20:52 +0000 Subject: [PATCH] Revision objects now always use the current name of users, loading it on demand if necessary (e.g. when given a $row with no user_name but rev_user is not 0) --- includes/Revision.php | 41 +++++++++++++++++++++++++++++++++-------- includes/User.php | 6 +++--- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/includes/Revision.php b/includes/Revision.php index ba885dc0bd..ec0508c3ff 100644 --- a/includes/Revision.php +++ b/includes/Revision.php @@ -4,6 +4,22 @@ * @todo document */ class Revision { + protected $mId; + protected $mPage; + protected $mUserText; + protected $mOrigUserText; + protected $mUser; + protected $mMinorEdit; + protected $mTimestamp; + protected $mDeleted; + protected $mSize; + protected $mParentId; + protected $mComment; + protected $mText; + protected $mTextRow; + protected $mTitle; + protected $mCurrent; + const DELETED_TEXT = 1; const DELETED_COMMENT = 2; const DELETED_USER = 4; @@ -348,7 +364,7 @@ class Revision { $this->mDeleted = intval( $row->rev_deleted ); if( !isset( $row->rev_parent_id ) ) { - $this->mParentId = is_null($row->rev_parent_id) ? null : 0; + $this->mParentId = is_null( $row->rev_parent_id ) ? null : 0; } else { $this->mParentId = intval( $row->rev_parent_id ); } @@ -376,13 +392,14 @@ class Revision { $this->mTextRow = null; } - // Use user_name for users and rev_user_text for IPs. - // Also fallback to rev_user_text if user_name not given. - if ( isset( $row->user_name ) ) { - $this->mUserText = $row->user_name; // logged-in user (ideally) - } else { - $this->mUserText = $row->rev_user_text; // IP user (ideally) + // Use user_name for users and rev_user_text for IPs... + $this->mUserText = null; // lazy load if left null + if ( $this->mUser == 0 ) { + $this->mUserText = $row->rev_user_text; // IP user + } elseif ( isset( $row->user_name ) ) { + $this->mUserText = $row->user_name; // logged-in user } + $this->mOrigUserText = $row->rev_user_text; } elseif( is_array( $row ) ) { // Build a new revision to be saved... global $wgUser; @@ -542,7 +559,7 @@ class Revision { } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_USER, $user ) ) { return ''; } else { - return $this->mUserText; + return $this->getRawUserText(); } } @@ -552,6 +569,14 @@ class Revision { * @return String */ public function getRawUserText() { + if ( $this->mUserText === null ) { + $this->mUserText = User::whoIs( $this->mUser ); // load on demand + if ( $this->mUserText === false ) { + # This shouldn't happen, but it can if the wiki was recovered + # via importing revs and there is no user table entry yet. + $this->mUserText = $this->mOrigUserText; + } + } return $this->mUserText; } diff --git a/includes/User.php b/includes/User.php index 7657a7eb53..e3e2c9a2b2 100644 --- a/includes/User.php +++ b/includes/User.php @@ -448,9 +448,9 @@ class User { /** * Get the username corresponding to a given user ID * @param $id Int User ID - * @return String The corresponding username + * @return String|false The corresponding username */ - static function whoIs( $id ) { + public static function whoIs( $id ) { $dbr = wfGetDB( DB_SLAVE ); return $dbr->selectField( 'user', 'user_name', array( 'user_id' => $id ), __METHOD__ ); } @@ -459,7 +459,7 @@ class User { * Get the real name of a user given their user ID * * @param $id Int User ID - * @return String The corresponding user's real name + * @return String|false The corresponding user's real name */ public static function whoIsReal( $id ) { $dbr = wfGetDB( DB_SLAVE ); -- 2.20.1