3 * Copyright © 2004 Brion Vibber, lcrocker, Tim Starling,
4 * Domas Mituzas, Antoine Musso, Jens Frank, Zhengzhu,
5 * 2006 Rob Church <robchur@gmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
26 use MediaWiki\MediaWikiServices
;
29 * This class is used to get a list of user. The ones with specials
30 * rights (sysop, bureaucrat, developer) will have them displayed
31 * next to their names.
35 class UsersPager
extends AlphabeticPager
{
38 * @var array[] A array with user ids as key and a array of groups as value
40 protected $userGroupCache;
43 protected $requestedGroup;
49 protected $temporaryGroupsOnly;
52 protected $creationSort;
58 protected $requestedUser;
61 * @param IContextSource|null $context
62 * @param array|null $par (Default null)
63 * @param bool|null $including Whether this page is being transcluded in
66 public function __construct( IContextSource
$context = null, $par = null, $including = null ) {
68 $this->setContext( $context );
71 $request = $this->getRequest();
73 $parms = explode( '/', $par );
74 $symsForAll = [ '*', 'user' ];
76 if ( $parms[0] != '' &&
77 ( in_array( $par, User
::getAllGroups() ) ||
in_array( $par, $symsForAll ) )
79 $this->requestedGroup
= $par;
80 $un = $request->getText( 'username' );
81 } elseif ( count( $parms ) == 2 ) {
82 $this->requestedGroup
= $parms[0];
85 $this->requestedGroup
= $request->getVal( 'group' );
86 $un = ( $par != '' ) ?
$par : $request->getText( 'username' );
89 if ( in_array( $this->requestedGroup
, $symsForAll ) ) {
90 $this->requestedGroup
= '';
92 $this->editsOnly
= $request->getBool( 'editsOnly' );
93 $this->temporaryGroupsOnly
= $request->getBool( 'temporaryGroupsOnly' );
94 $this->creationSort
= $request->getBool( 'creationSort' );
95 $this->including
= $including;
96 $this->mDefaultDirection
= $request->getBool( 'desc' )
97 ? IndexPager
::DIR_DESCENDING
98 : IndexPager
::DIR_ASCENDING
;
100 $this->requestedUser
= '';
103 $username = Title
::makeTitleSafe( NS_USER
, $un );
105 if ( !is_null( $username ) ) {
106 $this->requestedUser
= $username->getText();
110 parent
::__construct();
116 function getIndexField() {
117 return $this->creationSort ?
'user_id' : 'user_name';
123 function getQueryInfo() {
124 $dbr = wfGetDB( DB_REPLICA
);
127 // Don't show hidden names
128 if ( !MediaWikiServices
::getInstance()
129 ->getPermissionManager()
130 ->userHasRight( $this->getUser(), 'hideuser' )
132 $conds[] = 'ipb_deleted IS NULL OR ipb_deleted = 0';
137 if ( $this->requestedGroup
!= '' ||
$this->temporaryGroupsOnly
) {
138 $conds[] = 'ug_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() ) .
139 ( !$this->temporaryGroupsOnly ?
' OR ug_expiry IS NULL' : '' );
142 if ( $this->requestedGroup
!= '' ) {
143 $conds['ug_group'] = $this->requestedGroup
;
146 if ( $this->requestedUser
!= '' ) {
147 # Sorted either by account creation or name
148 if ( $this->creationSort
) {
149 $conds[] = 'user_id >= ' . intval( User
::idFromName( $this->requestedUser
) );
151 $conds[] = 'user_name >= ' . $dbr->addQuotes( $this->requestedUser
);
155 if ( $this->editsOnly
) {
156 $conds[] = 'user_editcount > 0';
159 $options['GROUP BY'] = $this->creationSort ?
'user_id' : 'user_name';
162 'tables' => [ 'user', 'user_groups', 'ipblocks' ],
164 'user_name' => $this->creationSort ?
'MAX(user_name)' : 'user_name',
165 'user_id' => $this->creationSort ?
'user_id' : 'MAX(user_id)',
166 'edits' => 'MAX(user_editcount)',
167 'creation' => 'MIN(user_registration)',
168 'ipb_deleted' => 'MAX(ipb_deleted)', // block/hide status
169 'ipb_sitewide' => 'MAX(ipb_sitewide)'
171 'options' => $options,
173 'user_groups' => [ 'LEFT JOIN', 'user_id=ug_user' ],
184 Hooks
::run( 'SpecialListusersQueryInfo', [ $this, &$query ] );
190 * @param stdClass $row
193 function formatRow( $row ) {
194 if ( $row->user_id
== 0 ) { # T18487
198 $userName = $row->user_name
;
200 $ulinks = Linker
::userLink( $row->user_id
, $userName );
201 $ulinks .= Linker
::userToolLinksRedContribs(
205 // don't render parentheses in HTML markup (CSS will provide)
209 $lang = $this->getLanguage();
212 $ugms = self
::getGroupMemberships( intval( $row->user_id
), $this->userGroupCache
);
214 if ( !$this->including
&& count( $ugms ) > 0 ) {
216 foreach ( $ugms as $ugm ) {
217 $list[] = $this->buildGroupLink( $ugm, $userName );
219 $groups = $lang->commaList( $list );
222 $item = $lang->specialList( $ulinks, $groups );
224 if ( $row->ipb_deleted
) {
225 $item = "<span class=\"deleted\">$item</span>";
229 if ( !$this->including
&& $this->getConfig()->get( 'Edititis' ) ) {
230 $count = $this->msg( 'usereditcount' )->numParams( $row->edits
)->escaped();
231 $edits = $this->msg( 'word-separator' )->escaped() . $this->msg( 'brackets', $count )->escaped();
235 # Some rows may be null
236 if ( !$this->including
&& $row->creation
) {
237 $user = $this->getUser();
238 $d = $lang->userDate( $row->creation
, $user );
239 $t = $lang->userTime( $row->creation
, $user );
240 $created = $this->msg( 'usercreated', $d, $t, $row->user_name
)->escaped();
241 $created = ' ' . $this->msg( 'parentheses' )->rawParams( $created )->escaped();
244 $blocked = !is_null( $row->ipb_deleted
) && $row->ipb_sitewide
=== '1' ?
245 ' ' . $this->msg( 'listusers-blocked', $userName )->escaped() :
248 Hooks
::run( 'SpecialListusersFormatRow', [ &$item, $row ] );
250 return Html
::rawElement( 'li', [], "{$item}{$edits}{$created}{$blocked}" );
253 protected function doBatchLookups() {
254 $batch = new LinkBatch();
256 # Give some pointers to make user links
257 foreach ( $this->mResult
as $row ) {
258 $batch->add( NS_USER
, $row->user_name
);
259 $batch->add( NS_USER_TALK
, $row->user_name
);
260 $userIds[] = $row->user_id
;
263 // Lookup groups for all the users
264 $dbr = wfGetDB( DB_REPLICA
);
265 $groupRes = $dbr->select(
267 UserGroupMembership
::selectFields(),
268 [ 'ug_user' => $userIds ],
273 foreach ( $groupRes as $row ) {
274 $ugm = UserGroupMembership
::newFromRow( $row );
275 if ( !$ugm->isExpired() ) {
276 $cache[$row->ug_user
][$row->ug_group
] = $ugm;
277 $groups[$row->ug_group
] = true;
281 // Give extensions a chance to add things like global user group data
282 // into the cache array to ensure proper output later on
283 Hooks
::run( 'UsersPagerDoBatchLookups', [ $dbr, $userIds, &$cache, &$groups ] );
285 $this->userGroupCache
= $cache;
287 // Add page of groups to link batch
288 foreach ( $groups as $group => $unused ) {
289 $groupPage = UserGroupMembership
::getGroupPage( $group );
291 $batch->addObj( $groupPage );
296 $this->mResult
->rewind();
302 function getPageHeader() {
303 $self = explode( '/', $this->getTitle()->getPrefixedDBkey(), 2 )[0];
305 $groupOptions = [ $this->msg( 'group-all' )->text() => '' ];
306 foreach ( $this->getAllGroups() as $group => $groupText ) {
307 $groupOptions[ $groupText ] = $group;
312 'class' => HTMLUserTextField
::class,
313 'label' => $this->msg( 'listusersfrom' )->text(),
314 'name' => 'username',
315 'default' => $this->requestedUser
,
318 'label' => $this->msg( 'group' )->text(),
320 'default' => $this->requestedGroup
,
321 'class' => HTMLSelectField
::class,
322 'options' => $groupOptions,
326 'label' => $this->msg( 'listusers-editsonly' )->text(),
327 'name' => 'editsOnly',
329 'default' => $this->editsOnly
331 'temporaryGroupsOnly' => [
333 'label' => $this->msg( 'listusers-temporarygroupsonly' )->text(),
334 'name' => 'temporaryGroupsOnly',
335 'id' => 'temporaryGroupsOnly',
336 'default' => $this->temporaryGroupsOnly
340 'label' => $this->msg( 'listusers-creationsort' )->text(),
341 'name' => 'creationSort',
342 'id' => 'creationSort',
343 'default' => $this->creationSort
347 'label' => $this->msg( 'listusers-desc' )->text(),
350 'default' => $this->mDefaultDirection
352 'limithiddenfield' => [
353 'class' => HTMLHiddenField
::class,
355 'default' => $this->mLimit
359 $beforeSubmitButtonHookOut = '';
360 Hooks
::run( 'SpecialListusersHeaderForm', [ $this, &$beforeSubmitButtonHookOut ] );
362 if ( $beforeSubmitButtonHookOut !== '' ) {
363 $formDescriptor[ 'beforeSubmitButtonHookOut' ] = [
364 'class' => HTMLInfoField
::class,
366 'default' => $beforeSubmitButtonHookOut
370 $formDescriptor[ 'submit' ] = [
371 'class' => HTMLSubmitField
::class,
372 'buttonlabel-message' => 'listusers-submit',
375 $beforeClosingFieldsetHookOut = '';
376 Hooks
::run( 'SpecialListusersHeader', [ $this, &$beforeClosingFieldsetHookOut ] );
378 if ( $beforeClosingFieldsetHookOut !== '' ) {
379 $formDescriptor[ 'beforeClosingFieldsetHookOut' ] = [
380 'class' => HTMLInfoField
::class,
382 'default' => $beforeClosingFieldsetHookOut
386 $htmlForm = HTMLForm
::factory( 'ooui', $formDescriptor, $this->getContext() );
389 ->setAction( Title
::newFromText( $self )->getLocalURL() )
390 ->setId( 'mw-listusers-form' )
391 ->setFormIdentifier( 'mw-listusers-form' )
392 ->suppressDefaultSubmit()
393 ->setWrapperLegendMsg( 'listusers' );
394 return $htmlForm->prepareForm()->getHTML( true );
398 * Get a list of all explicit groups
401 function getAllGroups() {
403 foreach ( User
::getAllGroups() as $group ) {
404 $result[$group] = UserGroupMembership
::getGroupName( $group );
412 * Preserve group and username offset parameters when paging
415 function getDefaultQuery() {
416 $query = parent
::getDefaultQuery();
417 if ( $this->requestedGroup
!= '' ) {
418 $query['group'] = $this->requestedGroup
;
420 if ( $this->requestedUser
!= '' ) {
421 $query['username'] = $this->requestedUser
;
423 Hooks
::run( 'SpecialListusersDefaultQuery', [ $this, &$query ] );
429 * Get an associative array containing groups the specified user belongs to,
430 * and the relevant UserGroupMembership objects
432 * @param int $uid User id
433 * @param array[]|null $cache
434 * @return UserGroupMembership[] (group name => UserGroupMembership object)
436 protected static function getGroupMemberships( $uid, $cache = null ) {
437 if ( $cache === null ) {
438 $user = User
::newFromId( $uid );
439 return $user->getGroupMemberships();
441 return $cache[$uid] ??
[];
446 * Format a link to a group description page
448 * @param string|UserGroupMembership $group Group name or UserGroupMembership object
449 * @param string $username
452 protected function buildGroupLink( $group, $username ) {
453 return UserGroupMembership
::getLink( $group, $this->getContext(), 'html', $username );