3 * Implements Special:Listusers
5 * Copyright © 2004 Brion Vibber, lcrocker, Tim Starling,
6 * Domas Mituzas, Ashar Voultoiz, Jens Frank, Zhengzhu,
7 * 2006 Rob Church <robchur@gmail.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
25 * @ingroup SpecialPage
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.
33 * @ingroup SpecialPage
35 class UsersPager
extends AlphabeticPager
{
37 function __construct( $par=null ) {
39 $parms = explode( '/', ($par = ( $par !== null ) ?
$par : '' ) );
40 $symsForAll = array( '*', 'user' );
41 if ( $parms[0] != '' && ( in_array( $par, User
::getAllGroups() ) ||
in_array( $par, $symsForAll ) ) ) {
42 $this->requestedGroup
= $par;
43 $un = $wgRequest->getText( 'username' );
44 } else if ( count( $parms ) == 2 ) {
45 $this->requestedGroup
= $parms[0];
48 $this->requestedGroup
= $wgRequest->getVal( 'group' );
49 $un = ( $par != '' ) ?
$par : $wgRequest->getText( 'username' );
51 if ( in_array( $this->requestedGroup
, $symsForAll ) ) {
52 $this->requestedGroup
= '';
54 $this->editsOnly
= $wgRequest->getBool( 'editsOnly' );
55 $this->creationSort
= $wgRequest->getBool( 'creationSort' );
57 $this->requestedUser
= '';
59 $username = Title
::makeTitleSafe( NS_USER
, $un );
60 if( ! is_null( $username ) ) {
61 $this->requestedUser
= $username->getText();
64 parent
::__construct();
68 function getIndexField() {
69 return $this->creationSort ?
'user_id' : 'user_name';
72 function getQueryInfo() {
74 $dbr = wfGetDB( DB_SLAVE
);
76 // Don't show hidden names
77 if( !$wgUser->isAllowed('hideuser') )
78 $conds[] = 'ipb_deleted IS NULL';
79 if( $this->requestedGroup
!= '' ) {
80 $conds['ug_group'] = $this->requestedGroup
;
83 $useIndex = $dbr->useIndexClause( $this->creationSort ?
'PRIMARY' : 'user_name');
85 if( $this->requestedUser
!= '' ) {
86 # Sorted either by account creation or name
87 if( $this->creationSort
) {
88 $conds[] = 'user_id >= ' . intval( User
::idFromName( $this->requestedUser
) );
90 $conds[] = 'user_name >= ' . $dbr->addQuotes( $this->requestedUser
);
93 if( $this->editsOnly
) {
94 $conds[] = 'user_editcount > 0';
97 list ($user,$user_groups,$ipblocks) = $dbr->tableNamesN('user','user_groups','ipblocks');
100 'tables' => " $user $useIndex LEFT JOIN $user_groups ON user_id=ug_user
101 LEFT JOIN $ipblocks ON user_id=ipb_user AND ipb_deleted=1 AND ipb_auto=0 ",
103 $this->creationSort ?
'MAX(user_name) AS user_name' : 'user_name',
104 $this->creationSort ?
'user_id' : 'MAX(user_id) AS user_id',
105 'MAX(user_editcount) AS edits',
106 'COUNT(ug_group) AS numgroups',
107 'MAX(ug_group) AS singlegroup', // the usergroup if there is only one
108 'MIN(user_registration) AS creation',
109 'MAX(ipb_deleted) AS ipb_deleted' // block/hide status
111 'options' => array('GROUP BY' => $this->creationSort ?
'user_id' : 'user_name'),
115 wfRunHooks( 'SpecialListusersQueryInfo', array( $this, &$query ) );
119 function formatRow( $row ) {
122 if ($row->user_id
== 0) #Bug 16487
125 $userPage = Title
::makeTitle( NS_USER
, $row->user_name
);
126 $name = $this->getSkin()->link( $userPage, htmlspecialchars( $userPage->getText() ) );
128 if( $row->numgroups
> 1 ||
( $this->requestedGroup
&& $row->numgroups
== 1 ) ) {
130 foreach( self
::getGroups( $row->user_id
) as $group )
131 $list[] = self
::buildGroupLink( $group );
132 $groups = $wgLang->commaList( $list );
133 } elseif( $row->numgroups
== 1 ) {
134 $groups = self
::buildGroupLink( $row->singlegroup
);
139 $item = wfSpecialList( $name, $groups );
140 if( $row->ipb_deleted
) {
141 $item = "<span class=\"deleted\">$item</span>";
146 $editCount = $wgLang->formatNum( $row->edits
);
147 $edits = ' [' . wfMsgExt( 'usereditcount', array( 'parsemag', 'escape' ), $editCount ) . ']';
153 # Some rows may be NULL
154 if( $row->creation
) {
155 $d = $wgLang->date( wfTimestamp( TS_MW
, $row->creation
), true );
156 $t = $wgLang->time( wfTimestamp( TS_MW
, $row->creation
), true );
157 $created = ' (' . wfMsg( 'usercreated', $d, $t ) . ')';
158 $created = htmlspecialchars( $created );
161 wfRunHooks( 'SpecialListusersFormatRow', array( &$item, $row ) );
162 return "<li>{$item}{$edits}{$created}</li>";
166 if( !$this->mQueryDone
) {
169 $this->mResult
->rewind();
170 $batch = new LinkBatch
;
171 while ( $row = $this->mResult
->fetchObject() ) {
172 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->user_name
) );
175 $this->mResult
->rewind();
176 return parent
::getBody();
179 function getPageHeader( ) {
181 $self = $this->getTitle();
184 $out = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'id' => 'mw-listusers-form' ) ) .
185 Xml
::fieldset( wfMsg( 'listusers' ) ) .
186 Xml
::hidden( 'title', $self->getPrefixedDbKey() );
189 $out .= Xml
::label( wfMsg( 'listusersfrom' ), 'offset' ) . ' ' .
190 Xml
::input( 'username', 20, $this->requestedUser
, array( 'id' => 'offset' ) ) . ' ';
192 # Group drop-down list
193 $out .= Xml
::label( wfMsg( 'group' ), 'group' ) . ' ' .
194 Xml
::openElement('select', array( 'name' => 'group', 'id' => 'group' ) ) .
195 Xml
::option( wfMsg( 'group-all' ), '' );
196 foreach( $this->getAllGroups() as $group => $groupText )
197 $out .= Xml
::option( $groupText, $group, $group == $this->requestedGroup
);
198 $out .= Xml
::closeElement( 'select' ) . '<br />';
199 $out .= Xml
::checkLabel( wfMsg('listusers-editsonly'), 'editsOnly', 'editsOnly', $this->editsOnly
);
201 $out .= Xml
::checkLabel( wfMsg('listusers-creationsort'), 'creationSort', 'creationSort', $this->creationSort
);
204 wfRunHooks( 'SpecialListusersHeaderForm', array( $this, &$out ) );
206 # Submit button and form bottom
207 $out .= Xml
::hidden( 'limit', $this->mLimit
);
208 $out .= Xml
::submitButton( wfMsg( 'allpagessubmit' ) );
209 wfRunHooks( 'SpecialListusersHeader', array( $this, &$out ) );
210 $out .= Xml
::closeElement( 'fieldset' ) .
211 Xml
::closeElement( 'form' );
217 * Get a list of all explicit groups
220 function getAllGroups() {
222 foreach( User
::getAllGroups() as $group ) {
223 $result[$group] = User
::getGroupName( $group );
230 * Preserve group and username offset parameters when paging
233 function getDefaultQuery() {
234 $query = parent
::getDefaultQuery();
235 if( $this->requestedGroup
!= '' )
236 $query['group'] = $this->requestedGroup
;
237 if( $this->requestedUser
!= '' )
238 $query['username'] = $this->requestedUser
;
239 wfRunHooks( 'SpecialListusersDefaultQuery', array( $this, &$query ) );
244 * Get a list of groups the specified user belongs to
246 * @param $uid Integer: user id
249 protected static function getGroups( $uid ) {
250 $user = User
::newFromId( $uid );
251 $groups = array_diff( $user->getEffectiveGroups(), $user->getImplicitGroups() );
256 * Format a link to a group description page
258 * @param $group String: group name
261 protected static function buildGroupLink( $group ) {
262 static $cache = array();
263 if( !isset( $cache[$group] ) )
264 $cache[$group] = User
::makeGroupLinkHtml( $group, htmlspecialchars( User
::getGroupMember( $group ) ) );
265 return $cache[$group];
271 * $par string (optional) A group to list users from
273 function wfSpecialListusers( $par = null ) {
276 $up = new UsersPager($par);
278 # getBody() first to check, if empty
279 $usersbody = $up->getBody();
280 $s = Xml
::openElement( 'div', array('class' => 'mw-spcontent') );
281 $s .= $up->getPageHeader();
283 $s .= $up->getNavigationBar();
284 $s .= '<ul>' . $usersbody . '</ul>';
285 $s .= $up->getNavigationBar() ;
287 $s .= '<p>' . wfMsgHTML('listusers-noresult') . '</p>';
289 $s .= Xml
::closeElement( 'div' );
290 $wgOut->addHTML( $s );