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 $groups_list = self
::getGroups( $row->user_id
);
129 if( count( $groups_list ) > 0 ) {
131 foreach( $groups_list as $group )
132 $list[] = self
::buildGroupLink( $group );
133 $groups = $wgLang->commaList( $list );
138 $item = wfSpecialList( $name, $groups );
139 if( $row->ipb_deleted
) {
140 $item = "<span class=\"deleted\">$item</span>";
145 $editCount = $wgLang->formatNum( $row->edits
);
146 $edits = ' [' . wfMsgExt( 'usereditcount', array( 'parsemag', 'escape' ), $editCount ) . ']';
152 # Some rows may be NULL
153 if( $row->creation
) {
154 $d = $wgLang->date( wfTimestamp( TS_MW
, $row->creation
), true );
155 $t = $wgLang->time( wfTimestamp( TS_MW
, $row->creation
), true );
156 $created = ' (' . wfMsg( 'usercreated', $d, $t ) . ')';
157 $created = htmlspecialchars( $created );
160 wfRunHooks( 'SpecialListusersFormatRow', array( &$item, $row ) );
161 return "<li>{$item}{$edits}{$created}</li>";
165 if( !$this->mQueryDone
) {
168 $this->mResult
->rewind();
169 $batch = new LinkBatch
;
170 foreach ( $this->mResult
as $row ) {
171 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->user_name
) );
174 $this->mResult
->rewind();
175 return parent
::getBody();
178 function getPageHeader( ) {
180 $self = $this->getTitle();
183 $out = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'id' => 'mw-listusers-form' ) ) .
184 Xml
::fieldset( wfMsg( 'listusers' ) ) .
185 Html
::hidden( 'title', $self->getPrefixedDbKey() );
188 $out .= Xml
::label( wfMsg( 'listusersfrom' ), 'offset' ) . ' ' .
189 Xml
::input( 'username', 20, $this->requestedUser
, array( 'id' => 'offset' ) ) . ' ';
191 # Group drop-down list
192 $out .= Xml
::label( wfMsg( 'group' ), 'group' ) . ' ' .
193 Xml
::openElement('select', array( 'name' => 'group', 'id' => 'group' ) ) .
194 Xml
::option( wfMsg( 'group-all' ), '' );
195 foreach( $this->getAllGroups() as $group => $groupText )
196 $out .= Xml
::option( $groupText, $group, $group == $this->requestedGroup
);
197 $out .= Xml
::closeElement( 'select' ) . '<br />';
198 $out .= Xml
::checkLabel( wfMsg('listusers-editsonly'), 'editsOnly', 'editsOnly', $this->editsOnly
);
200 $out .= Xml
::checkLabel( wfMsg('listusers-creationsort'), 'creationSort', 'creationSort', $this->creationSort
);
203 wfRunHooks( 'SpecialListusersHeaderForm', array( $this, &$out ) );
205 # Submit button and form bottom
206 $out .= Html
::hidden( 'limit', $this->mLimit
);
207 $out .= Xml
::submitButton( wfMsg( 'allpagessubmit' ) );
208 wfRunHooks( 'SpecialListusersHeader', array( $this, &$out ) );
209 $out .= Xml
::closeElement( 'fieldset' ) .
210 Xml
::closeElement( 'form' );
216 * Get a list of all explicit groups
219 function getAllGroups() {
221 foreach( User
::getAllGroups() as $group ) {
222 $result[$group] = User
::getGroupName( $group );
229 * Preserve group and username offset parameters when paging
232 function getDefaultQuery() {
233 $query = parent
::getDefaultQuery();
234 if( $this->requestedGroup
!= '' )
235 $query['group'] = $this->requestedGroup
;
236 if( $this->requestedUser
!= '' )
237 $query['username'] = $this->requestedUser
;
238 wfRunHooks( 'SpecialListusersDefaultQuery', array( $this, &$query ) );
243 * Get a list of groups the specified user belongs to
245 * @param $uid Integer: user id
248 protected static function getGroups( $uid ) {
249 $user = User
::newFromId( $uid );
250 $groups = array_diff( $user->getEffectiveGroups(), $user->getImplicitGroups() );
255 * Format a link to a group description page
257 * @param $group String: group name
260 protected static function buildGroupLink( $group ) {
261 static $cache = array();
262 if( !isset( $cache[$group] ) )
263 $cache[$group] = User
::makeGroupLinkHtml( $group, htmlspecialchars( User
::getGroupMember( $group ) ) );
264 return $cache[$group];
270 * $par string (optional) A group to list users from
272 function wfSpecialListusers( $par = null ) {
275 $up = new UsersPager($par);
277 # getBody() first to check, if empty
278 $usersbody = $up->getBody();
279 $s = Xml
::openElement( 'div', array('class' => 'mw-spcontent') );
280 $s .= $up->getPageHeader();
282 $s .= $up->getNavigationBar();
283 $s .= '<ul>' . $usersbody . '</ul>';
284 $s .= $up->getNavigationBar() ;
286 $s .= '<p>' . wfMsgHTML('listusers-noresult') . '</p>';
288 $s .= Xml
::closeElement( 'div' );
289 $wgOut->addHTML( $s );