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
;
117 if ( $this->requestedUser
!= '' ) {
118 # Sorted either by account creation or name
119 if ( $this->creationSort
) {
120 $conds[] = 'user_id >= ' . intval( User
::idFromName( $this->requestedUser
) );
122 $conds[] = 'user_name >= ' . $dbr->addQuotes( $this->requestedUser
);
126 if ( $this->editsOnly
) {
127 $conds[] = 'user_editcount > 0';
130 $options['GROUP BY'] = $this->creationSort ?
'user_id' : 'user_name';
133 'tables' => [ 'user', 'user_groups', 'ipblocks' ],
135 'user_name' => $this->creationSort ?
'MAX(user_name)' : 'user_name',
136 'user_id' => $this->creationSort ?
'user_id' : 'MAX(user_id)',
137 'edits' => 'MAX(user_editcount)',
138 'creation' => 'MIN(user_registration)',
139 'ipb_deleted' => 'MAX(ipb_deleted)' // block/hide status
141 'options' => $options,
143 'user_groups' => [ 'LEFT JOIN', 'user_id=ug_user' ],
154 Hooks
::run( 'SpecialListusersQueryInfo', [ $this, &$query ] );
160 * @param stdClass $row
163 function formatRow( $row ) {
164 if ( $row->user_id
== 0 ) { # Bug 16487
168 $userName = $row->user_name
;
170 $ulinks = Linker
::userLink( $row->user_id
, $userName );
171 $ulinks .= Linker
::userToolLinksRedContribs(
177 $lang = $this->getLanguage();
180 $groups_list = self
::getGroups( intval( $row->user_id
), $this->userGroupCache
);
182 if ( !$this->including
&& count( $groups_list ) > 0 ) {
184 foreach ( $groups_list as $group ) {
185 $list[] = self
::buildGroupLink( $group, $userName );
187 $groups = $lang->commaList( $list );
190 $item = $lang->specialList( $ulinks, $groups );
192 if ( $row->ipb_deleted
) {
193 $item = "<span class=\"deleted\">$item</span>";
197 if ( !$this->including
&& $this->getConfig()->get( 'Edititis' ) ) {
198 $count = $this->msg( 'usereditcount' )->numParams( $row->edits
)->escaped();
199 $edits = $this->msg( 'word-separator' )->escaped() . $this->msg( 'brackets', $count )->escaped();
203 # Some rows may be null
204 if ( !$this->including
&& $row->creation
) {
205 $user = $this->getUser();
206 $d = $lang->userDate( $row->creation
, $user );
207 $t = $lang->userTime( $row->creation
, $user );
208 $created = $this->msg( 'usercreated', $d, $t, $row->user_name
)->escaped();
209 $created = ' ' . $this->msg( 'parentheses' )->rawParams( $created )->escaped();
211 $blocked = !is_null( $row->ipb_deleted
) ?
212 ' ' . $this->msg( 'listusers-blocked', $userName )->escaped() :
215 Hooks
::run( 'SpecialListusersFormatRow', [ &$item, $row ] );
217 return Html
::rawElement( 'li', [], "{$item}{$edits}{$created}{$blocked}" );
220 function doBatchLookups() {
221 $batch = new LinkBatch();
223 # Give some pointers to make user links
224 foreach ( $this->mResult
as $row ) {
225 $batch->add( NS_USER
, $row->user_name
);
226 $batch->add( NS_USER_TALK
, $row->user_name
);
227 $userIds[] = $row->user_id
;
230 // Lookup groups for all the users
231 $dbr = wfGetDB( DB_REPLICA
);
232 $groupRes = $dbr->select(
234 [ 'ug_user', 'ug_group' ],
235 [ 'ug_user' => $userIds ],
240 foreach ( $groupRes as $row ) {
241 $cache[intval( $row->ug_user
)][] = $row->ug_group
;
242 $groups[$row->ug_group
] = true;
245 // Give extensions a chance to add things like global user group data
246 // into the cache array to ensure proper output later on
247 Hooks
::run( 'UsersPagerDoBatchLookups', [ $dbr, $userIds, &$cache, &$groups ] );
249 $this->userGroupCache
= $cache;
251 // Add page of groups to link batch
252 foreach ( $groups as $group => $unused ) {
253 $groupPage = User
::getGroupPage( $group );
255 $batch->addObj( $groupPage );
260 $this->mResult
->rewind();
266 function getPageHeader() {
267 list( $self ) = explode( '/', $this->getTitle()->getPrefixedDBkey() );
269 $this->getOutput()->addModules( 'mediawiki.userSuggest' );
272 $out = Xml
::openElement(
274 [ 'method' => 'get', 'action' => wfScript(), 'id' => 'mw-listusers-form' ]
276 Xml
::fieldset( $this->msg( 'listusers' )->text() ) .
277 Html
::hidden( 'title', $self );
279 # Username field (with autocompletion support)
280 $out .= Xml
::label( $this->msg( 'listusersfrom' )->text(), 'offset' ) . ' ' .
283 $this->requestedUser
,
286 'class' => 'mw-autocomplete-user',
289 'autofocus' => $this->requestedUser
=== ''
293 # Group drop-down list
294 $sel = new XmlSelect( 'group', 'group', $this->requestedGroup
);
295 $sel->addOption( $this->msg( 'group-all' )->text(), '' );
296 foreach ( $this->getAllGroups() as $group => $groupText ) {
297 $sel->addOption( $groupText, $group );
300 $out .= Xml
::label( $this->msg( 'group' )->text(), 'group' ) . ' ';
301 $out .= $sel->getHTML() . '<br />';
302 $out .= Xml
::checkLabel(
303 $this->msg( 'listusers-editsonly' )->text(),
309 $out .= Xml
::checkLabel(
310 $this->msg( 'listusers-creationsort' )->text(),
316 $out .= Xml
::checkLabel(
317 $this->msg( 'listusers-desc' )->text(),
320 $this->mDefaultDirection
324 Hooks
::run( 'SpecialListusersHeaderForm', [ $this, &$out ] );
326 # Submit button and form bottom
327 $out .= Html
::hidden( 'limit', $this->mLimit
);
328 $out .= Xml
::submitButton( $this->msg( 'listusers-submit' )->text() );
329 Hooks
::run( 'SpecialListusersHeader', [ $this, &$out ] );
330 $out .= Xml
::closeElement( 'fieldset' ) .
331 Xml
::closeElement( 'form' );
337 * Get a list of all explicit groups
340 function getAllGroups() {
342 foreach ( User
::getAllGroups() as $group ) {
343 $result[$group] = User
::getGroupName( $group );
351 * Preserve group and username offset parameters when paging
354 function getDefaultQuery() {
355 $query = parent
::getDefaultQuery();
356 if ( $this->requestedGroup
!= '' ) {
357 $query['group'] = $this->requestedGroup
;
359 if ( $this->requestedUser
!= '' ) {
360 $query['username'] = $this->requestedUser
;
362 Hooks
::run( 'SpecialListusersDefaultQuery', [ $this, &$query ] );
368 * Get a list of groups the specified user belongs to
370 * @param int $uid User id
371 * @param array|null $cache
374 protected static function getGroups( $uid, $cache = null ) {
375 if ( $cache === null ) {
376 $user = User
::newFromId( $uid );
377 $effectiveGroups = $user->getEffectiveGroups();
379 $effectiveGroups = isset( $cache[$uid] ) ?
$cache[$uid] : [];
381 $groups = array_diff( $effectiveGroups, User
::getImplicitGroups() );
387 * Format a link to a group description page
389 * @param string $group Group name
390 * @param string $username Username
393 protected static function buildGroupLink( $group, $username ) {
394 return User
::makeGroupLinkHTML(
396 User
::getGroupMember( $group, $username )