MediaWiki REL1_40
SpecialActiveUsers.php
Go to the documentation of this file.
1<?php
31
38
40 private $linkBatchFactory;
41
43 private $loadBalancer;
44
46 private $userGroupManager;
47
49 private $userIdentityLookup;
50
57 public function __construct(
58 LinkBatchFactory $linkBatchFactory,
59 ILoadBalancer $loadBalancer,
60 UserGroupManager $userGroupManager,
61 UserIdentityLookup $userIdentityLookup
62 ) {
63 parent::__construct( 'Activeusers' );
64 $this->linkBatchFactory = $linkBatchFactory;
65 $this->loadBalancer = $loadBalancer;
66 $this->userGroupManager = $userGroupManager;
67 $this->userIdentityLookup = $userIdentityLookup;
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->loadBalancer,
99 $this->userGroupManager,
100 $this->userIdentityLookup,
101 $opts
102 );
103 $usersBody = $pager->getBody();
104
105 $this->buildForm();
106
107 if ( $usersBody ) {
108 $out->addHTML(
109 $pager->getNavigationBar() .
110 Html::rawElement( 'ul', [], $usersBody ) .
111 $pager->getNavigationBar()
112 );
113 $out->addModuleStyles( 'mediawiki.interface.helpers.styles' );
114 } else {
115 $out->addWikiMsg( 'activeusers-noresult' );
116 }
117 }
118
122 protected function buildForm() {
123 $groups = $this->userGroupManager->listAllGroups();
124
125 $options = [];
126 $lang = $this->getLanguage();
127 foreach ( $groups as $group ) {
128 $msg = htmlspecialchars( $lang->getGroupName( $group ) );
129 $options[$msg] = $group;
130 }
131 ksort( $options );
132
133 // Backwards-compatibility with old URLs
134 $req = $this->getRequest();
135 $excludeDefault = [];
136 if ( $req->getCheck( 'hidebots' ) ) {
137 $excludeDefault[] = 'bot';
138 }
139 if ( $req->getCheck( 'hidesysops' ) ) {
140 $excludeDefault[] = 'sysop';
141 }
142
143 $formDescriptor = [
144 'username' => [
145 'type' => 'user',
146 'name' => 'username',
147 'label-message' => 'activeusers-from',
148 ],
149 'groups' => [
150 'type' => 'multiselect',
151 'dropdown' => true,
152 'flatlist' => true,
153 'name' => 'groups',
154 'label-message' => 'activeusers-groups',
155 'options' => $options,
156 ],
157 'excludegroups' => [
158 'type' => 'multiselect',
159 'dropdown' => true,
160 'flatlist' => true,
161 'name' => 'excludegroups',
162 'label-message' => 'activeusers-excludegroups',
163 'options' => $options,
164 'default' => $excludeDefault,
165 ],
166 ];
167
168 HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
169 // For the 'multiselect' field values to be preserved on submit
170 ->setFormIdentifier( 'specialactiveusers' )
171 ->setPreHtml( $this->getIntroText() )
172 ->setWrapperLegendMsg( 'activeusers' )
173 ->setSubmitTextMsg( 'activeusers-submit' )
174 // prevent setting subpage and 'username' parameter at the same time
175 ->setTitle( $this->getPageTitle() )
176 ->setMethod( 'get' )
177 ->prepareForm()
178 ->displayForm( false );
179 }
180
185 protected function getIntroText() {
186 $days = $this->getConfig()->get( MainConfigNames::ActiveUserDays );
187
188 $intro = $this->msg( 'activeusers-intro' )->numParams( $days )->parse();
189
190 // Mention the level of cache staleness...
191 $dbr = $this->loadBalancer->getConnection( ILoadBalancer::DB_REPLICA );
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}
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
This class is used to get a list of active users.
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:55
A class containing constants representing the names of configuration variables.
Implements Special:Activeusers.
__construct(LinkBatchFactory $linkBatchFactory, ILoadBalancer $loadBalancer, UserGroupManager $userGroupManager, UserIdentityLookup $userIdentityLookup)
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.
Parent class for all special pages.
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages Per default the message key is the canonical name o...
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
getOutput()
Get the OutputPage being used for this instance.
getContext()
Gets the context this SpecialPage is executed in.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getConfig()
Shortcut to get main config object.
getRequest()
Get the WebRequest being used for this instance.
getPageTitle( $subpage=false)
Get a self-referential title object.
getLanguage()
Shortcut to get user's language.
This class is a delegate to ILBFactory for a given database cluster.
if(!isset( $args[0])) $lang