MediaWiki master
SpecialActiveUsers.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Specials;
22
34
41
42 private LinkBatchFactory $linkBatchFactory;
43 private IConnectionProvider $dbProvider;
44 private UserGroupManager $userGroupManager;
45 private UserIdentityLookup $userIdentityLookup;
46 private HideUserUtils $hideUserUtils;
47
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
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
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
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
228class_alias( SpecialActiveUsers::class, 'SpecialActiveUsers' );
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Helpers for building queries that determine whether a user is hidden.
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:208
Helper class to keep track of options when mixing links and form elements.
This class is a collection of static functions that serve two purposes:
Definition Html.php:56
A class containing constants representing the names of configuration variables.
const ActiveUserDays
Name constant for the ActiveUserDays setting, for use with Config::get()
This class is used to get a list of active users.
Parent class for all special pages.
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
getPageTitle( $subpage=false)
Get a self-referential title object.
getConfig()
Shortcut to get main config object.
getContext()
Gets the context this SpecialPage is executed in.
getRequest()
Get the WebRequest being used for this instance.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getOutput()
Get the OutputPage being used for this instance.
getLanguage()
Shortcut to get user's language.
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages By default the message key is the canonical name of...
Implements Special:Activeusers.
__construct(LinkBatchFactory $linkBatchFactory, IConnectionProvider $dbProvider, UserGroupManager $userGroupManager, UserIdentityLookup $userIdentityLookup, HideUserUtils $hideUserUtils)
buildForm()
Generate and output the form.
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
getIntroText()
Return introductory message.
Provide primary and replica IDatabase connections.