MediaWiki master
UserCache.php
Go to the documentation of this file.
1<?php
24namespace MediaWiki\Cache;
25
27use Psr\Log\LoggerInterface;
29
33class UserCache {
34 protected $cache = []; // (uid => property => value)
35 protected $typesCached = []; // (uid => cache type => 1)
36
38 private $logger;
39
41 private $linkBatchFactory;
42
44 private $dbProvider;
45
49 public static function singleton() {
50 return MediaWikiServices::getInstance()->getUserCache();
51 }
52
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
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
100 public function getUserName( $userId, $ip ) {
101 return $userId > 0 ? $this->getProp( $userId, 'name' ) : $ip;
102 }
103
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
175 protected function queryNeeded( $uid, $type, array $options ) {
176 return ( in_array( $type, $options ) && !isset( $this->typesCached[$uid][$type] ) );
177 }
178}
179
181class_alias( UserCache::class, 'UserCache' );
const NS_USER
Definition Defines.php:66
const NS_USER_TALK
Definition Defines.php:67
__construct(LoggerInterface $logger, IConnectionProvider $dbProvider, LinkBatchFactory $linkBatchFactory)
Uses dependency injection since 1.36.
Definition UserCache.php:60
getUserName( $userId, $ip)
Get the name of a user or return $ip if the user ID is 0.
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:77
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.