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