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