MediaWiki  1.28.0
UsersPager.php
Go to the documentation of this file.
1 <?php
33 class UsersPager extends AlphabeticPager {
34 
38  protected $userGroupCache;
39 
46  function __construct( IContextSource $context = null, $par = null, $including = null ) {
47  if ( $context ) {
48  $this->setContext( $context );
49  }
50 
51  $request = $this->getRequest();
52  $par = ( $par !== null ) ? $par : '';
53  $parms = explode( '/', $par );
54  $symsForAll = [ '*', 'user' ];
55 
56  if ( $parms[0] != '' &&
57  ( in_array( $par, User::getAllGroups() ) || in_array( $par, $symsForAll ) )
58  ) {
59  $this->requestedGroup = $par;
60  $un = $request->getText( 'username' );
61  } elseif ( count( $parms ) == 2 ) {
62  $this->requestedGroup = $parms[0];
63  $un = $parms[1];
64  } else {
65  $this->requestedGroup = $request->getVal( 'group' );
66  $un = ( $par != '' ) ? $par : $request->getText( 'username' );
67  }
68 
69  if ( in_array( $this->requestedGroup, $symsForAll ) ) {
70  $this->requestedGroup = '';
71  }
72  $this->editsOnly = $request->getBool( 'editsOnly' );
73  $this->creationSort = $request->getBool( 'creationSort' );
74  $this->including = $including;
75  $this->mDefaultDirection = $request->getBool( 'desc' )
78 
79  $this->requestedUser = '';
80 
81  if ( $un != '' ) {
83 
84  if ( !is_null( $username ) ) {
85  $this->requestedUser = $username->getText();
86  }
87  }
88 
89  parent::__construct();
90  }
91 
95  function getIndexField() {
96  return $this->creationSort ? 'user_id' : 'user_name';
97  }
98 
102  function getQueryInfo() {
103  $dbr = wfGetDB( DB_REPLICA );
104  $conds = [];
105 
106  // Don't show hidden names
107  if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
108  $conds[] = 'ipb_deleted IS NULL OR ipb_deleted = 0';
109  }
110 
111  $options = [];
112 
113  if ( $this->requestedGroup != '' ) {
114  $conds['ug_group'] = $this->requestedGroup;
115  }
116 
117  if ( $this->requestedUser != '' ) {
118  # Sorted either by account creation or name
119  if ( $this->creationSort ) {
120  $conds[] = 'user_id >= ' . intval( User::idFromName( $this->requestedUser ) );
121  } else {
122  $conds[] = 'user_name >= ' . $dbr->addQuotes( $this->requestedUser );
123  }
124  }
125 
126  if ( $this->editsOnly ) {
127  $conds[] = 'user_editcount > 0';
128  }
129 
130  $options['GROUP BY'] = $this->creationSort ? 'user_id' : 'user_name';
131 
132  $query = [
133  'tables' => [ 'user', 'user_groups', 'ipblocks' ],
134  'fields' => [
135  'user_name' => $this->creationSort ? 'MAX(user_name)' : 'user_name',
136  'user_id' => $this->creationSort ? 'user_id' : 'MAX(user_id)',
137  'edits' => 'MAX(user_editcount)',
138  'creation' => 'MIN(user_registration)',
139  'ipb_deleted' => 'MAX(ipb_deleted)' // block/hide status
140  ],
141  'options' => $options,
142  'join_conds' => [
143  'user_groups' => [ 'LEFT JOIN', 'user_id=ug_user' ],
144  'ipblocks' => [
145  'LEFT JOIN', [
146  'user_id=ipb_user',
147  'ipb_auto' => 0
148  ]
149  ],
150  ],
151  'conds' => $conds
152  ];
153 
154  Hooks::run( 'SpecialListusersQueryInfo', [ $this, &$query ] );
155 
156  return $query;
157  }
158 
163  function formatRow( $row ) {
164  if ( $row->user_id == 0 ) { # Bug 16487
165  return '';
166  }
167 
168  $userName = $row->user_name;
169 
170  $ulinks = Linker::userLink( $row->user_id, $userName );
172  $row->user_id,
173  $userName,
174  (int)$row->edits
175  );
176 
177  $lang = $this->getLanguage();
178 
179  $groups = '';
180  $groups_list = self::getGroups( intval( $row->user_id ), $this->userGroupCache );
181 
182  if ( !$this->including && count( $groups_list ) > 0 ) {
183  $list = [];
184  foreach ( $groups_list as $group ) {
185  $list[] = self::buildGroupLink( $group, $userName );
186  }
187  $groups = $lang->commaList( $list );
188  }
189 
190  $item = $lang->specialList( $ulinks, $groups );
191 
192  if ( $row->ipb_deleted ) {
193  $item = "<span class=\"deleted\">$item</span>";
194  }
195 
196  $edits = '';
197  if ( !$this->including && $this->getConfig()->get( 'Edititis' ) ) {
198  $count = $this->msg( 'usereditcount' )->numParams( $row->edits )->escaped();
199  $edits = $this->msg( 'word-separator' )->escaped() . $this->msg( 'brackets', $count )->escaped();
200  }
201 
202  $created = '';
203  # Some rows may be null
204  if ( !$this->including && $row->creation ) {
205  $user = $this->getUser();
206  $d = $lang->userDate( $row->creation, $user );
207  $t = $lang->userTime( $row->creation, $user );
208  $created = $this->msg( 'usercreated', $d, $t, $row->user_name )->escaped();
209  $created = ' ' . $this->msg( 'parentheses' )->rawParams( $created )->escaped();
210  }
211  $blocked = !is_null( $row->ipb_deleted ) ?
212  ' ' . $this->msg( 'listusers-blocked', $userName )->escaped() :
213  '';
214 
215  Hooks::run( 'SpecialListusersFormatRow', [ &$item, $row ] );
216 
217  return Html::rawElement( 'li', [], "{$item}{$edits}{$created}{$blocked}" );
218  }
219 
220  function doBatchLookups() {
221  $batch = new LinkBatch();
222  $userIds = [];
223  # Give some pointers to make user links
224  foreach ( $this->mResult as $row ) {
225  $batch->add( NS_USER, $row->user_name );
226  $batch->add( NS_USER_TALK, $row->user_name );
227  $userIds[] = $row->user_id;
228  }
229 
230  // Lookup groups for all the users
231  $dbr = wfGetDB( DB_REPLICA );
232  $groupRes = $dbr->select(
233  'user_groups',
234  [ 'ug_user', 'ug_group' ],
235  [ 'ug_user' => $userIds ],
236  __METHOD__
237  );
238  $cache = [];
239  $groups = [];
240  foreach ( $groupRes as $row ) {
241  $cache[intval( $row->ug_user )][] = $row->ug_group;
242  $groups[$row->ug_group] = true;
243  }
244 
245  // Give extensions a chance to add things like global user group data
246  // into the cache array to ensure proper output later on
247  Hooks::run( 'UsersPagerDoBatchLookups', [ $dbr, $userIds, &$cache, &$groups ] );
248 
249  $this->userGroupCache = $cache;
250 
251  // Add page of groups to link batch
252  foreach ( $groups as $group => $unused ) {
253  $groupPage = User::getGroupPage( $group );
254  if ( $groupPage ) {
255  $batch->addObj( $groupPage );
256  }
257  }
258 
259  $batch->execute();
260  $this->mResult->rewind();
261  }
262 
266  function getPageHeader() {
267  list( $self ) = explode( '/', $this->getTitle()->getPrefixedDBkey() );
268 
269  $this->getOutput()->addModules( 'mediawiki.userSuggest' );
270 
271  # Form tag
273  'form',
274  [ 'method' => 'get', 'action' => wfScript(), 'id' => 'mw-listusers-form' ]
275  ) .
276  Xml::fieldset( $this->msg( 'listusers' )->text() ) .
277  Html::hidden( 'title', $self );
278 
279  # Username field (with autocompletion support)
280  $out .= Xml::label( $this->msg( 'listusersfrom' )->text(), 'offset' ) . ' ' .
281  Html::input(
282  'username',
283  $this->requestedUser,
284  'text',
285  [
286  'class' => 'mw-autocomplete-user',
287  'id' => 'offset',
288  'size' => 20,
289  'autofocus' => $this->requestedUser === ''
290  ]
291  ) . ' ';
292 
293  # Group drop-down list
294  $sel = new XmlSelect( 'group', 'group', $this->requestedGroup );
295  $sel->addOption( $this->msg( 'group-all' )->text(), '' );
296  foreach ( $this->getAllGroups() as $group => $groupText ) {
297  $sel->addOption( $groupText, $group );
298  }
299 
300  $out .= Xml::label( $this->msg( 'group' )->text(), 'group' ) . ' ';
301  $out .= $sel->getHTML() . '<br />';
303  $this->msg( 'listusers-editsonly' )->text(),
304  'editsOnly',
305  'editsOnly',
306  $this->editsOnly
307  );
308  $out .= '&#160;';
310  $this->msg( 'listusers-creationsort' )->text(),
311  'creationSort',
312  'creationSort',
313  $this->creationSort
314  );
315  $out .= '&#160;';
317  $this->msg( 'listusers-desc' )->text(),
318  'desc',
319  'desc',
320  $this->mDefaultDirection
321  );
322  $out .= '<br />';
323 
324  Hooks::run( 'SpecialListusersHeaderForm', [ $this, &$out ] );
325 
326  # Submit button and form bottom
327  $out .= Html::hidden( 'limit', $this->mLimit );
328  $out .= Xml::submitButton( $this->msg( 'listusers-submit' )->text() );
329  Hooks::run( 'SpecialListusersHeader', [ $this, &$out ] );
330  $out .= Xml::closeElement( 'fieldset' ) .
331  Xml::closeElement( 'form' );
332 
333  return $out;
334  }
335 
340  function getAllGroups() {
341  $result = [];
342  foreach ( User::getAllGroups() as $group ) {
343  $result[$group] = User::getGroupName( $group );
344  }
345  asort( $result );
346 
347  return $result;
348  }
349 
354  function getDefaultQuery() {
355  $query = parent::getDefaultQuery();
356  if ( $this->requestedGroup != '' ) {
357  $query['group'] = $this->requestedGroup;
358  }
359  if ( $this->requestedUser != '' ) {
360  $query['username'] = $this->requestedUser;
361  }
362  Hooks::run( 'SpecialListusersDefaultQuery', [ $this, &$query ] );
363 
364  return $query;
365  }
366 
374  protected static function getGroups( $uid, $cache = null ) {
375  if ( $cache === null ) {
376  $user = User::newFromId( $uid );
377  $effectiveGroups = $user->getEffectiveGroups();
378  } else {
379  $effectiveGroups = isset( $cache[$uid] ) ? $cache[$uid] : [];
380  }
381  $groups = array_diff( $effectiveGroups, User::getImplicitGroups() );
382 
383  return $groups;
384  }
385 
393  protected static function buildGroupLink( $group, $username ) {
395  $group,
397  );
398  }
399 
400 }
const DIR_DESCENDING
Definition: IndexPager.php:73
setContext(IContextSource $context)
Set the IContextSource object.
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
Interface for objects which can provide a MediaWiki context on request.
wfGetDB($db, $groups=[], $wiki=false)
Get a Database object.
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
Definition: hooks.txt:802
within a display generated by the Derivative if and wherever such third party notices normally appear The contents of the NOTICE file are for informational purposes only and do not modify the License You may add Your own attribution notices within Derivative Works that You alongside or as an addendum to the NOTICE text from the provided that such additional attribution notices cannot be construed as modifying the License You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for or distribution of Your or for any such Derivative Works as a provided Your and distribution of the Work otherwise complies with the conditions stated in this License Submission of Contributions Unless You explicitly state any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this without any additional terms or conditions Notwithstanding the nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions Trademarks This License does not grant permission to use the trade service or product names of the except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file Disclaimer of Warranty Unless required by applicable law or agreed to in Licensor provides the WITHOUT WARRANTIES OR CONDITIONS OF ANY either express or including
null for the local wiki Added should default to null in handler for backwards compatibility add a value to it if you want to add a cookie that have to vary cache options can modify $query
Definition: hooks.txt:1555
getLanguage()
Get the Language object.
This class is used to get a list of user.
Definition: UsersPager.php:33
wfScript($script= 'index')
Get the path to a specified script file, respecting file extensions; this is a wrapper around $wgScri...
static getImplicitGroups()
Get a list of implicit groups.
Definition: User.php:4746
static rawElement($element, $attribs=[], $contents= '')
Returns an HTML element in a string.
Definition: Html.php:209
if(!isset($args[0])) $lang
IndexPager with an alphabetic list and a formatted navigation bar.
static hidden($name, $value, array $attribs=[])
Convenience function to produce an input element with type=hidden.
Definition: Html.php:758
Class for generating HTML " element.
Definition: Html.php:675
static idFromName($name, $flags=self::READ_NORMAL)
Get database id given a user name.
Definition: User.php:728
$count
static checkLabel($label, $name, $id, $checked=false, $attribs=[])
Convenience function to build an HTML checkbox with a label.
Definition: Xml.php:420
const DB_REPLICA
Definition: defines.php:22
const NS_USER_TALK
Definition: Defines.php:59
const DIR_ASCENDING
Constants for the $mDefaultDirection field.
Definition: IndexPager.php:72
static getGroups($uid, $cache=null)
Get a list of groups the specified user belongs to.
Definition: UsersPager.php:374
getUser()
Get the User object.
formatRow($row)
Definition: UsersPager.php:163
getOutput()
Get the OutputPage object.