MediaWiki
REL1_30
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
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 =
''
) {
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
$table = [
'user'
];
104
$conds = [
'user_id'
=> $usersToQuery ];
105
$fields = [
'user_name'
,
'user_real_name'
,
'user_registration'
,
'user_id'
];
106
107
$comment = __METHOD__;
108
if
( strval( $caller ) !==
''
) {
109
$comment .=
"/$caller"
;
110
}
111
112
$res
=
$dbr
->select( $table, $fields, $conds, $comment );
113
foreach
(
$res
as $row ) {
// load each user into cache
114
$userId = (int)$row->user_id;
115
$this->
cache
[$userId][
'name'
] = $row->user_name;
116
$this->
cache
[$userId][
'real_name'
] = $row->user_real_name;
117
$this->
cache
[$userId][
'registration'
] = $row->user_registration;
118
$usersToCheck[$userId] = $row->user_name;
119
}
120
}
121
122
$lb =
new
LinkBatch
();
123
foreach
( $usersToCheck as $userId => $name ) {
124
if
( $this->
queryNeeded
( $userId,
'userpage'
,
$options
) ) {
125
$lb->add( NS_USER, $name );
126
$this->typesCached[$userId][
'userpage'
] = 1;
127
}
128
if
( $this->
queryNeeded
( $userId,
'usertalk'
,
$options
) ) {
129
$lb->add(
NS_USER_TALK
, $name );
130
$this->typesCached[$userId][
'usertalk'
] = 1;
131
}
132
}
133
$lb->execute();
134
}
135
144
protected
function
queryNeeded
( $uid,
$type
, array
$options
) {
145
return
( in_array(
$type
,
$options
) && !isset( $this->typesCached[$uid][
$type
] ) );
146
}
147
}
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:1048
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition
GlobalFunctions.php:2857
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
UserCache\$typesCached
$typesCached
Definition
UserCache.php:29
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:144
UserCache\$cache
$cache
Definition
UserCache.php:28
UserCache\doQuery
doQuery(array $userIds, $options=[], $caller='')
Preloads user names for given list of users.
Definition
UserCache.php:82
UserCache\__construct
__construct()
Definition
UserCache.php:43
UserCache\singleton
static singleton()
Definition
UserCache.php:34
UserCache\getProp
getProp( $userId, $prop)
Get a property of a user based on their user ID.
Definition
UserCache.php:53
UserCache\getUserName
getUserName( $userId, $ip)
Get the name of a user or return $ip if the user ID is 0.
Definition
UserCache.php:72
$dbr
if(! $regexes) $dbr
Definition
cleanup.php:94
$res
$res
Definition
database.txt:21
$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:1971
NS_USER_TALK
const NS_USER_TALK
Definition
Defines.php:68
cache
you have access to all of the normal MediaWiki so you can get a DB use the cache
Definition
maintenance.txt:55
DB_REPLICA
const DB_REPLICA
Definition
defines.php:25
$type
$type
Definition
testCompression.php:48
includes
cache
UserCache.php
Generated on Mon Nov 25 2024 15:42:10 for MediaWiki by
1.10.0