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