MediaWiki  master
UsersPager.php
Go to the documentation of this file.
1 <?php
27 
35 class UsersPager extends AlphabeticPager {
36 
40  protected $userGroupCache;
41 
43  protected $requestedGroup;
44 
46  protected $editsOnly;
47 
50 
52  protected $creationSort;
53 
55  protected $including;
56 
58  protected $requestedUser;
59 
66  public function __construct( IContextSource $context = null, $par = null, $including = null ) {
67  if ( $context ) {
68  $this->setContext( $context );
69  }
70 
71  $request = $this->getRequest();
72  $par = $par ?? '';
73  $parms = explode( '/', $par );
74  $symsForAll = [ '*', 'user' ];
75 
76  if ( $parms[0] != '' &&
77  ( in_array( $par, User::getAllGroups() ) || in_array( $par, $symsForAll ) )
78  ) {
79  $this->requestedGroup = $par;
80  $un = $request->getText( 'username' );
81  } elseif ( count( $parms ) == 2 ) {
82  $this->requestedGroup = $parms[0];
83  $un = $parms[1];
84  } else {
85  $this->requestedGroup = $request->getVal( 'group' );
86  $un = ( $par != '' ) ? $par : $request->getText( 'username' );
87  }
88 
89  if ( in_array( $this->requestedGroup, $symsForAll ) ) {
90  $this->requestedGroup = '';
91  }
92  $this->editsOnly = $request->getBool( 'editsOnly' );
93  $this->temporaryGroupsOnly = $request->getBool( 'temporaryGroupsOnly' );
94  $this->creationSort = $request->getBool( 'creationSort' );
95  $this->including = $including;
96  $this->mDefaultDirection = $request->getBool( 'desc' )
99 
100  $this->requestedUser = '';
101 
102  if ( $un != '' ) {
103  $username = Title::makeTitleSafe( NS_USER, $un );
104 
105  if ( !is_null( $username ) ) {
106  $this->requestedUser = $username->getText();
107  }
108  }
109 
110  parent::__construct();
111  }
112 
116  function getIndexField() {
117  return $this->creationSort ? 'user_id' : 'user_name';
118  }
119 
123  function getQueryInfo() {
124  $dbr = wfGetDB( DB_REPLICA );
125  $conds = [];
126 
127  // Don't show hidden names
128  if ( !MediaWikiServices::getInstance()
130  ->userHasRight( $this->getUser(), 'hideuser' )
131  ) {
132  $conds[] = 'ipb_deleted IS NULL OR ipb_deleted = 0';
133  }
134 
135  $options = [];
136 
137  if ( $this->requestedGroup != '' || $this->temporaryGroupsOnly ) {
138  $conds[] = 'ug_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() ) .
139  ( !$this->temporaryGroupsOnly ? ' OR ug_expiry IS NULL' : '' );
140  }
141 
142  if ( $this->requestedGroup != '' ) {
143  $conds['ug_group'] = $this->requestedGroup;
144  }
145 
146  if ( $this->requestedUser != '' ) {
147  # Sorted either by account creation or name
148  if ( $this->creationSort ) {
149  $conds[] = 'user_id >= ' . intval( User::idFromName( $this->requestedUser ) );
150  } else {
151  $conds[] = 'user_name >= ' . $dbr->addQuotes( $this->requestedUser );
152  }
153  }
154 
155  if ( $this->editsOnly ) {
156  $conds[] = 'user_editcount > 0';
157  }
158 
159  $options['GROUP BY'] = $this->creationSort ? 'user_id' : 'user_name';
160 
161  $query = [
162  'tables' => [ 'user', 'user_groups', 'ipblocks' ],
163  'fields' => [
164  'user_name' => $this->creationSort ? 'MAX(user_name)' : 'user_name',
165  'user_id' => $this->creationSort ? 'user_id' : 'MAX(user_id)',
166  'edits' => 'MAX(user_editcount)',
167  'creation' => 'MIN(user_registration)',
168  'ipb_deleted' => 'MAX(ipb_deleted)', // block/hide status
169  'ipb_sitewide' => 'MAX(ipb_sitewide)'
170  ],
171  'options' => $options,
172  'join_conds' => [
173  'user_groups' => [ 'LEFT JOIN', 'user_id=ug_user' ],
174  'ipblocks' => [
175  'LEFT JOIN', [
176  'user_id=ipb_user',
177  'ipb_auto' => 0
178  ]
179  ],
180  ],
181  'conds' => $conds
182  ];
183 
184  Hooks::run( 'SpecialListusersQueryInfo', [ $this, &$query ] );
185 
186  return $query;
187  }
188 
193  function formatRow( $row ) {
194  if ( $row->user_id == 0 ) { # T18487
195  return '';
196  }
197 
198  $userName = $row->user_name;
199 
200  $ulinks = Linker::userLink( $row->user_id, $userName );
202  $row->user_id,
203  $userName,
204  (int)$row->edits,
205  // don't render parentheses in HTML markup (CSS will provide)
206  false
207  );
208 
209  $lang = $this->getLanguage();
210 
211  $groups = '';
212  $ugms = self::getGroupMemberships( intval( $row->user_id ), $this->userGroupCache );
213 
214  if ( !$this->including && count( $ugms ) > 0 ) {
215  $list = [];
216  foreach ( $ugms as $ugm ) {
217  $list[] = $this->buildGroupLink( $ugm, $userName );
218  }
219  $groups = $lang->commaList( $list );
220  }
221 
222  $item = $lang->specialList( $ulinks, $groups );
223 
224  if ( $row->ipb_deleted ) {
225  $item = "<span class=\"deleted\">$item</span>";
226  }
227 
228  $edits = '';
229  if ( !$this->including && $this->getConfig()->get( 'Edititis' ) ) {
230  $count = $this->msg( 'usereditcount' )->numParams( $row->edits )->escaped();
231  $edits = $this->msg( 'word-separator' )->escaped() . $this->msg( 'brackets', $count )->escaped();
232  }
233 
234  $created = '';
235  # Some rows may be null
236  if ( !$this->including && $row->creation ) {
237  $user = $this->getUser();
238  $d = $lang->userDate( $row->creation, $user );
239  $t = $lang->userTime( $row->creation, $user );
240  $created = $this->msg( 'usercreated', $d, $t, $row->user_name )->escaped();
241  $created = ' ' . $this->msg( 'parentheses' )->rawParams( $created )->escaped();
242  }
243 
244  $blocked = !is_null( $row->ipb_deleted ) && $row->ipb_sitewide === '1' ?
245  ' ' . $this->msg( 'listusers-blocked', $userName )->escaped() :
246  '';
247 
248  Hooks::run( 'SpecialListusersFormatRow', [ &$item, $row ] );
249 
250  return Html::rawElement( 'li', [], "{$item}{$edits}{$created}{$blocked}" );
251  }
252 
253  protected function doBatchLookups() {
254  $batch = new LinkBatch();
255  $userIds = [];
256  # Give some pointers to make user links
257  foreach ( $this->mResult as $row ) {
258  $batch->add( NS_USER, $row->user_name );
259  $batch->add( NS_USER_TALK, $row->user_name );
260  $userIds[] = $row->user_id;
261  }
262 
263  // Lookup groups for all the users
264  $dbr = wfGetDB( DB_REPLICA );
265  $groupRes = $dbr->select(
266  'user_groups',
268  [ 'ug_user' => $userIds ],
269  __METHOD__
270  );
271  $cache = [];
272  $groups = [];
273  foreach ( $groupRes as $row ) {
274  $ugm = UserGroupMembership::newFromRow( $row );
275  if ( !$ugm->isExpired() ) {
276  $cache[$row->ug_user][$row->ug_group] = $ugm;
277  $groups[$row->ug_group] = true;
278  }
279  }
280 
281  // Give extensions a chance to add things like global user group data
282  // into the cache array to ensure proper output later on
283  Hooks::run( 'UsersPagerDoBatchLookups', [ $dbr, $userIds, &$cache, &$groups ] );
284 
285  $this->userGroupCache = $cache;
286 
287  // Add page of groups to link batch
288  foreach ( $groups as $group => $unused ) {
289  $groupPage = UserGroupMembership::getGroupPage( $group );
290  if ( $groupPage ) {
291  $batch->addObj( $groupPage );
292  }
293  }
294 
295  $batch->execute();
296  $this->mResult->rewind();
297  }
298 
302  function getPageHeader() {
303  $self = explode( '/', $this->getTitle()->getPrefixedDBkey(), 2 )[0];
304 
305  $groupOptions = [ $this->msg( 'group-all' )->text() => '' ];
306  foreach ( $this->getAllGroups() as $group => $groupText ) {
307  $groupOptions[ $groupText ] = $group;
308  }
309 
310  $formDescriptor = [
311  'user' => [
312  'class' => HTMLUserTextField::class,
313  'label' => $this->msg( 'listusersfrom' )->text(),
314  'name' => 'username',
315  'default' => $this->requestedUser,
316  ],
317  'dropdown' => [
318  'label' => $this->msg( 'group' )->text(),
319  'name' => 'group',
320  'default' => $this->requestedGroup,
321  'class' => HTMLSelectField::class,
322  'options' => $groupOptions,
323  ],
324  'editsOnly' => [
325  'type' => 'check',
326  'label' => $this->msg( 'listusers-editsonly' )->text(),
327  'name' => 'editsOnly',
328  'id' => 'editsOnly',
329  'default' => $this->editsOnly
330  ],
331  'temporaryGroupsOnly' => [
332  'type' => 'check',
333  'label' => $this->msg( 'listusers-temporarygroupsonly' )->text(),
334  'name' => 'temporaryGroupsOnly',
335  'id' => 'temporaryGroupsOnly',
336  'default' => $this->temporaryGroupsOnly
337  ],
338  'creationSort' => [
339  'type' => 'check',
340  'label' => $this->msg( 'listusers-creationsort' )->text(),
341  'name' => 'creationSort',
342  'id' => 'creationSort',
343  'default' => $this->creationSort
344  ],
345  'desc' => [
346  'type' => 'check',
347  'label' => $this->msg( 'listusers-desc' )->text(),
348  'name' => 'desc',
349  'id' => 'desc',
350  'default' => $this->mDefaultDirection
351  ],
352  'limithiddenfield' => [
353  'class' => HTMLHiddenField::class,
354  'name' => 'limit',
355  'default' => $this->mLimit
356  ]
357  ];
358 
359  $beforeSubmitButtonHookOut = '';
360  Hooks::run( 'SpecialListusersHeaderForm', [ $this, &$beforeSubmitButtonHookOut ] );
361 
362  if ( $beforeSubmitButtonHookOut !== '' ) {
363  $formDescriptor[ 'beforeSubmitButtonHookOut' ] = [
364  'class' => HTMLInfoField::class,
365  'raw' => true,
366  'default' => $beforeSubmitButtonHookOut
367  ];
368  }
369 
370  $formDescriptor[ 'submit' ] = [
371  'class' => HTMLSubmitField::class,
372  'buttonlabel-message' => 'listusers-submit',
373  ];
374 
375  $beforeClosingFieldsetHookOut = '';
376  Hooks::run( 'SpecialListusersHeader', [ $this, &$beforeClosingFieldsetHookOut ] );
377 
378  if ( $beforeClosingFieldsetHookOut !== '' ) {
379  $formDescriptor[ 'beforeClosingFieldsetHookOut' ] = [
380  'class' => HTMLInfoField::class,
381  'raw' => true,
382  'default' => $beforeClosingFieldsetHookOut
383  ];
384  }
385 
386  $htmlForm = HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() );
387  $htmlForm
388  ->setMethod( 'get' )
389  ->setAction( Title::newFromText( $self )->getLocalURL() )
390  ->setId( 'mw-listusers-form' )
391  ->setFormIdentifier( 'mw-listusers-form' )
392  ->suppressDefaultSubmit()
393  ->setWrapperLegendMsg( 'listusers' );
394  return $htmlForm->prepareForm()->getHTML( true );
395  }
396 
401  function getAllGroups() {
402  $result = [];
403  foreach ( User::getAllGroups() as $group ) {
404  $result[$group] = UserGroupMembership::getGroupName( $group );
405  }
406  asort( $result );
407 
408  return $result;
409  }
410 
415  function getDefaultQuery() {
416  $query = parent::getDefaultQuery();
417  if ( $this->requestedGroup != '' ) {
418  $query['group'] = $this->requestedGroup;
419  }
420  if ( $this->requestedUser != '' ) {
421  $query['username'] = $this->requestedUser;
422  }
423  Hooks::run( 'SpecialListusersDefaultQuery', [ $this, &$query ] );
424 
425  return $query;
426  }
427 
436  protected static function getGroupMemberships( $uid, $cache = null ) {
437  if ( $cache === null ) {
438  $user = User::newFromId( $uid );
439  return $user->getGroupMemberships();
440  } else {
441  return $cache[$uid] ?? [];
442  }
443  }
444 
452  protected function buildGroupLink( $group, $username ) {
453  return UserGroupMembership::getLink( $group, $this->getContext(), 'html', $username );
454  }
455 }
const DIR_DESCENDING
Backwards-compatible constant for $mDefaultDirection field (do not change)
Definition: IndexPager.php:76
setContext(IContextSource $context)
string $requestedGroup
Definition: UsersPager.php:43
This class is used to get a list of user.
Definition: UsersPager.php:35
bool $editsOnly
Definition: UsersPager.php:46
static selectFields()
Returns the list of user_groups fields that should be selected to create a new user group membership...
bool $creationSort
Definition: UsersPager.php:52
bool $mDefaultDirection
$mDefaultDirection gives the direction to use when sorting results: DIR_ASCENDING or DIR_DESCENDING...
Definition: IndexPager.php:128
if(!isset( $args[0])) $lang
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:209
IndexPager with an alphabetic list and a formatted navigation bar.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
static userToolLinksRedContribs( $userId, $userText, $edits=null, $useParentheses=true)
Alias for userToolLinks( $userId, $userText, true );.
Definition: Linker.php:1024
bool null $including
Definition: UsersPager.php:55
static idFromName( $name, $flags=self::READ_NORMAL)
Get database id given a user name.
Definition: User.php:885
IContextSource $context
static getLink( $ugm, IContextSource $context, $format, $userName=null)
Gets a link for a user group, possibly including the expiry date if relevant.
static getAllGroups()
Return the set of defined explicit groups.
Definition: User.php:4879
Class representing a list of titles The execute() method checks them all for existence and adds them ...
Definition: LinkBatch.php:34
getPermissionManager()
formatRow( $row)
Definition: UsersPager.php:193
static factory( $displayFormat,... $arguments)
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:307
getDefaultQuery()
Preserve group and username offset parameters when paging.
Definition: UsersPager.php:415
$self
static getGroupName( $group)
Gets the localized friendly name for a group, if it exists.
string $requestedUser
Definition: UsersPager.php:58
getContext()
Get the base IContextSource object.
$cache
Definition: mcc.php:33
getAllGroups()
Get a list of all explicit groups.
Definition: UsersPager.php:401
bool $temporaryGroupsOnly
Definition: UsersPager.php:49
static getGroupMemberships( $uid, $cache=null)
Get an associative array containing groups the specified user belongs to, and the relevant UserGroupM...
Definition: UsersPager.php:436
static getGroupPage( $group)
Gets the title of a page describing a particular user group.
__construct(IContextSource $context=null, $par=null, $including=null)
Definition: UsersPager.php:66
static userLink( $userId, $userName, $altUserName=false)
Make user link (or user contributions for unregistered users)
Definition: Linker.php:898
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:610
static newFromId( $id)
Static factory method for creation from a given user ID.
Definition: User.php:560
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
array [] $userGroupCache
A array with user ids as key and a array of groups as value.
Definition: UsersPager.php:40
const DB_REPLICA
Definition: defines.php:25
buildGroupLink( $group, $username)
Format a link to a group description page.
Definition: UsersPager.php:452
const NS_USER_TALK
Definition: Defines.php:63
const DIR_ASCENDING
Backwards-compatible constant for $mDefaultDirection field (do not change)
Definition: IndexPager.php:74
static newFromRow( $row)
Creates a new UserGroupMembership object from a database row.
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:200
int $mLimit
The maximum number of entries to show.
Definition: IndexPager.php:92
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:317