MediaWiki  master
SpecialActiveUsers.php
Go to the documentation of this file.
1 <?php
24 namespace MediaWiki\Specials;
25 
26 use HTMLForm;
36 
43 
44  private LinkBatchFactory $linkBatchFactory;
45  private IConnectionProvider $dbProvider;
46  private UserGroupManager $userGroupManager;
47  private UserIdentityLookup $userIdentityLookup;
48 
55  public function __construct(
56  LinkBatchFactory $linkBatchFactory,
57  IConnectionProvider $dbProvider,
58  UserGroupManager $userGroupManager,
59  UserIdentityLookup $userIdentityLookup
60  ) {
61  parent::__construct( 'Activeusers' );
62  $this->linkBatchFactory = $linkBatchFactory;
63  $this->dbProvider = $dbProvider;
64  $this->userGroupManager = $userGroupManager;
65  $this->userIdentityLookup = $userIdentityLookup;
66  }
67 
71  public function execute( $par ) {
72  $out = $this->getOutput();
73 
74  $this->setHeaders();
75  $this->outputHeader();
76 
77  $opts = new FormOptions();
78 
79  $opts->add( 'username', '' );
80  $opts->add( 'groups', [] );
81  $opts->add( 'excludegroups', [] );
82  // Backwards-compatibility with old URLs
83  $opts->add( 'hidebots', false, FormOptions::BOOL );
84  $opts->add( 'hidesysops', false, FormOptions::BOOL );
85 
86  $opts->fetchValuesFromRequest( $this->getRequest() );
87 
88  if ( $par !== null ) {
89  $opts->setValue( 'username', $par );
90  }
91 
92  $pager = new ActiveUsersPager(
93  $this->getContext(),
94  $this->getHookContainer(),
95  $this->linkBatchFactory,
96  $this->dbProvider,
97  $this->userGroupManager,
98  $this->userIdentityLookup,
99  $opts
100  );
101  $usersBody = $pager->getBody();
102 
103  $this->buildForm();
104 
105  if ( $usersBody ) {
106  $out->addHTML(
107  $pager->getNavigationBar() .
108  Html::rawElement( 'ul', [], $usersBody ) .
109  $pager->getNavigationBar()
110  );
111  $out->addModuleStyles( 'mediawiki.interface.helpers.styles' );
112  } else {
113  $out->addWikiMsg( 'activeusers-noresult' );
114  }
115  }
116 
120  protected function buildForm() {
121  $groups = $this->userGroupManager->listAllGroups();
122 
123  $options = [];
124  $lang = $this->getLanguage();
125  foreach ( $groups as $group ) {
126  $msg = htmlspecialchars( $lang->getGroupName( $group ) );
127  $options[$msg] = $group;
128  }
129  ksort( $options );
130 
131  // Backwards-compatibility with old URLs
132  $req = $this->getRequest();
133  $excludeDefault = [];
134  if ( $req->getCheck( 'hidebots' ) ) {
135  $excludeDefault[] = 'bot';
136  }
137  if ( $req->getCheck( 'hidesysops' ) ) {
138  $excludeDefault[] = 'sysop';
139  }
140 
141  $formDescriptor = [
142  'username' => [
143  'type' => 'user',
144  'name' => 'username',
145  'label-message' => 'activeusers-from',
146  ],
147  'groups' => [
148  'type' => 'multiselect',
149  'dropdown' => true,
150  'flatlist' => true,
151  'name' => 'groups',
152  'label-message' => 'activeusers-groups',
153  'options' => $options,
154  ],
155  'excludegroups' => [
156  'type' => 'multiselect',
157  'dropdown' => true,
158  'flatlist' => true,
159  'name' => 'excludegroups',
160  'label-message' => 'activeusers-excludegroups',
161  'options' => $options,
162  'default' => $excludeDefault,
163  ],
164  ];
165 
166  HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
167  // For the 'multiselect' field values to be preserved on submit
168  ->setFormIdentifier( 'specialactiveusers' )
169  ->setPreHtml( $this->getIntroText() )
170  ->setWrapperLegendMsg( 'activeusers' )
171  ->setSubmitTextMsg( 'activeusers-submit' )
172  // prevent setting subpage and 'username' parameter at the same time
173  ->setTitle( $this->getPageTitle() )
174  ->setMethod( 'get' )
175  ->prepareForm()
176  ->displayForm( false );
177  }
178 
183  protected function getIntroText() {
184  $days = $this->getConfig()->get( MainConfigNames::ActiveUserDays );
185 
186  $intro = $this->msg( 'activeusers-intro' )->numParams( $days )->parse();
187 
188  // Mention the level of cache staleness...
189  $dbr = $this->dbProvider->getReplicaDatabase();
190 
191  $rcMax = $dbr->newSelectQueryBuilder()
192  ->select( 'MAX(rc_timestamp)' )
193  ->from( 'recentchanges' )
194  ->caller( __METHOD__ )->fetchField();
195  if ( $rcMax ) {
196  $cTime = $dbr->newSelectQueryBuilder()
197  ->select( 'qci_timestamp' )
198  ->from( 'querycache_info' )
199  ->where( [ 'qci_type' => 'activeusers' ] )
200  ->caller( __METHOD__ )->fetchField();
201  if ( $cTime ) {
202  $secondsOld = (int)wfTimestamp( TS_UNIX, $rcMax ) - (int)wfTimestamp( TS_UNIX, $cTime );
203  } else {
204  $rcMin = $dbr->newSelectQueryBuilder()
205  ->select( 'MIN(rc_timestamp)' )
206  ->from( 'recentchanges' )
207  ->caller( __METHOD__ )->fetchField();
208  $secondsOld = time() - (int)wfTimestamp( TS_UNIX, $rcMin );
209  }
210  if ( $secondsOld > 0 ) {
211  $intro .= $this->msg( 'cachedspecial-viewing-cached-ttl' )
212  ->durationParams( $secondsOld )->parseAsBlock();
213  }
214  }
215 
216  return $intro;
217  }
218 
219  protected function getGroupName() {
220  return 'users';
221  }
222 }
223 
227 class_alias( SpecialActiveUsers::class, 'SpecialActiveUsers' );
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition: HTMLForm.php:158
static factory( $displayFormat, $descriptor, IContextSource $context, $messagePrefix='')
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:360
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:57
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:239
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.
Definition: SpecialPage.php:66
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 Per default the message key is the canonical name o...
Implements Special:Activeusers.
__construct(LinkBatchFactory $linkBatchFactory, IConnectionProvider $dbProvider, 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.
Provide primary and replica IDatabase connections.