Declare dynamic properties
[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 /** @var string */
41 protected $requestedGroup;
42
43 /** @var bool */
44 protected $editsOnly;
45
46 /** @var bool */
47 protected $temporaryGroupsOnly;
48
49 /** @var bool */
50 protected $creationSort;
51
52 /** @var bool|null */
53 protected $including;
54
55 /** @var string */
56 protected $requestedUser;
57
58 /**
59 * @param IContextSource|null $context
60 * @param array|null $par (Default null)
61 * @param bool|null $including Whether this page is being transcluded in
62 * another page
63 */
64 public function __construct( IContextSource $context = null, $par = null, $including = null ) {
65 if ( $context ) {
66 $this->setContext( $context );
67 }
68
69 $request = $this->getRequest();
70 $par = $par ?? '';
71 $parms = explode( '/', $par );
72 $symsForAll = [ '*', 'user' ];
73
74 if ( $parms[0] != '' &&
75 ( in_array( $par, User::getAllGroups() ) || in_array( $par, $symsForAll ) )
76 ) {
77 $this->requestedGroup = $par;
78 $un = $request->getText( 'username' );
79 } elseif ( count( $parms ) == 2 ) {
80 $this->requestedGroup = $parms[0];
81 $un = $parms[1];
82 } else {
83 $this->requestedGroup = $request->getVal( 'group' );
84 $un = ( $par != '' ) ? $par : $request->getText( 'username' );
85 }
86
87 if ( in_array( $this->requestedGroup, $symsForAll ) ) {
88 $this->requestedGroup = '';
89 }
90 $this->editsOnly = $request->getBool( 'editsOnly' );
91 $this->temporaryGroupsOnly = $request->getBool( 'temporaryGroupsOnly' );
92 $this->creationSort = $request->getBool( 'creationSort' );
93 $this->including = $including;
94 $this->mDefaultDirection = $request->getBool( 'desc' )
95 ? IndexPager::DIR_DESCENDING
96 : IndexPager::DIR_ASCENDING;
97
98 $this->requestedUser = '';
99
100 if ( $un != '' ) {
101 $username = Title::makeTitleSafe( NS_USER, $un );
102
103 if ( !is_null( $username ) ) {
104 $this->requestedUser = $username->getText();
105 }
106 }
107
108 parent::__construct();
109 }
110
111 /**
112 * @return string
113 */
114 function getIndexField() {
115 return $this->creationSort ? 'user_id' : 'user_name';
116 }
117
118 /**
119 * @return array
120 */
121 function getQueryInfo() {
122 $dbr = wfGetDB( DB_REPLICA );
123 $conds = [];
124
125 // Don't show hidden names
126 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
127 $conds[] = 'ipb_deleted IS NULL OR ipb_deleted = 0';
128 }
129
130 $options = [];
131
132 if ( $this->requestedGroup != '' || $this->temporaryGroupsOnly ) {
133 $conds[] = 'ug_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() ) .
134 ( !$this->temporaryGroupsOnly ? ' OR ug_expiry IS NULL' : '' );
135 }
136
137 if ( $this->requestedGroup != '' ) {
138 $conds['ug_group'] = $this->requestedGroup;
139 }
140
141 if ( $this->requestedUser != '' ) {
142 # Sorted either by account creation or name
143 if ( $this->creationSort ) {
144 $conds[] = 'user_id >= ' . intval( User::idFromName( $this->requestedUser ) );
145 } else {
146 $conds[] = 'user_name >= ' . $dbr->addQuotes( $this->requestedUser );
147 }
148 }
149
150 if ( $this->editsOnly ) {
151 $conds[] = 'user_editcount > 0';
152 }
153
154 $options['GROUP BY'] = $this->creationSort ? 'user_id' : 'user_name';
155
156 $query = [
157 'tables' => [ 'user', 'user_groups', 'ipblocks' ],
158 'fields' => [
159 'user_name' => $this->creationSort ? 'MAX(user_name)' : 'user_name',
160 'user_id' => $this->creationSort ? 'user_id' : 'MAX(user_id)',
161 'edits' => 'MAX(user_editcount)',
162 'creation' => 'MIN(user_registration)',
163 'ipb_deleted' => 'MAX(ipb_deleted)', // block/hide status
164 'ipb_sitewide' => 'MAX(ipb_sitewide)'
165 ],
166 'options' => $options,
167 'join_conds' => [
168 'user_groups' => [ 'LEFT JOIN', 'user_id=ug_user' ],
169 'ipblocks' => [
170 'LEFT JOIN', [
171 'user_id=ipb_user',
172 'ipb_auto' => 0
173 ]
174 ],
175 ],
176 'conds' => $conds
177 ];
178
179 Hooks::run( 'SpecialListusersQueryInfo', [ $this, &$query ] );
180
181 return $query;
182 }
183
184 /**
185 * @param stdClass $row
186 * @return string
187 */
188 function formatRow( $row ) {
189 if ( $row->user_id == 0 ) { # T18487
190 return '';
191 }
192
193 $userName = $row->user_name;
194
195 $ulinks = Linker::userLink( $row->user_id, $userName );
196 $ulinks .= Linker::userToolLinksRedContribs(
197 $row->user_id,
198 $userName,
199 (int)$row->edits,
200 // don't render parentheses in HTML markup (CSS will provide)
201 false
202 );
203
204 $lang = $this->getLanguage();
205
206 $groups = '';
207 $ugms = self::getGroupMemberships( intval( $row->user_id ), $this->userGroupCache );
208
209 if ( !$this->including && count( $ugms ) > 0 ) {
210 $list = [];
211 foreach ( $ugms as $ugm ) {
212 $list[] = $this->buildGroupLink( $ugm, $userName );
213 }
214 $groups = $lang->commaList( $list );
215 }
216
217 $item = $lang->specialList( $ulinks, $groups );
218
219 if ( $row->ipb_deleted ) {
220 $item = "<span class=\"deleted\">$item</span>";
221 }
222
223 $edits = '';
224 if ( !$this->including && $this->getConfig()->get( 'Edititis' ) ) {
225 $count = $this->msg( 'usereditcount' )->numParams( $row->edits )->escaped();
226 $edits = $this->msg( 'word-separator' )->escaped() . $this->msg( 'brackets', $count )->escaped();
227 }
228
229 $created = '';
230 # Some rows may be null
231 if ( !$this->including && $row->creation ) {
232 $user = $this->getUser();
233 $d = $lang->userDate( $row->creation, $user );
234 $t = $lang->userTime( $row->creation, $user );
235 $created = $this->msg( 'usercreated', $d, $t, $row->user_name )->escaped();
236 $created = ' ' . $this->msg( 'parentheses' )->rawParams( $created )->escaped();
237 }
238
239 $blocked = !is_null( $row->ipb_deleted ) && $row->ipb_sitewide === '1' ?
240 ' ' . $this->msg( 'listusers-blocked', $userName )->escaped() :
241 '';
242
243 Hooks::run( 'SpecialListusersFormatRow', [ &$item, $row ] );
244
245 return Html::rawElement( 'li', [], "{$item}{$edits}{$created}{$blocked}" );
246 }
247
248 protected function doBatchLookups() {
249 $batch = new LinkBatch();
250 $userIds = [];
251 # Give some pointers to make user links
252 foreach ( $this->mResult as $row ) {
253 $batch->add( NS_USER, $row->user_name );
254 $batch->add( NS_USER_TALK, $row->user_name );
255 $userIds[] = $row->user_id;
256 }
257
258 // Lookup groups for all the users
259 $dbr = wfGetDB( DB_REPLICA );
260 $groupRes = $dbr->select(
261 'user_groups',
262 UserGroupMembership::selectFields(),
263 [ 'ug_user' => $userIds ],
264 __METHOD__
265 );
266 $cache = [];
267 $groups = [];
268 foreach ( $groupRes as $row ) {
269 $ugm = UserGroupMembership::newFromRow( $row );
270 if ( !$ugm->isExpired() ) {
271 $cache[$row->ug_user][$row->ug_group] = $ugm;
272 $groups[$row->ug_group] = true;
273 }
274 }
275
276 // Give extensions a chance to add things like global user group data
277 // into the cache array to ensure proper output later on
278 Hooks::run( 'UsersPagerDoBatchLookups', [ $dbr, $userIds, &$cache, &$groups ] );
279
280 $this->userGroupCache = $cache;
281
282 // Add page of groups to link batch
283 foreach ( $groups as $group => $unused ) {
284 $groupPage = UserGroupMembership::getGroupPage( $group );
285 if ( $groupPage ) {
286 $batch->addObj( $groupPage );
287 }
288 }
289
290 $batch->execute();
291 $this->mResult->rewind();
292 }
293
294 /**
295 * @return string
296 */
297 function getPageHeader() {
298 $self = explode( '/', $this->getTitle()->getPrefixedDBkey(), 2 )[0];
299
300 $groupOptions = [ $this->msg( 'group-all' )->text() => '' ];
301 foreach ( $this->getAllGroups() as $group => $groupText ) {
302 $groupOptions[ $groupText ] = $group;
303 }
304
305 $formDescriptor = [
306 'user' => [
307 'class' => HTMLUserTextField::class,
308 'label' => $this->msg( 'listusersfrom' )->text(),
309 'name' => 'username',
310 'default' => $this->requestedUser,
311 ],
312 'dropdown' => [
313 'label' => $this->msg( 'group' )->text(),
314 'name' => 'group',
315 'default' => $this->requestedGroup,
316 'class' => HTMLSelectField::class,
317 'options' => $groupOptions,
318 ],
319 'editsOnly' => [
320 'type' => 'check',
321 'label' => $this->msg( 'listusers-editsonly' )->text(),
322 'name' => 'editsOnly',
323 'id' => 'editsOnly',
324 'default' => $this->editsOnly
325 ],
326 'temporaryGroupsOnly' => [
327 'type' => 'check',
328 'label' => $this->msg( 'listusers-temporarygroupsonly' )->text(),
329 'name' => 'temporaryGroupsOnly',
330 'id' => 'temporaryGroupsOnly',
331 'default' => $this->temporaryGroupsOnly
332 ],
333 'creationSort' => [
334 'type' => 'check',
335 'label' => $this->msg( 'listusers-creationsort' )->text(),
336 'name' => 'creationSort',
337 'id' => 'creationSort',
338 'default' => $this->creationSort
339 ],
340 'desc' => [
341 'type' => 'check',
342 'label' => $this->msg( 'listusers-desc' )->text(),
343 'name' => 'desc',
344 'id' => 'desc',
345 'default' => $this->mDefaultDirection
346 ],
347 'limithiddenfield' => [
348 'class' => HTMLHiddenField::class,
349 'name' => 'limit',
350 'default' => $this->mLimit
351 ]
352 ];
353
354 $beforeSubmitButtonHookOut = '';
355 Hooks::run( 'SpecialListusersHeaderForm', [ $this, &$beforeSubmitButtonHookOut ] );
356
357 if ( $beforeSubmitButtonHookOut !== '' ) {
358 $formDescriptor[ 'beforeSubmitButtonHookOut' ] = [
359 'class' => HTMLInfoField::class,
360 'raw' => true,
361 'default' => $beforeSubmitButtonHookOut
362 ];
363 }
364
365 $formDescriptor[ 'submit' ] = [
366 'class' => HTMLSubmitField::class,
367 'buttonlabel-message' => 'listusers-submit',
368 ];
369
370 $beforeClosingFieldsetHookOut = '';
371 Hooks::run( 'SpecialListusersHeader', [ $this, &$beforeClosingFieldsetHookOut ] );
372
373 if ( $beforeClosingFieldsetHookOut !== '' ) {
374 $formDescriptor[ 'beforeClosingFieldsetHookOut' ] = [
375 'class' => HTMLInfoField::class,
376 'raw' => true,
377 'default' => $beforeClosingFieldsetHookOut
378 ];
379 }
380
381 $htmlForm = HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() );
382 $htmlForm
383 ->setMethod( 'get' )
384 ->setAction( Title::newFromText( $self )->getLocalURL() )
385 ->setId( 'mw-listusers-form' )
386 ->setFormIdentifier( 'mw-listusers-form' )
387 ->suppressDefaultSubmit()
388 ->setWrapperLegendMsg( 'listusers' );
389 return $htmlForm->prepareForm()->getHTML( true );
390 }
391
392 /**
393 * Get a list of all explicit groups
394 * @return array
395 */
396 function getAllGroups() {
397 $result = [];
398 foreach ( User::getAllGroups() as $group ) {
399 $result[$group] = UserGroupMembership::getGroupName( $group );
400 }
401 asort( $result );
402
403 return $result;
404 }
405
406 /**
407 * Preserve group and username offset parameters when paging
408 * @return array
409 */
410 function getDefaultQuery() {
411 $query = parent::getDefaultQuery();
412 if ( $this->requestedGroup != '' ) {
413 $query['group'] = $this->requestedGroup;
414 }
415 if ( $this->requestedUser != '' ) {
416 $query['username'] = $this->requestedUser;
417 }
418 Hooks::run( 'SpecialListusersDefaultQuery', [ $this, &$query ] );
419
420 return $query;
421 }
422
423 /**
424 * Get an associative array containing groups the specified user belongs to,
425 * and the relevant UserGroupMembership objects
426 *
427 * @param int $uid User id
428 * @param array[]|null $cache
429 * @return UserGroupMembership[] (group name => UserGroupMembership object)
430 */
431 protected static function getGroupMemberships( $uid, $cache = null ) {
432 if ( $cache === null ) {
433 $user = User::newFromId( $uid );
434 return $user->getGroupMemberships();
435 } else {
436 return $cache[$uid] ?? [];
437 }
438 }
439
440 /**
441 * Format a link to a group description page
442 *
443 * @param string|UserGroupMembership $group Group name or UserGroupMembership object
444 * @param string $username
445 * @return string
446 */
447 protected function buildGroupLink( $group, $username ) {
448 return UserGroupMembership::getLink( $group, $this->getContext(), 'html', $username );
449 }
450 }