Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 157
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 / 156
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 / 24
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\UserGroupManager;
34use MediaWiki\User\UserIdentityLookup;
35use MediaWiki\User\UserIdentityValue;
36use Wikimedia\Rdbms\IConnectionProvider;
37use Wikimedia\Rdbms\Subquery;
38
39/**
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.
43 *
44 * @ingroup Pager
45 */
46class ActiveUsersPager extends UsersPager {
47    /**
48     * @var FormOptions
49     */
50    protected $opts;
51
52    /**
53     * @var string[]
54     */
55    protected $groups;
56
57    /**
58     * @var array
59     */
60    private $blockStatusByUid;
61
62    /** @var int */
63    private $RCMaxAge;
64
65    /** @var string[] */
66    private $excludegroups;
67
68    /**
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
77     */
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,
86        FormOptions $opts
87    ) {
88        parent::__construct(
89            $context,
90            $hookContainer,
91            $linkBatchFactory,
92            $dbProvider,
93            $userGroupManager,
94            $userIdentityLookup,
95            $hideUserUtils,
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
311/**
312 * Retain the old class name for backwards compatibility.
313 * @deprecated since 1.41
314 */
315class_alias( ActiveUsersPager::class, 'ActiveUsersPager' );