MediaWiki  REL1_20
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 
00067         public function doQuery( array $userIds, $options = array(), $caller = '' ) {
00068                 wfProfileIn( __METHOD__ );
00069 
00070                 $usersToCheck = array();
00071                 $usersToQuery = array();
00072 
00073                 foreach ( $userIds as $userId ) {
00074                         $userId = (int)$userId;
00075                         if ( $userId <= 0 ) {
00076                                 continue; // skip anons
00077                         }
00078                         if ( isset( $this->cache[$userId]['name'] ) ) {
00079                                 $usersToCheck[$userId] = $this->cache[$userId]['name']; // already have name
00080                         } else {
00081                                 $usersToQuery[] = $userId; // we need to get the name
00082                         }
00083                 }
00084 
00085                 // Lookup basic info for users not yet loaded...
00086                 if ( count( $usersToQuery ) ) {
00087                         $dbr = wfGetDB( DB_SLAVE );
00088                         $table = array( 'user' );
00089                         $conds = array( 'user_id' => $usersToQuery );
00090                         $fields = array( 'user_name', 'user_real_name', 'user_registration', 'user_id' );
00091 
00092                         $comment = __METHOD__;
00093                         if ( strval( $caller ) !== '' ) {
00094                                 $comment .= "/$caller";
00095                         }
00096 
00097                         $res = $dbr->select( $table, $fields, $conds, $comment );
00098                         foreach ( $res as $row ) { // load each user into cache
00099                                 $userId = (int)$row->user_id;
00100                                 $this->cache[$userId]['name'] = $row->user_name;
00101                                 $this->cache[$userId]['real_name'] = $row->user_real_name;
00102                                 $this->cache[$userId]['registration'] = $row->user_registration;
00103                                 $usersToCheck[$userId] = $row->user_name;
00104                         }
00105                 }
00106 
00107                 $lb = new LinkBatch();
00108                 foreach ( $usersToCheck as $userId => $name ) {
00109                         if ( $this->queryNeeded( $userId, 'userpage', $options ) ) {
00110                                 $lb->add( NS_USER, str_replace( ' ', '_', $row->user_name ) );
00111                                 $this->typesCached[$userId]['userpage'] = 1;
00112                         }
00113                         if ( $this->queryNeeded( $userId, 'usertalk', $options ) ) {
00114                                 $lb->add( NS_USER_TALK, str_replace( ' ', '_', $row->user_name ) );
00115                                 $this->typesCached[$userId]['usertalk'] = 1;
00116                         }
00117                 }
00118                 $lb->execute();
00119 
00120                 wfProfileOut( __METHOD__ );
00121         }
00122 
00131         protected function queryNeeded( $uid, $type, array $options ) {
00132                 return ( in_array( $type, $options ) && !isset( $this->typesCached[$uid][$type] ) );
00133         }
00134 }