Follow-up 8697ba8: No need for two dependencies on the same module
[lhc/web/wiklou.git] / includes / cache / UserCache.php
1 <?php
2 /**
3 * Caches current user names and other info based on user IDs.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Cache
22 */
23
24 /**
25 * @since 1.20
26 */
27 class UserCache {
28 protected $cache = []; // (uid => property => value)
29 protected $typesCached = []; // (uid => cache type => 1)
30
31 /**
32 * @return UserCache
33 */
34 public static function singleton() {
35 static $instance = null;
36 if ( $instance === null ) {
37 $instance = new self();
38 }
39
40 return $instance;
41 }
42
43 protected function __construct() {
44 }
45
46 /**
47 * Get a property of a user based on their user ID
48 *
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
52 */
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( [ $userId ] ); // cache miss
57 }
58
59 return $this->cache[$userId][$prop] ?? false; // user does not exist?
60 }
61
62 /**
63 * Get the name of a user or return $ip if the user ID is 0
64 *
65 * @param int $userId
66 * @param string $ip
67 * @return string
68 * @since 1.22
69 */
70 public function getUserName( $userId, $ip ) {
71 return $userId > 0 ? $this->getProp( $userId, 'name' ) : $ip;
72 }
73
74 /**
75 * Preloads user names for given list of users.
76 * @param array $userIds List of user IDs
77 * @param array $options Option flags; include 'userpage' and 'usertalk'
78 * @param string $caller The calling method
79 */
80 public function doQuery( array $userIds, $options = [], $caller = '' ) {
81 global $wgActorTableSchemaMigrationStage;
82
83 $usersToCheck = [];
84 $usersToQuery = [];
85
86 $userIds = array_unique( $userIds );
87
88 foreach ( $userIds as $userId ) {
89 $userId = (int)$userId;
90 if ( $userId <= 0 ) {
91 continue; // skip anons
92 }
93 if ( isset( $this->cache[$userId]['name'] ) ) {
94 $usersToCheck[$userId] = $this->cache[$userId]['name']; // already have name
95 } else {
96 $usersToQuery[] = $userId; // we need to get the name
97 }
98 }
99
100 // Lookup basic info for users not yet loaded...
101 if ( count( $usersToQuery ) ) {
102 $dbr = wfGetDB( DB_REPLICA );
103 $tables = [ 'user' ];
104 $conds = [ 'user_id' => $usersToQuery ];
105 $fields = [ 'user_name', 'user_real_name', 'user_registration', 'user_id' ];
106 $joinConds = [];
107
108 // Technically we shouldn't allow this without SCHEMA_COMPAT_READ_NEW,
109 // but it does little harm and might be needed for write callers loading a User.
110 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_NEW ) {
111 $tables[] = 'actor';
112 $fields[] = 'actor_id';
113 $joinConds['actor'] = [
114 ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_READ_NEW ) ? 'JOIN' : 'LEFT JOIN',
115 [ 'actor_user = user_id' ]
116 ];
117 }
118
119 $comment = __METHOD__;
120 if ( strval( $caller ) !== '' ) {
121 $comment .= "/$caller";
122 }
123
124 $res = $dbr->select( $tables, $fields, $conds, $comment, [], $joinConds );
125 foreach ( $res as $row ) { // load each user into cache
126 $userId = (int)$row->user_id;
127 $this->cache[$userId]['name'] = $row->user_name;
128 $this->cache[$userId]['real_name'] = $row->user_real_name;
129 $this->cache[$userId]['registration'] = $row->user_registration;
130 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_NEW ) {
131 $this->cache[$userId]['actor'] = $row->actor_id;
132 }
133 $usersToCheck[$userId] = $row->user_name;
134 }
135 }
136
137 $lb = new LinkBatch();
138 foreach ( $usersToCheck as $userId => $name ) {
139 if ( $this->queryNeeded( $userId, 'userpage', $options ) ) {
140 $lb->add( NS_USER, $name );
141 $this->typesCached[$userId]['userpage'] = 1;
142 }
143 if ( $this->queryNeeded( $userId, 'usertalk', $options ) ) {
144 $lb->add( NS_USER_TALK, $name );
145 $this->typesCached[$userId]['usertalk'] = 1;
146 }
147 }
148 $lb->execute();
149 }
150
151 /**
152 * Check if a cache type is in $options and was not loaded for this user
153 *
154 * @param int $uid User ID
155 * @param string $type Cache type
156 * @param array $options Requested cache types
157 * @return bool
158 */
159 protected function queryNeeded( $uid, $type, array $options ) {
160 return ( in_array( $type, $options ) && !isset( $this->typesCached[$uid][$type] ) );
161 }
162 }