MediaWiki REL1_34
UserCache.php
Go to the documentation of this file.
1<?php
27class UserCache {
28 protected $cache = []; // (uid => property => value)
29 protected $typesCached = []; // (uid => cache type => 1)
30
34 public static function singleton() {
35 static $instance = null;
36 if ( $instance === null ) {
37 $instance = new self();
38 }
39
40 return $instance;
41 }
42
43 protected function __construct() {
44 }
45
53 public function getProp( $userId, $prop ) {
54 if ( !isset( $this->cache[$userId][$prop] ) ) {
55 wfDebug( __METHOD__ . ": querying DB for prop '$prop' for user ID '$userId'.\n" );
56 $this->doQuery( [ $userId ] ); // cache miss
57 }
58
59 return $this->cache[$userId][$prop] ?? false; // user does not exist?
60 }
61
70 public function getUserName( $userId, $ip ) {
71 return $userId > 0 ? $this->getProp( $userId, 'name' ) : $ip;
72 }
73
80 public function doQuery( array $userIds, $options = [], $caller = '' ) {
81 $usersToCheck = [];
82 $usersToQuery = [];
83
84 $userIds = array_unique( $userIds );
85
86 foreach ( $userIds as $userId ) {
87 $userId = (int)$userId;
88 if ( $userId <= 0 ) {
89 continue; // skip anons
90 }
91 if ( isset( $this->cache[$userId]['name'] ) ) {
92 $usersToCheck[$userId] = $this->cache[$userId]['name']; // already have name
93 } else {
94 $usersToQuery[] = $userId; // we need to get the name
95 }
96 }
97
98 // Lookup basic info for users not yet loaded...
99 if ( count( $usersToQuery ) ) {
101 $tables = [ 'user', 'actor' ];
102 $conds = [ 'user_id' => $usersToQuery ];
103 $fields = [ 'user_name', 'user_real_name', 'user_registration', 'user_id', 'actor_id' ];
104 $joinConds = [
105 'actor' => [ 'JOIN', 'actor_user = user_id' ],
106 ];
107
108 $comment = __METHOD__;
109 if ( strval( $caller ) !== '' ) {
110 $comment .= "/$caller";
111 }
112
113 $res = $dbr->select( $tables, $fields, $conds, $comment, [], $joinConds );
114 foreach ( $res as $row ) { // load each user into cache
115 $userId = (int)$row->user_id;
116 $this->cache[$userId]['name'] = $row->user_name;
117 $this->cache[$userId]['real_name'] = $row->user_real_name;
118 $this->cache[$userId]['registration'] = $row->user_registration;
119 $this->cache[$userId]['actor'] = $row->actor_id;
120 $usersToCheck[$userId] = $row->user_name;
121 }
122 }
123
124 $lb = new LinkBatch();
125 foreach ( $usersToCheck as $userId => $name ) {
126 if ( $this->queryNeeded( $userId, 'userpage', $options ) ) {
127 $lb->add( NS_USER, $name );
128 $this->typesCached[$userId]['userpage'] = 1;
129 }
130 if ( $this->queryNeeded( $userId, 'usertalk', $options ) ) {
131 $lb->add( NS_USER_TALK, $name );
132 $this->typesCached[$userId]['usertalk'] = 1;
133 }
134 }
135 $lb->execute();
136 }
137
146 protected function queryNeeded( $uid, $type, array $options ) {
147 return ( in_array( $type, $options ) && !isset( $this->typesCached[$uid][$type] ) );
148 }
149}
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Class representing a list of titles The execute() method checks them all for existence and adds them ...
Definition LinkBatch.php:34
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.
Definition UserCache.php:80
static singleton()
Definition UserCache.php:34
getProp( $userId, $prop)
Get a property of a user based on their user ID.
Definition UserCache.php:53
getUserName( $userId, $ip)
Get the name of a user or return $ip if the user ID is 0.
Definition UserCache.php:70
const NS_USER
Definition Defines.php:71
const NS_USER_TALK
Definition Defines.php:72
const DB_REPLICA
Definition defines.php:25