MediaWiki REL1_31
UsersPager.php
Go to the documentation of this file.
1<?php
34
38 protected $userGroupCache;
39
46 function __construct( IContextSource $context = null, $par = null, $including = null ) {
47 if ( $context ) {
48 $this->setContext( $context );
49 }
50
51 $request = $this->getRequest();
52 $par = ( $par !== null ) ? $par : '';
53 $parms = explode( '/', $par );
54 $symsForAll = [ '*', 'user' ];
55
56 if ( $parms[0] != '' &&
57 ( in_array( $par, User::getAllGroups() ) || in_array( $par, $symsForAll ) )
58 ) {
59 $this->requestedGroup = $par;
60 $un = $request->getText( 'username' );
61 } elseif ( count( $parms ) == 2 ) {
62 $this->requestedGroup = $parms[0];
63 $un = $parms[1];
64 } else {
65 $this->requestedGroup = $request->getVal( 'group' );
66 $un = ( $par != '' ) ? $par : $request->getText( 'username' );
67 }
68
69 if ( in_array( $this->requestedGroup, $symsForAll ) ) {
70 $this->requestedGroup = '';
71 }
72 $this->editsOnly = $request->getBool( 'editsOnly' );
73 $this->creationSort = $request->getBool( 'creationSort' );
74 $this->including = $including;
75 $this->mDefaultDirection = $request->getBool( 'desc' )
78
79 $this->requestedUser = '';
80
81 if ( $un != '' ) {
82 $username = Title::makeTitleSafe( NS_USER, $un );
83
84 if ( !is_null( $username ) ) {
85 $this->requestedUser = $username->getText();
86 }
87 }
88
89 parent::__construct();
90 }
91
95 function getIndexField() {
96 return $this->creationSort ? 'user_id' : 'user_name';
97 }
98
102 function getQueryInfo() {
104 $conds = [];
105
106 // Don't show hidden names
107 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
108 $conds[] = 'ipb_deleted IS NULL OR ipb_deleted = 0';
109 }
110
111 $options = [];
112
113 if ( $this->requestedGroup != '' ) {
114 $conds['ug_group'] = $this->requestedGroup;
115 $conds[] = 'ug_expiry IS NULL OR ug_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() );
116 }
117
118 if ( $this->requestedUser != '' ) {
119 # Sorted either by account creation or name
120 if ( $this->creationSort ) {
121 $conds[] = 'user_id >= ' . intval( User::idFromName( $this->requestedUser ) );
122 } else {
123 $conds[] = 'user_name >= ' . $dbr->addQuotes( $this->requestedUser );
124 }
125 }
126
127 if ( $this->editsOnly ) {
128 $conds[] = 'user_editcount > 0';
129 }
130
131 $options['GROUP BY'] = $this->creationSort ? 'user_id' : 'user_name';
132
133 $query = [
134 'tables' => [ 'user', 'user_groups', 'ipblocks' ],
135 'fields' => [
136 'user_name' => $this->creationSort ? 'MAX(user_name)' : 'user_name',
137 'user_id' => $this->creationSort ? 'user_id' : 'MAX(user_id)',
138 'edits' => 'MAX(user_editcount)',
139 'creation' => 'MIN(user_registration)',
140 'ipb_deleted' => 'MAX(ipb_deleted)' // block/hide status
141 ],
142 'options' => $options,
143 'join_conds' => [
144 'user_groups' => [ 'LEFT JOIN', 'user_id=ug_user' ],
145 'ipblocks' => [
146 'LEFT JOIN', [
147 'user_id=ipb_user',
148 'ipb_auto' => 0
149 ]
150 ],
151 ],
152 'conds' => $conds
153 ];
154
155 Hooks::run( 'SpecialListusersQueryInfo', [ $this, &$query ] );
156
157 return $query;
158 }
159
164 function formatRow( $row ) {
165 if ( $row->user_id == 0 ) { # T18487
166 return '';
167 }
168
169 $userName = $row->user_name;
170
171 $ulinks = Linker::userLink( $row->user_id, $userName );
173 $row->user_id,
174 $userName,
175 (int)$row->edits
176 );
177
178 $lang = $this->getLanguage();
179
180 $groups = '';
181 $ugms = self::getGroupMemberships( intval( $row->user_id ), $this->userGroupCache );
182
183 if ( !$this->including && count( $ugms ) > 0 ) {
184 $list = [];
185 foreach ( $ugms as $ugm ) {
186 $list[] = $this->buildGroupLink( $ugm, $userName );
187 }
188 $groups = $lang->commaList( $list );
189 }
190
191 $item = $lang->specialList( $ulinks, $groups );
192
193 if ( $row->ipb_deleted ) {
194 $item = "<span class=\"deleted\">$item</span>";
195 }
196
197 $edits = '';
198 if ( !$this->including && $this->getConfig()->get( 'Edititis' ) ) {
199 $count = $this->msg( 'usereditcount' )->numParams( $row->edits )->escaped();
200 $edits = $this->msg( 'word-separator' )->escaped() . $this->msg( 'brackets', $count )->escaped();
201 }
202
203 $created = '';
204 # Some rows may be null
205 if ( !$this->including && $row->creation ) {
206 $user = $this->getUser();
207 $d = $lang->userDate( $row->creation, $user );
208 $t = $lang->userTime( $row->creation, $user );
209 $created = $this->msg( 'usercreated', $d, $t, $row->user_name )->escaped();
210 $created = ' ' . $this->msg( 'parentheses' )->rawParams( $created )->escaped();
211 }
212 $blocked = !is_null( $row->ipb_deleted ) ?
213 ' ' . $this->msg( 'listusers-blocked', $userName )->escaped() :
214 '';
215
216 Hooks::run( 'SpecialListusersFormatRow', [ &$item, $row ] );
217
218 return Html::rawElement( 'li', [], "{$item}{$edits}{$created}{$blocked}" );
219 }
220
221 function doBatchLookups() {
222 $batch = new LinkBatch();
223 $userIds = [];
224 # Give some pointers to make user links
225 foreach ( $this->mResult as $row ) {
226 $batch->add( NS_USER, $row->user_name );
227 $batch->add( NS_USER_TALK, $row->user_name );
228 $userIds[] = $row->user_id;
229 }
230
231 // Lookup groups for all the users
233 $groupRes = $dbr->select(
234 'user_groups',
235 UserGroupMembership::selectFields(),
236 [ 'ug_user' => $userIds ],
237 __METHOD__
238 );
239 $cache = [];
240 $groups = [];
241 foreach ( $groupRes as $row ) {
242 $ugm = UserGroupMembership::newFromRow( $row );
243 if ( !$ugm->isExpired() ) {
244 $cache[$row->ug_user][$row->ug_group] = $ugm;
245 $groups[$row->ug_group] = true;
246 }
247 }
248
249 // Give extensions a chance to add things like global user group data
250 // into the cache array to ensure proper output later on
251 Hooks::run( 'UsersPagerDoBatchLookups', [ $dbr, $userIds, &$cache, &$groups ] );
252
253 $this->userGroupCache = $cache;
254
255 // Add page of groups to link batch
256 foreach ( $groups as $group => $unused ) {
257 $groupPage = UserGroupMembership::getGroupPage( $group );
258 if ( $groupPage ) {
259 $batch->addObj( $groupPage );
260 }
261 }
262
263 $batch->execute();
264 $this->mResult->rewind();
265 }
266
270 function getPageHeader() {
271 list( $self ) = explode( '/', $this->getTitle()->getPrefixedDBkey() );
272
273 $groupOptions = [ $this->msg( 'group-all' )->text() => '' ];
274 foreach ( $this->getAllGroups() as $group => $groupText ) {
275 $groupOptions[ $groupText ] = $group;
276 }
277
278 $formDescriptor = [
279 'user' => [
280 'class' => HTMLUserTextField::class,
281 'label' => $this->msg( 'listusersfrom' )->text(),
282 'name' => 'username',
283 'default' => $this->requestedUser,
284 ],
285 'dropdown' => [
286 'label' => $this->msg( 'group' )->text(),
287 'name' => 'group',
288 'default' => $this->requestedGroup,
289 'class' => HTMLSelectField::class,
290 'options' => $groupOptions,
291 ],
292 'editsOnly' => [
293 'type' => 'check',
294 'label' => $this->msg( 'listusers-editsonly' )->text(),
295 'name' => 'editsOnly',
296 'id' => 'editsOnly',
297 'default' => $this->editsOnly
298 ],
299 'creationSort' => [
300 'type' => 'check',
301 'label' => $this->msg( 'listusers-creationsort' )->text(),
302 'name' => 'creationSort',
303 'id' => 'creationSort',
304 'default' => $this->creationSort
305 ],
306 'desc' => [
307 'type' => 'check',
308 'label' => $this->msg( 'listusers-desc' )->text(),
309 'name' => 'desc',
310 'id' => 'desc',
311 'default' => $this->mDefaultDirection
312 ],
313 'limithiddenfield' => [
314 'class' => HTMLHiddenField::class,
315 'name' => 'limit',
316 'default' => $this->mLimit
317 ]
318 ];
319
320 $beforeSubmitButtonHookOut = '';
321 Hooks::run( 'SpecialListusersHeaderForm', [ $this, &$beforeSubmitButtonHookOut ] );
322
323 if ( $beforeSubmitButtonHookOut !== '' ) {
324 $formDescriptor[ 'beforeSubmitButtonHookOut' ] = [
325 'class' => HTMLInfoField::class,
326 'raw' => true,
327 'default' => $beforeSubmitButtonHookOut
328 ];
329 }
330
331 $formDescriptor[ 'submit' ] = [
332 'class' => HTMLSubmitField::class,
333 'buttonlabel-message' => 'listusers-submit',
334 ];
335
336 $beforeClosingFieldsetHookOut = '';
337 Hooks::run( 'SpecialListusersHeader', [ $this, &$beforeClosingFieldsetHookOut ] );
338
339 if ( $beforeClosingFieldsetHookOut !== '' ) {
340 $formDescriptor[ 'beforeClosingFieldsetHookOut' ] = [
341 'class' => HTMLInfoField::class,
342 'raw' => true,
343 'default' => $beforeClosingFieldsetHookOut
344 ];
345 }
346
347 $htmlForm = HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() );
348 $htmlForm
349 ->setMethod( 'get' )
350 ->setAction( Title::newFromText( $self )->getLocalURL() )
351 ->setId( 'mw-listusers-form' )
352 ->setFormIdentifier( 'mw-listusers-form' )
353 ->suppressDefaultSubmit()
354 ->setWrapperLegendMsg( 'listusers' );
355 return $htmlForm->prepareForm()->getHTML( true );
356 }
357
362 function getAllGroups() {
363 $result = [];
364 foreach ( User::getAllGroups() as $group ) {
365 $result[$group] = UserGroupMembership::getGroupName( $group );
366 }
367 asort( $result );
368
369 return $result;
370 }
371
376 function getDefaultQuery() {
377 $query = parent::getDefaultQuery();
378 if ( $this->requestedGroup != '' ) {
379 $query['group'] = $this->requestedGroup;
380 }
381 if ( $this->requestedUser != '' ) {
382 $query['username'] = $this->requestedUser;
383 }
384 Hooks::run( 'SpecialListusersDefaultQuery', [ $this, &$query ] );
385
386 return $query;
387 }
388
397 protected static function getGroupMemberships( $uid, $cache = null ) {
398 if ( $cache === null ) {
399 $user = User::newFromId( $uid );
400 return $user->getGroupMemberships();
401 } else {
402 return isset( $cache[$uid] ) ? $cache[$uid] : [];
403 }
404 }
405
413 protected function buildGroupLink( $group, $username ) {
414 return UserGroupMembership::getLink( $group, $this->getContext(), 'html', $username );
415 }
416}
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
IndexPager with an alphabetic list and a formatted navigation bar.
msg( $key)
Get a Message object with context set Parameters are the same as wfMessage()
IContextSource $context
getContext()
Get the base IContextSource object.
setContext(IContextSource $context)
const DIR_ASCENDING
Constants for the $mDefaultDirection field.
$mDefaultDirection
$mDefaultDirection gives the direction to use when sorting results: DIR_ASCENDING or DIR_DESCENDING.
const DIR_DESCENDING
Class representing a list of titles The execute() method checks them all for existence and adds them ...
Definition LinkBatch.php:34
static userLink( $userId, $userName, $altUserName=false)
Make user link (or user contributions for unregistered users)
Definition Linker.php:893
static userToolLinksRedContribs( $userId, $userText, $edits=null)
Alias for userToolLinks( $userId, $userText, true );.
Definition Linker.php:993
static getAllGroups()
Return the set of defined explicit groups.
Definition User.php:5099
static newFromId( $id)
Static factory method for creation from a given user ID.
Definition User.php:614
static idFromName( $name, $flags=self::READ_NORMAL)
Get database id given a user name.
Definition User.php:883
This class is used to get a list of user.
getAllGroups()
Get a list of all explicit groups.
__construct(IContextSource $context=null, $par=null, $including=null)
array[] $userGroupCache
A array with user ids as key and a array of groups as value.
buildGroupLink( $group, $username)
Format a link to a group description page.
static getGroupMemberships( $uid, $cache=null)
Get an associative array containing groups the specified user belongs to, and the relevant UserGroupM...
formatRow( $row)
doBatchLookups()
Called from getBody(), before getStartBody() is called and after doQuery() was called.
getDefaultQuery()
Preserve group and username offset parameters when paging.
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition deferred.txt:11
namespace being checked & $result
Definition hooks.txt:2323
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on $request
Definition hooks.txt:2806
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
this hook is for auditing only or null if authentication failed before getting that far $username
Definition hooks.txt:785
null for the local wiki Added should default to null in handler for backwards compatibility add a value to it if you want to add a cookie that have to vary cache options can modify $query
Definition hooks.txt:1620
const NS_USER_TALK
Definition Defines.php:77
Interface for objects which can provide a MediaWiki context on request.
$batch
Definition linkcache.txt:23
$cache
Definition mcc.php:33
const DB_REPLICA
Definition defines.php:25
if(!isset( $args[0])) $lang