MediaWiki  master
ActiveUsersPager.php
Go to the documentation of this file.
1 <?php
27 
36 
40  protected $opts;
41 
45  protected $groups;
46 
51 
53  private $RCMaxAge;
54 
56  private $excludegroups;
57 
67  public function __construct(
71  HookContainer $hookContainer,
73  ILoadBalancer $loadBalancer,
75  ) {
76  parent::__construct(
77  $context,
78  null,
79  null,
81  $hookContainer,
83  $loadBalancer,
85  );
86 
87  $this->RCMaxAge = $this->getConfig()->get( 'ActiveUserDays' );
88  $this->requestedUser = '';
89 
90  $un = $opts->getValue( 'username' );
91  if ( $un != '' ) {
92  $username = Title::makeTitleSafe( NS_USER, $un );
93  if ( $username !== null ) {
94  $this->requestedUser = $username->getText();
95  }
96  }
97 
98  $this->groups = $opts->getValue( 'groups' );
99  $this->excludegroups = $opts->getValue( 'excludegroups' );
100  // Backwards-compatibility with old URLs
101  if ( $opts->getValue( 'hidebots' ) ) {
102  $this->excludegroups[] = 'bot';
103  }
104  if ( $opts->getValue( 'hidesysops' ) ) {
105  $this->excludegroups[] = 'sysop';
106  }
107  }
108 
109  public function getIndexField() {
110  return 'qcc_title';
111  }
112 
113  public function getQueryInfo( $data = null ) {
114  $dbr = $this->getDatabase();
115 
116  $activeUserSeconds = $this->getConfig()->get( 'ActiveUserDays' ) * 86400;
117  $timestamp = $dbr->timestamp( wfTimestamp( TS_UNIX ) - $activeUserSeconds );
118  $fname = __METHOD__ . ' (' . $this->getSqlComment() . ')';
119 
120  // Inner subselect to pull the active users out of querycachetwo
121  $tables = [ 'querycachetwo', 'user', 'actor' ];
122  $fields = [ 'qcc_title', 'user_id', 'actor_id' ];
123  $jconds = [
124  'user' => [ 'JOIN', 'user_name = qcc_title' ],
125  'actor' => [ 'JOIN', 'actor_user = user_id' ],
126  ];
127  $conds = [
128  'qcc_type' => 'activeusers',
129  'qcc_namespace' => NS_USER,
130  ];
131  $options = [];
132  if ( $data !== null ) {
133  $options['ORDER BY'] = 'qcc_title ' . $data['order'];
134  $options['LIMIT'] = $data['limit'];
135  $conds = array_merge( $conds, $data['conds'] );
136  }
137  if ( $this->requestedUser != '' ) {
138  $conds[] = 'qcc_title >= ' . $dbr->addQuotes( $this->requestedUser );
139  }
140  if ( $this->groups !== [] ) {
141  $tables['ug1'] = 'user_groups';
142  $jconds['ug1'] = [ 'JOIN', 'ug1.ug_user = user_id' ];
143  $conds['ug1.ug_group'] = $this->groups;
144  $conds[] = 'ug1.ug_expiry IS NULL OR ug1.ug_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() );
145  }
146  if ( $this->excludegroups !== [] ) {
147  $tables['ug2'] = 'user_groups';
148  $jconds['ug2'] = [ 'LEFT JOIN', [
149  'ug2.ug_user = user_id',
150  'ug2.ug_group' => $this->excludegroups,
151  'ug2.ug_expiry IS NULL OR ug2.ug_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() ),
152  ] ];
153  $conds['ug2.ug_user'] = null;
154  }
155  if ( !$this->canSeeHideuser() ) {
156  $conds[] = 'NOT EXISTS (' . $dbr->selectSQLText(
157  'ipblocks', '1', [ 'ipb_user=user_id', 'ipb_deleted' => 1 ], __METHOD__
158  ) . ')';
159  }
160  $subquery = $dbr->buildSelectSubquery( $tables, $fields, $conds, $fname, $options, $jconds );
161 
162  // Outer query to select the recent edit counts for the selected active users
163  $tables = [ 'qcc_users' => $subquery, 'recentchanges' ];
164  $jconds = [ 'recentchanges' => [ 'LEFT JOIN', [
165  'rc_actor = actor_id',
166  'rc_type != ' . $dbr->addQuotes( RC_EXTERNAL ), // Don't count wikidata.
167  'rc_type != ' . $dbr->addQuotes( RC_CATEGORIZE ), // Don't count categorization changes.
168  'rc_log_type IS NULL OR rc_log_type != ' . $dbr->addQuotes( 'newusers' ),
169  'rc_timestamp >= ' . $dbr->addQuotes( $timestamp ),
170  ] ] ];
171  $conds = [];
172 
173  return [
174  'tables' => $tables,
175  'fields' => [
176  'qcc_title',
177  'user_name' => 'qcc_title',
178  'user_id' => 'user_id',
179  'recentedits' => 'COUNT(rc_id)'
180  ],
181  'options' => [ 'GROUP BY' => [ 'qcc_title', 'user_id' ] ],
182  'conds' => $conds,
183  'join_conds' => $jconds,
184  ];
185  }
186 
187  protected function buildQueryInfo( $offset, $limit, $order ) {
188  $fname = __METHOD__ . ' (' . $this->getSqlComment() . ')';
189 
190  $sortColumns = array_merge( [ $this->mIndexField ], $this->mExtraSortFields );
191  if ( $order === self::QUERY_ASCENDING ) {
192  $dir = 'ASC';
193  $orderBy = $sortColumns;
194  $operator = $this->mIncludeOffset ? '>=' : '>';
195  } else {
196  $dir = 'DESC';
197  $orderBy = [];
198  foreach ( $sortColumns as $col ) {
199  $orderBy[] = $col . ' DESC';
200  }
201  $operator = $this->mIncludeOffset ? '<=' : '<';
202  }
203  $info = $this->getQueryInfo( [
204  'limit' => intval( $limit ),
205  'order' => $dir,
206  'conds' =>
207  $offset != '' ? [ $this->mIndexField . $operator . $this->getDatabase()->addQuotes( $offset ) ] : [],
208  ] );
209 
210  $tables = $info['tables'];
211  $fields = $info['fields'];
212  $conds = $info['conds'];
213  $options = $info['options'];
214  $join_conds = $info['join_conds'];
215  $options['ORDER BY'] = $orderBy;
216  return [ $tables, $fields, $conds, $fname, $options, $join_conds ];
217  }
218 
219  protected function doBatchLookups() {
220  parent::doBatchLookups();
221 
222  $uids = [];
223  foreach ( $this->mResult as $row ) {
224  $uids[] = $row->user_id;
225  }
226  // Fetch the block status of the user for showing "(blocked)" text and for
227  // striking out names of suppressed users when privileged user views the list.
228  // Although the first query already hits the block table for un-privileged, this
229  // is done in two queries to avoid huge quicksorts and to make COUNT(*) correct.
230  $dbr = $this->getDatabase();
231  $res = $dbr->select( 'ipblocks',
232  [ 'ipb_user', 'deleted' => 'MAX(ipb_deleted)', 'sitewide' => 'MAX(ipb_sitewide)' ],
233  [ 'ipb_user' => $uids ],
234  __METHOD__,
235  [ 'GROUP BY' => [ 'ipb_user' ] ]
236  );
237  $this->blockStatusByUid = [];
238  foreach ( $res as $row ) {
239  $this->blockStatusByUid[$row->ipb_user] = [
240  'deleted' => $row->deleted,
241  'sitewide' => $row->sitewide,
242  ];
243  }
244  $this->mResult->seek( 0 );
245  }
246 
247  public function formatRow( $row ) {
248  $userName = $row->user_name;
249 
250  $ulinks = Linker::userLink( $row->user_id, $userName );
251  $ulinks .= Linker::userToolLinks(
252  $row->user_id,
253  $userName,
254  // Should the contributions link be red if the user has no edits (using default)
255  false,
256  // Customisation flags (using default 0)
257  0,
258  // User edit count (using default)
259  null,
260  // do not wrap the message in parentheses (CSS will provide these)
261  false
262  );
263 
264  $lang = $this->getLanguage();
265 
266  $list = [];
267 
268  $ugms = self::getGroupMemberships( intval( $row->user_id ), $this->userGroupCache );
269  foreach ( $ugms as $ugm ) {
270  $list[] = $this->buildGroupLink( $ugm, $userName );
271  }
272 
273  $groups = $lang->commaList( $list );
274 
275  $item = $lang->specialList( $ulinks, $groups );
276 
277  // If there is a block, 'deleted' and 'sitewide' are both set on
278  // $this->blockStatusByUid[$row->user_id].
279  $blocked = '';
280  $isBlocked = isset( $this->blockStatusByUid[$row->user_id] );
281  if ( $isBlocked ) {
282  if ( $this->blockStatusByUid[$row->user_id]['deleted'] == 1 ) {
283  $item = "<span class=\"deleted\">$item</span>";
284  }
285  if ( $this->blockStatusByUid[$row->user_id]['sitewide'] == 1 ) {
286  $blocked = ' ' . $this->msg( 'listusers-blocked', $userName )->escaped();
287  }
288  }
289  $count = $this->msg( 'activeusers-count' )->numParams( $row->recentedits )
290  ->params( $userName )->numParams( $this->RCMaxAge )->escaped();
291 
292  return Html::rawElement( 'li', [], "{$item} [{$count}]{$blocked}" );
293  }
294 
295 }
ContextSource\$context
IContextSource $context
Definition: ContextSource.php:38
ContextSource\getConfig
getConfig()
Definition: ContextSource.php:71
Linker\userLink
static userLink( $userId, $userName, $altUserName=false)
Make user link (or user contributions for unregistered users)
Definition: Linker.php:895
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:37
FormOptions\getValue
getValue( $name)
Get the value for the given option name.
Definition: FormOptions.php:182
ActiveUsersPager\buildQueryInfo
buildQueryInfo( $offset, $limit, $order)
Build variables to use by the database wrapper.
Definition: ActiveUsersPager.php:187
UsersPager\buildGroupLink
buildGroupLink( $group, $username)
Format a link to a group description page.
Definition: UsersPager.php:492
UsersPager\$linkBatchFactory
LinkBatchFactory $linkBatchFactory
Definition: UsersPager.php:66
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1831
Linker\userToolLinks
static userToolLinks( $userId, $userText, $redContribsWhenNoEdits=false, $flags=0, $edits=null, $useParentheses=true)
Generate standard user tool links (talk, contributions, block link, etc.)
Definition: Linker.php:940
ActiveUsersPager\$blockStatusByUid
array $blockStatusByUid
Definition: ActiveUsersPager.php:50
IndexPager\getDatabase
getDatabase()
Get the Database object in use.
Definition: IndexPager.php:244
ActiveUsersPager\getQueryInfo
getQueryInfo( $data=null)
Definition: ActiveUsersPager.php:113
ActiveUsersPager\$excludegroups
string[] $excludegroups
Definition: ActiveUsersPager.php:56
$res
$res
Definition: testCompression.php:57
ActiveUsersPager\$RCMaxAge
int $RCMaxAge
Definition: ActiveUsersPager.php:53
$dbr
$dbr
Definition: testCompression.php:54
ContextSource\getLanguage
getLanguage()
Definition: ContextSource.php:151
ActiveUsersPager
This class is used to get a list of active users.
Definition: ActiveUsersPager.php:35
MediaWiki\User\UserGroupManager
Managers user groups.
Definition: UserGroupManager.php:52
MediaWiki\Cache\LinkBatchFactory
Definition: LinkBatchFactory.php:38
UsersPager
This class is used to get a list of user.
Definition: UsersPager.php:40
UsersPager\$permissionManager
PermissionManager $permissionManager
Definition: UsersPager.php:72
ActiveUsersPager\formatRow
formatRow( $row)
Definition: ActiveUsersPager.php:247
ContextSource\msg
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Definition: ContextSource.php:195
Title\makeTitleSafe
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:650
ActiveUsersPager\getIndexField
getIndexField()
Definition: ActiveUsersPager.php:109
MediaWiki\Permissions\PermissionManager
A service class for checking permissions To obtain an instance, use MediaWikiServices::getInstance()-...
Definition: PermissionManager.php:51
RC_EXTERNAL
const RC_EXTERNAL
Definition: Defines.php:128
UsersPager\$userGroupManager
UserGroupManager $userGroupManager
Definition: UsersPager.php:75
NS_USER
const NS_USER
Definition: Defines.php:65
ActiveUsersPager\$opts
FormOptions $opts
Definition: ActiveUsersPager.php:40
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:57
UsersPager\getGroupMemberships
static getGroupMemberships( $uid, $cache=null)
Get an associative array containing groups the specified user belongs to, and the relevant UserGroupM...
Definition: UsersPager.php:476
IndexPager\getSqlComment
getSqlComment()
Get some text to go in brackets in the "function name" part of the SQL comment.
Definition: IndexPager.php:429
UsersPager\getQueryInfo
getQueryInfo()
Definition: UsersPager.php:160
RC_CATEGORIZE
const RC_CATEGORIZE
Definition: Defines.php:129
Html\rawElement
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:212
ActiveUsersPager\$groups
string[] $groups
Definition: ActiveUsersPager.php:45
ActiveUsersPager\__construct
__construct(?IContextSource $context, FormOptions $opts, LinkBatchFactory $linkBatchFactory, HookContainer $hookContainer, PermissionManager $permissionManager, ILoadBalancer $loadBalancer, UserGroupManager $userGroupManager)
Definition: ActiveUsersPager.php:67
MediaWiki\HookContainer\HookContainer
HookContainer class.
Definition: HookContainer.php:45
FormOptions
Helper class to keep track of options when mixing links and form elements.
Definition: FormOptions.php:35
ActiveUsersPager\doBatchLookups
doBatchLookups()
Called from getBody(), before getStartBody() is called and after doQuery() was called.
Definition: ActiveUsersPager.php:219
Wikimedia\Rdbms\ILoadBalancer
Database cluster connection, tracking, load balancing, and transaction manager interface.
Definition: ILoadBalancer.php:81
UsersPager\canSeeHideuser
canSeeHideuser()
Definition: UsersPager.php:433