Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 158
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
ActiveUsersPager
0.00% covered (danger)
0.00%
0 / 157
0.00% covered (danger)
0.00%
0 / 6
702
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
30
 getIndexField
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getQueryInfo
0.00% covered (danger)
0.00%
0 / 56
0.00% covered (danger)
0.00%
0 / 1
42
 buildQueryInfo
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
42
 doBatchLookups
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
12
 formatRow
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2/**
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.
7 *
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.
12 *
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
17 *
18 * @file
19 * @ingroup Pager
20 */
21
22namespace MediaWiki\Pager;
23
24use MediaWiki\Block\HideUserUtils;
25use MediaWiki\Cache\LinkBatchFactory;
26use MediaWiki\Context\IContextSource;
27use MediaWiki\HookContainer\HookContainer;
28use MediaWiki\Html\FormOptions;
29use MediaWiki\Html\Html;
30use MediaWiki\Linker\Linker;
31use MediaWiki\MainConfigNames;
32use MediaWiki\Title\Title;
33use MediaWiki\User\TempUser\TempUserConfig;
34use MediaWiki\User\UserGroupManager;
35use MediaWiki\User\UserIdentityLookup;
36use MediaWiki\User\UserIdentityValue;
37use Wikimedia\Rdbms\IConnectionProvider;
38use Wikimedia\Rdbms\Subquery;
39
40/**
41 * This class is used to get a list of active users. The ones with specials
42 * rights (sysop, bureaucrat, developer) will have them displayed
43 * next to their names.
44 *
45 * @ingroup Pager
46 */
47class ActiveUsersPager extends UsersPager {
48    /**
49     * @var FormOptions
50     */
51    protected $opts;
52
53    /**
54     * @var string[]
55     */
56    protected $groups;
57
58    /**
59     * @var array
60     */
61    private $blockStatusByUid;
62
63    /** @var int */
64    private $RCMaxAge;
65
66    /** @var string[] */
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,
76        HideUserUtils $hideUserUtils,
77        TempUserConfig $tempUserConfig,
78        FormOptions $opts
79    ) {
80        parent::__construct(
81            $context,
82            $hookContainer,
83            $linkBatchFactory,
84            $dbProvider,
85            $userGroupManager,
86            $userIdentityLookup,
87            $hideUserUtils,
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
304/**
305 * Retain the old class name for backwards compatibility.
306 * @deprecated since 1.41
307 */
308class_alias( ActiveUsersPager::class, 'ActiveUsersPager' );