MediaWiki  master
SpecialActiveUsers.php
Go to the documentation of this file.
1 <?php
27 
34 
37 
39  private $loadBalancer;
40 
43 
49  public function __construct(
53  ) {
54  parent::__construct( 'Activeusers' );
55  $this->linkBatchFactory = $linkBatchFactory;
56  $this->loadBalancer = $loadBalancer;
57  $this->userGroupManager = $userGroupManager;
58  }
59 
63  public function execute( $par ) {
64  $out = $this->getOutput();
65 
66  $this->setHeaders();
67  $this->outputHeader();
68 
69  $opts = new FormOptions();
70 
71  $opts->add( 'username', '' );
72  $opts->add( 'groups', [] );
73  $opts->add( 'excludegroups', [] );
74  // Backwards-compatibility with old URLs
75  $opts->add( 'hidebots', false, FormOptions::BOOL );
76  $opts->add( 'hidesysops', false, FormOptions::BOOL );
77 
78  $opts->fetchValuesFromRequest( $this->getRequest() );
79 
80  if ( $par !== null ) {
81  $opts->setValue( 'username', $par );
82  }
83 
84  $pager = new ActiveUsersPager(
85  $this->getContext(),
86  $opts,
87  $this->linkBatchFactory,
88  $this->getHookContainer(),
89  $this->loadBalancer,
90  $this->userGroupManager
91  );
92  $usersBody = $pager->getBody();
93 
94  $this->buildForm();
95 
96  if ( $usersBody ) {
97  $out->addHTML(
98  $pager->getNavigationBar() .
99  Html::rawElement( 'ul', [], $usersBody ) .
100  $pager->getNavigationBar()
101  );
102  $out->addModuleStyles( 'mediawiki.interface.helpers.styles' );
103  } else {
104  $out->addWikiMsg( 'activeusers-noresult' );
105  }
106  }
107 
111  protected function buildForm() {
112  $groups = $this->userGroupManager->listAllGroups();
113 
114  $options = [];
115  $lang = $this->getLanguage();
116  foreach ( $groups as $group ) {
117  $msg = htmlspecialchars( $lang->getGroupName( $group ) );
118  $options[$msg] = $group;
119  }
120  ksort( $options );
121 
122  // Backwards-compatibility with old URLs
123  $req = $this->getRequest();
124  $excludeDefault = [];
125  if ( $req->getCheck( 'hidebots' ) ) {
126  $excludeDefault[] = 'bot';
127  }
128  if ( $req->getCheck( 'hidesysops' ) ) {
129  $excludeDefault[] = 'sysop';
130  }
131 
132  $formDescriptor = [
133  'username' => [
134  'type' => 'user',
135  'name' => 'username',
136  'label-message' => 'activeusers-from',
137  ],
138  'groups' => [
139  'type' => 'multiselect',
140  'dropdown' => true,
141  'flatlist' => true,
142  'name' => 'groups',
143  'label-message' => 'activeusers-groups',
144  'options' => $options,
145  ],
146  'excludegroups' => [
147  'type' => 'multiselect',
148  'dropdown' => true,
149  'flatlist' => true,
150  'name' => 'excludegroups',
151  'label-message' => 'activeusers-excludegroups',
152  'options' => $options,
153  'default' => $excludeDefault,
154  ],
155  ];
156 
157  HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
158  // For the 'multiselect' field values to be preserved on submit
159  ->setFormIdentifier( 'specialactiveusers' )
160  ->setIntro( $this->getIntroText() )
161  ->setWrapperLegendMsg( 'activeusers' )
162  ->setSubmitTextMsg( 'activeusers-submit' )
163  // prevent setting subpage and 'username' parameter at the same time
164  ->setAction( $this->getPageTitle()->getLocalURL() )
165  ->setMethod( 'get' )
166  ->prepareForm()
167  ->displayForm( false );
168  }
169 
174  protected function getIntroText() {
175  $days = $this->getConfig()->get( 'ActiveUserDays' );
176 
177  $intro = $this->msg( 'activeusers-intro' )->numParams( $days )->parse();
178 
179  // Mention the level of cache staleness...
180  $dbr = $this->loadBalancer->getConnectionRef( ILoadBalancer::DB_REPLICA, 'recentchanges' );
181  $rcMax = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', '', __METHOD__ );
182  if ( $rcMax ) {
183  $cTime = $dbr->selectField( 'querycache_info',
184  'qci_timestamp',
185  [ 'qci_type' => 'activeusers' ],
186  __METHOD__
187  );
188  if ( $cTime ) {
189  $secondsOld = wfTimestamp( TS_UNIX, $rcMax ) - wfTimestamp( TS_UNIX, $cTime );
190  } else {
191  $rcMin = $dbr->selectField( 'recentchanges', 'MIN(rc_timestamp)', '', __METHOD__ );
192  $secondsOld = time() - wfTimestamp( TS_UNIX, $rcMin );
193  }
194  if ( $secondsOld > 0 ) {
195  $intro .= $this->msg( 'cachedspecial-viewing-cached-ttl' )
196  ->durationParams( $secondsOld )->parseAsBlock();
197  }
198  }
199 
200  return $intro;
201  }
202 
203  protected function getGroupName() {
204  return 'users';
205  }
206 }
SpecialPage\getPageTitle
getPageTitle( $subpage=false)
Get a self-referential title object.
Definition: SpecialPage.php:744
SpecialPage\msg
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:912
SpecialActiveUsers\execute
execute( $par)
Definition: SpecialActiveUsers.php:63
SpecialActiveUsers\getGroupName
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Definition: SpecialActiveUsers.php:203
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:790
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:37
SpecialActiveUsers\$userGroupManager
UserGroupManager $userGroupManager
Definition: SpecialActiveUsers.php:42
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1665
SpecialPage\getHookContainer
getHookContainer()
Definition: SpecialPage.php:1082
SpecialActiveUsers\buildForm
buildForm()
Generate and output the form.
Definition: SpecialActiveUsers.php:111
SpecialPage\getLanguage
getLanguage()
Shortcut to get user's language.
Definition: SpecialPage.php:830
$dbr
$dbr
Definition: testCompression.php:54
ActiveUsersPager
This class is used to get a list of active users.
Definition: ActiveUsersPager.php:35
MediaWiki\User\UserGroupManager
Managers user groups.
Definition: UserGroupManager.php:52
SpecialPage\getConfig
getConfig()
Shortcut to get main config object.
Definition: SpecialPage.php:878
MediaWiki\Cache\LinkBatchFactory
Definition: LinkBatchFactory.php:39
SpecialActiveUsers\__construct
__construct(LinkBatchFactory $linkBatchFactory, ILoadBalancer $loadBalancer, UserGroupManager $userGroupManager)
Definition: SpecialActiveUsers.php:49
SpecialPage\setHeaders
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
Definition: SpecialPage.php:618
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
SpecialActiveUsers\getIntroText
getIntroText()
Return introductory message.
Definition: SpecialActiveUsers.php:174
SpecialPage\getContext
getContext()
Gets the context this SpecialPage is executed in.
Definition: SpecialPage.php:764
SpecialPage
Parent class for all special pages.
Definition: SpecialPage.php:43
SpecialPage\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: SpecialPage.php:780
SpecialActiveUsers\$linkBatchFactory
LinkBatchFactory $linkBatchFactory
Definition: SpecialActiveUsers.php:36
SpecialActiveUsers
Implements Special:Activeusers.
Definition: SpecialActiveUsers.php:33
Html\rawElement
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:210
FormOptions
Helper class to keep track of options when mixing links and form elements.
Definition: FormOptions.php:35
SpecialActiveUsers\$loadBalancer
ILoadBalancer $loadBalancer
Definition: SpecialActiveUsers.php:39
HTMLForm\factory
static factory( $displayFormat,... $arguments)
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:326
SpecialPage\outputHeader
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages Per default the message key is the canonical name o...
Definition: SpecialPage.php:709
Wikimedia\Rdbms\ILoadBalancer
Database cluster connection, tracking, load balancing, and transaction manager interface.
Definition: ILoadBalancer.php:81
FormOptions\BOOL
const BOOL
Boolean type, maps guessType() to WebRequest::getBool()
Definition: FormOptions.php:51