MediaWiki  1.33.0
UserCache.php
Go to the documentation of this file.
1 <?php
27 class 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 = '' ) {
82 
83  $usersToCheck = [];
84  $usersToQuery = [];
85 
86  $userIds = array_unique( $userIds );
87 
88  foreach ( $userIds as $userId ) {
89  $userId = (int)$userId;
90  if ( $userId <= 0 ) {
91  continue; // skip anons
92  }
93  if ( isset( $this->cache[$userId]['name'] ) ) {
94  $usersToCheck[$userId] = $this->cache[$userId]['name']; // already have name
95  } else {
96  $usersToQuery[] = $userId; // we need to get the name
97  }
98  }
99 
100  // Lookup basic info for users not yet loaded...
101  if ( count( $usersToQuery ) ) {
102  $dbr = wfGetDB( DB_REPLICA );
103  $tables = [ 'user' ];
104  $conds = [ 'user_id' => $usersToQuery ];
105  $fields = [ 'user_name', 'user_real_name', 'user_registration', 'user_id' ];
106  $joinConds = [];
107 
108  // Technically we shouldn't allow this without SCHEMA_COMPAT_READ_NEW,
109  // but it does little harm and might be needed for write callers loading a User.
111  $tables[] = 'actor';
112  $fields[] = 'actor_id';
113  $joinConds['actor'] = [
114  ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_READ_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 }
if
if($IP===false)
Definition: cleanupArchiveUserText.php:4
SCHEMA_COMPAT_READ_NEW
const SCHEMA_COMPAT_READ_NEW
Definition: Defines.php:287
LinkBatch
Class representing a list of titles The execute() method checks them all for existence and adds them ...
Definition: LinkBatch.php:34
UserCache
Definition: UserCache.php:27
captcha-old.count
count
Definition: captcha-old.py:249
UserCache\queryNeeded
queryNeeded( $uid, $type, array $options)
Check if a cache type is in $options and was not loaded for this user.
Definition: UserCache.php:159
$tables
this hook is for auditing only 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:979
$res
$res
Definition: database.txt:21
cache
you have access to all of the normal MediaWiki so you can get a DB use the cache
Definition: maintenance.txt:52
UserCache\__construct
__construct()
Definition: UserCache.php:43
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
$dbr
$dbr
Definition: testCompression.php:50
UserCache\getProp
getProp( $userId, $prop)
Get a property of a user based on their user ID.
Definition: UserCache.php:53
UserCache\$typesCached
$typesCached
Definition: UserCache.php:29
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2636
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:949
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:271
NS_USER_TALK
const NS_USER_TALK
Definition: Defines.php:67
UserCache\doQuery
doQuery(array $userIds, $options=[], $caller='')
Preloads user names for given list of users.
Definition: UserCache.php:80
UserCache\getUserName
getUserName( $userId, $ip)
Get the name of a user or return $ip if the user ID is 0.
Definition: UserCache.php:70
UserCache\$cache
$cache
Definition: UserCache.php:28
$options
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:1985
SCHEMA_COMPAT_NEW
const SCHEMA_COMPAT_NEW
Definition: Defines.php:291
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
UserCache\singleton
static singleton()
Definition: UserCache.php:34
NS_USER
const NS_USER
Definition: Defines.php:66
$type
$type
Definition: testCompression.php:48
$wgActorTableSchemaMigrationStage
int $wgActorTableSchemaMigrationStage
Actor table schema migration stage.
Definition: DefaultSettings.php:8979