Support offsets in prefix searching
[mediawiki.git] / includes / specials / SpecialListusers.php
blobfa2099417799463b11717ad0ac644681804cdcf4
1 <?php
2 /**
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
24 * @file
25 * @ingroup SpecialPage
28 /**
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 /**
38 * @param IContextSource $context
39 * @param array $par (Default null)
40 * @param bool $including Whether this page is being transcluded in
41 * another page
43 function __construct( IContextSource $context = null, $par = null, $including = null ) {
44 if ( $context ) {
45 $this->setContext( $context );
48 $request = $this->getRequest();
49 $par = ( $par !== null ) ? $par : '';
50 $parms = explode( '/', $par );
51 $symsForAll = array( '*', 'user' );
53 if ( $parms[0] != '' &&
54 ( in_array( $par, User::getAllGroups() ) || in_array( $par, $symsForAll ) )
55 ) {
56 $this->requestedGroup = $par;
57 $un = $request->getText( 'username' );
58 } elseif ( count( $parms ) == 2 ) {
59 $this->requestedGroup = $parms[0];
60 $un = $parms[1];
61 } else {
62 $this->requestedGroup = $request->getVal( 'group' );
63 $un = ( $par != '' ) ? $par : $request->getText( 'username' );
66 if ( in_array( $this->requestedGroup, $symsForAll ) ) {
67 $this->requestedGroup = '';
69 $this->editsOnly = $request->getBool( 'editsOnly' );
70 $this->creationSort = $request->getBool( 'creationSort' );
71 $this->including = $including;
72 $this->mDefaultDirection = $request->getBool( 'desc' )
73 ? IndexPager::DIR_DESCENDING
74 : IndexPager::DIR_ASCENDING;
76 $this->requestedUser = '';
78 if ( $un != '' ) {
79 $username = Title::makeTitleSafe( NS_USER, $un );
81 if ( !is_null( $username ) ) {
82 $this->requestedUser = $username->getText();
86 parent::__construct();
89 /**
90 * @return string
92 function getIndexField() {
93 return $this->creationSort ? 'user_id' : 'user_name';
96 /**
97 * @return array
99 function getQueryInfo() {
100 $dbr = wfGetDB( DB_SLAVE );
101 $conds = array();
103 // Don't show hidden names
104 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
105 $conds[] = 'ipb_deleted IS NULL OR ipb_deleted = 0';
108 $options = array();
110 if ( $this->requestedGroup != '' ) {
111 $conds['ug_group'] = $this->requestedGroup;
114 if ( $this->requestedUser != '' ) {
115 # Sorted either by account creation or name
116 if ( $this->creationSort ) {
117 $conds[] = 'user_id >= ' . intval( User::idFromName( $this->requestedUser ) );
118 } else {
119 $conds[] = 'user_name >= ' . $dbr->addQuotes( $this->requestedUser );
123 if ( $this->editsOnly ) {
124 $conds[] = 'user_editcount > 0';
127 $options['GROUP BY'] = $this->creationSort ? 'user_id' : 'user_name';
129 $query = array(
130 'tables' => array( 'user', 'user_groups', 'ipblocks' ),
131 'fields' => array(
132 'user_name' => $this->creationSort ? 'MAX(user_name)' : 'user_name',
133 'user_id' => $this->creationSort ? 'user_id' : 'MAX(user_id)',
134 'edits' => 'MAX(user_editcount)',
135 'numgroups' => 'COUNT(ug_group)',
136 'singlegroup' => 'MAX(ug_group)', // the usergroup if there is only one
137 'creation' => 'MIN(user_registration)',
138 'ipb_deleted' => 'MAX(ipb_deleted)' // block/hide status
140 'options' => $options,
141 'join_conds' => array(
142 'user_groups' => array( 'LEFT JOIN', 'user_id=ug_user' ),
143 'ipblocks' => array(
144 'LEFT JOIN', array(
145 'user_id=ipb_user',
146 'ipb_auto' => 0
150 'conds' => $conds
153 wfRunHooks( 'SpecialListusersQueryInfo', array( $this, &$query ) );
155 return $query;
159 * @param stdClass $row
160 * @return string
162 function formatRow( $row ) {
163 if ( $row->user_id == 0 ) { #Bug 16487
164 return '';
167 $userName = $row->user_name;
169 $ulinks = Linker::userLink( $row->user_id, $userName );
170 $ulinks .= Linker::userToolLinksRedContribs(
171 $row->user_id,
172 $userName,
173 (int)$row->edits
176 $lang = $this->getLanguage();
178 $groups = '';
179 $groups_list = self::getGroups( $row->user_id );
181 if ( !$this->including && count( $groups_list ) > 0 ) {
182 $list = array();
183 foreach ( $groups_list as $group ) {
184 $list[] = self::buildGroupLink( $group, $userName );
186 $groups = $lang->commaList( $list );
189 $item = $lang->specialList( $ulinks, $groups );
191 if ( $row->ipb_deleted ) {
192 $item = "<span class=\"deleted\">$item</span>";
195 $edits = '';
196 if ( !$this->including && $this->getConfig()->get( 'Edititis' ) ) {
197 $count = $this->msg( 'usereditcount' )->numParams( $row->edits )->escaped();
198 $edits = $this->msg( 'word-separator' )->escaped() . $this->msg( 'brackets', $count )->escaped();
201 $created = '';
202 # Some rows may be null
203 if ( !$this->including && $row->creation ) {
204 $user = $this->getUser();
205 $d = $lang->userDate( $row->creation, $user );
206 $t = $lang->userTime( $row->creation, $user );
207 $created = $this->msg( 'usercreated', $d, $t, $row->user_name )->escaped();
208 $created = ' ' . $this->msg( 'parentheses' )->rawParams( $created )->escaped();
210 $blocked = !is_null( $row->ipb_deleted ) ?
211 ' ' . $this->msg( 'listusers-blocked', $userName )->escaped() :
214 wfRunHooks( 'SpecialListusersFormatRow', array( &$item, $row ) );
216 return Html::rawElement( 'li', array(), "{$item}{$edits}{$created}{$blocked}" );
219 function doBatchLookups() {
220 $batch = new LinkBatch();
221 # Give some pointers to make user links
222 foreach ( $this->mResult as $row ) {
223 $batch->add( NS_USER, $row->user_name );
224 $batch->add( NS_USER_TALK, $row->user_name );
226 $batch->execute();
227 $this->mResult->rewind();
231 * @return string
233 function getPageHeader() {
234 list( $self ) = explode( '/', $this->getTitle()->getPrefixedDBkey() );
236 # Form tag
237 $out = Xml::openElement(
238 'form',
239 array( 'method' => 'get', 'action' => wfScript(), 'id' => 'mw-listusers-form' )
241 Xml::fieldset( $this->msg( 'listusers' )->text() ) .
242 Html::hidden( 'title', $self );
244 # Username field
245 $out .= Xml::label( $this->msg( 'listusersfrom' )->text(), 'offset' ) . ' ' .
246 Html::input(
247 'username',
248 $this->requestedUser,
249 'text',
250 array(
251 'id' => 'offset',
252 'size' => 20,
253 'autofocus' => $this->requestedUser === ''
255 ) . ' ';
257 # Group drop-down list
258 $out .= Xml::label( $this->msg( 'group' )->text(), 'group' ) . ' ' .
259 Xml::openElement( 'select', array( 'name' => 'group', 'id' => 'group' ) ) .
260 Xml::option( $this->msg( 'group-all' )->text(), '' );
261 foreach ( $this->getAllGroups() as $group => $groupText ) {
262 $out .= Xml::option( $groupText, $group, $group == $this->requestedGroup );
264 $out .= Xml::closeElement( 'select' ) . '<br />';
265 $out .= Xml::checkLabel(
266 $this->msg( 'listusers-editsonly' )->text(),
267 'editsOnly',
268 'editsOnly',
269 $this->editsOnly
271 $out .= '&#160;';
272 $out .= Xml::checkLabel(
273 $this->msg( 'listusers-creationsort' )->text(),
274 'creationSort',
275 'creationSort',
276 $this->creationSort
278 $out .= '&#160;';
279 $out .= Xml::checkLabel(
280 $this->msg( 'listusers-desc' )->text(),
281 'desc',
282 'desc',
283 $this->mDefaultDirection
285 $out .= '<br />';
287 wfRunHooks( 'SpecialListusersHeaderForm', array( $this, &$out ) );
289 # Submit button and form bottom
290 $out .= Html::hidden( 'limit', $this->mLimit );
291 $out .= Xml::submitButton( $this->msg( 'allpagessubmit' )->text() );
292 wfRunHooks( 'SpecialListusersHeader', array( $this, &$out ) );
293 $out .= Xml::closeElement( 'fieldset' ) .
294 Xml::closeElement( 'form' );
296 return $out;
300 * Get a list of all explicit groups
301 * @return array
303 function getAllGroups() {
304 $result = array();
305 foreach ( User::getAllGroups() as $group ) {
306 $result[$group] = User::getGroupName( $group );
308 asort( $result );
310 return $result;
314 * Preserve group and username offset parameters when paging
315 * @return array
317 function getDefaultQuery() {
318 $query = parent::getDefaultQuery();
319 if ( $this->requestedGroup != '' ) {
320 $query['group'] = $this->requestedGroup;
322 if ( $this->requestedUser != '' ) {
323 $query['username'] = $this->requestedUser;
325 wfRunHooks( 'SpecialListusersDefaultQuery', array( $this, &$query ) );
327 return $query;
331 * Get a list of groups the specified user belongs to
333 * @param int $uid User id
334 * @return array
336 protected static function getGroups( $uid ) {
337 $user = User::newFromId( $uid );
338 $groups = array_diff( $user->getEffectiveGroups(), User::getImplicitGroups() );
340 return $groups;
344 * Format a link to a group description page
346 * @param string $group Group name
347 * @param string $username Username
348 * @return string
350 protected static function buildGroupLink( $group, $username ) {
351 return User::makeGroupLinkHtml(
352 $group,
353 htmlspecialchars( User::getGroupMember( $group, $username ) )
359 * @ingroup SpecialPage
361 class SpecialListUsers extends IncludableSpecialPage {
363 * Constructor
365 public function __construct() {
366 parent::__construct( 'Listusers' );
370 * Show the special page
372 * @param string $par (optional) A group to list users from
374 public function execute( $par ) {
375 $this->setHeaders();
376 $this->outputHeader();
378 $up = new UsersPager( $this->getContext(), $par, $this->including() );
380 # getBody() first to check, if empty
381 $usersbody = $up->getBody();
383 $s = '';
384 if ( !$this->including() ) {
385 $s = $up->getPageHeader();
388 if ( $usersbody ) {
389 $s .= $up->getNavigationBar();
390 $s .= Html::rawElement( 'ul', array(), $usersbody );
391 $s .= $up->getNavigationBar();
392 } else {
393 $s .= $this->msg( 'listusers-noresult' )->parseAsBlock();
396 $this->getOutput()->addHTML( $s );
400 * Return an array of subpages that this special page will accept.
402 * @return string[] subpages
404 public function getSubpagesForPrefixSearch() {
405 return User::getAllGroups();
408 protected function getGroupName() {
409 return 'users';
414 * Redirect page: Special:ListAdmins --> Special:ListUsers/sysop.
416 * @ingroup SpecialPage
418 class SpecialListAdmins extends SpecialRedirectToSpecial {
419 function __construct() {
420 parent::__construct( 'Listadmins', 'Listusers', 'sysop' );
425 * Redirect page: Special:ListBots --> Special:ListUsers/bot.
427 * @ingroup SpecialPage
429 class SpecialListBots extends SpecialRedirectToSpecial {
430 function __construct() {
431 parent::__construct( 'Listbots', 'Listusers', 'bot' );