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