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