Merge "Hide exceptions in MediaWiki::doJobs() as deferred updates do."
[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
73 $this->requestedUser = '';
74
75 if ( $un != '' ) {
76 $username = Title::makeTitleSafe( NS_USER, $un );
77
78 if ( !is_null( $username ) ) {
79 $this->requestedUser = $username->getText();
80 }
81 }
82
83 parent::__construct();
84 }
85
86 /**
87 * @return string
88 */
89 function getIndexField() {
90 return $this->creationSort ? 'user_id' : 'user_name';
91 }
92
93 /**
94 * @return Array
95 */
96 function getQueryInfo() {
97 $dbr = wfGetDB( DB_SLAVE );
98 $conds = array();
99
100 // Don't show hidden names
101 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
102 $conds[] = 'ipb_deleted IS NULL OR ipb_deleted = 0';
103 }
104
105 $options = array();
106
107 if ( $this->requestedGroup != '' ) {
108 $conds['ug_group'] = $this->requestedGroup;
109 }
110
111 if ( $this->requestedUser != '' ) {
112 # Sorted either by account creation or name
113 if ( $this->creationSort ) {
114 $conds[] = 'user_id >= ' . intval( User::idFromName( $this->requestedUser ) );
115 } else {
116 $conds[] = 'user_name >= ' . $dbr->addQuotes( $this->requestedUser );
117 }
118 }
119
120 if ( $this->editsOnly ) {
121 $conds[] = 'user_editcount > 0';
122 }
123
124 $options['GROUP BY'] = $this->creationSort ? 'user_id' : 'user_name';
125
126 $query = array(
127 'tables' => array( 'user', 'user_groups', 'ipblocks' ),
128 'fields' => array(
129 'user_name' => $this->creationSort ? 'MAX(user_name)' : 'user_name',
130 'user_id' => $this->creationSort ? 'user_id' : 'MAX(user_id)',
131 'edits' => 'MAX(user_editcount)',
132 'numgroups' => 'COUNT(ug_group)',
133 'singlegroup' => 'MAX(ug_group)', // the usergroup if there is only one
134 'creation' => 'MIN(user_registration)',
135 'ipb_deleted' => 'MAX(ipb_deleted)' // block/hide status
136 ),
137 'options' => $options,
138 'join_conds' => array(
139 'user_groups' => array( 'LEFT JOIN', 'user_id=ug_user' ),
140 'ipblocks' => array(
141 'LEFT JOIN', array(
142 'user_id=ipb_user',
143 'ipb_auto' => 0
144 )
145 ),
146 ),
147 'conds' => $conds
148 );
149
150 wfRunHooks( 'SpecialListusersQueryInfo', array( $this, &$query ) );
151
152 return $query;
153 }
154
155 /**
156 * @param $row Object
157 * @return String
158 */
159 function formatRow( $row ) {
160 if ( $row->user_id == 0 ) { #Bug 16487
161 return '';
162 }
163
164 $userName = $row->user_name;
165
166 $ulinks = Linker::userLink( $row->user_id, $userName );
167 $ulinks .= Linker::userToolLinksRedContribs(
168 $row->user_id,
169 $userName,
170 (int)$row->edits
171 );
172
173 $lang = $this->getLanguage();
174
175 $groups = '';
176 $groups_list = self::getGroups( $row->user_id );
177
178 if ( !$this->including && count( $groups_list ) > 0 ) {
179 $list = array();
180 foreach ( $groups_list as $group ) {
181 $list[] = self::buildGroupLink( $group, $userName );
182 }
183 $groups = $lang->commaList( $list );
184 }
185
186 $item = $lang->specialList( $ulinks, $groups );
187
188 if ( $row->ipb_deleted ) {
189 $item = "<span class=\"deleted\">$item</span>";
190 }
191
192 $edits = '';
193 global $wgEdititis;
194 if ( !$this->including && $wgEdititis ) {
195 // @fixme i18n issue: Hardcoded square brackets.
196 $edits = ' [' .
197 $this->msg( 'usereditcount' )->numParams( $row->edits )->escaped() .
198 ']';
199 }
200
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();
209 }
210 $blocked = !is_null( $row->ipb_deleted ) ?
211 ' ' . $this->msg( 'listusers-blocked', $userName )->escaped() :
212 '';
213
214 wfRunHooks( 'SpecialListusersFormatRow', array( &$item, $row ) );
215
216 return Html::rawElement( 'li', array(), "{$item}{$edits}{$created}{$blocked}" );
217 }
218
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 );
225 }
226 $batch->execute();
227 $this->mResult->rewind();
228 }
229
230 /**
231 * @return string
232 */
233 function getPageHeader() {
234 global $wgScript;
235
236 list( $self ) = explode( '/', $this->getTitle()->getPrefixedDBkey() );
237
238 # Form tag
239 $out = Xml::openElement(
240 'form',
241 array( 'method' => 'get', 'action' => $wgScript, '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 .= '<br />';
281
282 wfRunHooks( 'SpecialListusersHeaderForm', array( $this, &$out ) );
283
284 # Submit button and form bottom
285 $out .= Html::hidden( 'limit', $this->mLimit );
286 $out .= Xml::submitButton( $this->msg( 'allpagessubmit' )->text() );
287 wfRunHooks( 'SpecialListusersHeader', array( $this, &$out ) );
288 $out .= Xml::closeElement( 'fieldset' ) .
289 Xml::closeElement( 'form' );
290
291 return $out;
292 }
293
294 /**
295 * Get a list of all explicit groups
296 * @return array
297 */
298 function getAllGroups() {
299 $result = array();
300 foreach ( User::getAllGroups() as $group ) {
301 $result[$group] = User::getGroupName( $group );
302 }
303 asort( $result );
304
305 return $result;
306 }
307
308 /**
309 * Preserve group and username offset parameters when paging
310 * @return array
311 */
312 function getDefaultQuery() {
313 $query = parent::getDefaultQuery();
314 if ( $this->requestedGroup != '' ) {
315 $query['group'] = $this->requestedGroup;
316 }
317 if ( $this->requestedUser != '' ) {
318 $query['username'] = $this->requestedUser;
319 }
320 wfRunHooks( 'SpecialListusersDefaultQuery', array( $this, &$query ) );
321
322 return $query;
323 }
324
325 /**
326 * Get a list of groups the specified user belongs to
327 *
328 * @param $uid Integer: user id
329 * @return array
330 */
331 protected static function getGroups( $uid ) {
332 $user = User::newFromId( $uid );
333 $groups = array_diff( $user->getEffectiveGroups(), User::getImplicitGroups() );
334
335 return $groups;
336 }
337
338 /**
339 * Format a link to a group description page
340 *
341 * @param string $group group name
342 * @param string $username Username
343 * @return string
344 */
345 protected static function buildGroupLink( $group, $username ) {
346 return User::makeGroupLinkHtml(
347 $group,
348 htmlspecialchars( User::getGroupMember( $group, $username ) )
349 );
350 }
351 }
352
353 /**
354 * @ingroup SpecialPage
355 */
356 class SpecialListUsers extends IncludableSpecialPage {
357 /**
358 * Constructor
359 */
360 public function __construct() {
361 parent::__construct( 'Listusers' );
362 }
363
364 /**
365 * Show the special page
366 *
367 * @param string $par (optional) A group to list users from
368 */
369 public function execute( $par ) {
370 $this->setHeaders();
371 $this->outputHeader();
372
373 $up = new UsersPager( $this->getContext(), $par, $this->including() );
374
375 # getBody() first to check, if empty
376 $usersbody = $up->getBody();
377
378 $s = '';
379 if ( !$this->including() ) {
380 $s = $up->getPageHeader();
381 }
382
383 if ( $usersbody ) {
384 $s .= $up->getNavigationBar();
385 $s .= Html::rawElement( 'ul', array(), $usersbody );
386 $s .= $up->getNavigationBar();
387 } else {
388 $s .= $this->msg( 'listusers-noresult' )->parseAsBlock();
389 }
390
391 $this->getOutput()->addHTML( $s );
392 }
393
394 protected function getGroupName() {
395 return 'users';
396 }
397 }