MediaWiki REL1_39
UsersPager.php
Go to the documentation of this file.
1<?php
34
43
47 protected $userGroupCache;
48
51
53 protected $editsOnly;
54
57
59 protected $creationSort;
60
62 protected $including;
63
65 protected $requestedUser;
66
68 private $hookRunner;
69
71 private $linkBatchFactory;
72
74 private $userGroupManager;
75
86 public function __construct(
87 IContextSource $context,
88 HookContainer $hookContainer,
89 LinkBatchFactory $linkBatchFactory,
90 ILoadBalancer $loadBalancer,
91 UserGroupManager $userGroupManager,
92 $par,
93 $including
94 ) {
95 $this->setContext( $context );
96
97 $request = $this->getRequest();
98 $par = $par ?? '';
99 $parms = explode( '/', $par );
100 $symsForAll = [ '*', 'user' ];
101
102 if ( $parms[0] != '' &&
103 ( in_array( $par, $userGroupManager->listAllGroups() ) || in_array( $par, $symsForAll ) )
104 ) {
105 $this->requestedGroup = $par;
106 $un = $request->getText( 'username' );
107 } elseif ( count( $parms ) == 2 ) {
108 $this->requestedGroup = $parms[0];
109 $un = $parms[1];
110 } else {
111 $this->requestedGroup = $request->getVal( 'group' );
112 $un = ( $par != '' ) ? $par : $request->getText( 'username' );
113 }
114
115 if ( in_array( $this->requestedGroup, $symsForAll ) ) {
116 $this->requestedGroup = '';
117 }
118 $this->editsOnly = $request->getBool( 'editsOnly' );
119 $this->temporaryGroupsOnly = $request->getBool( 'temporaryGroupsOnly' );
120 $this->creationSort = $request->getBool( 'creationSort' );
121 $this->including = $including;
122 $this->mDefaultDirection = $request->getBool( 'desc' )
125
126 $this->requestedUser = '';
127
128 if ( $un != '' ) {
129 $username = Title::makeTitleSafe( NS_USER, $un );
130
131 if ( $username !== null ) {
132 $this->requestedUser = $username->getText();
133 }
134 }
135
136 // Set database before parent constructor to avoid setting it there with wfGetDB
137 $this->mDb = $loadBalancer->getConnectionRef( ILoadBalancer::DB_REPLICA );
138 parent::__construct();
139 $this->userGroupManager = $userGroupManager;
140 $this->hookRunner = new HookRunner( $hookContainer );
141 $this->linkBatchFactory = $linkBatchFactory;
142 }
143
147 public function getIndexField() {
148 return $this->creationSort ? 'user_id' : 'user_name';
149 }
150
154 public function getQueryInfo() {
155 $dbr = $this->getDatabase();
156 $conds = [];
157
158 // Don't show hidden names
159 if ( !$this->canSeeHideuser() ) {
160 $conds[] = 'ipb_deleted IS NULL OR ipb_deleted = 0';
161 }
162
163 $options = [];
164
165 if ( $this->requestedGroup != '' || $this->temporaryGroupsOnly ) {
166 $conds[] = 'ug_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() ) .
167 ( !$this->temporaryGroupsOnly ? ' OR ug_expiry IS NULL' : '' );
168 }
169
170 if ( $this->requestedGroup != '' ) {
171 $conds['ug_group'] = $this->requestedGroup;
172 }
173
174 if ( $this->requestedUser != '' ) {
175 # Sorted either by account creation or name
176 if ( $this->creationSort ) {
177 $conds[] = 'user_id >= ' . intval( User::idFromName( $this->requestedUser ) );
178 } else {
179 $conds[] = 'user_name >= ' . $dbr->addQuotes( $this->requestedUser );
180 }
181 }
182
183 if ( $this->editsOnly ) {
184 $conds[] = 'user_editcount > 0';
185 }
186
187 $options['GROUP BY'] = $this->creationSort ? 'user_id' : 'user_name';
188
189 $query = [
190 'tables' => [ 'user', 'user_groups', 'ipblocks' ],
191 'fields' => [
192 'user_name' => $this->creationSort ? 'MAX(user_name)' : 'user_name',
193 'user_id' => $this->creationSort ? 'user_id' : 'MAX(user_id)',
194 'edits' => 'MAX(user_editcount)',
195 'creation' => 'MIN(user_registration)',
196 'ipb_deleted' => 'MAX(ipb_deleted)', // block/hide status
197 'ipb_sitewide' => 'MAX(ipb_sitewide)'
198 ],
199 'options' => $options,
200 'join_conds' => [
201 'user_groups' => [ 'LEFT JOIN', 'user_id=ug_user' ],
202 'ipblocks' => [
203 'LEFT JOIN', [
204 'user_id=ipb_user',
205 'ipb_auto' => 0
206 ]
207 ],
208 ],
209 'conds' => $conds
210 ];
211
212 $this->hookRunner->onSpecialListusersQueryInfo( $this, $query );
213
214 return $query;
215 }
216
221 public function formatRow( $row ) {
222 if ( $row->user_id == 0 ) { # T18487
223 return '';
224 }
225
226 $userName = $row->user_name;
227
228 $ulinks = Linker::userLink( $row->user_id, $userName );
230 $row->user_id,
231 $userName,
232 (int)$row->edits,
233 // don't render parentheses in HTML markup (CSS will provide)
234 false
235 );
236
237 $lang = $this->getLanguage();
238
239 $groups = '';
240 $userIdentity = new UserIdentityValue( intval( $row->user_id ), $userName );
241 $ugms = $this->getGroupMemberships( $userIdentity );
242
243 if ( !$this->including && count( $ugms ) > 0 ) {
244 $list = [];
245 foreach ( $ugms as $ugm ) {
246 $list[] = $this->buildGroupLink( $ugm, $userName );
247 }
248 $groups = $lang->commaList( $list );
249 }
250
251 $item = $lang->specialList( $ulinks, $groups );
252
253 if ( $row->ipb_deleted ) {
254 $item = "<span class=\"deleted\">$item</span>";
255 }
256
257 $edits = '';
258 if ( !$this->including && $this->getConfig()->get( MainConfigNames::Edititis ) ) {
259 $count = $this->msg( 'usereditcount' )->numParams( $row->edits )->escaped();
260 $edits = $this->msg( 'word-separator' )->escaped() . $this->msg( 'brackets', $count )->escaped();
261 }
262
263 $created = '';
264 # Some rows may be null
265 if ( !$this->including && $row->creation ) {
266 $user = $this->getUser();
267 $d = $lang->userDate( $row->creation, $user );
268 $t = $lang->userTime( $row->creation, $user );
269 $created = $this->msg( 'usercreated', $d, $t, $row->user_name )->escaped();
270 $created = ' ' . $this->msg( 'parentheses' )->rawParams( $created )->escaped();
271 }
272
273 $blocked = $row->ipb_deleted !== null && $row->ipb_sitewide === '1' ?
274 ' ' . $this->msg( 'listusers-blocked', $userName )->escaped() :
275 '';
276
277 $this->hookRunner->onSpecialListusersFormatRow( $item, $row );
278
279 return Html::rawElement( 'li', [], "{$item}{$edits}{$created}{$blocked}" );
280 }
281
282 protected function doBatchLookups() {
283 $batch = $this->linkBatchFactory->newLinkBatch();
284 $userIds = [];
285 # Give some pointers to make user links
286 foreach ( $this->mResult as $row ) {
287 $batch->add( NS_USER, $row->user_name );
288 $batch->add( NS_USER_TALK, $row->user_name );
289 $userIds[] = $row->user_id;
290 }
291
292 // Lookup groups for all the users
293 $dbr = $this->getDatabase();
294 $groupsQueryInfo = $this->userGroupManager->getQueryInfo();
295 $groupRes = $dbr->select(
296 $groupsQueryInfo['tables'],
297 $groupsQueryInfo['fields'],
298 [ 'ug_user' => $userIds ],
299 __METHOD__,
300 $groupsQueryInfo['joins']
301 );
302 $cache = [];
303 $groups = [];
304 foreach ( $groupRes as $row ) {
305 $ugm = $this->userGroupManager->newGroupMembershipFromRow( $row );
306 if ( !$ugm->isExpired() ) {
307 $cache[$row->ug_user][$row->ug_group] = $ugm;
308 $groups[$row->ug_group] = true;
309 }
310 }
311
312 // Give extensions a chance to add things like global user group data
313 // into the cache array to ensure proper output later on
314 $this->hookRunner->onUsersPagerDoBatchLookups( $dbr, $userIds, $cache, $groups );
315
316 $this->userGroupCache = $cache;
317
318 // Add page of groups to link batch
319 foreach ( $groups as $group => $unused ) {
320 $groupPage = UserGroupMembership::getGroupPage( $group );
321 if ( $groupPage ) {
322 $batch->addObj( $groupPage );
323 }
324 }
325
326 $batch->execute();
327 $this->mResult->rewind();
328 }
329
333 public function getPageHeader() {
334 $self = explode( '/', $this->getTitle()->getPrefixedDBkey(), 2 )[0];
335
336 $groupOptions = [ $this->msg( 'group-all' )->text() => '' ];
337 foreach ( $this->getAllGroups() as $group => $groupText ) {
338 $groupOptions[ $groupText ] = $group;
339 }
340
341 $formDescriptor = [
342 'user' => [
343 'class' => HTMLUserTextField::class,
344 'label' => $this->msg( 'listusersfrom' )->text(),
345 'name' => 'username',
346 'default' => $this->requestedUser,
347 ],
348 'dropdown' => [
349 'label' => $this->msg( 'group' )->text(),
350 'name' => 'group',
351 'default' => $this->requestedGroup,
352 'class' => HTMLSelectField::class,
353 'options' => $groupOptions,
354 ],
355 'editsOnly' => [
356 'type' => 'check',
357 'label' => $this->msg( 'listusers-editsonly' )->text(),
358 'name' => 'editsOnly',
359 'id' => 'editsOnly',
360 'default' => $this->editsOnly
361 ],
362 'temporaryGroupsOnly' => [
363 'type' => 'check',
364 'label' => $this->msg( 'listusers-temporarygroupsonly' )->text(),
365 'name' => 'temporaryGroupsOnly',
366 'id' => 'temporaryGroupsOnly',
367 'default' => $this->temporaryGroupsOnly
368 ],
369 'creationSort' => [
370 'type' => 'check',
371 'label' => $this->msg( 'listusers-creationsort' )->text(),
372 'name' => 'creationSort',
373 'id' => 'creationSort',
374 'default' => $this->creationSort
375 ],
376 'desc' => [
377 'type' => 'check',
378 'label' => $this->msg( 'listusers-desc' )->text(),
379 'name' => 'desc',
380 'id' => 'desc',
381 'default' => $this->mDefaultDirection
382 ],
383 'limithiddenfield' => [
384 'class' => HTMLHiddenField::class,
385 'name' => 'limit',
386 'default' => $this->mLimit
387 ]
388 ];
389
390 $beforeSubmitButtonHookOut = '';
391 $this->hookRunner->onSpecialListusersHeaderForm( $this, $beforeSubmitButtonHookOut );
392
393 if ( $beforeSubmitButtonHookOut !== '' ) {
394 $formDescriptor[ 'beforeSubmitButtonHookOut' ] = [
395 'class' => HTMLInfoField::class,
396 'raw' => true,
397 'default' => $beforeSubmitButtonHookOut
398 ];
399 }
400
401 $formDescriptor[ 'submit' ] = [
402 'class' => HTMLSubmitField::class,
403 'buttonlabel-message' => 'listusers-submit',
404 ];
405
406 $beforeClosingFieldsetHookOut = '';
407 $this->hookRunner->onSpecialListusersHeader( $this, $beforeClosingFieldsetHookOut );
408
409 if ( $beforeClosingFieldsetHookOut !== '' ) {
410 $formDescriptor[ 'beforeClosingFieldsetHookOut' ] = [
411 'class' => HTMLInfoField::class,
412 'raw' => true,
413 'default' => $beforeClosingFieldsetHookOut
414 ];
415 }
416
417 $htmlForm = HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() );
418 $htmlForm
419 ->setMethod( 'get' )
420 ->setTitle( Title::newFromText( $self ) )
421 ->setId( 'mw-listusers-form' )
422 ->setFormIdentifier( 'mw-listusers-form' )
423 ->suppressDefaultSubmit()
424 ->setWrapperLegendMsg( 'listusers' );
425 return $htmlForm->prepareForm()->getHTML( true );
426 }
427
428 protected function canSeeHideuser() {
429 return $this->getAuthority()->isAllowed( 'hideuser' );
430 }
431
436 private function getAllGroups() {
437 $result = [];
438 $lang = $this->getLanguage();
439 foreach ( $this->userGroupManager->listAllGroups() as $group ) {
440 $result[$group] = $lang->getGroupName( $group );
441 }
442 asort( $result );
443
444 return $result;
445 }
446
451 public function getDefaultQuery() {
452 $query = parent::getDefaultQuery();
453 if ( $this->requestedGroup != '' ) {
454 $query['group'] = $this->requestedGroup;
455 }
456 if ( $this->requestedUser != '' ) {
457 $query['username'] = $this->requestedUser;
458 }
459 $this->hookRunner->onSpecialListusersDefaultQuery( $this, $query );
460
461 return $query;
462 }
463
471 protected function getGroupMemberships( $user ) {
472 if ( $this->userGroupCache === null ) {
473 return $this->userGroupManager->getUserGroupMemberships( $user );
474 } else {
475 return $this->userGroupCache[$user->getId()] ?? [];
476 }
477 }
478
486 protected function buildGroupLink( $group, $username ) {
487 return UserGroupMembership::getLink( $group, $this->getContext(), 'html', $username );
488 }
489}
getUser()
getAuthority()
const NS_USER
Definition Defines.php:66
const NS_USER_TALK
Definition Defines.php:67
getContext()
IndexPager with an alphabetic list and a formatted navigation bar.
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
setContext(IContextSource $context)
const DIR_ASCENDING
Backwards-compatible constant for $mDefaultDirection field (do not change)
getDatabase()
Get the Database object in use.
const DIR_DESCENDING
Backwards-compatible constant for $mDefaultDirection field (do not change)
static userLink( $userId, $userName, $altUserName=false)
Make user link (or user contributions for unregistered users)
Definition Linker.php:1114
static userToolLinksRedContribs( $userId, $userText, $edits=null, $useParentheses=true)
Alias for userToolLinks( $userId, $userText, true );.
Definition Linker.php:1242
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
A class containing constants representing the names of configuration variables.
listAllGroups()
Return the set of defined explicit groups.
Value object representing a user's identity.
static idFromName( $name, $flags=self::READ_NORMAL)
Get database id given a user name.
Definition User.php:936
This class is used to get a list of user.
bool $temporaryGroupsOnly
bool null $including
string $requestedUser
array[] $userGroupCache
A array with user ids as key and a array of groups as value.
bool $editsOnly
buildGroupLink( $group, $username)
Format a link to a group description page.
__construct(IContextSource $context, HookContainer $hookContainer, LinkBatchFactory $linkBatchFactory, ILoadBalancer $loadBalancer, UserGroupManager $userGroupManager, $par, $including)
formatRow( $row)
getGroupMemberships( $user)
Get an associative array containing groups the specified user belongs to, and the relevant UserGroupM...
bool $creationSort
doBatchLookups()
Called from getBody(), before getStartBody() is called and after doQuery() was called.
getDefaultQuery()
Preserve group and username offset parameters when paging.
string $requestedGroup
Interface for objects which can provide a MediaWiki context on request.
Interface for objects representing user identity.
Create and track the database connections and transactions for a given database cluster.
getConnectionRef( $i, $groups=[], $domain=false, $flags=0)
$cache
Definition mcc.php:33
if(!isset( $args[0])) $lang