MediaWiki master
ActiveUsersPager.php
Go to the documentation of this file.
1<?php
22namespace MediaWiki\Pager;
23
39
51 protected $opts;
52
56 protected $groups;
57
61 private $blockStatusByUid;
62
64 private $RCMaxAge;
65
67 private $excludegroups;
68
69 public function __construct(
70 IContextSource $context,
71 HookContainer $hookContainer,
72 LinkBatchFactory $linkBatchFactory,
73 IConnectionProvider $dbProvider,
74 UserGroupManager $userGroupManager,
75 UserIdentityLookup $userIdentityLookup,
77 TempUserConfig $tempUserConfig,
79 ) {
80 parent::__construct(
81 $context,
82 $hookContainer,
83 $linkBatchFactory,
84 $dbProvider,
85 $userGroupManager,
86 $userIdentityLookup,
88 $tempUserConfig,
89 null,
90 null
91 );
92
93 $this->RCMaxAge = $this->getConfig()->get( MainConfigNames::ActiveUserDays );
94 $this->requestedUser = '';
95
96 $un = $opts->getValue( 'username' );
97 if ( $un != '' ) {
98 $username = Title::makeTitleSafe( NS_USER, $un );
99 if ( $username !== null ) {
100 $this->requestedUser = $username->getText();
101 }
102 }
103
104 $this->groups = $opts->getValue( 'groups' );
105 $this->excludegroups = $opts->getValue( 'excludegroups' );
106 // Backwards-compatibility with old URLs
107 if ( $opts->getValue( 'hidebots' ) ) {
108 $this->excludegroups[] = 'bot';
109 }
110 if ( $opts->getValue( 'hidesysops' ) ) {
111 $this->excludegroups[] = 'sysop';
112 }
113 }
114
115 public function getIndexField() {
116 return 'qcc_title';
117 }
118
119 public function getQueryInfo( $data = null ) {
120 $dbr = $this->getDatabase();
121
122 $activeUserSeconds = $this->getConfig()->get( MainConfigNames::ActiveUserDays ) * 86400;
123 $timestamp = $dbr->timestamp( (int)wfTimestamp( TS_UNIX ) - $activeUserSeconds );
124 $fname = __METHOD__ . ' (' . $this->getSqlComment() . ')';
125
126 // Inner subselect to pull the active users out of querycachetwo
127 $subquery = $dbr->newSelectQueryBuilder()
128 ->select( [ 'qcc_title', 'user_id', 'actor_id' ] )
129 ->from( 'querycachetwo' )
130 ->join( 'user', null, 'user_name = qcc_title' )
131 ->join( 'actor', null, 'actor_user = user_id' )
132 ->where( [
133 'qcc_type' => 'activeusers',
134 'qcc_namespace' => NS_USER,
135 ] )
136 ->caller( $fname );
137 if ( $data !== null ) {
138 $subquery
139 ->orderBy( 'qcc_title', $data['order'] )
140 ->limit( $data['limit'] )
141 ->andWhere( $data['conds'] );
142 }
143 if ( $this->requestedUser != '' ) {
144 $subquery->andWhere( $dbr->expr( 'qcc_title', '>=', $this->requestedUser ) );
145 }
146 if ( $this->groups !== [] ) {
147 $subquery
148 ->join( 'user_groups', 'ug1', 'ug1.ug_user = user_id' )
149 ->andWhere( [
150 'ug1.ug_group' => $this->groups,
151 $dbr->expr( 'ug1.ug_expiry', '=', null )->or( 'ug1.ug_expiry', '>=', $dbr->timestamp() ),
152 ] );
153 }
154 if ( $this->excludegroups !== [] ) {
155 $subquery
156 ->leftJoin( 'user_groups', 'ug2', [
157 'ug2.ug_user = user_id',
158 'ug2.ug_group' => $this->excludegroups,
159 $dbr->expr( 'ug2.ug_expiry', '=', null )->or( 'ug2.ug_expiry', '>=', $dbr->timestamp() ),
160 ] )
161 ->andWhere( [ 'ug2.ug_user' => null ] );
162 }
163 if ( !$this->canSeeHideuser() ) {
164 $subquery->andWhere( $this->hideUserUtils->getExpression( $dbr ) );
165 }
166
167 // Outer query to select the recent edit counts for the selected active users
168 return [
169 'tables' => [ 'qcc_users' => new Subquery( $subquery->getSQL() ), 'recentchanges' ],
170 'fields' => [
171 'qcc_title',
172 'user_name' => 'qcc_title',
173 'user_id' => 'user_id',
174 'recentedits' => 'COUNT(DISTINCT rc_id)'
175 ],
176 'options' => [ 'GROUP BY' => [ 'qcc_title', 'user_id' ] ],
177 'conds' => [],
178 'join_conds' => [ 'recentchanges' => [ 'LEFT JOIN', [
179 'rc_actor = actor_id',
180 $dbr->expr( 'rc_type', '!=', RC_EXTERNAL ), // Don't count wikidata.
181 $dbr->expr( 'rc_type', '!=', RC_CATEGORIZE ), // Don't count categorization changes.
182 $dbr->expr( 'rc_log_type', '=', null )->or( 'rc_log_type', '!=', 'newusers' ),
183 $dbr->expr( 'rc_timestamp', '>=', $timestamp ),
184 ] ] ],
185 ];
186 }
187
188 protected function buildQueryInfo( $offset, $limit, $order ) {
189 $fname = __METHOD__ . ' (' . $this->getSqlComment() . ')';
190
191 $sortColumns = array_merge( [ $this->mIndexField ], $this->mExtraSortFields );
192 if ( $order === self::QUERY_ASCENDING ) {
193 $dir = 'ASC';
194 $orderBy = $sortColumns;
195 $operator = $this->mIncludeOffset ? '>=' : '>';
196 } else {
197 $dir = 'DESC';
198 $orderBy = [];
199 foreach ( $sortColumns as $col ) {
200 $orderBy[] = $col . ' DESC';
201 }
202 $operator = $this->mIncludeOffset ? '<=' : '<';
203 }
204 $info = $this->getQueryInfo( [
205 'limit' => intval( $limit ),
206 'order' => $dir,
207 'conds' =>
208 $offset != '' ? [ $this->getDatabase()->expr( $this->mIndexField, $operator, $offset ) ] : [],
209 ] );
210
211 $tables = $info['tables'];
212 $fields = $info['fields'];
213 $conds = $info['conds'];
214 $options = $info['options'];
215 $join_conds = $info['join_conds'];
216 $options['ORDER BY'] = $orderBy;
217 return [ $tables, $fields, $conds, $fname, $options, $join_conds ];
218 }
219
220 protected function doBatchLookups() {
221 parent::doBatchLookups();
222
223 $uids = [];
224 foreach ( $this->mResult as $row ) {
225 $uids[] = (int)$row->user_id;
226 }
227 // Fetch the block status of the user for showing "(blocked)" text and for
228 // striking out names of suppressed users when privileged user views the list.
229 // Although the first query already hits the block table for un-privileged, this
230 // is done in two queries to avoid huge quicksorts and to make COUNT(*) correct.
231 $dbr = $this->getDatabase();
232 $res = $dbr->newSelectQueryBuilder()
233 ->select( [
234 'bt_user',
235 'deleted' => 'MAX(bl_deleted)',
236 'sitewide' => 'MAX(bl_sitewide)'
237 ] )
238 ->from( 'block_target' )
239 ->join( 'block', null, 'bl_target=bt_id' )
240 ->where( [ 'bt_user' => $uids ] )
241 ->groupBy( [ 'bt_user' ] )
242 ->caller( __METHOD__ )->fetchResultSet();
243 $this->blockStatusByUid = [];
244 foreach ( $res as $row ) {
245 $this->blockStatusByUid[$row->bt_user] = [
246 'deleted' => $row->deleted,
247 'sitewide' => $row->sitewide,
248 ];
249 }
250 $this->mResult->seek( 0 );
251 }
252
253 public function formatRow( $row ) {
254 $userName = $row->user_name;
255
256 $ulinks = Linker::userLink( $row->user_id, $userName );
257 $ulinks .= Linker::userToolLinks(
258 $row->user_id,
259 $userName,
260 // Should the contributions link be red if the user has no edits (using default)
261 false,
262 // Customisation flags (using default 0)
263 0,
264 // User edit count (using default)
265 null,
266 // do not wrap the message in parentheses (CSS will provide these)
267 false
268 );
269
270 $lang = $this->getLanguage();
271
272 $list = [];
273
274 $userIdentity = new UserIdentityValue( intval( $row->user_id ), $userName );
275 $ugms = $this->getGroupMemberships( $userIdentity );
276 foreach ( $ugms as $ugm ) {
277 $list[] = $this->buildGroupLink( $ugm, $userName );
278 }
279
280 $groups = $lang->commaList( $list );
281
282 $item = $lang->specialList( $ulinks, $groups );
283
284 // If there is a block, 'deleted' and 'sitewide' are both set on
285 // $this->blockStatusByUid[$row->user_id].
286 $blocked = '';
287 $isBlocked = isset( $this->blockStatusByUid[$row->user_id] );
288 if ( $isBlocked ) {
289 if ( $this->blockStatusByUid[$row->user_id]['deleted'] == 1 ) {
290 $item = "<span class=\"deleted\">$item</span>";
291 }
292 if ( $this->blockStatusByUid[$row->user_id]['sitewide'] == 1 ) {
293 $blocked = ' ' . $this->msg( 'listusers-blocked', $userName )->escaped();
294 }
295 }
296 $count = $this->msg( 'activeusers-count' )->numParams( $row->recentedits )
297 ->params( $userName )->numParams( $this->RCMaxAge )->escaped();
298
299 return Html::rawElement( 'li', [], "{$item} [{$count}]{$blocked}" );
300 }
301
302}
303
308class_alias( ActiveUsersPager::class, 'ActiveUsersPager' );
const NS_USER
Definition Defines.php:67
const RC_EXTERNAL
Definition Defines.php:120
const RC_CATEGORIZE
Definition Defines.php:121
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Helpers for building queries that determine whether a user is hidden.
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Helper class to keep track of options when mixing links and form elements.
getValue( $name)
Get the value for the given option name.
This class is a collection of static functions that serve two purposes:
Definition Html.php:56
Some internal bits split of from Skin.php.
Definition Linker.php:62
A class containing constants representing the names of configuration variables.
const ActiveUserDays
Name constant for the ActiveUserDays setting, for use with Config::get()
This class is used to get a list of active users.
doBatchLookups()
Called from getBody(), before getStartBody() is called and after doQuery() was called.
buildQueryInfo( $offset, $limit, $order)
Build variables to use by the database wrapper.
__construct(IContextSource $context, HookContainer $hookContainer, LinkBatchFactory $linkBatchFactory, IConnectionProvider $dbProvider, UserGroupManager $userGroupManager, UserIdentityLookup $userIdentityLookup, HideUserUtils $hideUserUtils, TempUserConfig $tempUserConfig, FormOptions $opts)
getDatabase()
Get the Database object in use.
getSqlComment()
Get some text to go in brackets in the "function name" part of the SQL comment.
This class is used to get a list of user.
getGroupMemberships( $user)
Get an associative array containing groups the specified user belongs to, and the relevant UserGroupM...
buildGroupLink( $group, $username)
Format a link to a group description page.
HideUserUtils $hideUserUtils
Represents a title within MediaWiki.
Definition Title.php:78
Manage user group memberships.
Value object representing a user's identity.
Interface for objects which can provide a MediaWiki context on request.
Interface for temporary user creation config and name matching.
Service for looking up UserIdentity.
Provide primary and replica IDatabase connections.