MediaWiki  master
SpecialActiveUsers.php
Go to the documentation of this file.
1 <?php
24 namespace MediaWiki\Specials;
25 
27 use HTMLForm;
34 use SpecialPage;
36 
43 
45  private $linkBatchFactory;
46 
48  private $loadBalancer;
49 
51  private $userGroupManager;
52 
54  private $userIdentityLookup;
55 
62  public function __construct(
63  LinkBatchFactory $linkBatchFactory,
64  ILoadBalancer $loadBalancer,
65  UserGroupManager $userGroupManager,
66  UserIdentityLookup $userIdentityLookup
67  ) {
68  parent::__construct( 'Activeusers' );
69  $this->linkBatchFactory = $linkBatchFactory;
70  $this->loadBalancer = $loadBalancer;
71  $this->userGroupManager = $userGroupManager;
72  $this->userIdentityLookup = $userIdentityLookup;
73  }
74 
78  public function execute( $par ) {
79  $out = $this->getOutput();
80 
81  $this->setHeaders();
82  $this->outputHeader();
83 
84  $opts = new FormOptions();
85 
86  $opts->add( 'username', '' );
87  $opts->add( 'groups', [] );
88  $opts->add( 'excludegroups', [] );
89  // Backwards-compatibility with old URLs
90  $opts->add( 'hidebots', false, FormOptions::BOOL );
91  $opts->add( 'hidesysops', false, FormOptions::BOOL );
92 
93  $opts->fetchValuesFromRequest( $this->getRequest() );
94 
95  if ( $par !== null ) {
96  $opts->setValue( 'username', $par );
97  }
98 
99  $pager = new ActiveUsersPager(
100  $this->getContext(),
101  $this->getHookContainer(),
102  $this->linkBatchFactory,
103  $this->loadBalancer,
104  $this->userGroupManager,
105  $this->userIdentityLookup,
106  $opts
107  );
108  $usersBody = $pager->getBody();
109 
110  $this->buildForm();
111 
112  if ( $usersBody ) {
113  $out->addHTML(
114  $pager->getNavigationBar() .
115  Html::rawElement( 'ul', [], $usersBody ) .
116  $pager->getNavigationBar()
117  );
118  $out->addModuleStyles( 'mediawiki.interface.helpers.styles' );
119  } else {
120  $out->addWikiMsg( 'activeusers-noresult' );
121  }
122  }
123 
127  protected function buildForm() {
128  $groups = $this->userGroupManager->listAllGroups();
129 
130  $options = [];
131  $lang = $this->getLanguage();
132  foreach ( $groups as $group ) {
133  $msg = htmlspecialchars( $lang->getGroupName( $group ) );
134  $options[$msg] = $group;
135  }
136  ksort( $options );
137 
138  // Backwards-compatibility with old URLs
139  $req = $this->getRequest();
140  $excludeDefault = [];
141  if ( $req->getCheck( 'hidebots' ) ) {
142  $excludeDefault[] = 'bot';
143  }
144  if ( $req->getCheck( 'hidesysops' ) ) {
145  $excludeDefault[] = 'sysop';
146  }
147 
148  $formDescriptor = [
149  'username' => [
150  'type' => 'user',
151  'name' => 'username',
152  'label-message' => 'activeusers-from',
153  ],
154  'groups' => [
155  'type' => 'multiselect',
156  'dropdown' => true,
157  'flatlist' => true,
158  'name' => 'groups',
159  'label-message' => 'activeusers-groups',
160  'options' => $options,
161  ],
162  'excludegroups' => [
163  'type' => 'multiselect',
164  'dropdown' => true,
165  'flatlist' => true,
166  'name' => 'excludegroups',
167  'label-message' => 'activeusers-excludegroups',
168  'options' => $options,
169  'default' => $excludeDefault,
170  ],
171  ];
172 
173  HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
174  // For the 'multiselect' field values to be preserved on submit
175  ->setFormIdentifier( 'specialactiveusers' )
176  ->setPreHtml( $this->getIntroText() )
177  ->setWrapperLegendMsg( 'activeusers' )
178  ->setSubmitTextMsg( 'activeusers-submit' )
179  // prevent setting subpage and 'username' parameter at the same time
180  ->setTitle( $this->getPageTitle() )
181  ->setMethod( 'get' )
182  ->prepareForm()
183  ->displayForm( false );
184  }
185 
190  protected function getIntroText() {
191  $days = $this->getConfig()->get( MainConfigNames::ActiveUserDays );
192 
193  $intro = $this->msg( 'activeusers-intro' )->numParams( $days )->parse();
194 
195  // Mention the level of cache staleness...
196  $dbr = $this->loadBalancer->getConnection( ILoadBalancer::DB_REPLICA );
197  $rcMax = $dbr->newSelectQueryBuilder()
198  ->select( 'MAX(rc_timestamp)' )
199  ->from( 'recentchanges' )
200  ->caller( __METHOD__ )->fetchField();
201  if ( $rcMax ) {
202  $cTime = $dbr->newSelectQueryBuilder()
203  ->select( 'qci_timestamp' )
204  ->from( 'querycache_info' )
205  ->where( [ 'qci_type' => 'activeusers' ] )
206  ->caller( __METHOD__ )->fetchField();
207  if ( $cTime ) {
208  $secondsOld = (int)wfTimestamp( TS_UNIX, $rcMax ) - (int)wfTimestamp( TS_UNIX, $cTime );
209  } else {
210  $rcMin = $dbr->newSelectQueryBuilder()
211  ->select( 'MIN(rc_timestamp)' )
212  ->from( 'recentchanges' )
213  ->caller( __METHOD__ )->fetchField();
214  $secondsOld = time() - (int)wfTimestamp( TS_UNIX, $rcMin );
215  }
216  if ( $secondsOld > 0 ) {
217  $intro .= $this->msg( 'cachedspecial-viewing-cached-ttl' )
218  ->durationParams( $secondsOld )->parseAsBlock();
219  }
220  }
221 
222  return $intro;
223  }
224 
225  protected function getGroupName() {
226  return 'users';
227  }
228 }
229 
233 class_alias( SpecialActiveUsers::class, 'SpecialActiveUsers' );
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.
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition: HTMLForm.php:153
static factory( $displayFormat, $descriptor, IContextSource $context, $messagePrefix='')
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:352
Helper class to keep track of options when mixing links and form elements.
Definition: FormOptions.php:41
const BOOL
Boolean type, maps guessType() to WebRequest::getBool()
Definition: FormOptions.php:57
This class is a collection of static functions that serve two purposes:
Definition: Html.php:55
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:219
A class containing constants representing the names of configuration variables.
const ActiveUserDays
Name constant for the ActiveUserDays setting, for use with Config::get()
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.
Definition: SpecialPage.php:45
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.
const DB_REPLICA
Request a replica DB connection.
if(!isset( $args[0])) $lang