Merge "Update docs/hooks.txt for ShowSearchHitTitle"
[mediawiki.git] / includes / specials / SpecialActiveusers.php
bloba01e9b2675d32d97276bf80d119ac9fa0ee60c25
1 <?php
2 /**
3 * Implements Special:Activeusers
5 * Copyright © 2008 Aaron Schulz
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
22 * @file
23 * @ingroup SpecialPage
26 /**
27 * Implements Special:Activeusers
29 * @ingroup SpecialPage
31 class SpecialActiveUsers extends SpecialPage {
33 /**
34 * Constructor
36 public function __construct() {
37 parent::__construct( 'Activeusers' );
40 /**
41 * Show the special page
43 * @param string $par Parameter passed to the page or null
45 public function execute( $par ) {
46 $out = $this->getOutput();
48 $this->setHeaders();
49 $this->outputHeader();
51 $opts = new FormOptions();
53 $opts->add( 'username', '' );
54 $opts->add( 'groups', [] );
55 $opts->add( 'excludegroups', [] );
56 // Backwards-compatibility with old URLs
57 $opts->add( 'hidebots', false, FormOptions::BOOL );
58 $opts->add( 'hidesysops', false, FormOptions::BOOL );
60 $opts->fetchValuesFromRequest( $this->getRequest() );
62 if ( $par !== null ) {
63 $opts->setValue( 'username', $par );
66 $pager = new ActiveUsersPager( $this->getContext(), $opts );
67 $usersBody = $pager->getBody();
69 $this->buildForm();
71 if ( $usersBody ) {
72 $out->addHTML(
73 $pager->getNavigationBar() .
74 Html::rawElement( 'ul', [], $usersBody ) .
75 $pager->getNavigationBar()
77 } else {
78 $out->addWikiMsg( 'activeusers-noresult' );
82 /**
83 * Generate and output the form
85 protected function buildForm() {
86 $groups = User::getAllGroups();
88 foreach ( $groups as $group ) {
89 $msg = htmlspecialchars( User::getGroupName( $group ) );
90 $options[$msg] = $group;
93 // Backwards-compatibility with old URLs
94 $req = $this->getRequest();
95 $excludeDefault = [];
96 if ( $req->getCheck( 'hidebots' ) ) {
97 $excludeDefault[] = 'bot';
99 if ( $req->getCheck( 'hidesysops' ) ) {
100 $excludeDefault[] = 'sysop';
103 $formDescriptor = [
104 'username' => [
105 'type' => 'user',
106 'name' => 'username',
107 'label-message' => 'activeusers-from',
109 'groups' => [
110 'type' => 'multiselect',
111 'dropdown' => true,
112 'flatlist' => true,
113 'name' => 'groups',
114 'label-message' => 'activeusers-groups',
115 'options' => $options,
117 'excludegroups' => [
118 'type' => 'multiselect',
119 'dropdown' => true,
120 'flatlist' => true,
121 'name' => 'excludegroups',
122 'label-message' => 'activeusers-excludegroups',
123 'options' => $options,
124 'default' => $excludeDefault,
128 HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
129 // For the 'multiselect' field values to be preserved on submit
130 ->setFormIdentifier( 'specialactiveusers' )
131 ->setIntro( $this->getIntroText() )
132 ->setWrapperLegendMsg( 'activeusers' )
133 ->setSubmitTextMsg( 'activeusers-submit' )
134 // prevent setting subpage and 'username' parameter at the same time
135 ->setAction( $this->getPageTitle()->getLocalURL() )
136 ->setMethod( 'get' )
137 ->prepareForm()
138 ->displayForm( false );
142 * Return introductory message.
143 * @return string
145 protected function getIntroText() {
146 $days = $this->getConfig()->get( 'ActiveUserDays' );
148 $intro = $this->msg( 'activeusers-intro' )->numParams( $days )->parse();
150 // Mention the level of cache staleness...
151 $dbr = wfGetDB( DB_REPLICA, 'recentchanges' );
152 $rcMax = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', '', __METHOD__ );
153 if ( $rcMax ) {
154 $cTime = $dbr->selectField( 'querycache_info',
155 'qci_timestamp',
156 [ 'qci_type' => 'activeusers' ],
157 __METHOD__
159 if ( $cTime ) {
160 $secondsOld = wfTimestamp( TS_UNIX, $rcMax ) - wfTimestamp( TS_UNIX, $cTime );
161 } else {
162 $rcMin = $dbr->selectField( 'recentchanges', 'MIN(rc_timestamp)' );
163 $secondsOld = time() - wfTimestamp( TS_UNIX, $rcMin );
165 if ( $secondsOld > 0 ) {
166 $intro .= $this->msg( 'cachedspecial-viewing-cached-ttl' )
167 ->durationParams( $secondsOld )->parseAsBlock();
171 return $intro;
174 protected function getGroupName() {
175 return 'users';