MediaWiki  REL1_22
UserCache.php
Go to the documentation of this file.
00001 <?php
00027 class UserCache {
00028     protected $cache = array(); // (uid => property => value)
00029     protected $typesCached = array(); // (uid => cache type => 1)
00030 
00034     public static function singleton() {
00035         static $instance = null;
00036         if ( $instance === null ) {
00037             $instance = new self();
00038         }
00039         return $instance;
00040     }
00041 
00042     protected function __construct() {}
00043 
00051     public function getProp( $userId, $prop ) {
00052         if ( !isset( $this->cache[$userId][$prop] ) ) {
00053             wfDebug( __METHOD__ . ": querying DB for prop '$prop' for user ID '$userId'.\n" );
00054             $this->doQuery( array( $userId ) ); // cache miss
00055         }
00056         return isset( $this->cache[$userId][$prop] )
00057             ? $this->cache[$userId][$prop]
00058             : false; // user does not exist?
00059     }
00060 
00068     public function getUserName( $userId, $ip ) {
00069         return $userId > 0 ? $this->getProp( $userId, 'name' ) : $ip;
00070     }
00071 
00078     public function doQuery( array $userIds, $options = array(), $caller = '' ) {
00079         wfProfileIn( __METHOD__ );
00080 
00081         $usersToCheck = array();
00082         $usersToQuery = array();
00083 
00084         $userIds = array_unique( $userIds );
00085 
00086         foreach ( $userIds as $userId ) {
00087             $userId = (int)$userId;
00088             if ( $userId <= 0 ) {
00089                 continue; // skip anons
00090             }
00091             if ( isset( $this->cache[$userId]['name'] ) ) {
00092                 $usersToCheck[$userId] = $this->cache[$userId]['name']; // already have name
00093             } else {
00094                 $usersToQuery[] = $userId; // we need to get the name
00095             }
00096         }
00097 
00098         // Lookup basic info for users not yet loaded...
00099         if ( count( $usersToQuery ) ) {
00100             $dbr = wfGetDB( DB_SLAVE );
00101             $table = array( 'user' );
00102             $conds = array( 'user_id' => $usersToQuery );
00103             $fields = array( 'user_name', 'user_real_name', 'user_registration', 'user_id' );
00104 
00105             $comment = __METHOD__;
00106             if ( strval( $caller ) !== '' ) {
00107                 $comment .= "/$caller";
00108             }
00109 
00110             $res = $dbr->select( $table, $fields, $conds, $comment );
00111             foreach ( $res as $row ) { // load each user into cache
00112                 $userId = (int)$row->user_id;
00113                 $this->cache[$userId]['name'] = $row->user_name;
00114                 $this->cache[$userId]['real_name'] = $row->user_real_name;
00115                 $this->cache[$userId]['registration'] = $row->user_registration;
00116                 $usersToCheck[$userId] = $row->user_name;
00117             }
00118         }
00119 
00120         $lb = new LinkBatch();
00121         foreach ( $usersToCheck as $userId => $name ) {
00122             if ( $this->queryNeeded( $userId, 'userpage', $options ) ) {
00123                 $lb->add( NS_USER, str_replace( ' ', '_', $row->user_name ) );
00124                 $this->typesCached[$userId]['userpage'] = 1;
00125             }
00126             if ( $this->queryNeeded( $userId, 'usertalk', $options ) ) {
00127                 $lb->add( NS_USER_TALK, str_replace( ' ', '_', $row->user_name ) );
00128                 $this->typesCached[$userId]['usertalk'] = 1;
00129             }
00130         }
00131         $lb->execute();
00132 
00133         wfProfileOut( __METHOD__ );
00134     }
00135 
00144     protected function queryNeeded( $uid, $type, array $options ) {
00145         return ( in_array( $type, $options ) && !isset( $this->typesCached[$uid][$type] ) );
00146     }
00147 }