Added UserCache class for doing name/title batch lookups.
authorAaron Schulz <aschulz@wikimedia.org>
Sat, 19 May 2012 20:41:41 +0000 (13:41 -0700)
committerCatrope <roan.kattouw@gmail.com>
Tue, 14 Aug 2012 21:59:03 +0000 (14:59 -0700)
* Made Special:ListFiles be the first user of this class.

Change-Id: I2ea068d4765fe6ae12445786c38217119e79f823

includes/AutoLoader.php
includes/User.php
includes/cache/UserCache.php [new file with mode: 0644]
includes/specials/SpecialListfiles.php

index 2ea025e..752f09c 100644 (file)
@@ -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',
index f123267..a09d129 100644 (file)
@@ -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 (file)
index 0000000..6ec2366
--- /dev/null
@@ -0,0 +1,134 @@
+<?php
+/**
+ * Caches current user names and other info based on user IDs.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @ingroup Cache
+ */
+
+/**
+ * @since 1.20
+ */
+class UserCache {
+       protected $cache = array(); // (uid => 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] ) );
+       }
+}
index e5aed18..cc05522 100644 (file)
@@ -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 );