From: Aaron Schulz Date: Sat, 19 May 2012 20:41:41 +0000 (-0700) Subject: Added UserCache class for doing name/title batch lookups. X-Git-Tag: 1.31.0-rc.0~22738 X-Git-Url: http://git.cyclocoop.org/%28?a=commitdiff_plain;h=1b7045e341f898a45e6831ba04406a97c4802cda;p=lhc%2Fweb%2Fwiklou.git Added UserCache class for doing name/title batch lookups. * Made Special:ListFiles be the first user of this class. Change-Id: I2ea068d4765fe6ae12445786c38217119e79f823 --- diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index 2ea025e8c3..752f09c93a 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -258,6 +258,7 @@ $wgAutoloadLocalClasses = array( 'UserArrayFromResult' => 'includes/UserArray.php', 'UserBlockedError' => 'includes/Exception.php', 'UserNotLoggedIn' => 'includes/Exception.php', + 'UserCache' => 'includes/cache/UserCache.php', 'UserMailer' => 'includes/UserMailer.php', 'UserRightsProxy' => 'includes/UserRightsProxy.php', 'ViewCountUpdate' => 'includes/ViewCountUpdate.php', diff --git a/includes/User.php b/includes/User.php index f1232679e4..a09d129b34 100644 --- a/includes/User.php +++ b/includes/User.php @@ -468,8 +468,7 @@ class User { * @return String|bool The corresponding username */ public static function whoIs( $id ) { - $dbr = wfGetDB( DB_SLAVE ); - return $dbr->selectField( 'user', 'user_name', array( 'user_id' => $id ), __METHOD__ ); + return UserCache::singleton()->getProp( $id, 'name' ); } /** @@ -479,8 +478,7 @@ class User { * @return String|bool The corresponding user's real name */ public static function whoIsReal( $id ) { - $dbr = wfGetDB( DB_SLAVE ); - return $dbr->selectField( 'user', 'user_real_name', array( 'user_id' => $id ), __METHOD__ ); + return UserCache::singleton()->getProp( $id, 'real_name' ); } /** diff --git a/includes/cache/UserCache.php b/includes/cache/UserCache.php new file mode 100644 index 0000000000..6ec2366907 --- /dev/null +++ b/includes/cache/UserCache.php @@ -0,0 +1,134 @@ + property => value) + protected $typesCached = array(); // (uid => cache type => 1) + + /** + * @return UserCache + */ + public static function singleton() { + static $instance = null; + if ( $instance === null ) { + $instance = new self(); + } + return $instance; + } + + protected function __construct() {} + + /** + * Get a property of a user based on their user ID + * + * @param $userId integer User ID + * @param $prop string User property + * @return mixed The property or false if the user does not exist + */ + public function getProp( $userId, $prop ) { + if ( !isset( $this->cache[$userId][$prop] ) ) { + wfDebug( __METHOD__ . ": querying DB for prop '$prop' for user ID '$userId'.\n" ); + $this->doQuery( array( $userId ) ); // cache miss + } + return isset( $this->cache[$userId][$prop] ) + ? $this->cache[$userId][$prop] + : false; // user does not exist? + } + + /** + * Preloads user names for given list of users. + * @param $userIds Array List of user IDs + * @param $options Array Option flags; include 'userpage' and 'usertalk' + * @param $caller String: the calling method + */ + public function doQuery( array $userIds, $options = array(), $caller = '' ) { + wfProfileIn( __METHOD__ ); + + $usersToCheck = array(); + $usersToQuery = array(); + + foreach ( $userIds as $userId ) { + $userId = (int)$userId; + if ( $userId <= 0 ) { + continue; // skip anons + } + if ( isset( $this->cache[$userId]['name'] ) ) { + $usersToCheck[$userId] = $this->cache[$userId]['name']; // already have name + } else { + $usersToQuery[] = $userId; // we need to get the name + } + } + + // Lookup basic info for users not yet loaded... + if ( count( $usersToQuery ) ) { + $dbr = wfGetDB( DB_SLAVE ); + $table = array( 'user' ); + $conds = array( 'user_id' => $usersToQuery ); + $fields = array( 'user_name', 'user_real_name', 'user_registration', 'user_id' ); + + $comment = __METHOD__; + if ( strval( $caller ) !== '' ) { + $comment .= "/$caller"; + } + + $res = $dbr->select( $table, $fields, $conds, $comment ); + foreach ( $res as $row ) { // load each user into cache + $userId = (int)$row->user_id; + $this->cache[$userId]['name'] = $row->user_name; + $this->cache[$userId]['real_name'] = $row->user_real_name; + $this->cache[$userId]['registration'] = $row->user_registration; + $usersToCheck[$userId] = $row->user_name; + } + } + + $lb = new LinkBatch(); + foreach ( $usersToCheck as $userId => $name ) { + if ( $this->queryNeeded( $userId, 'userpage', $options ) ) { + $lb->add( NS_USER, str_replace( ' ', '_', $row->user_name ) ); + $this->typesCached[$userId]['userpage'] = 1; + } + if ( $this->queryNeeded( $userId, 'usertalk', $options ) ) { + $lb->add( NS_USER_TALK, str_replace( ' ', '_', $row->user_name ) ); + $this->typesCached[$userId]['usertalk'] = 1; + } + } + $lb->execute(); + + wfProfileOut( __METHOD__ ); + } + + /** + * Check if a cache type is in $options and was not loaded for this user + * + * @param $uid integer user ID + * @param $type string Cache type + * @param $options Array Requested cache types + * @return bool + */ + protected function queryNeeded( $uid, $type, array $options ) { + return ( in_array( $type, $options ) && !isset( $this->typesCached[$uid][$type] ) ); + } +} diff --git a/includes/specials/SpecialListfiles.php b/includes/specials/SpecialListfiles.php index e5aed18262..cc05522124 100644 --- a/includes/specials/SpecialListfiles.php +++ b/includes/specials/SpecialListfiles.php @@ -174,20 +174,14 @@ class ImageListPager extends TablePager { return 'img_timestamp'; } - function getStartBody() { - # Do a link batch query for user pages - if ( $this->mResult->numRows() ) { - $lb = new LinkBatch; - $this->mResult->seek( 0 ); - foreach ( $this->mResult as $row ) { - if ( $row->img_user ) { - $lb->add( NS_USER, str_replace( ' ', '_', $row->img_user_text ) ); - } - } - $lb->execute(); + function doBatchLookups() { + $userIds = array(); + $this->mResult->seek( 0 ); + foreach ( $this->mResult as $row ) { + $userIds[] = $row->img_user; } - - return parent::getStartBody(); + # Do a link batch query for names and userpages + UserCache::singleton()->doQuery( $userIds, array( 'userpage' ), __METHOD__ ); } function formatValue( $field, $value ) { @@ -217,9 +211,10 @@ class ImageListPager extends TablePager { } case 'img_user_text': if ( $this->mCurrentRow->img_user ) { + $name = User::whoIs( $this->mCurrentRow->img_user ); $link = Linker::link( - Title::makeTitle( NS_USER, $value ), - htmlspecialchars( $value ) + Title::makeTitle( NS_USER, $name ), + htmlspecialchars( $name ) ); } else { $link = htmlspecialchars( $value );