26 use Psr\Log\LoggerInterface;
40 private $linkBatchFactory;
43 private $loadBalancer;
49 return MediaWikiServices::getInstance()->getUserCache();
60 LoggerInterface $logger,
64 $this->logger = $logger;
65 $this->loadBalancer = $loadBalancer;
66 $this->linkBatchFactory = $linkBatchFactory;
76 public function getProp( $userId, $prop ) {
77 if ( !isset( $this->cache[$userId][$prop] ) ) {
79 'Querying DB for prop {prop} for user ID {userId}',
88 return $this->cache[$userId][$prop] ??
false;
100 return $userId > 0 ? $this->
getProp( $userId,
'name' ) : $ip;
109 public function doQuery( array $userIds, $options = [], $caller =
'' ) {
113 $userIds = array_unique( $userIds );
115 foreach ( $userIds as $userId ) {
116 $userId = (int)$userId;
117 if ( $userId <= 0 ) {
120 if ( isset( $this->cache[$userId][
'name'] ) ) {
121 $usersToCheck[$userId] = $this->cache[$userId][
'name'];
123 $usersToQuery[] = $userId;
128 if ( count( $usersToQuery ) ) {
130 $tables = [
'user',
'actor' ];
131 $conds = [
'user_id' => $usersToQuery ];
132 $fields = [
'user_name',
'user_real_name',
'user_registration',
'user_id',
'actor_id' ];
134 'actor' => [
'JOIN',
'actor_user = user_id' ],
137 $comment = __METHOD__;
138 if ( strval( $caller ) !==
'' ) {
139 $comment .=
"/$caller";
142 $res =
$dbr->select( $tables, $fields, $conds, $comment, [], $joinConds );
143 foreach (
$res as $row ) {
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;
153 $lb = $this->linkBatchFactory->newLinkBatch();
154 foreach ( $usersToCheck as $userId => $name ) {
155 if ( $this->
queryNeeded( $userId,
'userpage', $options ) ) {
157 $this->typesCached[$userId][
'userpage'] = 1;
159 if ( $this->
queryNeeded( $userId,
'usertalk', $options ) ) {
161 $this->typesCached[$userId][
'usertalk'] = 1;
176 return ( in_array(
$type, $options ) && !isset( $this->typesCached[$uid][
$type] ) );
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.
__construct(LoggerInterface $logger, ILoadBalancer $loadBalancer, LinkBatchFactory $linkBatchFactory)
Uses dependency injection since 1.36.
getProp( $userId, $prop)
Get a property of a user based on their user ID.
getUserName( $userId, $ip)
Get the name of a user or return $ip if the user ID is 0.