3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
22 namespace MediaWiki\Pager
;
24 use MediaWiki\Block\HideUserUtils
;
25 use MediaWiki\Cache\LinkBatchFactory
;
26 use MediaWiki\Context\IContextSource
;
27 use MediaWiki\HookContainer\HookContainer
;
28 use MediaWiki\Html\FormOptions
;
29 use MediaWiki\Html\Html
;
30 use MediaWiki\Linker\Linker
;
31 use MediaWiki\MainConfigNames
;
32 use MediaWiki\Title\Title
;
33 use MediaWiki\User\UserGroupManager
;
34 use MediaWiki\User\UserIdentityLookup
;
35 use MediaWiki\User\UserIdentityValue
;
36 use Wikimedia\Rdbms\IConnectionProvider
;
37 use Wikimedia\Rdbms\Subquery
;
40 * This class is used to get a list of active users. The ones with specials
41 * rights (sysop, bureaucrat, developer) will have them displayed
42 * next to their names.
46 class ActiveUsersPager
extends UsersPager
{
60 private $blockStatusByUid;
66 private $excludegroups;
69 * @param IContextSource $context
70 * @param HookContainer $hookContainer
71 * @param LinkBatchFactory $linkBatchFactory
72 * @param IConnectionProvider $dbProvider
73 * @param UserGroupManager $userGroupManager
74 * @param UserIdentityLookup $userIdentityLookup
75 * @param HideUserUtils $hideUserUtils
76 * @param FormOptions $opts
78 public function __construct(
79 IContextSource
$context,
80 HookContainer
$hookContainer,
81 LinkBatchFactory
$linkBatchFactory,
82 IConnectionProvider
$dbProvider,
83 UserGroupManager
$userGroupManager,
84 UserIdentityLookup
$userIdentityLookup,
85 HideUserUtils
$hideUserUtils,
100 $this->RCMaxAge
= $this->getConfig()->get( MainConfigNames
::ActiveUserDays
);
101 $this->requestedUser
= '';
103 $un = $opts->getValue( 'username' );
105 $username = Title
::makeTitleSafe( NS_USER
, $un );
106 if ( $username !== null ) {
107 $this->requestedUser
= $username->getText();
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';
117 if ( $opts->getValue( 'hidesysops' ) ) {
118 $this->excludegroups
[] = 'sysop';
122 public function getIndexField() {
126 public function getQueryInfo( $data = null ) {
127 $dbr = $this->getDatabase();
129 $activeUserSeconds = $this->getConfig()->get( MainConfigNames
::ActiveUserDays
) * 86400;
130 $timestamp = $dbr->timestamp( (int)wfTimestamp( TS_UNIX
) - $activeUserSeconds );
131 $fname = __METHOD__
. ' (' . $this->getSqlComment() . ')';
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' )
140 'qcc_type' => 'activeusers',
141 'qcc_namespace' => NS_USER
,
144 if ( $data !== null ) {
146 ->orderBy( 'qcc_title', $data['order'] )
147 ->limit( $data['limit'] )
148 ->andWhere( $data['conds'] );
150 if ( $this->requestedUser
!= '' ) {
151 $subquery->andWhere( $dbr->expr( 'qcc_title', '>=', $this->requestedUser
) );
153 if ( $this->groups
!== [] ) {
155 ->join( 'user_groups', 'ug1', 'ug1.ug_user = user_id' )
157 'ug1.ug_group' => $this->groups
,
158 $dbr->expr( 'ug1.ug_expiry', '=', null )->or( 'ug1.ug_expiry', '>=', $dbr->timestamp() ),
161 if ( $this->excludegroups
!== [] ) {
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() ),
168 ->andWhere( [ 'ug2.ug_user' => null ] );
170 if ( !$this->canSeeHideuser() ) {
171 $subquery->andWhere( $this->hideUserUtils
->getExpression( $dbr ) );
174 // Outer query to select the recent edit counts for the selected active users
176 'tables' => [ 'qcc_users' => new Subquery( $subquery->getSQL() ), 'recentchanges' ],
179 'user_name' => 'qcc_title',
180 'user_id' => 'user_id',
181 'recentedits' => 'COUNT(DISTINCT rc_id)'
183 'options' => [ 'GROUP BY' => [ 'qcc_title', 'user_id' ] ],
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 ),
195 protected function buildQueryInfo( $offset, $limit, $order ) {
196 $fname = __METHOD__
. ' (' . $this->getSqlComment() . ')';
198 $sortColumns = array_merge( [ $this->mIndexField
], $this->mExtraSortFields
);
199 if ( $order === self
::QUERY_ASCENDING
) {
201 $orderBy = $sortColumns;
202 $operator = $this->mIncludeOffset ?
'>=' : '>';
206 foreach ( $sortColumns as $col ) {
207 $orderBy[] = $col . ' DESC';
209 $operator = $this->mIncludeOffset ?
'<=' : '<';
211 $info = $this->getQueryInfo( [
212 'limit' => intval( $limit ),
215 $offset != '' ?
[ $this->getDatabase()->expr( $this->mIndexField
, $operator, $offset ) ] : [],
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 ];
227 protected function doBatchLookups() {
228 parent
::doBatchLookups();
231 foreach ( $this->mResult
as $row ) {
232 $uids[] = (int)$row->user_id
;
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()
242 'deleted' => 'MAX(bl_deleted)',
243 'sitewide' => 'MAX(bl_sitewide)'
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
,
257 $this->mResult
->seek( 0 );
260 public function formatRow( $row ) {
261 $userName = $row->user_name
;
263 $ulinks = Linker
::userLink( $row->user_id
, $userName );
264 $ulinks .= Linker
::userToolLinks(
267 // Should the contributions link be red if the user has no edits (using default)
269 // Customisation flags (using default 0)
271 // User edit count (using default)
273 // do not wrap the message in parentheses (CSS will provide these)
277 $lang = $this->getLanguage();
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 );
287 $groups = $lang->commaList( $list );
289 $item = $lang->specialList( $ulinks, $groups );
291 // If there is a block, 'deleted' and 'sitewide' are both set on
292 // $this->blockStatusByUid[$row->user_id].
294 $isBlocked = isset( $this->blockStatusByUid
[$row->user_id
] );
296 if ( $this->blockStatusByUid
[$row->user_id
]['deleted'] == 1 ) {
297 $item = "<span class=\"deleted\">$item</span>";
299 if ( $this->blockStatusByUid
[$row->user_id
]['sitewide'] == 1 ) {
300 $blocked = ' ' . $this->msg( 'listusers-blocked', $userName )->escaped();
303 $count = $this->msg( 'activeusers-count' )->numParams( $row->recentedits
)
304 ->params( $userName )->numParams( $this->RCMaxAge
)->escaped();
306 return Html
::rawElement( 'li', [], "{$item} [{$count}]{$blocked}" );
312 * Retain the old class name for backwards compatibility.
313 * @deprecated since 1.41
315 class_alias( ActiveUsersPager
::class, 'ActiveUsersPager' );