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