Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 114
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
SpecialActiveUsers
0.00% covered (danger)
0.00%
0 / 113
0.00% covered (danger)
0.00%
0 / 5
182
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 1
12
 buildForm
0.00% covered (danger)
0.00%
0 / 48
0.00% covered (danger)
0.00%
0 / 1
20
 getIntroText
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
20
 getGroupName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
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 */
20
21namespace MediaWiki\Specials;
22
23use MediaWiki\Block\HideUserUtils;
24use MediaWiki\Cache\LinkBatchFactory;
25use MediaWiki\Html\FormOptions;
26use MediaWiki\Html\Html;
27use MediaWiki\HTMLForm\HTMLForm;
28use MediaWiki\MainConfigNames;
29use MediaWiki\Pager\ActiveUsersPager;
30use MediaWiki\SpecialPage\SpecialPage;
31use MediaWiki\User\TempUser\TempUserConfig;
32use MediaWiki\User\UserGroupManager;
33use MediaWiki\User\UserIdentityLookup;
34use Wikimedia\Rdbms\IConnectionProvider;
35
36/**
37 * Implements Special:Activeusers
38 *
39 * @ingroup SpecialPage
40 */
41class SpecialActiveUsers extends SpecialPage {
42
43    private LinkBatchFactory $linkBatchFactory;
44    private IConnectionProvider $dbProvider;
45    private UserGroupManager $userGroupManager;
46    private UserIdentityLookup $userIdentityLookup;
47    private HideUserUtils $hideUserUtils;
48    private TempUserConfig $tempUserConfig;
49
50    public function __construct(
51        LinkBatchFactory $linkBatchFactory,
52        IConnectionProvider $dbProvider,
53        UserGroupManager $userGroupManager,
54        UserIdentityLookup $userIdentityLookup,
55        HideUserUtils $hideUserUtils,
56        TempUserConfig $tempUserConfig
57    ) {
58        parent::__construct( 'Activeusers' );
59        $this->linkBatchFactory = $linkBatchFactory;
60        $this->dbProvider = $dbProvider;
61        $this->userGroupManager = $userGroupManager;
62        $this->userIdentityLookup = $userIdentityLookup;
63        $this->hideUserUtils = $hideUserUtils;
64        $this->tempUserConfig = $tempUserConfig;
65    }
66
67    /**
68     * @param string|null $par Parameter passed to the page or null
69     */
70    public function execute( $par ) {
71        $out = $this->getOutput();
72
73        $this->setHeaders();
74        $this->outputHeader();
75
76        $opts = new FormOptions();
77
78        $opts->add( 'username', '' );
79        $opts->add( 'groups', [] );
80        $opts->add( 'excludegroups', [] );
81        // Backwards-compatibility with old URLs
82        $opts->add( 'hidebots', false, FormOptions::BOOL );
83        $opts->add( 'hidesysops', false, FormOptions::BOOL );
84
85        $opts->fetchValuesFromRequest( $this->getRequest() );
86
87        if ( $par !== null ) {
88            $opts->setValue( 'username', $par );
89        }
90
91        $pager = new ActiveUsersPager(
92            $this->getContext(),
93            $this->getHookContainer(),
94            $this->linkBatchFactory,
95            $this->dbProvider,
96            $this->userGroupManager,
97            $this->userIdentityLookup,
98            $this->hideUserUtils,
99            $this->tempUserConfig,
100            $opts
101        );
102        $usersBody = $pager->getBody();
103
104        $this->buildForm();
105
106        if ( $usersBody ) {
107            $out->addHTML(
108                $pager->getNavigationBar() .
109                Html::rawElement( 'ul', [], $usersBody ) .
110                $pager->getNavigationBar()
111            );
112            $out->addModuleStyles( 'mediawiki.interface.helpers.styles' );
113        } else {
114            $out->addWikiMsg( 'activeusers-noresult' );
115        }
116    }
117
118    /**
119     * Generate and output the form
120     */
121    protected function buildForm() {
122        $groups = $this->userGroupManager->listAllGroups();
123
124        $options = [];
125        $lang = $this->getLanguage();
126        foreach ( $groups as $group ) {
127            $msg = htmlspecialchars( $lang->getGroupName( $group ) );
128            $options[$msg] = $group;
129        }
130        ksort( $options );
131
132        // Backwards-compatibility with old URLs
133        $req = $this->getRequest();
134        $excludeDefault = [];
135        if ( $req->getCheck( 'hidebots' ) ) {
136            $excludeDefault[] = 'bot';
137        }
138        if ( $req->getCheck( 'hidesysops' ) ) {
139            $excludeDefault[] = 'sysop';
140        }
141
142        $formDescriptor = [
143            'username' => [
144                'type' => 'user',
145                'name' => 'username',
146                'label-message' => 'activeusers-from',
147            ],
148            'groups' => [
149                'type' => 'multiselect',
150                'dropdown' => true,
151                'flatlist' => true,
152                'name' => 'groups',
153                'label-message' => 'activeusers-groups',
154                'options' => $options,
155            ],
156            'excludegroups' => [
157                'type' => 'multiselect',
158                'dropdown' => true,
159                'flatlist' => true,
160                'name' => 'excludegroups',
161                'label-message' => 'activeusers-excludegroups',
162                'options' => $options,
163                'default' => $excludeDefault,
164            ],
165        ];
166
167        HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
168            // For the 'multiselect' field values to be preserved on submit
169            ->setFormIdentifier( 'specialactiveusers' )
170            ->setPreHtml( $this->getIntroText() )
171            ->setWrapperLegendMsg( 'activeusers' )
172            ->setSubmitTextMsg( 'activeusers-submit' )
173            // prevent setting subpage and 'username' parameter at the same time
174            ->setTitle( $this->getPageTitle() )
175            ->setMethod( 'get' )
176            ->prepareForm()
177            ->displayForm( false );
178    }
179
180    /**
181     * Return introductory message.
182     * @return string
183     */
184    protected function getIntroText() {
185        $days = $this->getConfig()->get( MainConfigNames::ActiveUserDays );
186
187        $intro = $this->msg( 'activeusers-intro' )->numParams( $days )->parse();
188
189        // Mention the level of cache staleness...
190        $dbr = $this->dbProvider->getReplicaDatabase();
191
192        $rcMax = $dbr->newSelectQueryBuilder()
193            ->select( 'MAX(rc_timestamp)' )
194            ->from( 'recentchanges' )
195            ->caller( __METHOD__ )->fetchField();
196        if ( $rcMax ) {
197            $cTime = $dbr->newSelectQueryBuilder()
198                ->select( 'qci_timestamp' )
199                ->from( 'querycache_info' )
200                ->where( [ 'qci_type' => 'activeusers' ] )
201                ->caller( __METHOD__ )->fetchField();
202            if ( $cTime ) {
203                $secondsOld = (int)wfTimestamp( TS_UNIX, $rcMax ) - (int)wfTimestamp( TS_UNIX, $cTime );
204            } else {
205                $rcMin = $dbr->newSelectQueryBuilder()
206                    ->select( 'MIN(rc_timestamp)' )
207                    ->from( 'recentchanges' )
208                    ->caller( __METHOD__ )->fetchField();
209                $secondsOld = time() - (int)wfTimestamp( TS_UNIX, $rcMin );
210            }
211            if ( $secondsOld > 0 ) {
212                $intro .= $this->msg( 'cachedspecial-viewing-cached-ttl' )
213                    ->durationParams( $secondsOld )->parseAsBlock();
214            }
215        }
216
217        return $intro;
218    }
219
220    protected function getGroupName() {
221        return 'users';
222    }
223}
224
225/** @deprecated class alias since 1.41 */
226class_alias( SpecialActiveUsers::class, 'SpecialActiveUsers' );