MediaWiki REL1_31
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 isset( $this->cache[$userId][$prop] )
60 ? $this->cache[$userId][$prop]
61 : false; // user does not exist?
62 }
63
72 public function getUserName( $userId, $ip ) {
73 return $userId > 0 ? $this->getProp( $userId, 'name' ) : $ip;
74 }
75
82 public function doQuery( array $userIds, $options = [], $caller = '' ) {
84
85 $usersToCheck = [];
86 $usersToQuery = [];
87
88 $userIds = array_unique( $userIds );
89
90 foreach ( $userIds as $userId ) {
91 $userId = (int)$userId;
92 if ( $userId <= 0 ) {
93 continue; // skip anons
94 }
95 if ( isset( $this->cache[$userId]['name'] ) ) {
96 $usersToCheck[$userId] = $this->cache[$userId]['name']; // already have name
97 } else {
98 $usersToQuery[] = $userId; // we need to get the name
99 }
100 }
101
102 // Lookup basic info for users not yet loaded...
103 if ( count( $usersToQuery ) ) {
105 $tables = [ 'user' ];
106 $conds = [ 'user_id' => $usersToQuery ];
107 $fields = [ 'user_name', 'user_real_name', 'user_registration', 'user_id' ];
108 $joinConds = [];
109
111 $tables[] = 'actor';
112 $fields[] = 'actor_id';
113 $joinConds['actor'] = [
114 $wgActorTableSchemaMigrationStage === MIGRATION_NEW ? 'JOIN' : 'LEFT JOIN',
115 [ 'actor_user = user_id' ]
116 ];
117 }
118
119 $comment = __METHOD__;
120 if ( strval( $caller ) !== '' ) {
121 $comment .= "/$caller";
122 }
123
124 $res = $dbr->select( $tables, $fields, $conds, $comment, [], $joinConds );
125 foreach ( $res as $row ) { // load each user into cache
126 $userId = (int)$row->user_id;
127 $this->cache[$userId]['name'] = $row->user_name;
128 $this->cache[$userId]['real_name'] = $row->user_real_name;
129 $this->cache[$userId]['registration'] = $row->user_registration;
131 $this->cache[$userId]['actor'] = $row->actor_id;
132 }
133 $usersToCheck[$userId] = $row->user_name;
134 }
135 }
136
137 $lb = new LinkBatch();
138 foreach ( $usersToCheck as $userId => $name ) {
139 if ( $this->queryNeeded( $userId, 'userpage', $options ) ) {
140 $lb->add( NS_USER, $name );
141 $this->typesCached[$userId]['userpage'] = 1;
142 }
143 if ( $this->queryNeeded( $userId, 'usertalk', $options ) ) {
144 $lb->add( NS_USER_TALK, $name );
145 $this->typesCached[$userId]['usertalk'] = 1;
146 }
147 }
148 $lb->execute();
149 }
150
159 protected function queryNeeded( $uid, $type, array $options ) {
160 return ( in_array( $type, $options ) && !isset( $this->typesCached[$uid][$type] ) );
161 }
162}
int $wgActorTableSchemaMigrationStage
Actor table schema migration stage.
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:82
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:72
$res
Definition database.txt:21
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist & $tables
Definition hooks.txt:1015
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition hooks.txt:2001
const MIGRATION_NEW
Definition Defines.php:305
const NS_USER_TALK
Definition Defines.php:77
const MIGRATION_OLD
Definition Defines.php:302
you have access to all of the normal MediaWiki so you can get a DB use the cache
const DB_REPLICA
Definition defines.php:25