Merge "User: Avoid deprecated Linker::link()"
[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 /**
32 * Constructor
33 */
34 public function __construct() {
35 parent::__construct( 'Activeusers' );
36 }
37
38 /**
39 * Show the special page
40 *
41 * @param string $par Parameter passed to the page or null
42 */
43 public function execute( $par ) {
44 $out = $this->getOutput();
45
46 $this->setHeaders();
47 $this->outputHeader();
48
49 $opts = new FormOptions();
50
51 $opts->add( 'username', '' );
52 $opts->add( 'groups', [] );
53 $opts->add( 'excludegroups', [] );
54 // Backwards-compatibility with old URLs
55 $opts->add( 'hidebots', false, FormOptions::BOOL );
56 $opts->add( 'hidesysops', false, FormOptions::BOOL );
57
58 $opts->fetchValuesFromRequest( $this->getRequest() );
59
60 if ( $par !== null ) {
61 $opts->setValue( 'username', $par );
62 }
63
64 $pager = new ActiveUsersPager( $this->getContext(), $opts );
65 $usersBody = $pager->getBody();
66
67 $this->buildForm();
68
69 if ( $usersBody ) {
70 $out->addHTML(
71 $pager->getNavigationBar() .
72 Html::rawElement( 'ul', [], $usersBody ) .
73 $pager->getNavigationBar()
74 );
75 } else {
76 $out->addWikiMsg( 'activeusers-noresult' );
77 }
78 }
79
80 /**
81 * Generate and output the form
82 */
83 protected function buildForm() {
84 $groups = User::getAllGroups();
85
86 foreach ( $groups as $group ) {
87 $msg = htmlspecialchars( UserGroupMembership::getGroupName( $group ) );
88 $options[$msg] = $group;
89 }
90
91 // Backwards-compatibility with old URLs
92 $req = $this->getRequest();
93 $excludeDefault = [];
94 if ( $req->getCheck( 'hidebots' ) ) {
95 $excludeDefault[] = 'bot';
96 }
97 if ( $req->getCheck( 'hidesysops' ) ) {
98 $excludeDefault[] = 'sysop';
99 }
100
101 $formDescriptor = [
102 'username' => [
103 'type' => 'user',
104 'name' => 'username',
105 'label-message' => 'activeusers-from',
106 ],
107 'groups' => [
108 'type' => 'multiselect',
109 'dropdown' => true,
110 'flatlist' => true,
111 'name' => 'groups',
112 'label-message' => 'activeusers-groups',
113 'options' => $options,
114 ],
115 'excludegroups' => [
116 'type' => 'multiselect',
117 'dropdown' => true,
118 'flatlist' => true,
119 'name' => 'excludegroups',
120 'label-message' => 'activeusers-excludegroups',
121 'options' => $options,
122 'default' => $excludeDefault,
123 ],
124 ];
125
126 HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
127 // For the 'multiselect' field values to be preserved on submit
128 ->setFormIdentifier( 'specialactiveusers' )
129 ->setIntro( $this->getIntroText() )
130 ->setWrapperLegendMsg( 'activeusers' )
131 ->setSubmitTextMsg( 'activeusers-submit' )
132 // prevent setting subpage and 'username' parameter at the same time
133 ->setAction( $this->getPageTitle()->getLocalURL() )
134 ->setMethod( 'get' )
135 ->prepareForm()
136 ->displayForm( false );
137 }
138
139 /**
140 * Return introductory message.
141 * @return string
142 */
143 protected function getIntroText() {
144 $days = $this->getConfig()->get( 'ActiveUserDays' );
145
146 $intro = $this->msg( 'activeusers-intro' )->numParams( $days )->parse();
147
148 // Mention the level of cache staleness...
149 $dbr = wfGetDB( DB_REPLICA, 'recentchanges' );
150 $rcMax = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', '', __METHOD__ );
151 if ( $rcMax ) {
152 $cTime = $dbr->selectField( 'querycache_info',
153 'qci_timestamp',
154 [ 'qci_type' => 'activeusers' ],
155 __METHOD__
156 );
157 if ( $cTime ) {
158 $secondsOld = wfTimestamp( TS_UNIX, $rcMax ) - wfTimestamp( TS_UNIX, $cTime );
159 } else {
160 $rcMin = $dbr->selectField( 'recentchanges', 'MIN(rc_timestamp)' );
161 $secondsOld = time() - wfTimestamp( TS_UNIX, $rcMin );
162 }
163 if ( $secondsOld > 0 ) {
164 $intro .= $this->msg( 'cachedspecial-viewing-cached-ttl' )
165 ->durationParams( $secondsOld )->parseAsBlock();
166 }
167 }
168
169 return $intro;
170 }
171
172 protected function getGroupName() {
173 return 'users';
174 }
175 }