Merge "Add batch lookup for user groups on Special:ListUsers"
[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 private $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 'numgroups' => 'COUNT(ug_group)',
141 'singlegroup' => 'MAX(ug_group)', // the usergroup if there is only one
142 'creation' => 'MIN(user_registration)',
143 'ipb_deleted' => 'MAX(ipb_deleted)' // block/hide status
144 ),
145 'options' => $options,
146 'join_conds' => array(
147 'user_groups' => array( 'LEFT JOIN', 'user_id=ug_user' ),
148 'ipblocks' => array(
149 'LEFT JOIN', array(
150 'user_id=ipb_user',
151 'ipb_auto' => 0
152 )
153 ),
154 ),
155 'conds' => $conds
156 );
157
158 Hooks::run( 'SpecialListusersQueryInfo', array( $this, &$query ) );
159
160 return $query;
161 }
162
163 /**
164 * @param stdClass $row
165 * @return string
166 */
167 function formatRow( $row ) {
168 if ( $row->user_id == 0 ) { #Bug 16487
169 return '';
170 }
171
172 $userName = $row->user_name;
173
174 $ulinks = Linker::userLink( $row->user_id, $userName );
175 $ulinks .= Linker::userToolLinksRedContribs(
176 $row->user_id,
177 $userName,
178 (int)$row->edits
179 );
180
181 $lang = $this->getLanguage();
182
183 $groups = '';
184 $groups_list = self::getGroups( intval( $row->user_id ), $this->userGroupCache );
185
186 if ( !$this->including && count( $groups_list ) > 0 ) {
187 $list = array();
188 foreach ( $groups_list as $group ) {
189 $list[] = self::buildGroupLink( $group, $userName );
190 }
191 $groups = $lang->commaList( $list );
192 }
193
194 $item = $lang->specialList( $ulinks, $groups );
195
196 if ( $row->ipb_deleted ) {
197 $item = "<span class=\"deleted\">$item</span>";
198 }
199
200 $edits = '';
201 if ( !$this->including && $this->getConfig()->get( 'Edititis' ) ) {
202 $count = $this->msg( 'usereditcount' )->numParams( $row->edits )->escaped();
203 $edits = $this->msg( 'word-separator' )->escaped() . $this->msg( 'brackets', $count )->escaped();
204 }
205
206 $created = '';
207 # Some rows may be null
208 if ( !$this->including && $row->creation ) {
209 $user = $this->getUser();
210 $d = $lang->userDate( $row->creation, $user );
211 $t = $lang->userTime( $row->creation, $user );
212 $created = $this->msg( 'usercreated', $d, $t, $row->user_name )->escaped();
213 $created = ' ' . $this->msg( 'parentheses' )->rawParams( $created )->escaped();
214 }
215 $blocked = !is_null( $row->ipb_deleted ) ?
216 ' ' . $this->msg( 'listusers-blocked', $userName )->escaped() :
217 '';
218
219 Hooks::run( 'SpecialListusersFormatRow', array( &$item, $row ) );
220
221 return Html::rawElement( 'li', array(), "{$item}{$edits}{$created}{$blocked}" );
222 }
223
224 function doBatchLookups() {
225 $batch = new LinkBatch();
226 $userIds = array();
227 # Give some pointers to make user links
228 foreach ( $this->mResult as $row ) {
229 $batch->add( NS_USER, $row->user_name );
230 $batch->add( NS_USER_TALK, $row->user_name );
231 $userIds[] = $row->user_id;
232 }
233
234 // Lookup groups for all the users
235 $dbr = wfGetDB( DB_SLAVE );
236 $groupRes = $dbr->select(
237 'user_groups',
238 array( 'ug_user', 'ug_group' ),
239 array( 'ug_user' => $userIds ),
240 __METHOD__
241 );
242 $cache = array();
243 $groups = array();
244 foreach ( $groupRes as $row ) {
245 $cache[intval( $row->ug_user )][] = $row->ug_group;
246 $groups[$row->ug_group] = true;
247 }
248 $this->userGroupCache = $cache;
249
250 // Add page of groups to link batch
251 foreach( $groups as $group => $unused ) {
252 $groupPage = User::getGroupPage( $group );
253 if ( $groupPage ) {
254 $batch->addObj( $groupPage );
255 }
256 }
257
258 $batch->execute();
259 $this->mResult->rewind();
260 }
261
262 /**
263 * @return string
264 */
265 function getPageHeader() {
266 list( $self ) = explode( '/', $this->getTitle()->getPrefixedDBkey() );
267
268 # Form tag
269 $out = Xml::openElement(
270 'form',
271 array( 'method' => 'get', 'action' => wfScript(), 'id' => 'mw-listusers-form' )
272 ) .
273 Xml::fieldset( $this->msg( 'listusers' )->text() ) .
274 Html::hidden( 'title', $self );
275
276 # Username field
277 $out .= Xml::label( $this->msg( 'listusersfrom' )->text(), 'offset' ) . ' ' .
278 Html::input(
279 'username',
280 $this->requestedUser,
281 'text',
282 array(
283 'id' => 'offset',
284 'size' => 20,
285 'autofocus' => $this->requestedUser === ''
286 )
287 ) . ' ';
288
289 # Group drop-down list
290 $out .= Xml::label( $this->msg( 'group' )->text(), 'group' ) . ' ' .
291 Xml::openElement( 'select', array( 'name' => 'group', 'id' => 'group' ) ) .
292 Xml::option( $this->msg( 'group-all' )->text(), '' );
293 foreach ( $this->getAllGroups() as $group => $groupText ) {
294 $out .= Xml::option( $groupText, $group, $group == $this->requestedGroup );
295 }
296 $out .= Xml::closeElement( 'select' ) . '<br />';
297 $out .= Xml::checkLabel(
298 $this->msg( 'listusers-editsonly' )->text(),
299 'editsOnly',
300 'editsOnly',
301 $this->editsOnly
302 );
303 $out .= '&#160;';
304 $out .= Xml::checkLabel(
305 $this->msg( 'listusers-creationsort' )->text(),
306 'creationSort',
307 'creationSort',
308 $this->creationSort
309 );
310 $out .= '&#160;';
311 $out .= Xml::checkLabel(
312 $this->msg( 'listusers-desc' )->text(),
313 'desc',
314 'desc',
315 $this->mDefaultDirection
316 );
317 $out .= '<br />';
318
319 Hooks::run( 'SpecialListusersHeaderForm', array( $this, &$out ) );
320
321 # Submit button and form bottom
322 $out .= Html::hidden( 'limit', $this->mLimit );
323 $out .= Xml::submitButton( $this->msg( 'allpagessubmit' )->text() );
324 Hooks::run( 'SpecialListusersHeader', array( $this, &$out ) );
325 $out .= Xml::closeElement( 'fieldset' ) .
326 Xml::closeElement( 'form' );
327
328 return $out;
329 }
330
331 /**
332 * Get a list of all explicit groups
333 * @return array
334 */
335 function getAllGroups() {
336 $result = array();
337 foreach ( User::getAllGroups() as $group ) {
338 $result[$group] = User::getGroupName( $group );
339 }
340 asort( $result );
341
342 return $result;
343 }
344
345 /**
346 * Preserve group and username offset parameters when paging
347 * @return array
348 */
349 function getDefaultQuery() {
350 $query = parent::getDefaultQuery();
351 if ( $this->requestedGroup != '' ) {
352 $query['group'] = $this->requestedGroup;
353 }
354 if ( $this->requestedUser != '' ) {
355 $query['username'] = $this->requestedUser;
356 }
357 Hooks::run( 'SpecialListusersDefaultQuery', array( $this, &$query ) );
358
359 return $query;
360 }
361
362 /**
363 * Get a list of groups the specified user belongs to
364 *
365 * @param int $uid User id
366 * @param array|null $cache
367 * @return array
368 */
369 protected static function getGroups( $uid, $cache = null ) {
370 if ( $cache === null ) {
371 $user = User::newFromId( $uid );
372 $effectiveGroups = $user->getEffectiveGroups();
373 } else {
374 $effectiveGroups = isset( $cache[$uid] ) ? $cache[$uid] : array();
375 }
376 $groups = array_diff( $effectiveGroups, User::getImplicitGroups() );
377
378 return $groups;
379 }
380
381 /**
382 * Format a link to a group description page
383 *
384 * @param string $group Group name
385 * @param string $username Username
386 * @return string
387 */
388 protected static function buildGroupLink( $group, $username ) {
389 return User::makeGroupLinkHtml(
390 $group,
391 htmlspecialchars( User::getGroupMember( $group, $username ) )
392 );
393 }
394 }
395
396 /**
397 * @ingroup SpecialPage
398 */
399 class SpecialListUsers extends IncludableSpecialPage {
400 /**
401 * Constructor
402 */
403 public function __construct() {
404 parent::__construct( 'Listusers' );
405 }
406
407 /**
408 * Show the special page
409 *
410 * @param string $par (optional) A group to list users from
411 */
412 public function execute( $par ) {
413 $this->setHeaders();
414 $this->outputHeader();
415
416 $up = new UsersPager( $this->getContext(), $par, $this->including() );
417
418 # getBody() first to check, if empty
419 $usersbody = $up->getBody();
420
421 $s = '';
422 if ( !$this->including() ) {
423 $s = $up->getPageHeader();
424 }
425
426 if ( $usersbody ) {
427 $s .= $up->getNavigationBar();
428 $s .= Html::rawElement( 'ul', array(), $usersbody );
429 $s .= $up->getNavigationBar();
430 } else {
431 $s .= $this->msg( 'listusers-noresult' )->parseAsBlock();
432 }
433
434 $this->getOutput()->addHTML( $s );
435 }
436
437 /**
438 * Return an array of subpages that this special page will accept.
439 *
440 * @return string[] subpages
441 */
442 public function getSubpagesForPrefixSearch() {
443 return User::getAllGroups();
444 }
445
446 protected function getGroupName() {
447 return 'users';
448 }
449 }
450
451 /**
452 * Redirect page: Special:ListAdmins --> Special:ListUsers/sysop.
453 *
454 * @ingroup SpecialPage
455 */
456 class SpecialListAdmins extends SpecialRedirectToSpecial {
457 function __construct() {
458 parent::__construct( 'Listadmins', 'Listusers', 'sysop' );
459 }
460 }
461
462 /**
463 * Redirect page: Special:ListBots --> Special:ListUsers/bot.
464 *
465 * @ingroup SpecialPage
466 */
467 class SpecialListBots extends SpecialRedirectToSpecial {
468 function __construct() {
469 parent::__construct( 'Listbots', 'Listusers', 'bot' );
470 }
471 }