MediaWiki  master
UserCache.php
Go to the documentation of this file.
1 <?php
26 use Psr\Log\LoggerInterface;
28 
32 class UserCache {
33  protected $cache = []; // (uid => property => value)
34  protected $typesCached = []; // (uid => cache type => 1)
35 
37  private $logger;
38 
40  private $linkBatchFactory;
41 
43  private $dbProvider;
44 
48  public static function singleton() {
49  return MediaWikiServices::getInstance()->getUserCache();
50  }
51 
59  public function __construct(
60  LoggerInterface $logger,
61  IConnectionProvider $dbProvider,
62  LinkBatchFactory $linkBatchFactory
63  ) {
64  $this->logger = $logger;
65  $this->dbProvider = $dbProvider;
66  $this->linkBatchFactory = $linkBatchFactory;
67  }
68 
76  public function getProp( $userId, $prop ) {
77  if ( !isset( $this->cache[$userId][$prop] ) ) {
78  $this->logger->debug(
79  'Querying DB for prop {prop} for user ID {userId}',
80  [
81  'prop' => $prop,
82  'userId' => $userId,
83  ]
84  );
85  $this->doQuery( [ $userId ] ); // cache miss
86  }
87 
88  return $this->cache[$userId][$prop] ?? false; // user does not exist?
89  }
90 
99  public function getUserName( $userId, $ip ) {
100  return $userId > 0 ? $this->getProp( $userId, 'name' ) : $ip;
101  }
102 
109  public function doQuery( array $userIds, $options = [], $caller = '' ) {
110  $usersToCheck = [];
111  $usersToQuery = [];
112 
113  $userIds = array_unique( $userIds );
114 
115  foreach ( $userIds as $userId ) {
116  $userId = (int)$userId;
117  if ( $userId <= 0 ) {
118  continue; // skip anons
119  }
120  if ( isset( $this->cache[$userId]['name'] ) ) {
121  $usersToCheck[$userId] = $this->cache[$userId]['name']; // already have name
122  } else {
123  $usersToQuery[] = $userId; // we need to get the name
124  }
125  }
126 
127  // Lookup basic info for users not yet loaded...
128  if ( count( $usersToQuery ) ) {
129  $dbr = $this->dbProvider->getReplicaDatabase();
130  $queryBuilder = $dbr->newSelectQueryBuilder()
131  ->select( [ 'user_name', 'user_real_name', 'user_registration', 'user_id', 'actor_id' ] )
132  ->from( 'user' )
133  ->join( 'actor', null, 'actor_user = user_id' )
134  ->where( [ 'user_id' => $usersToQuery ] );
135 
136  $comment = __METHOD__;
137  if ( strval( $caller ) !== '' ) {
138  $comment .= "/$caller";
139  }
140 
141  $res = $queryBuilder->caller( $comment )->fetchResultSet();
142  foreach ( $res as $row ) { // load each user into cache
143  $userId = (int)$row->user_id;
144  $this->cache[$userId]['name'] = $row->user_name;
145  $this->cache[$userId]['real_name'] = $row->user_real_name;
146  $this->cache[$userId]['registration'] = $row->user_registration;
147  $this->cache[$userId]['actor'] = $row->actor_id;
148  $usersToCheck[$userId] = $row->user_name;
149  }
150  }
151 
152  $lb = $this->linkBatchFactory->newLinkBatch();
153  foreach ( $usersToCheck as $userId => $name ) {
154  if ( $this->queryNeeded( $userId, 'userpage', $options ) ) {
155  $lb->add( NS_USER, $name );
156  $this->typesCached[$userId]['userpage'] = 1;
157  }
158  if ( $this->queryNeeded( $userId, 'usertalk', $options ) ) {
159  $lb->add( NS_USER_TALK, $name );
160  $this->typesCached[$userId]['usertalk'] = 1;
161  }
162  }
163  $lb->execute();
164  }
165 
174  protected function queryNeeded( $uid, $type, array $options ) {
175  return ( in_array( $type, $options ) && !isset( $this->typesCached[$uid][$type] ) );
176  }
177 }
const NS_USER
Definition: Defines.php:66
const NS_USER_TALK
Definition: Defines.php:67
Service locator for MediaWiki core services.
queryNeeded( $uid, $type, array $options)
Check if a cache type is in $options and was not loaded for this user.
Definition: UserCache.php:174
doQuery(array $userIds, $options=[], $caller='')
Preloads user names for given list of users.
Definition: UserCache.php:109
static singleton()
Definition: UserCache.php:48
getProp( $userId, $prop)
Get a property of a user based on their user ID.
Definition: UserCache.php:76
__construct(LoggerInterface $logger, IConnectionProvider $dbProvider, LinkBatchFactory $linkBatchFactory)
Uses dependency injection since 1.36.
Definition: UserCache.php:59
getUserName( $userId, $ip)
Get the name of a user or return $ip if the user ID is 0.
Definition: UserCache.php:99
Provide primary and replica IDatabase connections.