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