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 $loadBalancer;
44 
48  public static function singleton() {
49  return MediaWikiServices::getInstance()->getUserCache();
50  }
51 
59  public function __construct(
60  LoggerInterface $logger,
61  ILoadBalancer $loadBalancer,
62  LinkBatchFactory $linkBatchFactory
63  ) {
64  $this->logger = $logger;
65  $this->loadBalancer = $loadBalancer;
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->loadBalancer->getConnectionRef( DB_REPLICA );
130  $tables = [ 'user', 'actor' ];
131  $conds = [ 'user_id' => $usersToQuery ];
132  $fields = [ 'user_name', 'user_real_name', 'user_registration', 'user_id', 'actor_id' ];
133  $joinConds = [
134  'actor' => [ 'JOIN', 'actor_user = user_id' ],
135  ];
136 
137  $comment = __METHOD__;
138  if ( strval( $caller ) !== '' ) {
139  $comment .= "/$caller";
140  }
141 
142  $res = $dbr->select( $tables, $fields, $conds, $comment, [], $joinConds );
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 }
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:175
doQuery(array $userIds, $options=[], $caller='')
Preloads user names for given list of users.
Definition: UserCache.php:109
__construct(LoggerInterface $logger, ILoadBalancer $loadBalancer, LinkBatchFactory $linkBatchFactory)
Uses dependency injection since 1.36.
Definition: UserCache.php:59
static singleton()
Definition: UserCache.php:48
getProp( $userId, $prop)
Get a property of a user based on their user ID.
Definition: UserCache.php:76
getUserName( $userId, $ip)
Get the name of a user or return $ip if the user ID is 0.
Definition: UserCache.php:99
This class is a delegate to ILBFactory for a given database cluster.
const DB_REPLICA
Definition: defines.php:26