MediaWiki master
ActiveUsersPager.php
Go to the documentation of this file.
1<?php
22namespace MediaWiki\Pager;
23
38
50 protected $opts;
51
55 protected $groups;
56
60 private $blockStatusByUid;
61
63 private $RCMaxAge;
64
66 private $excludegroups;
67
78 public function __construct(
79 IContextSource $context,
80 HookContainer $hookContainer,
81 LinkBatchFactory $linkBatchFactory,
82 IConnectionProvider $dbProvider,
83 UserGroupManager $userGroupManager,
84 UserIdentityLookup $userIdentityLookup,
87 ) {
88 parent::__construct(
89 $context,
90 $hookContainer,
91 $linkBatchFactory,
92 $dbProvider,
93 $userGroupManager,
94 $userIdentityLookup,
96 null,
97 null
98 );
99
100 $this->RCMaxAge = $this->getConfig()->get( MainConfigNames::ActiveUserDays );
101 $this->requestedUser = '';
102
103 $un = $opts->getValue( 'username' );
104 if ( $un != '' ) {
105 $username = Title::makeTitleSafe( NS_USER, $un );
106 if ( $username !== null ) {
107 $this->requestedUser = $username->getText();
108 }
109 }
110
111 $this->groups = $opts->getValue( 'groups' );
112 $this->excludegroups = $opts->getValue( 'excludegroups' );
113 // Backwards-compatibility with old URLs
114 if ( $opts->getValue( 'hidebots' ) ) {
115 $this->excludegroups[] = 'bot';
116 }
117 if ( $opts->getValue( 'hidesysops' ) ) {
118 $this->excludegroups[] = 'sysop';
119 }
120 }
121
122 public function getIndexField() {
123 return 'qcc_title';
124 }
125
126 public function getQueryInfo( $data = null ) {
127 $dbr = $this->getDatabase();
128
129 $activeUserSeconds = $this->getConfig()->get( MainConfigNames::ActiveUserDays ) * 86400;
130 $timestamp = $dbr->timestamp( (int)wfTimestamp( TS_UNIX ) - $activeUserSeconds );
131 $fname = __METHOD__ . ' (' . $this->getSqlComment() . ')';
132
133 // Inner subselect to pull the active users out of querycachetwo
134 $subquery = $dbr->newSelectQueryBuilder()
135 ->select( [ 'qcc_title', 'user_id', 'actor_id' ] )
136 ->from( 'querycachetwo' )
137 ->join( 'user', null, 'user_name = qcc_title' )
138 ->join( 'actor', null, 'actor_user = user_id' )
139 ->where( [
140 'qcc_type' => 'activeusers',
141 'qcc_namespace' => NS_USER,
142 ] )
143 ->caller( $fname );
144 if ( $data !== null ) {
145 $subquery
146 ->orderBy( 'qcc_title', $data['order'] )
147 ->limit( $data['limit'] )
148 ->andWhere( $data['conds'] );
149 }
150 if ( $this->requestedUser != '' ) {
151 $subquery->andWhere( $dbr->expr( 'qcc_title', '>=', $this->requestedUser ) );
152 }
153 if ( $this->groups !== [] ) {
154 $subquery
155 ->join( 'user_groups', 'ug1', 'ug1.ug_user = user_id' )
156 ->andWhere( [
157 'ug1.ug_group' => $this->groups,
158 $dbr->expr( 'ug1.ug_expiry', '=', null )->or( 'ug1.ug_expiry', '>=', $dbr->timestamp() ),
159 ] );
160 }
161 if ( $this->excludegroups !== [] ) {
162 $subquery
163 ->leftJoin( 'user_groups', 'ug2', [
164 'ug2.ug_user = user_id',
165 'ug2.ug_group' => $this->excludegroups,
166 $dbr->expr( 'ug2.ug_expiry', '=', null )->or( 'ug2.ug_expiry', '>=', $dbr->timestamp() ),
167 ] )
168 ->andWhere( [ 'ug2.ug_user' => null ] );
169 }
170 if ( !$this->canSeeHideuser() ) {
171 $subquery->andWhere( $this->hideUserUtils->getExpression( $dbr ) );
172 }
173
174 // Outer query to select the recent edit counts for the selected active users
175 return [
176 'tables' => [ 'qcc_users' => new Subquery( $subquery->getSQL() ), 'recentchanges' ],
177 'fields' => [
178 'qcc_title',
179 'user_name' => 'qcc_title',
180 'user_id' => 'user_id',
181 'recentedits' => 'COUNT(DISTINCT rc_id)'
182 ],
183 'options' => [ 'GROUP BY' => [ 'qcc_title', 'user_id' ] ],
184 'conds' => [],
185 'join_conds' => [ 'recentchanges' => [ 'LEFT JOIN', [
186 'rc_actor = actor_id',
187 $dbr->expr( 'rc_type', '!=', RC_EXTERNAL ), // Don't count wikidata.
188 $dbr->expr( 'rc_type', '!=', RC_CATEGORIZE ), // Don't count categorization changes.
189 $dbr->expr( 'rc_log_type', '=', null )->or( 'rc_log_type', '!=', 'newusers' ),
190 $dbr->expr( 'rc_timestamp', '>=', $timestamp ),
191 ] ] ],
192 ];
193 }
194
195 protected function buildQueryInfo( $offset, $limit, $order ) {
196 $fname = __METHOD__ . ' (' . $this->getSqlComment() . ')';
197
198 $sortColumns = array_merge( [ $this->mIndexField ], $this->mExtraSortFields );
199 if ( $order === self::QUERY_ASCENDING ) {
200 $dir = 'ASC';
201 $orderBy = $sortColumns;
202 $operator = $this->mIncludeOffset ? '>=' : '>';
203 } else {
204 $dir = 'DESC';
205 $orderBy = [];
206 foreach ( $sortColumns as $col ) {
207 $orderBy[] = $col . ' DESC';
208 }
209 $operator = $this->mIncludeOffset ? '<=' : '<';
210 }
211 $info = $this->getQueryInfo( [
212 'limit' => intval( $limit ),
213 'order' => $dir,
214 'conds' =>
215 $offset != '' ? [ $this->getDatabase()->expr( $this->mIndexField, $operator, $offset ) ] : [],
216 ] );
217
218 $tables = $info['tables'];
219 $fields = $info['fields'];
220 $conds = $info['conds'];
221 $options = $info['options'];
222 $join_conds = $info['join_conds'];
223 $options['ORDER BY'] = $orderBy;
224 return [ $tables, $fields, $conds, $fname, $options, $join_conds ];
225 }
226
227 protected function doBatchLookups() {
228 parent::doBatchLookups();
229
230 $uids = [];
231 foreach ( $this->mResult as $row ) {
232 $uids[] = (int)$row->user_id;
233 }
234 // Fetch the block status of the user for showing "(blocked)" text and for
235 // striking out names of suppressed users when privileged user views the list.
236 // Although the first query already hits the block table for un-privileged, this
237 // is done in two queries to avoid huge quicksorts and to make COUNT(*) correct.
238 $dbr = $this->getDatabase();
239 $res = $dbr->newSelectQueryBuilder()
240 ->select( [
241 'bt_user',
242 'deleted' => 'MAX(bl_deleted)',
243 'sitewide' => 'MAX(bl_sitewide)'
244 ] )
245 ->from( 'block_target' )
246 ->join( 'block', null, 'bl_target=bt_id' )
247 ->where( [ 'bt_user' => $uids ] )
248 ->groupBy( [ 'bt_user' ] )
249 ->caller( __METHOD__ )->fetchResultSet();
250 $this->blockStatusByUid = [];
251 foreach ( $res as $row ) {
252 $this->blockStatusByUid[$row->bt_user] = [
253 'deleted' => $row->deleted,
254 'sitewide' => $row->sitewide,
255 ];
256 }
257 $this->mResult->seek( 0 );
258 }
259
260 public function formatRow( $row ) {
261 $userName = $row->user_name;
262
263 $ulinks = Linker::userLink( $row->user_id, $userName );
264 $ulinks .= Linker::userToolLinks(
265 $row->user_id,
266 $userName,
267 // Should the contributions link be red if the user has no edits (using default)
268 false,
269 // Customisation flags (using default 0)
270 0,
271 // User edit count (using default)
272 null,
273 // do not wrap the message in parentheses (CSS will provide these)
274 false
275 );
276
277 $lang = $this->getLanguage();
278
279 $list = [];
280
281 $userIdentity = new UserIdentityValue( intval( $row->user_id ), $userName );
282 $ugms = $this->getGroupMemberships( $userIdentity );
283 foreach ( $ugms as $ugm ) {
284 $list[] = $this->buildGroupLink( $ugm, $userName );
285 }
286
287 $groups = $lang->commaList( $list );
288
289 $item = $lang->specialList( $ulinks, $groups );
290
291 // If there is a block, 'deleted' and 'sitewide' are both set on
292 // $this->blockStatusByUid[$row->user_id].
293 $blocked = '';
294 $isBlocked = isset( $this->blockStatusByUid[$row->user_id] );
295 if ( $isBlocked ) {
296 if ( $this->blockStatusByUid[$row->user_id]['deleted'] == 1 ) {
297 $item = "<span class=\"deleted\">$item</span>";
298 }
299 if ( $this->blockStatusByUid[$row->user_id]['sitewide'] == 1 ) {
300 $blocked = ' ' . $this->msg( 'listusers-blocked', $userName )->escaped();
301 }
302 }
303 $count = $this->msg( 'activeusers-count' )->numParams( $row->recentedits )
304 ->params( $userName )->numParams( $this->RCMaxAge )->escaped();
305
306 return Html::rawElement( 'li', [], "{$item} [{$count}]{$blocked}" );
307 }
308
309}
310
315class_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:63
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, 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:79
Value object representing a user's identity.
Interface for objects which can provide a MediaWiki context on request.
Provide primary and replica IDatabase connections.