SpecialLinkSearch: clean up munged query variable handling
[mediawiki.git] / includes / cache / UserCache.php
blob8a42489c71c7ba1c6fdea21e1b715cdf2affd88e
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();
40 return $instance;
43 protected function __construct() {
46 /**
47 * Get a property of a user based on their user ID
49 * @param int $userId User ID
50 * @param string $prop User property
51 * @return mixed|bool The property or false if the user does not exist
53 public function getProp( $userId, $prop ) {
54 if ( !isset( $this->cache[$userId][$prop] ) ) {
55 wfDebug( __METHOD__ . ": querying DB for prop '$prop' for user ID '$userId'.\n" );
56 $this->doQuery( array( $userId ) ); // cache miss
59 return isset( $this->cache[$userId][$prop] )
60 ? $this->cache[$userId][$prop]
61 : false; // user does not exist?
64 /**
65 * Get the name of a user or return $ip if the user ID is 0
67 * @param int $userId
68 * @param string $ip
69 * @return string
70 * @since 1.22
72 public function getUserName( $userId, $ip ) {
73 return $userId > 0 ? $this->getProp( $userId, 'name' ) : $ip;
76 /**
77 * Preloads user names for given list of users.
78 * @param array $userIds List of user IDs
79 * @param array $options Option flags; include 'userpage' and 'usertalk'
80 * @param string $caller The calling method
82 public function doQuery( array $userIds, $options = array(), $caller = '' ) {
84 $usersToCheck = array();
85 $usersToQuery = array();
87 $userIds = array_unique( $userIds );
89 foreach ( $userIds as $userId ) {
90 $userId = (int)$userId;
91 if ( $userId <= 0 ) {
92 continue; // skip anons
94 if ( isset( $this->cache[$userId]['name'] ) ) {
95 $usersToCheck[$userId] = $this->cache[$userId]['name']; // already have name
96 } else {
97 $usersToQuery[] = $userId; // we need to get the name
101 // Lookup basic info for users not yet loaded...
102 if ( count( $usersToQuery ) ) {
103 $dbr = wfGetDB( DB_SLAVE );
104 $table = array( 'user' );
105 $conds = array( 'user_id' => $usersToQuery );
106 $fields = array( 'user_name', 'user_real_name', 'user_registration', 'user_id' );
108 $comment = __METHOD__;
109 if ( strval( $caller ) !== '' ) {
110 $comment .= "/$caller";
113 $res = $dbr->select( $table, $fields, $conds, $comment );
114 foreach ( $res as $row ) { // load each user into cache
115 $userId = (int)$row->user_id;
116 $this->cache[$userId]['name'] = $row->user_name;
117 $this->cache[$userId]['real_name'] = $row->user_real_name;
118 $this->cache[$userId]['registration'] = $row->user_registration;
119 $usersToCheck[$userId] = $row->user_name;
123 $lb = new LinkBatch();
124 foreach ( $usersToCheck as $userId => $name ) {
125 if ( $this->queryNeeded( $userId, 'userpage', $options ) ) {
126 $lb->add( NS_USER, str_replace( ' ', '_', $row->user_name ) );
127 $this->typesCached[$userId]['userpage'] = 1;
129 if ( $this->queryNeeded( $userId, 'usertalk', $options ) ) {
130 $lb->add( NS_USER_TALK, str_replace( ' ', '_', $row->user_name ) );
131 $this->typesCached[$userId]['usertalk'] = 1;
134 $lb->execute();
139 * Check if a cache type is in $options and was not loaded for this user
141 * @param int $uid User ID
142 * @param string $type Cache type
143 * @param array $options Requested cache types
144 * @return bool
146 protected function queryNeeded( $uid, $type, array $options ) {
147 return ( in_array( $type, $options ) && !isset( $this->typesCached[$uid][$type] ) );