Merge "Align "What's this" vertically"
[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 foreach ( $groups as $group ) {
84 $msg = htmlspecialchars( UserGroupMembership::getGroupName( $group ) );
85 $options[$msg] = $group;
86 }
87
88 // Backwards-compatibility with old URLs
89 $req = $this->getRequest();
90 $excludeDefault = [];
91 if ( $req->getCheck( 'hidebots' ) ) {
92 $excludeDefault[] = 'bot';
93 }
94 if ( $req->getCheck( 'hidesysops' ) ) {
95 $excludeDefault[] = 'sysop';
96 }
97
98 $formDescriptor = [
99 'username' => [
100 'type' => 'user',
101 'name' => 'username',
102 'label-message' => 'activeusers-from',
103 ],
104 'groups' => [
105 'type' => 'multiselect',
106 'dropdown' => true,
107 'flatlist' => true,
108 'name' => 'groups',
109 'label-message' => 'activeusers-groups',
110 'options' => $options,
111 ],
112 'excludegroups' => [
113 'type' => 'multiselect',
114 'dropdown' => true,
115 'flatlist' => true,
116 'name' => 'excludegroups',
117 'label-message' => 'activeusers-excludegroups',
118 'options' => $options,
119 'default' => $excludeDefault,
120 ],
121 ];
122
123 HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
124 // For the 'multiselect' field values to be preserved on submit
125 ->setFormIdentifier( 'specialactiveusers' )
126 ->setIntro( $this->getIntroText() )
127 ->setWrapperLegendMsg( 'activeusers' )
128 ->setSubmitTextMsg( 'activeusers-submit' )
129 // prevent setting subpage and 'username' parameter at the same time
130 ->setAction( $this->getPageTitle()->getLocalURL() )
131 ->setMethod( 'get' )
132 ->prepareForm()
133 ->displayForm( false );
134 }
135
136 /**
137 * Return introductory message.
138 * @return string
139 */
140 protected function getIntroText() {
141 $days = $this->getConfig()->get( 'ActiveUserDays' );
142
143 $intro = $this->msg( 'activeusers-intro' )->numParams( $days )->parse();
144
145 // Mention the level of cache staleness...
146 $dbr = wfGetDB( DB_REPLICA, 'recentchanges' );
147 $rcMax = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', '', __METHOD__ );
148 if ( $rcMax ) {
149 $cTime = $dbr->selectField( 'querycache_info',
150 'qci_timestamp',
151 [ 'qci_type' => 'activeusers' ],
152 __METHOD__
153 );
154 if ( $cTime ) {
155 $secondsOld = wfTimestamp( TS_UNIX, $rcMax ) - wfTimestamp( TS_UNIX, $cTime );
156 } else {
157 $rcMin = $dbr->selectField( 'recentchanges', 'MIN(rc_timestamp)' );
158 $secondsOld = time() - wfTimestamp( TS_UNIX, $rcMin );
159 }
160 if ( $secondsOld > 0 ) {
161 $intro .= $this->msg( 'cachedspecial-viewing-cached-ttl' )
162 ->durationParams( $secondsOld )->parseAsBlock();
163 }
164 }
165
166 return $intro;
167 }
168
169 protected function getGroupName() {
170 return 'users';
171 }
172 }