Merge "Http::getProxy() method to get proxy configuration"
[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 * This class is used to get a list of active users. The ones with specials
28 * rights (sysop, bureaucrat, developer) will have them displayed
29 * next to their names.
30 *
31 * @ingroup SpecialPage
32 */
33 class ActiveUsersPager extends UsersPager {
34 /**
35 * @var FormOptions
36 */
37 protected $opts;
38
39 /**
40 * @var array
41 */
42 protected $hideGroups = [];
43
44 /**
45 * @var array
46 */
47 protected $hideRights = [];
48
49 /**
50 * @var array
51 */
52 private $blockStatusByUid;
53
54 /**
55 * @param IContextSource $context
56 * @param null $group Unused
57 * @param string $par Parameter passed to the page
58 */
59 function __construct( IContextSource $context = null, $group = null, $par = null ) {
60 parent::__construct( $context );
61
62 $this->RCMaxAge = $this->getConfig()->get( 'ActiveUserDays' );
63 $un = $this->getRequest()->getText( 'username', $par );
64 $this->requestedUser = '';
65 if ( $un != '' ) {
66 $username = Title::makeTitleSafe( NS_USER, $un );
67 if ( !is_null( $username ) ) {
68 $this->requestedUser = $username->getText();
69 }
70 }
71
72 $this->setupOptions();
73 }
74
75 public function setupOptions() {
76 $this->opts = new FormOptions();
77
78 $this->opts->add( 'hidebots', false, FormOptions::BOOL );
79 $this->opts->add( 'hidesysops', false, FormOptions::BOOL );
80
81 $this->opts->fetchValuesFromRequest( $this->getRequest() );
82
83 if ( $this->opts->getValue( 'hidebots' ) == 1 ) {
84 $this->hideRights[] = 'bot';
85 }
86 if ( $this->opts->getValue( 'hidesysops' ) == 1 ) {
87 $this->hideGroups[] = 'sysop';
88 }
89 }
90
91 function getIndexField() {
92 return 'qcc_title';
93 }
94
95 function getQueryInfo() {
96 $dbr = $this->getDatabase();
97
98 $activeUserSeconds = $this->getConfig()->get( 'ActiveUserDays' ) * 86400;
99 $timestamp = $dbr->timestamp( wfTimestamp( TS_UNIX ) - $activeUserSeconds );
100 $conds = [
101 'qcc_type' => 'activeusers',
102 'qcc_namespace' => NS_USER,
103 'user_name = qcc_title',
104 'rc_user_text = qcc_title',
105 'rc_type != ' . $dbr->addQuotes( RC_EXTERNAL ), // Don't count wikidata.
106 'rc_type != ' . $dbr->addQuotes( RC_CATEGORIZE ), // Don't count categorization changes.
107 'rc_log_type IS NULL OR rc_log_type != ' . $dbr->addQuotes( 'newusers' ),
108 'rc_timestamp >= ' . $dbr->addQuotes( $timestamp ),
109 ];
110 if ( $this->requestedUser != '' ) {
111 $conds[] = 'qcc_title >= ' . $dbr->addQuotes( $this->requestedUser );
112 }
113 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
114 $conds[] = 'NOT EXISTS (' . $dbr->selectSQLText(
115 'ipblocks', '1', [ 'ipb_user=user_id', 'ipb_deleted' => 1 ]
116 ) . ')';
117 }
118
119 if ( $dbr->implicitGroupby() ) {
120 $options = [ 'GROUP BY' => [ 'qcc_title' ] ];
121 } else {
122 $options = [ 'GROUP BY' => [ 'user_name', 'user_id', 'qcc_title' ] ];
123 }
124
125 return [
126 'tables' => [ 'querycachetwo', 'user', 'recentchanges' ],
127 'fields' => [ 'user_name', 'user_id', 'recentedits' => 'COUNT(*)', 'qcc_title' ],
128 'options' => $options,
129 'conds' => $conds
130 ];
131 }
132
133 function doBatchLookups() {
134 parent::doBatchLookups();
135
136 $uids = [];
137 foreach ( $this->mResult as $row ) {
138 $uids[] = $row->user_id;
139 }
140 // Fetch the block status of the user for showing "(blocked)" text and for
141 // striking out names of suppressed users when privileged user views the list.
142 // Although the first query already hits the block table for un-privileged, this
143 // is done in two queries to avoid huge quicksorts and to make COUNT(*) correct.
144 $dbr = $this->getDatabase();
145 $res = $dbr->select( 'ipblocks',
146 [ 'ipb_user', 'MAX(ipb_deleted) AS block_status' ],
147 [ 'ipb_user' => $uids ],
148 __METHOD__,
149 [ 'GROUP BY' => [ 'ipb_user' ] ]
150 );
151 $this->blockStatusByUid = [];
152 foreach ( $res as $row ) {
153 $this->blockStatusByUid[$row->ipb_user] = $row->block_status; // 0 or 1
154 }
155 $this->mResult->seek( 0 );
156 }
157
158 function formatRow( $row ) {
159 $userName = $row->user_name;
160
161 $ulinks = Linker::userLink( $row->user_id, $userName );
162 $ulinks .= Linker::userToolLinks( $row->user_id, $userName );
163
164 $lang = $this->getLanguage();
165
166 $list = [];
167 $user = User::newFromId( $row->user_id );
168
169 // User right filter
170 foreach ( $this->hideRights as $right ) {
171 // Calling User::getRights() within the loop so that
172 // if the hideRights() filter is empty, we don't have to
173 // trigger the lazy-init of the big userrights array in the
174 // User object
175 if ( in_array( $right, $user->getRights() ) ) {
176 return '';
177 }
178 }
179
180 // User group filter
181 // Note: This is a different loop than for user rights,
182 // because we're reusing it to build the group links
183 // at the same time
184 $groups_list = self::getGroups( intval( $row->user_id ), $this->userGroupCache );
185 foreach ( $groups_list as $group ) {
186 if ( in_array( $group, $this->hideGroups ) ) {
187 return '';
188 }
189 $list[] = self::buildGroupLink( $group, $userName );
190 }
191
192 $groups = $lang->commaList( $list );
193
194 $item = $lang->specialList( $ulinks, $groups );
195
196 $isBlocked = isset( $this->blockStatusByUid[$row->user_id] );
197 if ( $isBlocked && $this->blockStatusByUid[$row->user_id] == 1 ) {
198 $item = "<span class=\"deleted\">$item</span>";
199 }
200 $count = $this->msg( 'activeusers-count' )->numParams( $row->recentedits )
201 ->params( $userName )->numParams( $this->RCMaxAge )->escaped();
202 $blocked = $isBlocked ? ' ' . $this->msg( 'listusers-blocked', $userName )->escaped() : '';
203
204 return Html::rawElement( 'li', [], "{$item} [{$count}]{$blocked}" );
205 }
206
207 function getPageHeader() {
208 $self = $this->getTitle();
209 $limit = $this->mLimit ? Html::hidden( 'limit', $this->mLimit ) : '';
210
211 # Form tag
212 $out = Xml::openElement( 'form', [ 'method' => 'get', 'action' => wfScript() ] );
213 $out .= Xml::fieldset( $this->msg( 'activeusers' )->text() ) . "\n";
214 $out .= Html::hidden( 'title', $self->getPrefixedDBkey() ) . $limit . "\n";
215
216 # Username field (with autocompletion support)
217 $this->getOutput()->addModules( 'mediawiki.userSuggest' );
218 $out .= Xml::inputLabel(
219 $this->msg( 'activeusers-from' )->text(),
220 'username',
221 'offset',
222 20,
223 $this->requestedUser,
224 [
225 'class' => 'mw-ui-input-inline mw-autocomplete-user',
226 'tabindex' => 1,
227 ] + (
228 // Set autofocus on blank input
229 $this->requestedUser === '' ? [ 'autofocus' => '' ] : []
230 )
231 ) . '<br />';
232
233 $out .= Xml::checkLabel( $this->msg( 'activeusers-hidebots' )->text(),
234 'hidebots', 'hidebots', $this->opts->getValue( 'hidebots' ), [ 'tabindex' => 2 ] );
235
236 $out .= Xml::checkLabel(
237 $this->msg( 'activeusers-hidesysops' )->text(),
238 'hidesysops',
239 'hidesysops',
240 $this->opts->getValue( 'hidesysops' ),
241 [ 'tabindex' => 3 ]
242 ) . '<br />';
243
244 # Submit button and form bottom
245 $out .= Xml::submitButton(
246 $this->msg( 'activeusers-submit' )->text(),
247 [ 'tabindex' => 4 ]
248 ) . "\n";
249 $out .= Xml::closeElement( 'fieldset' );
250 $out .= Xml::closeElement( 'form' );
251
252 return $out;
253 }
254 }
255
256 /**
257 * @ingroup SpecialPage
258 */
259 class SpecialActiveUsers extends SpecialPage {
260
261 /**
262 * Constructor
263 */
264 public function __construct() {
265 parent::__construct( 'Activeusers' );
266 }
267
268 /**
269 * Show the special page
270 *
271 * @param string $par Parameter passed to the page or null
272 */
273 public function execute( $par ) {
274 $days = $this->getConfig()->get( 'ActiveUserDays' );
275
276 $this->setHeaders();
277 $this->outputHeader();
278
279 $out = $this->getOutput();
280 $out->wrapWikiMsg( "<div class='mw-activeusers-intro'>\n$1\n</div>",
281 [ 'activeusers-intro', $this->getLanguage()->formatNum( $days ) ] );
282
283 // Mention the level of cache staleness...
284 $dbr = wfGetDB( DB_SLAVE, 'recentchanges' );
285 $rcMax = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', '', __METHOD__ );
286 if ( $rcMax ) {
287 $cTime = $dbr->selectField( 'querycache_info',
288 'qci_timestamp',
289 [ 'qci_type' => 'activeusers' ],
290 __METHOD__
291 );
292 if ( $cTime ) {
293 $secondsOld = wfTimestamp( TS_UNIX, $rcMax ) - wfTimestamp( TS_UNIX, $cTime );
294 } else {
295 $rcMin = $dbr->selectField( 'recentchanges', 'MIN(rc_timestamp)' );
296 $secondsOld = time() - wfTimestamp( TS_UNIX, $rcMin );
297 }
298 if ( $secondsOld > 0 ) {
299 $out->addWikiMsg( 'cachedspecial-viewing-cached-ttl',
300 $this->getLanguage()->formatDuration( $secondsOld ) );
301 }
302 }
303
304 $up = new ActiveUsersPager( $this->getContext(), null, $par );
305
306 # getBody() first to check, if empty
307 $usersbody = $up->getBody();
308
309 $out->addHTML( $up->getPageHeader() );
310 if ( $usersbody ) {
311 $out->addHTML(
312 $up->getNavigationBar() .
313 Html::rawElement( 'ul', [], $usersbody ) .
314 $up->getNavigationBar()
315 );
316 } else {
317 $out->addWikiMsg( 'activeusers-noresult' );
318 }
319 }
320
321 protected function getGroupName() {
322 return 'users';
323 }
324 }