Merge "Typo fix"
[mediawiki.git] / includes / cache / UserCache.php
blob6085f586e987253d89663d372cf0c267f77b51f5
1 <?php
2 /**
3 * Caches current user names and other info based on user IDs.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup Cache
24 /**
25 * @since 1.20
27 class UserCache {
28 protected $cache = array(); // (uid => property => value)
29 protected $typesCached = array(); // (uid => cache type => 1)
31 /**
32 * @return UserCache
34 public static function singleton() {
35 static $instance = null;
36 if ( $instance === null ) {
37 $instance = new self();
39 return $instance;
42 protected function __construct() {}
44 /**
45 * Get a property of a user based on their user ID
47 * @param $userId integer User ID
48 * @param string $prop User property
49 * @return mixed The property or false if the user does not exist
51 public function getProp( $userId, $prop ) {
52 if ( !isset( $this->cache[$userId][$prop] ) ) {
53 wfDebug( __METHOD__ . ": querying DB for prop '$prop' for user ID '$userId'.\n" );
54 $this->doQuery( array( $userId ) ); // cache miss
56 return isset( $this->cache[$userId][$prop] )
57 ? $this->cache[$userId][$prop]
58 : false; // user does not exist?
61 /**
62 * Get the name of a user or return $ip if the user ID is 0
64 * @param integer $userId
65 * @param string $ip
66 * @since 1.22
68 public function getUserName( $userId, $ip ) {
69 return $userId > 0 ? $this->getProp( $userId, 'name' ) : $ip;
72 /**
73 * Preloads user names for given list of users.
74 * @param array $userIds List of user IDs
75 * @param array $options Option flags; include 'userpage' and 'usertalk'
76 * @param string $caller the calling method
78 public function doQuery( array $userIds, $options = array(), $caller = '' ) {
79 wfProfileIn( __METHOD__ );
81 $usersToCheck = array();
82 $usersToQuery = array();
84 $userIds = array_unique( $userIds );
86 foreach ( $userIds as $userId ) {
87 $userId = (int)$userId;
88 if ( $userId <= 0 ) {
89 continue; // skip anons
91 if ( isset( $this->cache[$userId]['name'] ) ) {
92 $usersToCheck[$userId] = $this->cache[$userId]['name']; // already have name
93 } else {
94 $usersToQuery[] = $userId; // we need to get the name
98 // Lookup basic info for users not yet loaded...
99 if ( count( $usersToQuery ) ) {
100 $dbr = wfGetDB( DB_SLAVE );
101 $table = array( 'user' );
102 $conds = array( 'user_id' => $usersToQuery );
103 $fields = array( 'user_name', 'user_real_name', 'user_registration', 'user_id' );
105 $comment = __METHOD__;
106 if ( strval( $caller ) !== '' ) {
107 $comment .= "/$caller";
110 $res = $dbr->select( $table, $fields, $conds, $comment );
111 foreach ( $res as $row ) { // load each user into cache
112 $userId = (int)$row->user_id;
113 $this->cache[$userId]['name'] = $row->user_name;
114 $this->cache[$userId]['real_name'] = $row->user_real_name;
115 $this->cache[$userId]['registration'] = $row->user_registration;
116 $usersToCheck[$userId] = $row->user_name;
120 $lb = new LinkBatch();
121 foreach ( $usersToCheck as $userId => $name ) {
122 if ( $this->queryNeeded( $userId, 'userpage', $options ) ) {
123 $lb->add( NS_USER, str_replace( ' ', '_', $row->user_name ) );
124 $this->typesCached[$userId]['userpage'] = 1;
126 if ( $this->queryNeeded( $userId, 'usertalk', $options ) ) {
127 $lb->add( NS_USER_TALK, str_replace( ' ', '_', $row->user_name ) );
128 $this->typesCached[$userId]['usertalk'] = 1;
131 $lb->execute();
133 wfProfileOut( __METHOD__ );
137 * Check if a cache type is in $options and was not loaded for this user
139 * @param $uid integer user ID
140 * @param string $type Cache type
141 * @param array $options Requested cache types
142 * @return bool
144 protected function queryNeeded( $uid, $type, array $options ) {
145 return ( in_array( $type, $options ) && !isset( $this->typesCached[$uid][$type] ) );