MediaWiki master
SpecialActiveUsers.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Specials;
22
35
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
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
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
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
226class_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:210
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:57
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.
buildForm()
Generate and output the form.
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
__construct(LinkBatchFactory $linkBatchFactory, IConnectionProvider $dbProvider, UserGroupManager $userGroupManager, UserIdentityLookup $userIdentityLookup, HideUserUtils $hideUserUtils, TempUserConfig $tempUserConfig)
getIntroText()
Return introductory message.
Manage user group memberships.
Interface for temporary user creation config and name matching.
Service for looking up UserIdentity.
Provide primary and replica IDatabase connections.