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 protected $requestedGroup;
47 protected $temporaryGroupsOnly;
50 protected $creationSort;
56 protected $requestedUser;
59 * @param IContextSource|null $context
60 * @param array|null $par (Default null)
61 * @param bool|null $including Whether this page is being transcluded in
64 public function __construct( IContextSource
$context = null, $par = null, $including = null ) {
66 $this->setContext( $context );
69 $request = $this->getRequest();
71 $parms = explode( '/', $par );
72 $symsForAll = [ '*', 'user' ];
74 if ( $parms[0] != '' &&
75 ( in_array( $par, User
::getAllGroups() ) ||
in_array( $par, $symsForAll ) )
77 $this->requestedGroup
= $par;
78 $un = $request->getText( 'username' );
79 } elseif ( count( $parms ) == 2 ) {
80 $this->requestedGroup
= $parms[0];
83 $this->requestedGroup
= $request->getVal( 'group' );
84 $un = ( $par != '' ) ?
$par : $request->getText( 'username' );
87 if ( in_array( $this->requestedGroup
, $symsForAll ) ) {
88 $this->requestedGroup
= '';
90 $this->editsOnly
= $request->getBool( 'editsOnly' );
91 $this->temporaryGroupsOnly
= $request->getBool( 'temporaryGroupsOnly' );
92 $this->creationSort
= $request->getBool( 'creationSort' );
93 $this->including
= $including;
94 $this->mDefaultDirection
= $request->getBool( 'desc' )
95 ? IndexPager
::DIR_DESCENDING
96 : IndexPager
::DIR_ASCENDING
;
98 $this->requestedUser
= '';
101 $username = Title
::makeTitleSafe( NS_USER
, $un );
103 if ( !is_null( $username ) ) {
104 $this->requestedUser
= $username->getText();
108 parent
::__construct();
114 function getIndexField() {
115 return $this->creationSort ?
'user_id' : 'user_name';
121 function getQueryInfo() {
122 $dbr = wfGetDB( DB_REPLICA
);
125 // Don't show hidden names
126 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
127 $conds[] = 'ipb_deleted IS NULL OR ipb_deleted = 0';
132 if ( $this->requestedGroup
!= '' ||
$this->temporaryGroupsOnly
) {
133 $conds[] = 'ug_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() ) .
134 ( !$this->temporaryGroupsOnly ?
' OR ug_expiry IS NULL' : '' );
137 if ( $this->requestedGroup
!= '' ) {
138 $conds['ug_group'] = $this->requestedGroup
;
141 if ( $this->requestedUser
!= '' ) {
142 # Sorted either by account creation or name
143 if ( $this->creationSort
) {
144 $conds[] = 'user_id >= ' . intval( User
::idFromName( $this->requestedUser
) );
146 $conds[] = 'user_name >= ' . $dbr->addQuotes( $this->requestedUser
);
150 if ( $this->editsOnly
) {
151 $conds[] = 'user_editcount > 0';
154 $options['GROUP BY'] = $this->creationSort ?
'user_id' : 'user_name';
157 'tables' => [ 'user', 'user_groups', 'ipblocks' ],
159 'user_name' => $this->creationSort ?
'MAX(user_name)' : 'user_name',
160 'user_id' => $this->creationSort ?
'user_id' : 'MAX(user_id)',
161 'edits' => 'MAX(user_editcount)',
162 'creation' => 'MIN(user_registration)',
163 'ipb_deleted' => 'MAX(ipb_deleted)', // block/hide status
164 'ipb_sitewide' => 'MAX(ipb_sitewide)'
166 'options' => $options,
168 'user_groups' => [ 'LEFT JOIN', 'user_id=ug_user' ],
179 Hooks
::run( 'SpecialListusersQueryInfo', [ $this, &$query ] );
185 * @param stdClass $row
188 function formatRow( $row ) {
189 if ( $row->user_id
== 0 ) { # T18487
193 $userName = $row->user_name
;
195 $ulinks = Linker
::userLink( $row->user_id
, $userName );
196 $ulinks .= Linker
::userToolLinksRedContribs(
200 // don't render parentheses in HTML markup (CSS will provide)
204 $lang = $this->getLanguage();
207 $ugms = self
::getGroupMemberships( intval( $row->user_id
), $this->userGroupCache
);
209 if ( !$this->including
&& count( $ugms ) > 0 ) {
211 foreach ( $ugms as $ugm ) {
212 $list[] = $this->buildGroupLink( $ugm, $userName );
214 $groups = $lang->commaList( $list );
217 $item = $lang->specialList( $ulinks, $groups );
219 if ( $row->ipb_deleted
) {
220 $item = "<span class=\"deleted\">$item</span>";
224 if ( !$this->including
&& $this->getConfig()->get( 'Edititis' ) ) {
225 $count = $this->msg( 'usereditcount' )->numParams( $row->edits
)->escaped();
226 $edits = $this->msg( 'word-separator' )->escaped() . $this->msg( 'brackets', $count )->escaped();
230 # Some rows may be null
231 if ( !$this->including
&& $row->creation
) {
232 $user = $this->getUser();
233 $d = $lang->userDate( $row->creation
, $user );
234 $t = $lang->userTime( $row->creation
, $user );
235 $created = $this->msg( 'usercreated', $d, $t, $row->user_name
)->escaped();
236 $created = ' ' . $this->msg( 'parentheses' )->rawParams( $created )->escaped();
239 $blocked = !is_null( $row->ipb_deleted
) && $row->ipb_sitewide
=== '1' ?
240 ' ' . $this->msg( 'listusers-blocked', $userName )->escaped() :
243 Hooks
::run( 'SpecialListusersFormatRow', [ &$item, $row ] );
245 return Html
::rawElement( 'li', [], "{$item}{$edits}{$created}{$blocked}" );
248 protected function doBatchLookups() {
249 $batch = new LinkBatch();
251 # Give some pointers to make user links
252 foreach ( $this->mResult
as $row ) {
253 $batch->add( NS_USER
, $row->user_name
);
254 $batch->add( NS_USER_TALK
, $row->user_name
);
255 $userIds[] = $row->user_id
;
258 // Lookup groups for all the users
259 $dbr = wfGetDB( DB_REPLICA
);
260 $groupRes = $dbr->select(
262 UserGroupMembership
::selectFields(),
263 [ 'ug_user' => $userIds ],
268 foreach ( $groupRes as $row ) {
269 $ugm = UserGroupMembership
::newFromRow( $row );
270 if ( !$ugm->isExpired() ) {
271 $cache[$row->ug_user
][$row->ug_group
] = $ugm;
272 $groups[$row->ug_group
] = true;
276 // Give extensions a chance to add things like global user group data
277 // into the cache array to ensure proper output later on
278 Hooks
::run( 'UsersPagerDoBatchLookups', [ $dbr, $userIds, &$cache, &$groups ] );
280 $this->userGroupCache
= $cache;
282 // Add page of groups to link batch
283 foreach ( $groups as $group => $unused ) {
284 $groupPage = UserGroupMembership
::getGroupPage( $group );
286 $batch->addObj( $groupPage );
291 $this->mResult
->rewind();
297 function getPageHeader() {
298 $self = explode( '/', $this->getTitle()->getPrefixedDBkey(), 2 )[0];
300 $groupOptions = [ $this->msg( 'group-all' )->text() => '' ];
301 foreach ( $this->getAllGroups() as $group => $groupText ) {
302 $groupOptions[ $groupText ] = $group;
307 'class' => HTMLUserTextField
::class,
308 'label' => $this->msg( 'listusersfrom' )->text(),
309 'name' => 'username',
310 'default' => $this->requestedUser
,
313 'label' => $this->msg( 'group' )->text(),
315 'default' => $this->requestedGroup
,
316 'class' => HTMLSelectField
::class,
317 'options' => $groupOptions,
321 'label' => $this->msg( 'listusers-editsonly' )->text(),
322 'name' => 'editsOnly',
324 'default' => $this->editsOnly
326 'temporaryGroupsOnly' => [
328 'label' => $this->msg( 'listusers-temporarygroupsonly' )->text(),
329 'name' => 'temporaryGroupsOnly',
330 'id' => 'temporaryGroupsOnly',
331 'default' => $this->temporaryGroupsOnly
335 'label' => $this->msg( 'listusers-creationsort' )->text(),
336 'name' => 'creationSort',
337 'id' => 'creationSort',
338 'default' => $this->creationSort
342 'label' => $this->msg( 'listusers-desc' )->text(),
345 'default' => $this->mDefaultDirection
347 'limithiddenfield' => [
348 'class' => HTMLHiddenField
::class,
350 'default' => $this->mLimit
354 $beforeSubmitButtonHookOut = '';
355 Hooks
::run( 'SpecialListusersHeaderForm', [ $this, &$beforeSubmitButtonHookOut ] );
357 if ( $beforeSubmitButtonHookOut !== '' ) {
358 $formDescriptor[ 'beforeSubmitButtonHookOut' ] = [
359 'class' => HTMLInfoField
::class,
361 'default' => $beforeSubmitButtonHookOut
365 $formDescriptor[ 'submit' ] = [
366 'class' => HTMLSubmitField
::class,
367 'buttonlabel-message' => 'listusers-submit',
370 $beforeClosingFieldsetHookOut = '';
371 Hooks
::run( 'SpecialListusersHeader', [ $this, &$beforeClosingFieldsetHookOut ] );
373 if ( $beforeClosingFieldsetHookOut !== '' ) {
374 $formDescriptor[ 'beforeClosingFieldsetHookOut' ] = [
375 'class' => HTMLInfoField
::class,
377 'default' => $beforeClosingFieldsetHookOut
381 $htmlForm = HTMLForm
::factory( 'ooui', $formDescriptor, $this->getContext() );
384 ->setAction( Title
::newFromText( $self )->getLocalURL() )
385 ->setId( 'mw-listusers-form' )
386 ->setFormIdentifier( 'mw-listusers-form' )
387 ->suppressDefaultSubmit()
388 ->setWrapperLegendMsg( 'listusers' );
389 return $htmlForm->prepareForm()->getHTML( true );
393 * Get a list of all explicit groups
396 function getAllGroups() {
398 foreach ( User
::getAllGroups() as $group ) {
399 $result[$group] = UserGroupMembership
::getGroupName( $group );
407 * Preserve group and username offset parameters when paging
410 function getDefaultQuery() {
411 $query = parent
::getDefaultQuery();
412 if ( $this->requestedGroup
!= '' ) {
413 $query['group'] = $this->requestedGroup
;
415 if ( $this->requestedUser
!= '' ) {
416 $query['username'] = $this->requestedUser
;
418 Hooks
::run( 'SpecialListusersDefaultQuery', [ $this, &$query ] );
424 * Get an associative array containing groups the specified user belongs to,
425 * and the relevant UserGroupMembership objects
427 * @param int $uid User id
428 * @param array[]|null $cache
429 * @return UserGroupMembership[] (group name => UserGroupMembership object)
431 protected static function getGroupMemberships( $uid, $cache = null ) {
432 if ( $cache === null ) {
433 $user = User
::newFromId( $uid );
434 return $user->getGroupMemberships();
436 return $cache[$uid] ??
[];
441 * Format a link to a group description page
443 * @param string|UserGroupMembership $group Group name or UserGroupMembership object
444 * @param string $username
447 protected function buildGroupLink( $group, $username ) {
448 return UserGroupMembership
::getLink( $group, $this->getContext(), 'html', $username );