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
27 * This class is used to get a list of user. The ones with specials
28 * rights (sysop, bureaucrat, developer) will have them displayed
29 * next to their names.
33 class UsersPager
extends AlphabeticPager
{
36 * @var array[] A array with user ids as key and a array of groups as value
38 protected $userGroupCache;
41 * @param IContextSource|null $context
42 * @param array|null $par (Default null)
43 * @param bool|null $including Whether this page is being transcluded in
46 public function __construct( IContextSource
$context = null, $par = null, $including = null ) {
48 $this->setContext( $context );
51 $request = $this->getRequest();
53 $parms = explode( '/', $par );
54 $symsForAll = [ '*', 'user' ];
56 if ( $parms[0] != '' &&
57 ( in_array( $par, User
::getAllGroups() ) ||
in_array( $par, $symsForAll ) )
59 $this->requestedGroup
= $par;
60 $un = $request->getText( 'username' );
61 } elseif ( count( $parms ) == 2 ) {
62 $this->requestedGroup
= $parms[0];
65 $this->requestedGroup
= $request->getVal( 'group' );
66 $un = ( $par != '' ) ?
$par : $request->getText( 'username' );
69 if ( in_array( $this->requestedGroup
, $symsForAll ) ) {
70 $this->requestedGroup
= '';
72 $this->editsOnly
= $request->getBool( 'editsOnly' );
73 $this->temporaryGroupsOnly
= $request->getBool( 'temporaryGroupsOnly' );
74 $this->creationSort
= $request->getBool( 'creationSort' );
75 $this->including
= $including;
76 $this->mDefaultDirection
= $request->getBool( 'desc' )
77 ? IndexPager
::DIR_DESCENDING
78 : IndexPager
::DIR_ASCENDING
;
80 $this->requestedUser
= '';
83 $username = Title
::makeTitleSafe( NS_USER
, $un );
85 if ( !is_null( $username ) ) {
86 $this->requestedUser
= $username->getText();
90 parent
::__construct();
96 function getIndexField() {
97 return $this->creationSort ?
'user_id' : 'user_name';
103 function getQueryInfo() {
104 $dbr = wfGetDB( DB_REPLICA
);
107 // Don't show hidden names
108 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
109 $conds[] = 'ipb_deleted IS NULL OR ipb_deleted = 0';
114 if ( $this->requestedGroup
!= '' ||
$this->temporaryGroupsOnly
) {
115 $conds[] = 'ug_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() ) .
116 ( !$this->temporaryGroupsOnly ?
' OR ug_expiry IS NULL' : '' );
119 if ( $this->requestedGroup
!= '' ) {
120 $conds['ug_group'] = $this->requestedGroup
;
123 if ( $this->requestedUser
!= '' ) {
124 # Sorted either by account creation or name
125 if ( $this->creationSort
) {
126 $conds[] = 'user_id >= ' . intval( User
::idFromName( $this->requestedUser
) );
128 $conds[] = 'user_name >= ' . $dbr->addQuotes( $this->requestedUser
);
132 if ( $this->editsOnly
) {
133 $conds[] = 'user_editcount > 0';
136 $options['GROUP BY'] = $this->creationSort ?
'user_id' : 'user_name';
139 'tables' => [ 'user', 'user_groups', 'ipblocks' ],
141 'user_name' => $this->creationSort ?
'MAX(user_name)' : 'user_name',
142 'user_id' => $this->creationSort ?
'user_id' : 'MAX(user_id)',
143 'edits' => 'MAX(user_editcount)',
144 'creation' => 'MIN(user_registration)',
145 'ipb_deleted' => 'MAX(ipb_deleted)', // block/hide status
146 'ipb_sitewide' => 'MAX(ipb_sitewide)'
148 'options' => $options,
150 'user_groups' => [ 'LEFT JOIN', 'user_id=ug_user' ],
161 Hooks
::run( 'SpecialListusersQueryInfo', [ $this, &$query ] );
167 * @param stdClass $row
170 function formatRow( $row ) {
171 if ( $row->user_id
== 0 ) { # T18487
175 $userName = $row->user_name
;
177 $ulinks = Linker
::userLink( $row->user_id
, $userName );
178 $ulinks .= Linker
::userToolLinksRedContribs(
182 // don't render parentheses in HTML markup (CSS will provide)
186 $lang = $this->getLanguage();
189 $ugms = self
::getGroupMemberships( intval( $row->user_id
), $this->userGroupCache
);
191 if ( !$this->including
&& count( $ugms ) > 0 ) {
193 foreach ( $ugms as $ugm ) {
194 $list[] = $this->buildGroupLink( $ugm, $userName );
196 $groups = $lang->commaList( $list );
199 $item = $lang->specialList( $ulinks, $groups );
201 if ( $row->ipb_deleted
) {
202 $item = "<span class=\"deleted\">$item</span>";
206 if ( !$this->including
&& $this->getConfig()->get( 'Edititis' ) ) {
207 $count = $this->msg( 'usereditcount' )->numParams( $row->edits
)->escaped();
208 $edits = $this->msg( 'word-separator' )->escaped() . $this->msg( 'brackets', $count )->escaped();
212 # Some rows may be null
213 if ( !$this->including
&& $row->creation
) {
214 $user = $this->getUser();
215 $d = $lang->userDate( $row->creation
, $user );
216 $t = $lang->userTime( $row->creation
, $user );
217 $created = $this->msg( 'usercreated', $d, $t, $row->user_name
)->escaped();
218 $created = ' ' . $this->msg( 'parentheses' )->rawParams( $created )->escaped();
221 $blocked = !is_null( $row->ipb_deleted
) && $row->ipb_sitewide
=== '1' ?
222 ' ' . $this->msg( 'listusers-blocked', $userName )->escaped() :
225 Hooks
::run( 'SpecialListusersFormatRow', [ &$item, $row ] );
227 return Html
::rawElement( 'li', [], "{$item}{$edits}{$created}{$blocked}" );
230 protected function doBatchLookups() {
231 $batch = new LinkBatch();
233 # Give some pointers to make user links
234 foreach ( $this->mResult
as $row ) {
235 $batch->add( NS_USER
, $row->user_name
);
236 $batch->add( NS_USER_TALK
, $row->user_name
);
237 $userIds[] = $row->user_id
;
240 // Lookup groups for all the users
241 $dbr = wfGetDB( DB_REPLICA
);
242 $groupRes = $dbr->select(
244 UserGroupMembership
::selectFields(),
245 [ 'ug_user' => $userIds ],
250 foreach ( $groupRes as $row ) {
251 $ugm = UserGroupMembership
::newFromRow( $row );
252 if ( !$ugm->isExpired() ) {
253 $cache[$row->ug_user
][$row->ug_group
] = $ugm;
254 $groups[$row->ug_group
] = true;
258 // Give extensions a chance to add things like global user group data
259 // into the cache array to ensure proper output later on
260 Hooks
::run( 'UsersPagerDoBatchLookups', [ $dbr, $userIds, &$cache, &$groups ] );
262 $this->userGroupCache
= $cache;
264 // Add page of groups to link batch
265 foreach ( $groups as $group => $unused ) {
266 $groupPage = UserGroupMembership
::getGroupPage( $group );
268 $batch->addObj( $groupPage );
273 $this->mResult
->rewind();
279 function getPageHeader() {
280 list( $self ) = explode( '/', $this->getTitle()->getPrefixedDBkey() );
282 $groupOptions = [ $this->msg( 'group-all' )->text() => '' ];
283 foreach ( $this->getAllGroups() as $group => $groupText ) {
284 $groupOptions[ $groupText ] = $group;
289 'class' => HTMLUserTextField
::class,
290 'label' => $this->msg( 'listusersfrom' )->text(),
291 'name' => 'username',
292 'default' => $this->requestedUser
,
295 'label' => $this->msg( 'group' )->text(),
297 'default' => $this->requestedGroup
,
298 'class' => HTMLSelectField
::class,
299 'options' => $groupOptions,
303 'label' => $this->msg( 'listusers-editsonly' )->text(),
304 'name' => 'editsOnly',
306 'default' => $this->editsOnly
308 'temporaryGroupsOnly' => [
310 'label' => $this->msg( 'listusers-temporarygroupsonly' )->text(),
311 'name' => 'temporaryGroupsOnly',
312 'id' => 'temporaryGroupsOnly',
313 'default' => $this->temporaryGroupsOnly
317 'label' => $this->msg( 'listusers-creationsort' )->text(),
318 'name' => 'creationSort',
319 'id' => 'creationSort',
320 'default' => $this->creationSort
324 'label' => $this->msg( 'listusers-desc' )->text(),
327 'default' => $this->mDefaultDirection
329 'limithiddenfield' => [
330 'class' => HTMLHiddenField
::class,
332 'default' => $this->mLimit
336 $beforeSubmitButtonHookOut = '';
337 Hooks
::run( 'SpecialListusersHeaderForm', [ $this, &$beforeSubmitButtonHookOut ] );
339 if ( $beforeSubmitButtonHookOut !== '' ) {
340 $formDescriptor[ 'beforeSubmitButtonHookOut' ] = [
341 'class' => HTMLInfoField
::class,
343 'default' => $beforeSubmitButtonHookOut
347 $formDescriptor[ 'submit' ] = [
348 'class' => HTMLSubmitField
::class,
349 'buttonlabel-message' => 'listusers-submit',
352 $beforeClosingFieldsetHookOut = '';
353 Hooks
::run( 'SpecialListusersHeader', [ $this, &$beforeClosingFieldsetHookOut ] );
355 if ( $beforeClosingFieldsetHookOut !== '' ) {
356 $formDescriptor[ 'beforeClosingFieldsetHookOut' ] = [
357 'class' => HTMLInfoField
::class,
359 'default' => $beforeClosingFieldsetHookOut
363 $htmlForm = HTMLForm
::factory( 'ooui', $formDescriptor, $this->getContext() );
366 ->setAction( Title
::newFromText( $self )->getLocalURL() )
367 ->setId( 'mw-listusers-form' )
368 ->setFormIdentifier( 'mw-listusers-form' )
369 ->suppressDefaultSubmit()
370 ->setWrapperLegendMsg( 'listusers' );
371 return $htmlForm->prepareForm()->getHTML( true );
375 * Get a list of all explicit groups
378 function getAllGroups() {
380 foreach ( User
::getAllGroups() as $group ) {
381 $result[$group] = UserGroupMembership
::getGroupName( $group );
389 * Preserve group and username offset parameters when paging
392 function getDefaultQuery() {
393 $query = parent
::getDefaultQuery();
394 if ( $this->requestedGroup
!= '' ) {
395 $query['group'] = $this->requestedGroup
;
397 if ( $this->requestedUser
!= '' ) {
398 $query['username'] = $this->requestedUser
;
400 Hooks
::run( 'SpecialListusersDefaultQuery', [ $this, &$query ] );
406 * Get an associative array containing groups the specified user belongs to,
407 * and the relevant UserGroupMembership objects
409 * @param int $uid User id
410 * @param array[]|null $cache
411 * @return UserGroupMembership[] (group name => UserGroupMembership object)
413 protected static function getGroupMemberships( $uid, $cache = null ) {
414 if ( $cache === null ) {
415 $user = User
::newFromId( $uid );
416 return $user->getGroupMemberships();
418 return $cache[$uid] ??
[];
423 * Format a link to a group description page
425 * @param string|UserGroupMembership $group Group name or UserGroupMembership object
426 * @param string $username
429 protected function buildGroupLink( $group, $username ) {
430 return UserGroupMembership
::getLink( $group, $this->getContext(), 'html', $username );