Debian Codegraph @

includes/specials/SpecialActiveUsers.php

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