Merge "mediawiki.action.edit.preview: New module replacing preview.js"
[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 'user_name' => $this->creationSort ? 'MAX(user_name)' : 'user_name',
120 'user_id' => $this->creationSort ? 'user_id' : 'MAX(user_id)',
121 'edits' => 'MAX(user_editcount)',
122 'numgroups' => 'COUNT(ug_group)',
123 'singlegroup' => 'MAX(ug_group)', // the usergroup if there is only one
124 'creation' => 'MIN(user_registration)',
125 'ipb_deleted' => 'MAX(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', array(
131 'user_id=ipb_user',
132 'ipb_deleted' => 1,
133 'ipb_auto' => 0
134 )),
135 ),
136 'conds' => $conds
137 );
138
139 wfRunHooks( 'SpecialListusersQueryInfo', array( $this, &$query ) );
140 return $query;
141 }
142
143 /**
144 * @param $row Object
145 * @return String
146 */
147 function formatRow( $row ) {
148 if ( $row->user_id == 0 ) { #Bug 16487
149 return '';
150 }
151
152 $userName = $row->user_name;
153
154 $ulinks = Linker::userLink( $row->user_id, $userName );
155 $ulinks .= Linker::userToolLinks( $row->user_id, $userName );
156
157 $lang = $this->getLanguage();
158
159 $groups = '';
160 $groups_list = self::getGroups( $row->user_id );
161 if( !$this->including && count( $groups_list ) > 0 ) {
162 $list = array();
163 foreach( $groups_list as $group )
164 $list[] = self::buildGroupLink( $group, $userName );
165 $groups = $lang->commaList( $list );
166 }
167
168 $item = $lang->specialList( $ulinks, $groups );
169 if( $row->ipb_deleted ) {
170 $item = "<span class=\"deleted\">$item</span>";
171 }
172
173 $edits = '';
174 global $wgEdititis;
175 if ( !$this->including && $wgEdititis ) {
176 $edits = ' [' . $this->msg( 'usereditcount' )->numParams( $row->edits )->escaped() . ']';
177 }
178
179 $created = '';
180 # Some rows may be NULL
181 if( !$this->including && $row->creation ) {
182 $user = $this->getUser();
183 $d = $lang->userDate( $row->creation, $user );
184 $t = $lang->userTime( $row->creation, $user );
185 $created = $this->msg( 'usercreated', $d, $t, $row->user_name )->escaped();
186 $created = ' ' . $this->msg( 'parentheses' )->rawParams( $created )->escaped();
187 }
188
189 wfRunHooks( 'SpecialListusersFormatRow', array( &$item, $row ) );
190 return Html::rawElement( 'li', array(), "{$item}{$edits}{$created}" );
191 }
192
193 function doBatchLookups() {
194 $batch = new LinkBatch();
195 # Give some pointers to make user links
196 foreach ( $this->mResult as $row ) {
197 $batch->add( NS_USER, $row->user_name );
198 $batch->add( NS_USER_TALK, $row->user_name );
199 }
200 $batch->execute();
201 $this->mResult->rewind();
202 }
203
204 /**
205 * @return string
206 */
207 function getPageHeader( ) {
208 global $wgScript;
209
210 list( $self ) = explode( '/', $this->getTitle()->getPrefixedDBkey() );
211
212 # Form tag
213 $out = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'id' => 'mw-listusers-form' ) ) .
214 Xml::fieldset( $this->msg( 'listusers' )->text() ) .
215 Html::hidden( 'title', $self );
216
217 # Username field
218 $out .= Xml::label( $this->msg( 'listusersfrom' )->text(), 'offset' ) . ' ' .
219 Xml::input( 'username', 20, $this->requestedUser, array( 'id' => 'offset' ) ) . ' ';
220
221 # Group drop-down list
222 $out .= Xml::label( $this->msg( 'group' )->text(), 'group' ) . ' ' .
223 Xml::openElement('select', array( 'name' => 'group', 'id' => 'group' ) ) .
224 Xml::option( $this->msg( 'group-all' )->text(), '' );
225 foreach( $this->getAllGroups() as $group => $groupText )
226 $out .= Xml::option( $groupText, $group, $group == $this->requestedGroup );
227 $out .= Xml::closeElement( 'select' ) . '<br />';
228 $out .= Xml::checkLabel( $this->msg( 'listusers-editsonly' )->text(), 'editsOnly', 'editsOnly', $this->editsOnly );
229 $out .= '&#160;';
230 $out .= Xml::checkLabel( $this->msg( 'listusers-creationsort' )->text(), 'creationSort', 'creationSort', $this->creationSort );
231 $out .= '<br />';
232
233 wfRunHooks( 'SpecialListusersHeaderForm', array( $this, &$out ) );
234
235 # Submit button and form bottom
236 $out .= Html::hidden( 'limit', $this->mLimit );
237 $out .= Xml::submitButton( $this->msg( 'allpagessubmit' )->text() );
238 wfRunHooks( 'SpecialListusersHeader', array( $this, &$out ) );
239 $out .= Xml::closeElement( 'fieldset' ) .
240 Xml::closeElement( 'form' );
241
242 return $out;
243 }
244
245 /**
246 * Get a list of all explicit groups
247 * @return array
248 */
249 function getAllGroups() {
250 $result = array();
251 foreach( User::getAllGroups() as $group ) {
252 $result[$group] = User::getGroupName( $group );
253 }
254 asort( $result );
255 return $result;
256 }
257
258 /**
259 * Preserve group and username offset parameters when paging
260 * @return array
261 */
262 function getDefaultQuery() {
263 $query = parent::getDefaultQuery();
264 if( $this->requestedGroup != '' ) {
265 $query['group'] = $this->requestedGroup;
266 }
267 if( $this->requestedUser != '' ) {
268 $query['username'] = $this->requestedUser;
269 }
270 wfRunHooks( 'SpecialListusersDefaultQuery', array( $this, &$query ) );
271 return $query;
272 }
273
274 /**
275 * Get a list of groups the specified user belongs to
276 *
277 * @param $uid Integer: user id
278 * @return array
279 */
280 protected static function getGroups( $uid ) {
281 $user = User::newFromId( $uid );
282 $groups = array_diff( $user->getEffectiveGroups(), User::getImplicitGroups() );
283 return $groups;
284 }
285
286 /**
287 * Format a link to a group description page
288 *
289 * @param $group String: group name
290 * @param $username String Username
291 * @return string
292 */
293 protected static function buildGroupLink( $group, $username ) {
294 return User::makeGroupLinkHtml( $group, htmlspecialchars( User::getGroupMember( $group, $username ) ) );
295 }
296 }
297
298 /**
299 * @ingroup SpecialPage
300 */
301 class SpecialListUsers extends IncludableSpecialPage {
302
303 /**
304 * Constructor
305 */
306 public function __construct() {
307 parent::__construct( 'Listusers' );
308 }
309
310 /**
311 * Show the special page
312 *
313 * @param $par string (optional) A group to list users from
314 */
315 public function execute( $par ) {
316 $this->setHeaders();
317 $this->outputHeader();
318
319 $up = new UsersPager( $this->getContext(), $par, $this->including() );
320
321 # getBody() first to check, if empty
322 $usersbody = $up->getBody();
323
324 $s = '';
325 if ( !$this->including() ) {
326 $s = $up->getPageHeader();
327 }
328
329 if( $usersbody ) {
330 $s .= $up->getNavigationBar();
331 $s .= Html::rawElement( 'ul', array(), $usersbody );
332 $s .= $up->getNavigationBar();
333 } else {
334 $s .= $this->msg( 'listusers-noresult' )->parseAsBlock();
335 }
336
337 $this->getOutput()->addHTML( $s );
338 }
339 }