MediaWiki  1.23.14
UserCache.php
Go to the documentation of this file.
1 <?php
27 class UserCache {
28  protected $cache = array(); // (uid => property => value)
29  protected $typesCached = array(); // (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( array( $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 = array(), $caller = '' ) {
83  wfProfileIn( __METHOD__ );
84 
85  $usersToCheck = array();
86  $usersToQuery = array();
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 ) ) {
104  $dbr = wfGetDB( DB_SLAVE );
105  $table = array( 'user' );
106  $conds = array( 'user_id' => $usersToQuery );
107  $fields = array( 'user_name', 'user_real_name', 'user_registration', 'user_id' );
108 
109  $comment = __METHOD__;
110  if ( strval( $caller ) !== '' ) {
111  $comment .= "/$caller";
112  }
113 
114  $res = $dbr->select( $table, $fields, $conds, $comment );
115  foreach ( $res as $row ) { // load each user into cache
116  $userId = (int)$row->user_id;
117  $this->cache[$userId]['name'] = $row->user_name;
118  $this->cache[$userId]['real_name'] = $row->user_real_name;
119  $this->cache[$userId]['registration'] = $row->user_registration;
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, str_replace( ' ', '_', $row->user_name ) );
128  $this->typesCached[$userId]['userpage'] = 1;
129  }
130  if ( $this->queryNeeded( $userId, 'usertalk', $options ) ) {
131  $lb->add( NS_USER_TALK, str_replace( ' ', '_', $row->user_name ) );
132  $this->typesCached[$userId]['usertalk'] = 1;
133  }
134  }
135  $lb->execute();
136 
137  wfProfileOut( __METHOD__ );
138  }
139 
148  protected function queryNeeded( $uid, $type, array $options ) {
149  return ( in_array( $type, $options ) && !isset( $this->typesCached[$uid][$type] ) );
150  }
151 }
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
LinkBatch
Class representing a list of titles The execute() method checks them all for existence and adds them ...
Definition: LinkBatch.php:30
UserCache
Definition: UserCache.php:27
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:148
wfGetDB
& wfGetDB( $db, $groups=array(), $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:3714
wfProfileIn
wfProfileIn( $functionname)
Begin profiling of a function.
Definition: Profiler.php:33
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
$dbr
$dbr
Definition: testCompression.php:48
$lb
if( $wgAPIRequestLog) $lb
Definition: api.php:126
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
wfProfileOut
wfProfileOut( $functionname='missing')
Stop profiling of a function.
Definition: Profiler.php:46
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
$comment
$comment
Definition: importImages.php:107
$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:1530
wfDebug
wfDebug( $text, $dest='all')
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:980
NS_USER_TALK
const NS_USER_TALK
Definition: Defines.php:82
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:336
UserCache\getUserName
getUserName( $userId, $ip)
Get the name of a user or return $ip if the user ID is 0.
Definition: UserCache.php:72
UserCache\doQuery
doQuery(array $userIds, $options=array(), $caller='')
Preloads user names for given list of users.
Definition: UserCache.php:82
UserCache\$cache
$cache
Definition: UserCache.php:28
DB_SLAVE
const DB_SLAVE
Definition: Defines.php:55
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:81
$res
$res
Definition: database.txt:21
$type
$type
Definition: testCompression.php:46