MediaWiki master
UserCache.php
Go to the documentation of this file.
1<?php
24namespace MediaWiki\Cache;
25
27use Psr\Log\LoggerInterface;
29
34class UserCache {
36 protected $cache = [];
38 protected $typesCached = [];
39
41 private $logger;
42
44 private $linkBatchFactory;
45
47 private $dbProvider;
48
53 public static function singleton() {
54 wfDeprecated( __METHOD__, '1.43' );
55 return MediaWikiServices::getInstance()->getUserCache();
56 }
57
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
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
105 public function getUserName( $userId, $ip ) {
106 return $userId > 0 ? $this->getProp( $userId, 'name' ) : $ip;
107 }
108
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
180 protected function queryNeeded( $uid, $type, array $options ) {
181 return ( in_array( $type, $options ) && !isset( $this->typesCached[$uid][$type] ) );
182 }
183}
184
186class_alias( UserCache::class, 'UserCache' );
const NS_USER
Definition Defines.php:67
const NS_USER_TALK
Definition Defines.php:68
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
array $cache
(uid => property => value)
Definition UserCache.php:36
__construct(LoggerInterface $logger, IConnectionProvider $dbProvider, LinkBatchFactory $linkBatchFactory)
Uses dependency injection since 1.36.
Definition UserCache.php:65
getUserName( $userId, $ip)
Get the name of a user or return $ip if the user ID is 0.
array $typesCached
(uid => cache type => 1)
Definition UserCache.php:38
queryNeeded( $uid, $type, array $options)
Check if a cache type is in $options and was not loaded for this user.
doQuery(array $userIds, $options=[], $caller='')
Preloads user names for given list of users.
getProp( $userId, $prop)
Get a property of a user based on their user ID.
Definition UserCache.php:82
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
Provide primary and replica IDatabase connections.