Switch some HTMLForms in special pages to OOUI
[lhc/web/wiklou.git] / includes / specials / SpecialListusers.php
1 <?php
2 /**
3 * Implements Special:Listusers
4 *
5 * Copyright © 2004 Brion Vibber, lcrocker, Tim Starling,
6 * Domas Mituzas, Antoine Musso, Jens Frank, Zhengzhu,
7 * 2006 Rob Church <robchur@gmail.com>
8 *
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.
13 *
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.
18 *
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
23 *
24 * @file
25 * @ingroup SpecialPage
26 */
27
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.
32 *
33 * @ingroup SpecialPage
34 */
35 class UsersPager extends AlphabeticPager {
36
37 /**
38 * @var array A array with user ids as key and a array of groups as value
39 */
40 protected $userGroupCache;
41
42 /**
43 * @param IContextSource $context
44 * @param array $par (Default null)
45 * @param bool $including Whether this page is being transcluded in
46 * another page
47 */
48 function __construct( IContextSource $context = null, $par = null, $including = null ) {
49 if ( $context ) {
50 $this->setContext( $context );
51 }
52
53 $request = $this->getRequest();
54 $par = ( $par !== null ) ? $par : '';
55 $parms = explode( '/', $par );
56 $symsForAll = array( '*', 'user' );
57
58 if ( $parms[0] != '' &&
59 ( in_array( $par, User::getAllGroups() ) || in_array( $par, $symsForAll ) )
60 ) {
61 $this->requestedGroup = $par;
62 $un = $request->getText( 'username' );
63 } elseif ( count( $parms ) == 2 ) {
64 $this->requestedGroup = $parms[0];
65 $un = $parms[1];
66 } else {
67 $this->requestedGroup = $request->getVal( 'group' );
68 $un = ( $par != '' ) ? $par : $request->getText( 'username' );
69 }
70
71 if ( in_array( $this->requestedGroup, $symsForAll ) ) {
72 $this->requestedGroup = '';
73 }
74 $this->editsOnly = $request->getBool( 'editsOnly' );
75 $this->creationSort = $request->getBool( 'creationSort' );
76 $this->including = $including;
77 $this->mDefaultDirection = $request->getBool( 'desc' )
78 ? IndexPager::DIR_DESCENDING
79 : IndexPager::DIR_ASCENDING;
80
81 $this->requestedUser = '';
82
83 if ( $un != '' ) {
84 $username = Title::makeTitleSafe( NS_USER, $un );
85
86 if ( !is_null( $username ) ) {
87 $this->requestedUser = $username->getText();
88 }
89 }
90
91 parent::__construct();
92 }
93
94 /**
95 * @return string
96 */
97 function getIndexField() {
98 return $this->creationSort ? 'user_id' : 'user_name';
99 }
100
101 /**
102 * @return array
103 */
104 function getQueryInfo() {
105 $dbr = wfGetDB( DB_SLAVE );
106 $conds = array();
107
108 // Don't show hidden names
109 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
110 $conds[] = 'ipb_deleted IS NULL OR ipb_deleted = 0';
111 }
112
113 $options = array();
114
115 if ( $this->requestedGroup != '' ) {
116 $conds['ug_group'] = $this->requestedGroup;
117 }
118
119 if ( $this->requestedUser != '' ) {
120 # Sorted either by account creation or name
121 if ( $this->creationSort ) {
122 $conds[] = 'user_id >= ' . intval( User::idFromName( $this->requestedUser ) );
123 } else {
124 $conds[] = 'user_name >= ' . $dbr->addQuotes( $this->requestedUser );
125 }
126 }
127
128 if ( $this->editsOnly ) {
129 $conds[] = 'user_editcount > 0';
130 }
131
132 $options['GROUP BY'] = $this->creationSort ? 'user_id' : 'user_name';
133
134 $query = array(
135 'tables' => array( 'user', 'user_groups', 'ipblocks' ),
136 'fields' => array(
137 'user_name' => $this->creationSort ? 'MAX(user_name)' : 'user_name',
138 'user_id' => $this->creationSort ? 'user_id' : 'MAX(user_id)',
139 'edits' => 'MAX(user_editcount)',
140 'creation' => 'MIN(user_registration)',
141 'ipb_deleted' => 'MAX(ipb_deleted)' // block/hide status
142 ),
143 'options' => $options,
144 'join_conds' => array(
145 'user_groups' => array( 'LEFT JOIN', 'user_id=ug_user' ),
146 'ipblocks' => array(
147 'LEFT JOIN', array(
148 'user_id=ipb_user',
149 'ipb_auto' => 0
150 )
151 ),
152 ),
153 'conds' => $conds
154 );
155
156 Hooks::run( 'SpecialListusersQueryInfo', array( $this, &$query ) );
157
158 return $query;
159 }
160
161 /**
162 * @param stdClass $row
163 * @return string
164 */
165 function formatRow( $row ) {
166 if ( $row->user_id == 0 ) { #Bug 16487
167 return '';
168 }
169
170 $userName = $row->user_name;
171
172 $ulinks = Linker::userLink( $row->user_id, $userName );
173 $ulinks .= Linker::userToolLinksRedContribs(
174 $row->user_id,
175 $userName,
176 (int)$row->edits
177 );
178
179 $lang = $this->getLanguage();
180
181 $groups = '';
182 $groups_list = self::getGroups( intval( $row->user_id ), $this->userGroupCache );
183
184 if ( !$this->including && count( $groups_list ) > 0 ) {
185 $list = array();
186 foreach ( $groups_list as $group ) {
187 $list[] = self::buildGroupLink( $group, $userName );
188 }
189 $groups = $lang->commaList( $list );
190 }
191
192 $item = $lang->specialList( $ulinks, $groups );
193
194 if ( $row->ipb_deleted ) {
195 $item = "<span class=\"deleted\">$item</span>";
196 }
197
198 $edits = '';
199 if ( !$this->including && $this->getConfig()->get( 'Edititis' ) ) {
200 $count = $this->msg( 'usereditcount' )->numParams( $row->edits )->escaped();
201 $edits = $this->msg( 'word-separator' )->escaped() . $this->msg( 'brackets', $count )->escaped();
202 }
203
204 $created = '';
205 # Some rows may be null
206 if ( !$this->including && $row->creation ) {
207 $user = $this->getUser();
208 $d = $lang->userDate( $row->creation, $user );
209 $t = $lang->userTime( $row->creation, $user );
210 $created = $this->msg( 'usercreated', $d, $t, $row->user_name )->escaped();
211 $created = ' ' . $this->msg( 'parentheses' )->rawParams( $created )->escaped();
212 }
213 $blocked = !is_null( $row->ipb_deleted ) ?
214 ' ' . $this->msg( 'listusers-blocked', $userName )->escaped() :
215 '';
216
217 Hooks::run( 'SpecialListusersFormatRow', array( &$item, $row ) );
218
219 return Html::rawElement( 'li', array(), "{$item}{$edits}{$created}{$blocked}" );
220 }
221
222 function doBatchLookups() {
223 $batch = new LinkBatch();
224 $userIds = array();
225 # Give some pointers to make user links
226 foreach ( $this->mResult as $row ) {
227 $batch->add( NS_USER, $row->user_name );
228 $batch->add( NS_USER_TALK, $row->user_name );
229 $userIds[] = $row->user_id;
230 }
231
232 // Lookup groups for all the users
233 $dbr = wfGetDB( DB_SLAVE );
234 $groupRes = $dbr->select(
235 'user_groups',
236 array( 'ug_user', 'ug_group' ),
237 array( 'ug_user' => $userIds ),
238 __METHOD__
239 );
240 $cache = array();
241 $groups = array();
242 foreach ( $groupRes as $row ) {
243 $cache[intval( $row->ug_user )][] = $row->ug_group;
244 $groups[$row->ug_group] = true;
245 }
246 $this->userGroupCache = $cache;
247
248 // Add page of groups to link batch
249 foreach ( $groups as $group => $unused ) {
250 $groupPage = User::getGroupPage( $group );
251 if ( $groupPage ) {
252 $batch->addObj( $groupPage );
253 }
254 }
255
256 $batch->execute();
257 $this->mResult->rewind();
258 }
259
260 /**
261 * @return string
262 */
263 function getPageHeader() {
264 list( $self ) = explode( '/', $this->getTitle()->getPrefixedDBkey() );
265
266 # Form tag
267 $out = Xml::openElement(
268 'form',
269 array( 'method' => 'get', 'action' => wfScript(), 'id' => 'mw-listusers-form' )
270 ) .
271 Xml::fieldset( $this->msg( 'listusers' )->text() ) .
272 Html::hidden( 'title', $self );
273
274 # Username field
275 $out .= Xml::label( $this->msg( 'listusersfrom' )->text(), 'offset' ) . ' ' .
276 Html::input(
277 'username',
278 $this->requestedUser,
279 'text',
280 array(
281 'id' => 'offset',
282 'size' => 20,
283 'autofocus' => $this->requestedUser === ''
284 )
285 ) . ' ';
286
287 # Group drop-down list
288 $out .= Xml::label( $this->msg( 'group' )->text(), 'group' ) . ' ' .
289 Xml::openElement( 'select', array( 'name' => 'group', 'id' => 'group' ) ) .
290 Xml::option( $this->msg( 'group-all' )->text(), '' );
291 foreach ( $this->getAllGroups() as $group => $groupText ) {
292 $out .= Xml::option( $groupText, $group, $group == $this->requestedGroup );
293 }
294 $out .= Xml::closeElement( 'select' ) . '<br />';
295 $out .= Xml::checkLabel(
296 $this->msg( 'listusers-editsonly' )->text(),
297 'editsOnly',
298 'editsOnly',
299 $this->editsOnly
300 );
301 $out .= '&#160;';
302 $out .= Xml::checkLabel(
303 $this->msg( 'listusers-creationsort' )->text(),
304 'creationSort',
305 'creationSort',
306 $this->creationSort
307 );
308 $out .= '&#160;';
309 $out .= Xml::checkLabel(
310 $this->msg( 'listusers-desc' )->text(),
311 'desc',
312 'desc',
313 $this->mDefaultDirection
314 );
315 $out .= '<br />';
316
317 Hooks::run( 'SpecialListusersHeaderForm', array( $this, &$out ) );
318
319 # Submit button and form bottom
320 $out .= Html::hidden( 'limit', $this->mLimit );
321 $out .= Xml::submitButton( $this->msg( 'allpagessubmit' )->text() );
322 Hooks::run( 'SpecialListusersHeader', array( $this, &$out ) );
323 $out .= Xml::closeElement( 'fieldset' ) .
324 Xml::closeElement( 'form' );
325
326 return $out;
327 }
328
329 /**
330 * Get a list of all explicit groups
331 * @return array
332 */
333 function getAllGroups() {
334 $result = array();
335 foreach ( User::getAllGroups() as $group ) {
336 $result[$group] = User::getGroupName( $group );
337 }
338 asort( $result );
339
340 return $result;
341 }
342
343 /**
344 * Preserve group and username offset parameters when paging
345 * @return array
346 */
347 function getDefaultQuery() {
348 $query = parent::getDefaultQuery();
349 if ( $this->requestedGroup != '' ) {
350 $query['group'] = $this->requestedGroup;
351 }
352 if ( $this->requestedUser != '' ) {
353 $query['username'] = $this->requestedUser;
354 }
355 Hooks::run( 'SpecialListusersDefaultQuery', array( $this, &$query ) );
356
357 return $query;
358 }
359
360 /**
361 * Get a list of groups the specified user belongs to
362 *
363 * @param int $uid User id
364 * @param array|null $cache
365 * @return array
366 */
367 protected static function getGroups( $uid, $cache = null ) {
368 if ( $cache === null ) {
369 $user = User::newFromId( $uid );
370 $effectiveGroups = $user->getEffectiveGroups();
371 } else {
372 $effectiveGroups = isset( $cache[$uid] ) ? $cache[$uid] : array();
373 }
374 $groups = array_diff( $effectiveGroups, User::getImplicitGroups() );
375
376 return $groups;
377 }
378
379 /**
380 * Format a link to a group description page
381 *
382 * @param string $group Group name
383 * @param string $username Username
384 * @return string
385 */
386 protected static function buildGroupLink( $group, $username ) {
387 return User::makeGroupLinkHtml(
388 $group,
389 User::getGroupMember( $group, $username )
390 );
391 }
392 }
393
394 /**
395 * @ingroup SpecialPage
396 */
397 class SpecialListUsers extends IncludableSpecialPage {
398 /**
399 * Constructor
400 */
401 public function __construct() {
402 parent::__construct( 'Listusers' );
403 }
404
405 /**
406 * Show the special page
407 *
408 * @param string $par (optional) A group to list users from
409 */
410 public function execute( $par ) {
411 $this->setHeaders();
412 $this->outputHeader();
413
414 $up = new UsersPager( $this->getContext(), $par, $this->including() );
415
416 # getBody() first to check, if empty
417 $usersbody = $up->getBody();
418
419 $s = '';
420 if ( !$this->including() ) {
421 $s = $up->getPageHeader();
422 }
423
424 if ( $usersbody ) {
425 $s .= $up->getNavigationBar();
426 $s .= Html::rawElement( 'ul', array(), $usersbody );
427 $s .= $up->getNavigationBar();
428 } else {
429 $s .= $this->msg( 'listusers-noresult' )->parseAsBlock();
430 }
431
432 $this->getOutput()->addHTML( $s );
433 }
434
435 /**
436 * Return an array of subpages that this special page will accept.
437 *
438 * @return string[] subpages
439 */
440 public function getSubpagesForPrefixSearch() {
441 return User::getAllGroups();
442 }
443
444 protected function getGroupName() {
445 return 'users';
446 }
447 }
448
449 /**
450 * Redirect page: Special:ListAdmins --> Special:ListUsers/sysop.
451 *
452 * @ingroup SpecialPage
453 */
454 class SpecialListAdmins extends SpecialRedirectToSpecial {
455 function __construct() {
456 parent::__construct( 'Listadmins', 'Listusers', 'sysop' );
457 }
458 }
459
460 /**
461 * Redirect page: Special:ListBots --> Special:ListUsers/bot.
462 *
463 * @ingroup SpecialPage
464 */
465 class SpecialListBots extends SpecialRedirectToSpecial {
466 function __construct() {
467 parent::__construct( 'Listbots', 'Listusers', 'bot' );
468 }
469 }