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