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 $par = ( $par !== null ) ?
$par : '';
44 $parms = explode( '/', $par );
45 $symsForAll = array( '*', 'user' );
46 if ( $parms[0] != '' && ( in_array( $par, User
::getAllGroups() ) ||
in_array( $par, $symsForAll ) ) ) {
47 $this->requestedGroup
= $par;
48 $un = $request->getText( 'username' );
49 } elseif ( count( $parms ) == 2 ) {
50 $this->requestedGroup
= $parms[0];
53 $this->requestedGroup
= $request->getVal( 'group' );
54 $un = ( $par != '' ) ?
$par : $request->getText( 'username' );
56 if ( in_array( $this->requestedGroup
, $symsForAll ) ) {
57 $this->requestedGroup
= '';
59 $this->editsOnly
= $request->getBool( 'editsOnly' );
60 $this->creationSort
= $request->getBool( 'creationSort' );
62 $this->requestedUser
= '';
64 $username = Title
::makeTitleSafe( NS_USER
, $un );
65 if( ! is_null( $username ) ) {
66 $this->requestedUser
= $username->getText();
69 parent
::__construct();
72 function getIndexField() {
73 return $this->creationSort ?
'user_id' : 'user_name';
76 function getQueryInfo() {
77 $dbr = wfGetDB( DB_SLAVE
);
79 // Don't show hidden names
80 if( !$this->getUser()->isAllowed('hideuser') ) {
81 $conds[] = 'ipb_deleted IS NULL';
86 if( $this->requestedGroup
!= '' ) {
87 $conds['ug_group'] = $this->requestedGroup
;
89 //$options['USE INDEX'] = $this->creationSort ? 'PRIMARY' : 'user_name';
91 if( $this->requestedUser
!= '' ) {
92 # Sorted either by account creation or name
93 if( $this->creationSort
) {
94 $conds[] = 'user_id >= ' . intval( User
::idFromName( $this->requestedUser
) );
96 $conds[] = 'user_name >= ' . $dbr->addQuotes( $this->requestedUser
);
99 if( $this->editsOnly
) {
100 $conds[] = 'user_editcount > 0';
103 $options['GROUP BY'] = $this->creationSort ?
'user_id' : 'user_name';
106 'tables' => array( 'user', 'user_groups', 'ipblocks'),
108 $this->creationSort ?
'MAX(user_name) AS user_name' : 'user_name',
109 $this->creationSort ?
'user_id' : 'MAX(user_id) AS user_id',
110 'MAX(user_editcount) AS edits',
111 'COUNT(ug_group) AS numgroups',
112 'MAX(ug_group) AS singlegroup', // the usergroup if there is only one
113 'MIN(user_registration) AS creation',
114 'MAX(ipb_deleted) AS ipb_deleted' // block/hide status
116 'options' => $options,
117 'join_conds' => array(
118 'user_groups' => array( 'LEFT JOIN', 'user_id=ug_user' ),
119 'ipblocks' => array( 'LEFT JOIN', 'user_id=ipb_user AND ipb_deleted=1 AND ipb_auto=0' ),
124 wfRunHooks( 'SpecialListusersQueryInfo', array( $this, &$query ) );
128 function formatRow( $row ) {
129 if ($row->user_id
== 0) #Bug 16487
132 $userName = $row->user_name
;
134 $ulinks = Linker
::userLink( $row->user_id
, $userName );
135 $ulinks .= Linker
::userToolLinks( $row->user_id
, $userName );
137 $userPage = Title
::makeTitle( NS_USER
, $row->user_name
);
138 $name = Linker
::link( $userPage, htmlspecialchars( $userPage->getText() ) );
140 $lang = $this->getLanguage();
142 $groups_list = self
::getGroups( $row->user_id
);
143 if( count( $groups_list ) > 0 ) {
145 foreach( $groups_list as $group )
146 $list[] = self
::buildGroupLink( $group, $userName );
147 $groups = $lang->commaList( $list );
152 $item = $lang->specialList( $ulinks, $groups );
153 if( $row->ipb_deleted
) {
154 $item = "<span class=\"deleted\">$item</span>";
159 $edits = ' [' . $this->msg( 'usereditcount' )->numParams( $row->edits
)->escaped() . ']';
165 # Some rows may be NULL
166 if( $row->creation
) {
167 $user = $this->getUser();
168 $d = $lang->userDate( $row->creation
, $user );
169 $t = $lang->userTime( $row->creation
, $user );
170 $created = $this->msg( 'usercreated', $d, $t, $row->user_name
)->escaped();
171 $created = ' ' . $this->msg( 'parentheses' )->rawParams( $created )->escaped();
174 wfRunHooks( 'SpecialListusersFormatRow', array( &$item, $row ) );
175 return "<li>{$item}{$edits}{$created}</li>";
178 function doBatchLookups() {
179 $batch = new LinkBatch();
180 # Give some pointers to make user links
181 foreach ( $this->mResult
as $row ) {
182 $batch->add( NS_USER
, $row->user_name
);
183 $batch->add( NS_USER_TALK
, $row->user_name
);
186 $this->mResult
->rewind();
189 function getPageHeader( ) {
191 // @todo Add a PrefixedBaseDBKey
192 list( $self ) = explode( '/', $this->getTitle()->getPrefixedDBkey() );
195 $out = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'id' => 'mw-listusers-form' ) ) .
196 Xml
::fieldset( $this->msg( 'listusers' )->text() ) .
197 Html
::hidden( 'title', $self );
200 $out .= Xml
::label( $this->msg( 'listusersfrom' )->text(), 'offset' ) . ' ' .
201 Xml
::input( 'username', 20, $this->requestedUser
, array( 'id' => 'offset' ) ) . ' ';
203 # Group drop-down list
204 $out .= Xml
::label( $this->msg( 'group' )->text(), 'group' ) . ' ' .
205 Xml
::openElement('select', array( 'name' => 'group', 'id' => 'group' ) ) .
206 Xml
::option( $this->msg( 'group-all' )->text(), '' );
207 foreach( $this->getAllGroups() as $group => $groupText )
208 $out .= Xml
::option( $groupText, $group, $group == $this->requestedGroup
);
209 $out .= Xml
::closeElement( 'select' ) . '<br />';
210 $out .= Xml
::checkLabel( $this->msg( 'listusers-editsonly' )->text(), 'editsOnly', 'editsOnly', $this->editsOnly
);
212 $out .= Xml
::checkLabel( $this->msg( 'listusers-creationsort' )->text(), 'creationSort', 'creationSort', $this->creationSort
);
215 wfRunHooks( 'SpecialListusersHeaderForm', array( $this, &$out ) );
217 # Submit button and form bottom
218 $out .= Html
::hidden( 'limit', $this->mLimit
);
219 $out .= Xml
::submitButton( $this->msg( 'allpagessubmit' )->text() );
220 wfRunHooks( 'SpecialListusersHeader', array( $this, &$out ) );
221 $out .= Xml
::closeElement( 'fieldset' ) .
222 Xml
::closeElement( 'form' );
228 * Get a list of all explicit groups
231 function getAllGroups() {
233 foreach( User
::getAllGroups() as $group ) {
234 $result[$group] = User
::getGroupName( $group );
241 * Preserve group and username offset parameters when paging
244 function getDefaultQuery() {
245 $query = parent
::getDefaultQuery();
246 if( $this->requestedGroup
!= '' )
247 $query['group'] = $this->requestedGroup
;
248 if( $this->requestedUser
!= '' )
249 $query['username'] = $this->requestedUser
;
250 wfRunHooks( 'SpecialListusersDefaultQuery', array( $this, &$query ) );
255 * Get a list of groups the specified user belongs to
257 * @param $uid Integer: user id
260 protected static function getGroups( $uid ) {
261 $user = User
::newFromId( $uid );
262 $groups = array_diff( $user->getEffectiveGroups(), User
::getImplicitGroups() );
267 * Format a link to a group description page
269 * @param $group String: group name
270 * @param $username String Username
273 protected static function buildGroupLink( $group, $username ) {
274 return User
::makeGroupLinkHtml( $group, htmlspecialchars( User
::getGroupMember( $group, $username ) ) );
279 * @ingroup SpecialPage
281 class SpecialListUsers
extends SpecialPage
{
286 public function __construct() {
287 parent
::__construct( 'Listusers' );
291 * Show the special page
293 * @param $par string (optional) A group to list users from
295 public function execute( $par ) {
297 $this->outputHeader();
299 $up = new UsersPager( $this->getContext(), $par );
301 # getBody() first to check, if empty
302 $usersbody = $up->getBody();
304 $s = $up->getPageHeader();
306 $s .= $up->getNavigationBar();
307 $s .= Html
::rawElement( 'ul', array(), $usersbody );
308 $s .= $up->getNavigationBar();
310 $s .= $this->msg( 'listusers-noresult' )->parseAsBlock();
313 $this->getOutput()->addHTML( $s );