Debian Codegraph @

includes/cache/UserCache.php

raw symbols SCIP
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 namespace MediaWiki\Cache;
25
26 use MediaWiki\MediaWikiServices;
27 use Psr\Log\LoggerInterface;
28 use Wikimedia\Rdbms\IConnectionProvider;
29
30 /**
31 * @since 1.20
32 * @deprecated since 1.43, use ActorStore
33 */
34 class UserCache {
35 /** @var array (uid => property => value) */
36 protected $cache = [];
37 /** @var array (uid => cache type => 1) */
38 protected $typesCached = [];
39
40 /** @var LoggerInterface */
41 private $logger;
42
43 /** @var LinkBatchFactory */
44 private $linkBatchFactory;
45
46 /** @var IConnectionProvider */
47 private $dbProvider;
48
49 /**
50 * @deprecated since 1.43, use MediaWikiServices::getInstance()->getUserCache()
51 * @return UserCache
52 */
53 public static function singleton() {
54 wfDeprecated( __METHOD__, '1.43' );
55 return MediaWikiServices::getInstance()->getUserCache();
56 }
57
58 /**
59 * Uses dependency injection since 1.36
60 *
61 * @param LoggerInterface $logger
62 * @param IConnectionProvider $dbProvider
63 * @param LinkBatchFactory $linkBatchFactory
64 */
65 public function __construct(
66 LoggerInterface $logger,
67 IConnectionProvider $dbProvider,
68 LinkBatchFactory $linkBatchFactory
69 ) {
70 $this->logger = $logger;
71 $this->dbProvider = $dbProvider;
72 $this->linkBatchFactory = $linkBatchFactory;
73 }
74
75 /**
76 * Get a property of a user based on their user ID
77 *
78 * @param int $userId
79 * @param string $prop User property
80 * @return mixed|false The property or false if the user does not exist
81 */
82 public function getProp( $userId, $prop ) {
83 if ( !isset( $this->cache[$userId][$prop] ) ) {
84 $this->logger->debug(
85 'Querying DB for prop {prop} for user ID {userId}',
86 [
87 'prop' => $prop,
88 'userId' => $userId,
89 ]
90 );
91 $this->doQuery( [ $userId ] ); // cache miss
92 }
93
94 return $this->cache[$userId][$prop] ?? false; // user does not exist?
95 }
96
97 /**
98 * Get the name of a user or return $ip if the user ID is 0
99 *
100 * @param int $userId
101 * @param string $ip
102 * @return string
103 * @since 1.22
104 */
105 public function getUserName( $userId, $ip ) {
106 return $userId > 0 ? $this->getProp( $userId, 'name' ) : $ip;
107 }
108
109 /**
110 * Preloads user names for given list of users.
111 * @param array $userIds List of user IDs
112 * @param array $options Option flags; include 'userpage' and 'usertalk'
113 * @param string $caller The calling method
114 */
115 public function doQuery( array $userIds, $options = [], $caller = '' ) {
116 $usersToCheck = [];
117 $usersToQuery = [];
118
119 $userIds = array_unique( $userIds );
120
121 foreach ( $userIds as $userId ) {
122 $userId = (int)$userId;
123 if ( $userId <= 0 ) {
124 continue; // skip anons
125 }
126 if ( isset( $this->cache[$userId]['name'] ) ) {
127 $usersToCheck[$userId] = $this->cache[$userId]['name']; // already have name
128 } else {
129 $usersToQuery[] = $userId; // we need to get the name
130 }
131 }
132
133 // Lookup basic info for users not yet loaded...
134 if ( count( $usersToQuery ) ) {
135 $dbr = $this->dbProvider->getReplicaDatabase();
136 $queryBuilder = $dbr->newSelectQueryBuilder()
137 ->select( [ 'user_name', 'user_real_name', 'user_registration', 'user_id', 'actor_id' ] )
138 ->from( 'user' )
139 ->join( 'actor', null, 'actor_user = user_id' )
140 ->where( [ 'user_id' => $usersToQuery ] );
141
142 $comment = __METHOD__;
143 if ( strval( $caller ) !== '' ) {
144 $comment .= "/$caller";
145 }
146
147 $res = $queryBuilder->caller( $comment )->fetchResultSet();
148 foreach ( $res as $row ) { // load each user into cache
149 $userId = (int)$row->user_id;
150 $this->cache[$userId]['name'] = $row->user_name;
151 $this->cache[$userId]['real_name'] = $row->user_real_name;
152 $this->cache[$userId]['registration'] = $row->user_registration;
153 $this->cache[$userId]['actor'] = $row->actor_id;
154 $usersToCheck[$userId] = $row->user_name;
155 }
156 }
157
158 $lb = $this->linkBatchFactory->newLinkBatch();
159 foreach ( $usersToCheck as $userId => $name ) {
160 if ( $this->queryNeeded( $userId, 'userpage', $options ) ) {
161 $lb->add( NS_USER, $name );
162 $this->typesCached[$userId]['userpage'] = 1;
163 }
164 if ( $this->queryNeeded( $userId, 'usertalk', $options ) ) {
165 $lb->add( NS_USER_TALK, $name );
166 $this->typesCached[$userId]['usertalk'] = 1;
167 }
168 }
169 $lb->execute();
170 }
171
172 /**
173 * Check if a cache type is in $options and was not loaded for this user
174 *
175 * @param int $uid User ID
176 * @param string $type Cache type
177 * @param array $options Requested cache types
178 * @return bool
179 */
180 protected function queryNeeded( $uid, $type, array $options ) {
181 return ( in_array( $type, $options ) && !isset( $this->typesCached[$uid][$type] ) );
182 }
183 }
184
185 /** @deprecated class alias since 1.42 */
186 class_alias( UserCache::class, 'UserCache' );