3 * Implements Special:Listusers
5 * Copyright © 2004 Brion Vibber, lcrocker, Tim Starling,
6 * Domas Mituzas, Antoine Musso, 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( IContextSource
$context = null, $par = null ) {
39 $this->setContext( $context );
42 $request = $this->getRequest();
43 $parms = explode( '/', ($par = ( $par !== null ) ?
$par : '' ) );
44 $symsForAll = array( '*', 'user' );
45 if ( $parms[0] != '' && ( in_array( $par, User
::getAllGroups() ) ||
in_array( $par, $symsForAll ) ) ) {
46 $this->requestedGroup
= $par;
47 $un = $request->getText( 'username' );
48 } elseif ( count( $parms ) == 2 ) {
49 $this->requestedGroup
= $parms[0];
52 $this->requestedGroup
= $request->getVal( 'group' );
53 $un = ( $par != '' ) ?
$par : $request->getText( 'username' );
55 if ( in_array( $this->requestedGroup
, $symsForAll ) ) {
56 $this->requestedGroup
= '';
58 $this->editsOnly
= $request->getBool( 'editsOnly' );
59 $this->creationSort
= $request->getBool( 'creationSort' );
61 $this->requestedUser
= '';
63 $username = Title
::makeTitleSafe( NS_USER
, $un );
64 if( ! is_null( $username ) ) {
65 $this->requestedUser
= $username->getText();
68 parent
::__construct();
71 function getIndexField() {
72 return $this->creationSort ?
'user_id' : 'user_name';
75 function getQueryInfo() {
76 $dbr = wfGetDB( DB_SLAVE
);
78 // Don't show hidden names
79 if( !$this->getUser()->isAllowed('hideuser') ) {
80 $conds[] = 'ipb_deleted IS NULL';
85 if( $this->requestedGroup
!= '' ) {
86 $conds['ug_group'] = $this->requestedGroup
;
88 //$options['USE INDEX'] = $this->creationSort ? 'PRIMARY' : 'user_name';
90 if( $this->requestedUser
!= '' ) {
91 # Sorted either by account creation or name
92 if( $this->creationSort
) {
93 $conds[] = 'user_id >= ' . intval( User
::idFromName( $this->requestedUser
) );
95 $conds[] = 'user_name >= ' . $dbr->addQuotes( $this->requestedUser
);
98 if( $this->editsOnly
) {
99 $conds[] = 'user_editcount > 0';
102 $options['GROUP BY'] = $this->creationSort ?
'user_id' : 'user_name';
105 'tables' => array( 'user', 'user_groups', 'ipblocks'),
107 $this->creationSort ?
'MAX(user_name) AS user_name' : 'user_name',
108 $this->creationSort ?
'user_id' : 'MAX(user_id) AS user_id',
109 'MAX(user_editcount) AS edits',
110 'COUNT(ug_group) AS numgroups',
111 'MAX(ug_group) AS singlegroup', // the usergroup if there is only one
112 'MIN(user_registration) AS creation',
113 'MAX(ipb_deleted) AS ipb_deleted' // block/hide status
115 'options' => $options,
116 'join_conds' => array(
117 'user_groups' => array( 'LEFT JOIN', 'user_id=ug_user' ),
118 'ipblocks' => array( 'LEFT JOIN', 'user_id=ipb_user AND ipb_deleted=1 AND ipb_auto=0' ),
123 wfRunHooks( 'SpecialListusersQueryInfo', array( $this, &$query ) );
127 function formatRow( $row ) {
128 if ($row->user_id
== 0) #Bug 16487
131 $userPage = Title
::makeTitle( NS_USER
, $row->user_name
);
132 $name = Linker
::link( $userPage, htmlspecialchars( $userPage->getText() ) );
134 $lang = $this->getLang();
136 $groups_list = self
::getGroups( $row->user_id
);
137 if( count( $groups_list ) > 0 ) {
139 foreach( $groups_list as $group )
140 $list[] = self
::buildGroupLink( $group, $userPage->getText() );
141 $groups = $lang->commaList( $list );
146 $item = $lang->specialList( $name, $groups );
147 if( $row->ipb_deleted
) {
148 $item = "<span class=\"deleted\">$item</span>";
153 $editCount = $lang->formatNum( $row->edits
);
154 $edits = ' [' . wfMsgExt( 'usereditcount', array( 'parsemag', 'escape' ), $editCount ) . ']';
160 # Some rows may be NULL
161 if( $row->creation
) {
162 $d = $lang->date( wfTimestamp( TS_MW
, $row->creation
), true );
163 $t = $lang->time( wfTimestamp( TS_MW
, $row->creation
), true );
164 $created = ' (' . wfMsg( 'usercreated', $d, $t ) . ')';
165 $created = htmlspecialchars( $created );
168 wfRunHooks( 'SpecialListusersFormatRow', array( &$item, $row ) );
169 return "<li>{$item}{$edits}{$created}</li>";
173 if( !$this->mQueryDone
) {
176 $this->mResult
->rewind();
177 $batch = new LinkBatch
;
178 foreach ( $this->mResult
as $row ) {
179 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->user_name
) );
182 $this->mResult
->rewind();
183 return parent
::getBody();
186 function getPageHeader( ) {
188 $self = $this->getTitle();
191 $out = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'id' => 'mw-listusers-form' ) ) .
192 Xml
::fieldset( wfMsg( 'listusers' ) ) .
193 Html
::hidden( 'title', $self->getPrefixedDbKey() );
196 $out .= Xml
::label( wfMsg( 'listusersfrom' ), 'offset' ) . ' ' .
197 Xml
::input( 'username', 20, $this->requestedUser
, array( 'id' => 'offset' ) ) . ' ';
199 # Group drop-down list
200 $out .= Xml
::label( wfMsg( 'group' ), 'group' ) . ' ' .
201 Xml
::openElement('select', array( 'name' => 'group', 'id' => 'group' ) ) .
202 Xml
::option( wfMsg( 'group-all' ), '' );
203 foreach( $this->getAllGroups() as $group => $groupText )
204 $out .= Xml
::option( $groupText, $group, $group == $this->requestedGroup
);
205 $out .= Xml
::closeElement( 'select' ) . '<br />';
206 $out .= Xml
::checkLabel( wfMsg('listusers-editsonly'), 'editsOnly', 'editsOnly', $this->editsOnly
);
208 $out .= Xml
::checkLabel( wfMsg('listusers-creationsort'), 'creationSort', 'creationSort', $this->creationSort
);
211 wfRunHooks( 'SpecialListusersHeaderForm', array( $this, &$out ) );
213 # Submit button and form bottom
214 $out .= Html
::hidden( 'limit', $this->mLimit
);
215 $out .= Xml
::submitButton( wfMsg( 'allpagessubmit' ) );
216 wfRunHooks( 'SpecialListusersHeader', array( $this, &$out ) );
217 $out .= Xml
::closeElement( 'fieldset' ) .
218 Xml
::closeElement( 'form' );
224 * Get a list of all explicit groups
227 function getAllGroups() {
229 foreach( User
::getAllGroups() as $group ) {
230 $result[$group] = User
::getGroupName( $group );
237 * Preserve group and username offset parameters when paging
240 function getDefaultQuery() {
241 $query = parent
::getDefaultQuery();
242 if( $this->requestedGroup
!= '' )
243 $query['group'] = $this->requestedGroup
;
244 if( $this->requestedUser
!= '' )
245 $query['username'] = $this->requestedUser
;
246 wfRunHooks( 'SpecialListusersDefaultQuery', array( $this, &$query ) );
251 * Get a list of groups the specified user belongs to
253 * @param $uid Integer: user id
256 protected static function getGroups( $uid ) {
257 $user = User
::newFromId( $uid );
258 $groups = array_diff( $user->getEffectiveGroups(), User
::getImplicitGroups() );
263 * Format a link to a group description page
265 * @param $group String: group name
268 protected static function buildGroupLink( $group, $username ) {
269 return User
::makeGroupLinkHtml( $group, htmlspecialchars( User
::getGroupMember( $group, $username ) ) );
274 * @ingroup SpecialPage
276 class SpecialListUsers
extends SpecialPage
{
281 public function __construct() {
282 parent
::__construct( 'Listusers' );
286 * Show the special page
288 * @param $par string (optional) A group to list users from
290 public function execute( $par ) {
292 $this->outputHeader();
294 $up = new UsersPager( $this->getContext(), $par );
296 # getBody() first to check, if empty
297 $usersbody = $up->getBody();
299 $s = $up->getPageHeader();
301 $s .= $up->getNavigationBar();
302 $s .= Html
::rawElement( 'ul', array(), $usersbody );
303 $s .= $up->getNavigationBar();
305 $s .= wfMessage( 'listusers-noresult' )->parseAsBlock();
308 $this->getOutput()->addHTML( $s );