e27fb5865511349c9a70f64a0a0f27f930a28f09
[lhc/web/wiklou.git] / includes / specials / pagers / UsersPager.php
1 <?php
2 /**
3 * Copyright © 2004 Brion Vibber, lcrocker, Tim Starling,
4 * Domas Mituzas, Antoine Musso, Jens Frank, Zhengzhu,
5 * 2006 Rob Church <robchur@gmail.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @file
23 * @ingroup Pager
24 */
25
26 /**
27 * This class is used to get a list of user. The ones with specials
28 * rights (sysop, bureaucrat, developer) will have them displayed
29 * next to their names.
30 *
31 * @ingroup Pager
32 */
33 class UsersPager extends AlphabeticPager {
34
35 /**
36 * @var array[] A array with user ids as key and a array of groups as value
37 */
38 protected $userGroupCache;
39
40 /**
41 * @param IContextSource|null $context
42 * @param array|null $par (Default null)
43 * @param bool|null $including Whether this page is being transcluded in
44 * another page
45 */
46 public function __construct( IContextSource $context = null, $par = null, $including = null ) {
47 $request = $this->getRequest();
48 $par = $par ?? '';
49 $parms = explode( '/', $par );
50 $symsForAll = [ '*', 'user' ];
51
52 if ( $parms[0] != '' &&
53 ( in_array( $par, User::getAllGroups() ) || in_array( $par, $symsForAll ) )
54 ) {
55 $this->requestedGroup = $par;
56 $un = $request->getText( 'username' );
57 } elseif ( count( $parms ) == 2 ) {
58 $this->requestedGroup = $parms[0];
59 $un = $parms[1];
60 } else {
61 $this->requestedGroup = $request->getVal( 'group' );
62 $un = ( $par != '' ) ? $par : $request->getText( 'username' );
63 }
64
65 if ( in_array( $this->requestedGroup, $symsForAll ) ) {
66 $this->requestedGroup = '';
67 }
68 $this->editsOnly = $request->getBool( 'editsOnly' );
69 $this->temporaryGroupsOnly = $request->getBool( 'temporaryGroupsOnly' );
70 $this->creationSort = $request->getBool( 'creationSort' );
71 $this->including = $including;
72 $this->mDefaultDirection = $request->getBool( 'desc' )
73 ? IndexPager::DIR_DESCENDING
74 : IndexPager::DIR_ASCENDING;
75
76 $this->requestedUser = '';
77
78 if ( $un != '' ) {
79 $username = Title::makeTitleSafe( NS_USER, $un );
80
81 if ( !is_null( $username ) ) {
82 $this->requestedUser = $username->getText();
83 }
84 }
85
86 parent::__construct( $context );
87 }
88
89 /**
90 * @return string
91 */
92 function getIndexField() {
93 return $this->creationSort ? 'user_id' : 'user_name';
94 }
95
96 /**
97 * @return array
98 */
99 function getQueryInfo() {
100 $dbr = wfGetDB( DB_REPLICA );
101 $conds = [];
102
103 // Don't show hidden names
104 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
105 $conds[] = 'ipb_deleted IS NULL OR ipb_deleted = 0';
106 }
107
108 $options = [];
109
110 if ( $this->requestedGroup != '' || $this->temporaryGroupsOnly ) {
111 $conds[] = 'ug_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() ) .
112 ( !$this->temporaryGroupsOnly ? ' OR ug_expiry IS NULL' : '' );
113 }
114
115 if ( $this->requestedGroup != '' ) {
116 $conds['ug_group'] = $this->requestedGroup;
117 }
118
119 if ( $this->requestedUser != '' ) {
120 # Sorted either by account creation or name
121 if ( $this->creationSort ) {
122 $conds[] = 'user_id >= ' . intval( User::idFromName( $this->requestedUser ) );
123 } else {
124 $conds[] = 'user_name >= ' . $dbr->addQuotes( $this->requestedUser );
125 }
126 }
127
128 if ( $this->editsOnly ) {
129 $conds[] = 'user_editcount > 0';
130 }
131
132 $options['GROUP BY'] = $this->creationSort ? 'user_id' : 'user_name';
133
134 $query = [
135 'tables' => [ 'user', 'user_groups', 'ipblocks' ],
136 'fields' => [
137 'user_name' => $this->creationSort ? 'MAX(user_name)' : 'user_name',
138 'user_id' => $this->creationSort ? 'user_id' : 'MAX(user_id)',
139 'edits' => 'MAX(user_editcount)',
140 'creation' => 'MIN(user_registration)',
141 'ipb_deleted' => 'MAX(ipb_deleted)', // block/hide status
142 'ipb_sitewide' => 'MAX(ipb_sitewide)'
143 ],
144 'options' => $options,
145 'join_conds' => [
146 'user_groups' => [ 'LEFT JOIN', 'user_id=ug_user' ],
147 'ipblocks' => [
148 'LEFT JOIN', [
149 'user_id=ipb_user',
150 'ipb_auto' => 0
151 ]
152 ],
153 ],
154 'conds' => $conds
155 ];
156
157 Hooks::run( 'SpecialListusersQueryInfo', [ $this, &$query ] );
158
159 return $query;
160 }
161
162 /**
163 * @param stdClass $row
164 * @return string
165 */
166 function formatRow( $row ) {
167 if ( $row->user_id == 0 ) { # T18487
168 return '';
169 }
170
171 $userName = $row->user_name;
172
173 $ulinks = Linker::userLink( $row->user_id, $userName );
174 $ulinks .= Linker::userToolLinksRedContribs(
175 $row->user_id,
176 $userName,
177 (int)$row->edits,
178 // don't render parentheses in HTML markup (CSS will provide)
179 false
180 );
181
182 $lang = $this->getLanguage();
183
184 $groups = '';
185 $ugms = self::getGroupMemberships( intval( $row->user_id ), $this->userGroupCache );
186
187 if ( !$this->including && count( $ugms ) > 0 ) {
188 $list = [];
189 foreach ( $ugms as $ugm ) {
190 $list[] = $this->buildGroupLink( $ugm, $userName );
191 }
192 $groups = $lang->commaList( $list );
193 }
194
195 $item = $lang->specialList( $ulinks, $groups );
196
197 if ( $row->ipb_deleted ) {
198 $item = "<span class=\"deleted\">$item</span>";
199 }
200
201 $edits = '';
202 if ( !$this->including && $this->getConfig()->get( 'Edititis' ) ) {
203 $count = $this->msg( 'usereditcount' )->numParams( $row->edits )->escaped();
204 $edits = $this->msg( 'word-separator' )->escaped() . $this->msg( 'brackets', $count )->escaped();
205 }
206
207 $created = '';
208 # Some rows may be null
209 if ( !$this->including && $row->creation ) {
210 $user = $this->getUser();
211 $d = $lang->userDate( $row->creation, $user );
212 $t = $lang->userTime( $row->creation, $user );
213 $created = $this->msg( 'usercreated', $d, $t, $row->user_name )->escaped();
214 $created = ' ' . $this->msg( 'parentheses' )->rawParams( $created )->escaped();
215 }
216
217 $blocked = !is_null( $row->ipb_deleted ) && $row->ipb_sitewide === '1' ?
218 ' ' . $this->msg( 'listusers-blocked', $userName )->escaped() :
219 '';
220
221 Hooks::run( 'SpecialListusersFormatRow', [ &$item, $row ] );
222
223 return Html::rawElement( 'li', [], "{$item}{$edits}{$created}{$blocked}" );
224 }
225
226 protected function doBatchLookups() {
227 $batch = new LinkBatch();
228 $userIds = [];
229 # Give some pointers to make user links
230 foreach ( $this->mResult as $row ) {
231 $batch->add( NS_USER, $row->user_name );
232 $batch->add( NS_USER_TALK, $row->user_name );
233 $userIds[] = $row->user_id;
234 }
235
236 // Lookup groups for all the users
237 $dbr = wfGetDB( DB_REPLICA );
238 $groupRes = $dbr->select(
239 'user_groups',
240 UserGroupMembership::selectFields(),
241 [ 'ug_user' => $userIds ],
242 __METHOD__
243 );
244 $cache = [];
245 $groups = [];
246 foreach ( $groupRes as $row ) {
247 $ugm = UserGroupMembership::newFromRow( $row );
248 if ( !$ugm->isExpired() ) {
249 $cache[$row->ug_user][$row->ug_group] = $ugm;
250 $groups[$row->ug_group] = true;
251 }
252 }
253
254 // Give extensions a chance to add things like global user group data
255 // into the cache array to ensure proper output later on
256 Hooks::run( 'UsersPagerDoBatchLookups', [ $dbr, $userIds, &$cache, &$groups ] );
257
258 $this->userGroupCache = $cache;
259
260 // Add page of groups to link batch
261 foreach ( $groups as $group => $unused ) {
262 $groupPage = UserGroupMembership::getGroupPage( $group );
263 if ( $groupPage ) {
264 $batch->addObj( $groupPage );
265 }
266 }
267
268 $batch->execute();
269 $this->mResult->rewind();
270 }
271
272 /**
273 * @return string
274 */
275 function getPageHeader() {
276 $self = explode( '/', $this->getTitle()->getPrefixedDBkey(), 2 )[0];
277
278 $groupOptions = [ $this->msg( 'group-all' )->text() => '' ];
279 foreach ( $this->getAllGroups() as $group => $groupText ) {
280 $groupOptions[ $groupText ] = $group;
281 }
282
283 $formDescriptor = [
284 'user' => [
285 'class' => HTMLUserTextField::class,
286 'label' => $this->msg( 'listusersfrom' )->text(),
287 'name' => 'username',
288 'default' => $this->requestedUser,
289 ],
290 'dropdown' => [
291 'label' => $this->msg( 'group' )->text(),
292 'name' => 'group',
293 'default' => $this->requestedGroup,
294 'class' => HTMLSelectField::class,
295 'options' => $groupOptions,
296 ],
297 'editsOnly' => [
298 'type' => 'check',
299 'label' => $this->msg( 'listusers-editsonly' )->text(),
300 'name' => 'editsOnly',
301 'id' => 'editsOnly',
302 'default' => $this->editsOnly
303 ],
304 'temporaryGroupsOnly' => [
305 'type' => 'check',
306 'label' => $this->msg( 'listusers-temporarygroupsonly' )->text(),
307 'name' => 'temporaryGroupsOnly',
308 'id' => 'temporaryGroupsOnly',
309 'default' => $this->temporaryGroupsOnly
310 ],
311 'creationSort' => [
312 'type' => 'check',
313 'label' => $this->msg( 'listusers-creationsort' )->text(),
314 'name' => 'creationSort',
315 'id' => 'creationSort',
316 'default' => $this->creationSort
317 ],
318 'desc' => [
319 'type' => 'check',
320 'label' => $this->msg( 'listusers-desc' )->text(),
321 'name' => 'desc',
322 'id' => 'desc',
323 'default' => $this->mDefaultDirection
324 ],
325 'limithiddenfield' => [
326 'class' => HTMLHiddenField::class,
327 'name' => 'limit',
328 'default' => $this->mLimit
329 ]
330 ];
331
332 $beforeSubmitButtonHookOut = '';
333 Hooks::run( 'SpecialListusersHeaderForm', [ $this, &$beforeSubmitButtonHookOut ] );
334
335 if ( $beforeSubmitButtonHookOut !== '' ) {
336 $formDescriptor[ 'beforeSubmitButtonHookOut' ] = [
337 'class' => HTMLInfoField::class,
338 'raw' => true,
339 'default' => $beforeSubmitButtonHookOut
340 ];
341 }
342
343 $formDescriptor[ 'submit' ] = [
344 'class' => HTMLSubmitField::class,
345 'buttonlabel-message' => 'listusers-submit',
346 ];
347
348 $beforeClosingFieldsetHookOut = '';
349 Hooks::run( 'SpecialListusersHeader', [ $this, &$beforeClosingFieldsetHookOut ] );
350
351 if ( $beforeClosingFieldsetHookOut !== '' ) {
352 $formDescriptor[ 'beforeClosingFieldsetHookOut' ] = [
353 'class' => HTMLInfoField::class,
354 'raw' => true,
355 'default' => $beforeClosingFieldsetHookOut
356 ];
357 }
358
359 $htmlForm = HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() );
360 $htmlForm
361 ->setMethod( 'get' )
362 ->setAction( Title::newFromText( $self )->getLocalURL() )
363 ->setId( 'mw-listusers-form' )
364 ->setFormIdentifier( 'mw-listusers-form' )
365 ->suppressDefaultSubmit()
366 ->setWrapperLegendMsg( 'listusers' );
367 return $htmlForm->prepareForm()->getHTML( true );
368 }
369
370 /**
371 * Get a list of all explicit groups
372 * @return array
373 */
374 function getAllGroups() {
375 $result = [];
376 foreach ( User::getAllGroups() as $group ) {
377 $result[$group] = UserGroupMembership::getGroupName( $group );
378 }
379 asort( $result );
380
381 return $result;
382 }
383
384 /**
385 * Preserve group and username offset parameters when paging
386 * @return array
387 */
388 function getDefaultQuery() {
389 $query = parent::getDefaultQuery();
390 if ( $this->requestedGroup != '' ) {
391 $query['group'] = $this->requestedGroup;
392 }
393 if ( $this->requestedUser != '' ) {
394 $query['username'] = $this->requestedUser;
395 }
396 Hooks::run( 'SpecialListusersDefaultQuery', [ $this, &$query ] );
397
398 return $query;
399 }
400
401 /**
402 * Get an associative array containing groups the specified user belongs to,
403 * and the relevant UserGroupMembership objects
404 *
405 * @param int $uid User id
406 * @param array[]|null $cache
407 * @return UserGroupMembership[] (group name => UserGroupMembership object)
408 */
409 protected static function getGroupMemberships( $uid, $cache = null ) {
410 if ( $cache === null ) {
411 $user = User::newFromId( $uid );
412 return $user->getGroupMemberships();
413 } else {
414 return $cache[$uid] ?? [];
415 }
416 }
417
418 /**
419 * Format a link to a group description page
420 *
421 * @param string|UserGroupMembership $group Group name or UserGroupMembership object
422 * @param string $username
423 * @return string
424 */
425 protected function buildGroupLink( $group, $username ) {
426 return UserGroupMembership::getLink( $group, $this->getContext(), 'html', $username );
427 }
428 }