26 use Psr\Log\LoggerInterface;
40 private $linkBatchFactory;
49 return MediaWikiServices::getInstance()->getUserCache();
60 LoggerInterface $logger,
64 $this->logger = $logger;
65 $this->dbProvider = $dbProvider;
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 ) ) {
129 $dbr = $this->dbProvider->getReplicaDatabase();
130 $queryBuilder = $dbr->newSelectQueryBuilder()
131 ->select( [
'user_name',
'user_real_name',
'user_registration',
'user_id',
'actor_id' ] )
133 ->join(
'actor',
null,
'actor_user = user_id' )
134 ->where( [
'user_id' => $usersToQuery ] );
136 $comment = __METHOD__;
137 if ( strval( $caller ) !==
'' ) {
138 $comment .=
"/$caller";
141 $res = $queryBuilder->caller( $comment )->fetchResultSet();
142 foreach ( $res as $row ) {
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;
152 $lb = $this->linkBatchFactory->newLinkBatch();
153 foreach ( $usersToCheck as $userId => $name ) {
154 if ( $this->
queryNeeded( $userId,
'userpage', $options ) ) {
156 $this->typesCached[$userId][
'userpage'] = 1;
158 if ( $this->
queryNeeded( $userId,
'usertalk', $options ) ) {
160 $this->typesCached[$userId][
'usertalk'] = 1;
175 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.
getProp( $userId, $prop)
Get a property of a user based on their user ID.
__construct(LoggerInterface $logger, IConnectionProvider $dbProvider, LinkBatchFactory $linkBatchFactory)
Uses dependency injection since 1.36.
getUserName( $userId, $ip)
Get the name of a user or return $ip if the user ID is 0.