MediaWiki  master
SpecialActiveUsers.php
Go to the documentation of this file.
1 <?php
28 
35 
38 
40  private $loadBalancer;
41 
44 
50  public function __construct(
54  ) {
55  parent::__construct( 'Activeusers' );
56  $this->linkBatchFactory = $linkBatchFactory;
57  $this->loadBalancer = $loadBalancer;
58  $this->userGroupManager = $userGroupManager;
59  }
60 
64  public function execute( $par ) {
65  $out = $this->getOutput();
66 
67  $this->setHeaders();
68  $this->outputHeader();
69 
70  $opts = new FormOptions();
71 
72  $opts->add( 'username', '' );
73  $opts->add( 'groups', [] );
74  $opts->add( 'excludegroups', [] );
75  // Backwards-compatibility with old URLs
76  $opts->add( 'hidebots', false, FormOptions::BOOL );
77  $opts->add( 'hidesysops', false, FormOptions::BOOL );
78 
79  $opts->fetchValuesFromRequest( $this->getRequest() );
80 
81  if ( $par !== null ) {
82  $opts->setValue( 'username', $par );
83  }
84 
85  $pager = new ActiveUsersPager(
86  $this->getContext(),
87  $this->getHookContainer(),
88  $this->linkBatchFactory,
89  $this->loadBalancer,
90  $this->userGroupManager,
91  $opts
92  );
93  $usersBody = $pager->getBody();
94 
95  $this->buildForm();
96 
97  if ( $usersBody ) {
98  $out->addHTML(
99  $pager->getNavigationBar() .
100  Html::rawElement( 'ul', [], $usersBody ) .
101  $pager->getNavigationBar()
102  );
103  $out->addModuleStyles( 'mediawiki.interface.helpers.styles' );
104  } else {
105  $out->addWikiMsg( 'activeusers-noresult' );
106  }
107  }
108 
112  protected function buildForm() {
113  $groups = $this->userGroupManager->listAllGroups();
114 
115  $options = [];
116  $lang = $this->getLanguage();
117  foreach ( $groups as $group ) {
118  $msg = htmlspecialchars( $lang->getGroupName( $group ) );
119  $options[$msg] = $group;
120  }
121  ksort( $options );
122 
123  // Backwards-compatibility with old URLs
124  $req = $this->getRequest();
125  $excludeDefault = [];
126  if ( $req->getCheck( 'hidebots' ) ) {
127  $excludeDefault[] = 'bot';
128  }
129  if ( $req->getCheck( 'hidesysops' ) ) {
130  $excludeDefault[] = 'sysop';
131  }
132 
133  $formDescriptor = [
134  'username' => [
135  'type' => 'user',
136  'name' => 'username',
137  'label-message' => 'activeusers-from',
138  ],
139  'groups' => [
140  'type' => 'multiselect',
141  'dropdown' => true,
142  'flatlist' => true,
143  'name' => 'groups',
144  'label-message' => 'activeusers-groups',
145  'options' => $options,
146  ],
147  'excludegroups' => [
148  'type' => 'multiselect',
149  'dropdown' => true,
150  'flatlist' => true,
151  'name' => 'excludegroups',
152  'label-message' => 'activeusers-excludegroups',
153  'options' => $options,
154  'default' => $excludeDefault,
155  ],
156  ];
157 
158  HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
159  // For the 'multiselect' field values to be preserved on submit
160  ->setFormIdentifier( 'specialactiveusers' )
161  ->setIntro( $this->getIntroText() )
162  ->setWrapperLegendMsg( 'activeusers' )
163  ->setSubmitTextMsg( 'activeusers-submit' )
164  // prevent setting subpage and 'username' parameter at the same time
165  ->setTitle( $this->getPageTitle() )
166  ->setMethod( 'get' )
167  ->prepareForm()
168  ->displayForm( false );
169  }
170 
175  protected function getIntroText() {
176  $days = $this->getConfig()->get( MainConfigNames::ActiveUserDays );
177 
178  $intro = $this->msg( 'activeusers-intro' )->numParams( $days )->parse();
179 
180  // Mention the level of cache staleness...
181  $dbr = $this->loadBalancer->getConnectionRef( ILoadBalancer::DB_REPLICA, 'recentchanges' );
182  $rcMax = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', '', __METHOD__ );
183  if ( $rcMax ) {
184  $cTime = $dbr->selectField( 'querycache_info',
185  'qci_timestamp',
186  [ 'qci_type' => 'activeusers' ],
187  __METHOD__
188  );
189  if ( $cTime ) {
190  $secondsOld = (int)wfTimestamp( TS_UNIX, $rcMax ) - (int)wfTimestamp( TS_UNIX, $cTime );
191  } else {
192  $rcMin = $dbr->selectField( 'recentchanges', 'MIN(rc_timestamp)', '', __METHOD__ );
193  $secondsOld = time() - (int)wfTimestamp( TS_UNIX, $rcMin );
194  }
195  if ( $secondsOld > 0 ) {
196  $intro .= $this->msg( 'cachedspecial-viewing-cached-ttl' )
197  ->durationParams( $secondsOld )->parseAsBlock();
198  }
199  }
200 
201  return $intro;
202  }
203 
204  protected function getGroupName() {
205  return 'users';
206  }
207 }
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.
Definition: FormOptions.php:35
const BOOL
Boolean type, maps guessType() to WebRequest::getBool()
Definition: FormOptions.php:51
static factory( $displayFormat, $descriptor, IContextSource $context, $messagePrefix='')
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:338
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:214
A class containing constants representing the names of configuration variables.
Implements Special:Activeusers.
__construct(LinkBatchFactory $linkBatchFactory, ILoadBalancer $loadBalancer, UserGroupManager $userGroupManager)
LinkBatchFactory $linkBatchFactory
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.
UserGroupManager $userGroupManager
ILoadBalancer $loadBalancer
Parent class for all special pages.
Definition: SpecialPage.php:44
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.
Database cluster connection, tracking, load balancing, and transaction manager interface.
const DB_REPLICA
Definition: defines.php:25
if(!isset( $args[0])) $lang