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 $context
42 * @param array $par (Default null)
43 * @param bool $including Whether this page is being transcluded in
46 function __construct( IContextSource
$context = null, $par = null, $including = null ) {
48 $this->setContext( $context );
51 $request = $this->getRequest();
52 $par = ( $par !== null ) ?
$par : '';
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->creationSort
= $request->getBool( 'creationSort' );
74 $this->including
= $including;
75 $this->mDefaultDirection
= $request->getBool( 'desc' )
76 ? IndexPager
::DIR_DESCENDING
77 : IndexPager
::DIR_ASCENDING
;
79 $this->requestedUser
= '';
82 $username = Title
::makeTitleSafe( NS_USER
, $un );
84 if ( !is_null( $username ) ) {
85 $this->requestedUser
= $username->getText();
89 parent
::__construct();
95 function getIndexField() {
96 return $this->creationSort ?
'user_id' : 'user_name';
102 function getQueryInfo() {
103 $dbr = wfGetDB( DB_REPLICA
);
106 // Don't show hidden names
107 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
108 $conds[] = 'ipb_deleted IS NULL OR ipb_deleted = 0';
113 if ( $this->requestedGroup
!= '' ) {
114 $conds['ug_group'] = $this->requestedGroup
;
115 if ( !$this->getConfig()->get( 'DisableUserGroupExpiry' ) ) {
116 $conds[] = 'ug_expiry IS NULL OR ug_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() );
120 if ( $this->requestedUser
!= '' ) {
121 # Sorted either by account creation or name
122 if ( $this->creationSort
) {
123 $conds[] = 'user_id >= ' . intval( User
::idFromName( $this->requestedUser
) );
125 $conds[] = 'user_name >= ' . $dbr->addQuotes( $this->requestedUser
);
129 if ( $this->editsOnly
) {
130 $conds[] = 'user_editcount > 0';
133 $options['GROUP BY'] = $this->creationSort ?
'user_id' : 'user_name';
136 'tables' => [ 'user', 'user_groups', 'ipblocks' ],
138 'user_name' => $this->creationSort ?
'MAX(user_name)' : 'user_name',
139 'user_id' => $this->creationSort ?
'user_id' : 'MAX(user_id)',
140 'edits' => 'MAX(user_editcount)',
141 'creation' => 'MIN(user_registration)',
142 'ipb_deleted' => 'MAX(ipb_deleted)' // block/hide status
144 'options' => $options,
146 'user_groups' => [ 'LEFT JOIN', 'user_id=ug_user' ],
157 Hooks
::run( 'SpecialListusersQueryInfo', [ $this, &$query ] );
163 * @param stdClass $row
166 function formatRow( $row ) {
167 if ( $row->user_id
== 0 ) { # Bug 16487
171 $userName = $row->user_name
;
173 $ulinks = Linker
::userLink( $row->user_id
, $userName );
174 $ulinks .= Linker
::userToolLinksRedContribs(
180 $lang = $this->getLanguage();
183 $ugms = self
::getGroupMemberships( intval( $row->user_id
), $this->userGroupCache
);
185 if ( !$this->including
&& count( $ugms ) > 0 ) {
187 foreach ( $ugms as $ugm ) {
188 $list[] = $this->buildGroupLink( $ugm, $userName );
190 $groups = $lang->commaList( $list );
193 $item = $lang->specialList( $ulinks, $groups );
195 if ( $row->ipb_deleted
) {
196 $item = "<span class=\"deleted\">$item</span>";
200 if ( !$this->including
&& $this->getConfig()->get( 'Edititis' ) ) {
201 $count = $this->msg( 'usereditcount' )->numParams( $row->edits
)->escaped();
202 $edits = $this->msg( 'word-separator' )->escaped() . $this->msg( 'brackets', $count )->escaped();
206 # Some rows may be null
207 if ( !$this->including
&& $row->creation
) {
208 $user = $this->getUser();
209 $d = $lang->userDate( $row->creation
, $user );
210 $t = $lang->userTime( $row->creation
, $user );
211 $created = $this->msg( 'usercreated', $d, $t, $row->user_name
)->escaped();
212 $created = ' ' . $this->msg( 'parentheses' )->rawParams( $created )->escaped();
214 $blocked = !is_null( $row->ipb_deleted
) ?
215 ' ' . $this->msg( 'listusers-blocked', $userName )->escaped() :
218 Hooks
::run( 'SpecialListusersFormatRow', [ &$item, $row ] );
220 return Html
::rawElement( 'li', [], "{$item}{$edits}{$created}{$blocked}" );
223 function doBatchLookups() {
224 $batch = new LinkBatch();
226 # Give some pointers to make user links
227 foreach ( $this->mResult
as $row ) {
228 $batch->add( NS_USER
, $row->user_name
);
229 $batch->add( NS_USER_TALK
, $row->user_name
);
230 $userIds[] = $row->user_id
;
233 // Lookup groups for all the users
234 $dbr = wfGetDB( DB_REPLICA
);
235 $groupRes = $dbr->select(
237 UserGroupMembership
::selectFields(),
238 [ 'ug_user' => $userIds ],
243 foreach ( $groupRes as $row ) {
244 $ugm = UserGroupMembership
::newFromRow( $row );
245 if ( !$ugm->isExpired() ) {
246 $cache[$row->ug_user
][$row->ug_group
] = $ugm;
247 $groups[$row->ug_group
] = true;
251 // Give extensions a chance to add things like global user group data
252 // into the cache array to ensure proper output later on
253 Hooks
::run( 'UsersPagerDoBatchLookups', [ $dbr, $userIds, &$cache, &$groups ] );
255 $this->userGroupCache
= $cache;
257 // Add page of groups to link batch
258 foreach ( $groups as $group => $unused ) {
259 $groupPage = UserGroupMembership
::getGroupPage( $group );
261 $batch->addObj( $groupPage );
266 $this->mResult
->rewind();
272 function getPageHeader() {
273 list( $self ) = explode( '/', $this->getTitle()->getPrefixedDBkey() );
275 $this->getOutput()->addModules( 'mediawiki.userSuggest' );
278 $out = Xml
::openElement(
280 [ 'method' => 'get', 'action' => wfScript(), 'id' => 'mw-listusers-form' ]
282 Xml
::fieldset( $this->msg( 'listusers' )->text() ) .
283 Html
::hidden( 'title', $self );
285 # Username field (with autocompletion support)
286 $out .= Xml
::label( $this->msg( 'listusersfrom' )->text(), 'offset' ) . ' ' .
289 $this->requestedUser
,
292 'class' => 'mw-autocomplete-user',
295 'autofocus' => $this->requestedUser
=== ''
299 # Group drop-down list
300 $sel = new XmlSelect( 'group', 'group', $this->requestedGroup
);
301 $sel->addOption( $this->msg( 'group-all' )->text(), '' );
302 foreach ( $this->getAllGroups() as $group => $groupText ) {
303 $sel->addOption( $groupText, $group );
306 $out .= Xml
::label( $this->msg( 'group' )->text(), 'group' ) . ' ';
307 $out .= $sel->getHTML() . '<br />';
308 $out .= Xml
::checkLabel(
309 $this->msg( 'listusers-editsonly' )->text(),
315 $out .= Xml
::checkLabel(
316 $this->msg( 'listusers-creationsort' )->text(),
322 $out .= Xml
::checkLabel(
323 $this->msg( 'listusers-desc' )->text(),
326 $this->mDefaultDirection
330 Hooks
::run( 'SpecialListusersHeaderForm', [ $this, &$out ] );
332 # Submit button and form bottom
333 $out .= Html
::hidden( 'limit', $this->mLimit
);
334 $out .= Xml
::submitButton( $this->msg( 'listusers-submit' )->text() );
335 Hooks
::run( 'SpecialListusersHeader', [ $this, &$out ] );
336 $out .= Xml
::closeElement( 'fieldset' ) .
337 Xml
::closeElement( 'form' );
343 * Get a list of all explicit groups
346 function getAllGroups() {
348 foreach ( User
::getAllGroups() as $group ) {
349 $result[$group] = UserGroupMembership
::getGroupName( $group );
357 * Preserve group and username offset parameters when paging
360 function getDefaultQuery() {
361 $query = parent
::getDefaultQuery();
362 if ( $this->requestedGroup
!= '' ) {
363 $query['group'] = $this->requestedGroup
;
365 if ( $this->requestedUser
!= '' ) {
366 $query['username'] = $this->requestedUser
;
368 Hooks
::run( 'SpecialListusersDefaultQuery', [ $this, &$query ] );
374 * Get an associative array containing groups the specified user belongs to,
375 * and the relevant UserGroupMembership objects
377 * @param int $uid User id
378 * @param array|null $cache
379 * @return array (group name => UserGroupMembership object)
381 protected static function getGroupMemberships( $uid, $cache = null ) {
382 if ( $cache === null ) {
383 $user = User
::newFromId( $uid );
384 return $user->getGroupMemberships();
386 return isset( $cache[$uid] ) ?
$cache[$uid] : [];
391 * Format a link to a group description page
393 * @param string|UserGroupMembership $group Group name or UserGroupMembership object
394 * @param string $username Username
397 protected function buildGroupLink( $group, $username ) {
398 return UserGroupMembership
::getLink( $group, $this->getContext(), 'html', $username );