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