Merge "Add support for PHP7 random_bytes in favor of mcrypt_create_iv"
[lhc/web/wiklou.git] / includes / specials / SpecialActiveusers.php
1 <?php
2 /**
3 * Implements Special:Activeusers
4 *
5 * Copyright © 2008 Aaron Schulz
6 *
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.
11 *
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.
16 *
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
21 *
22 * @file
23 * @ingroup SpecialPage
24 */
25
26 /**
27 * Implements Special:Activeusers
28 *
29 * @ingroup SpecialPage
30 */
31 class SpecialActiveUsers extends SpecialPage {
32
33 /**
34 * Constructor
35 */
36 public function __construct() {
37 parent::__construct( 'Activeusers' );
38 }
39
40 /**
41 * Show the special page
42 *
43 * @param string $par Parameter passed to the page or null
44 */
45 public function execute( $par ) {
46 $out = $this->getOutput();
47
48 $this->setHeaders();
49 $this->outputHeader();
50
51 $opts = new FormOptions();
52
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 );
59
60 $opts->fetchValuesFromRequest( $this->getRequest() );
61
62 if ( $par !== null ) {
63 $opts->setValue( 'username', $par );
64 }
65
66 $pager = new ActiveUsersPager( $this->getContext(), $opts );
67 $usersBody = $pager->getBody();
68
69 $this->buildForm();
70
71 if ( $usersBody ) {
72 $out->addHTML(
73 $pager->getNavigationBar() .
74 Html::rawElement( 'ul', [], $usersBody ) .
75 $pager->getNavigationBar()
76 );
77 } else {
78 $out->addWikiMsg( 'activeusers-noresult' );
79 }
80 }
81
82 /**
83 * Generate and output the form
84 */
85 protected function buildForm() {
86 $groups = User::getAllGroups();
87
88 foreach ( $groups as $group ) {
89 $msg = htmlspecialchars( UserGroupMembership::getGroupName( $group ) );
90 $options[$msg] = $group;
91 }
92
93 // Backwards-compatibility with old URLs
94 $req = $this->getRequest();
95 $excludeDefault = [];
96 if ( $req->getCheck( 'hidebots' ) ) {
97 $excludeDefault[] = 'bot';
98 }
99 if ( $req->getCheck( 'hidesysops' ) ) {
100 $excludeDefault[] = 'sysop';
101 }
102
103 $formDescriptor = [
104 'username' => [
105 'type' => 'user',
106 'name' => 'username',
107 'label-message' => 'activeusers-from',
108 ],
109 'groups' => [
110 'type' => 'multiselect',
111 'dropdown' => true,
112 'flatlist' => true,
113 'name' => 'groups',
114 'label-message' => 'activeusers-groups',
115 'options' => $options,
116 ],
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,
125 ],
126 ];
127
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 );
139 }
140
141 /**
142 * Return introductory message.
143 * @return string
144 */
145 protected function getIntroText() {
146 $days = $this->getConfig()->get( 'ActiveUserDays' );
147
148 $intro = $this->msg( 'activeusers-intro' )->numParams( $days )->parse();
149
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__
158 );
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 );
164 }
165 if ( $secondsOld > 0 ) {
166 $intro .= $this->msg( 'cachedspecial-viewing-cached-ttl' )
167 ->durationParams( $secondsOld )->parseAsBlock();
168 }
169 }
170
171 return $intro;
172 }
173
174 protected function getGroupName() {
175 return 'users';
176 }
177 }