summaryrefslogtreecommitdiff
path: root/includes/specials/SpecialActiveusers.php
blob: f983b4526fa705eb8e0d9f2258230507e46338c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
<?php
/**
 * Implements Special:Activeusers
 *
 * Copyright © 2008 Aaron Schulz
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 * @ingroup SpecialPage
 */

/**
 * This class is used to get a list of active users. The ones with specials
 * rights (sysop, bureaucrat, developer) will have them displayed
 * next to their names.
 *
 * @ingroup SpecialPage
 */
class ActiveUsersPager extends UsersPager {
	/**
	 * @var FormOptions
	 */
	protected $opts;

	/**
	 * @var array
	 */
	protected $hideGroups = array();

	/**
	 * @var array
	 */
	protected $hideRights = array();

	/**
	 * @var array
	 */
	private $blockStatusByUid;

	/**
	 * @param IContextSource $context
	 * @param null $group Unused
	 * @param string $par Parameter passed to the page
	 */
	function __construct( IContextSource $context = null, $group = null, $par = null ) {
		parent::__construct( $context );

		$this->RCMaxAge = $this->getConfig()->get( 'ActiveUserDays' );
		$un = $this->getRequest()->getText( 'username', $par );
		$this->requestedUser = '';
		if ( $un != '' ) {
			$username = Title::makeTitleSafe( NS_USER, $un );
			if ( !is_null( $username ) ) {
				$this->requestedUser = $username->getText();
			}
		}

		$this->setupOptions();
	}

	public function setupOptions() {
		$this->opts = new FormOptions();

		$this->opts->add( 'hidebots', false, FormOptions::BOOL );
		$this->opts->add( 'hidesysops', false, FormOptions::BOOL );

		$this->opts->fetchValuesFromRequest( $this->getRequest() );

		if ( $this->opts->getValue( 'hidebots' ) == 1 ) {
			$this->hideRights[] = 'bot';
		}
		if ( $this->opts->getValue( 'hidesysops' ) == 1 ) {
			$this->hideGroups[] = 'sysop';
		}
	}

	function getIndexField() {
		return 'qcc_title';
	}

	function getQueryInfo() {
		$dbr = $this->getDatabase();

		$activeUserSeconds = $this->getConfig()->get( 'ActiveUserDays' ) * 86400;
		$timestamp = $dbr->timestamp( wfTimestamp( TS_UNIX ) - $activeUserSeconds );
		$conds = array(
			'qcc_type' => 'activeusers',
			'qcc_namespace' => NS_USER,
			'user_name = qcc_title',
			'rc_user_text = qcc_title',
			'rc_type != ' . $dbr->addQuotes( RC_EXTERNAL ), // Don't count wikidata.
			'rc_log_type IS NULL OR rc_log_type != ' . $dbr->addQuotes( 'newusers' ),
			'rc_timestamp >= ' . $dbr->addQuotes( $timestamp ),
		);
		if ( $this->requestedUser != '' ) {
			$conds[] = 'qcc_title >= ' . $dbr->addQuotes( $this->requestedUser );
		}
		if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
			$conds[] = 'NOT EXISTS (' . $dbr->selectSQLText(
				'ipblocks', '1', array( 'ipb_user=user_id', 'ipb_deleted' => 1 )
			) . ')';
		}

		if ( $dbr->implicitGroupby() ) {
			$options = array( 'GROUP BY' => array( 'qcc_title' ) );
		} else {
			$options = array( 'GROUP BY' => array( 'user_name', 'user_id', 'qcc_title' ) );
		}

		return array(
			'tables' => array( 'querycachetwo', 'user', 'recentchanges' ),
			'fields' => array( 'user_name', 'user_id', 'recentedits' => 'COUNT(*)', 'qcc_title' ),
			'options' => $options,
			'conds' => $conds
		);
	}

	function doBatchLookups() {
		$uids = array();
		foreach ( $this->mResult as $row ) {
			$uids[] = $row->user_id;
		}
		// Fetch the block status of the user for showing "(blocked)" text and for
		// striking out names of suppressed users when privileged user views the list.
		// Although the first query already hits the block table for un-privileged, this
		// is done in two queries to avoid huge quicksorts and to make COUNT(*) correct.
		$dbr = $this->getDatabase();
		$res = $dbr->select( 'ipblocks',
			array( 'ipb_user', 'MAX(ipb_deleted) AS block_status' ),
			array( 'ipb_user' => $uids ),
			__METHOD__,
			array( 'GROUP BY' => array( 'ipb_user' ) )
		);
		$this->blockStatusByUid = array();
		foreach ( $res as $row ) {
			$this->blockStatusByUid[$row->ipb_user] = $row->block_status; // 0 or 1
		}
		$this->mResult->seek( 0 );
	}

	function formatRow( $row ) {
		$userName = $row->user_name;

		$ulinks = Linker::userLink( $row->user_id, $userName );
		$ulinks .= Linker::userToolLinks( $row->user_id, $userName );

		$lang = $this->getLanguage();

		$list = array();
		$user = User::newFromId( $row->user_id );

		// User right filter
		foreach ( $this->hideRights as $right ) {
			// Calling User::getRights() within the loop so that
			// if the hideRights() filter is empty, we don't have to
			// trigger the lazy-init of the big userrights array in the
			// User object
			if ( in_array( $right, $user->getRights() ) ) {
				return '';
			}
		}

		// User group filter
		// Note: This is a different loop than for user rights,
		// because we're reusing it to build the group links
		// at the same time
		foreach ( $user->getGroups() as $group ) {
			if ( in_array( $group, $this->hideGroups ) ) {
				return '';
			}
			$list[] = self::buildGroupLink( $group, $userName );
		}

		$groups = $lang->commaList( $list );

		$item = $lang->specialList( $ulinks, $groups );

		$isBlocked = isset( $this->blockStatusByUid[$row->user_id] );
		if ( $isBlocked && $this->blockStatusByUid[$row->user_id] == 1 ) {
			$item = "<span class=\"deleted\">$item</span>";
		}
		$count = $this->msg( 'activeusers-count' )->numParams( $row->recentedits )
			->params( $userName )->numParams( $this->RCMaxAge )->escaped();
		$blocked = $isBlocked ? ' ' . $this->msg( 'listusers-blocked', $userName )->escaped() : '';

		return Html::rawElement( 'li', array(), "{$item} [{$count}]{$blocked}" );
	}

	function getPageHeader() {
		$self = $this->getTitle();
		$limit = $this->mLimit ? Html::hidden( 'limit', $this->mLimit ) : '';

		# Form tag
		$out = Xml::openElement( 'form', array( 'method' => 'get', 'action' => wfScript() ) );
		$out .= Xml::fieldset( $this->msg( 'activeusers' )->text() ) . "\n";
		$out .= Html::hidden( 'title', $self->getPrefixedDBkey() ) . $limit . "\n";

		# Username field
		$out .= Xml::inputLabel( $this->msg( 'activeusers-from' )->text(),
			'username', 'offset', 20, $this->requestedUser, array( 'tabindex' => 1 ) ) . '<br />';

		$out .= Xml::checkLabel( $this->msg( 'activeusers-hidebots' )->text(),
			'hidebots', 'hidebots', $this->opts->getValue( 'hidebots' ), array( 'tabindex' => 2 ) );

		$out .= Xml::checkLabel(
			$this->msg( 'activeusers-hidesysops' )->text(),
			'hidesysops',
			'hidesysops',
			$this->opts->getValue( 'hidesysops' ),
			array( 'tabindex' => 3 )
		) . '<br />';

		# Submit button and form bottom
		$out .= Xml::submitButton(
			$this->msg( 'allpagessubmit' )->text(),
			array( 'tabindex' => 4 )
		) . "\n";
		$out .= Xml::closeElement( 'fieldset' );
		$out .= Xml::closeElement( 'form' );

		return $out;
	}
}

/**
 * @ingroup SpecialPage
 */
class SpecialActiveUsers extends SpecialPage {

	/**
	 * Constructor
	 */
	public function __construct() {
		parent::__construct( 'Activeusers' );
	}

	/**
	 * Show the special page
	 *
	 * @param string $par Parameter passed to the page or null
	 */
	public function execute( $par ) {
		$days = $this->getConfig()->get( 'ActiveUserDays' );

		$this->setHeaders();
		$this->outputHeader();

		$out = $this->getOutput();
		$out->wrapWikiMsg( "<div class='mw-activeusers-intro'>\n$1\n</div>",
			array( 'activeusers-intro', $this->getLanguage()->formatNum( $days ) ) );

		// Occasionally merge in new updates
		$seconds = min( self::mergeActiveUsers( 600, $days ), $days * 86400 );
		// Mention the level of staleness
		$out->addWikiMsg( 'cachedspecial-viewing-cached-ttl',
			$this->getLanguage()->formatDuration( $seconds ) );

		$up = new ActiveUsersPager( $this->getContext(), null, $par );

		# getBody() first to check, if empty
		$usersbody = $up->getBody();

		$out->addHTML( $up->getPageHeader() );
		if ( $usersbody ) {
			$out->addHTML(
				$up->getNavigationBar() .
				Html::rawElement( 'ul', array(), $usersbody ) .
				$up->getNavigationBar()
			);
		} else {
			$out->addWikiMsg( 'activeusers-noresult' );
		}
	}

	protected function getGroupName() {
		return 'users';
	}

	/**
	 * @param int $period Seconds (do updates no more often than this)
	 * @param int $days How many days user must be idle before he is considered inactive
	 * @return int How many seconds old the cache is
	 */
	public static function mergeActiveUsers( $period, $days ) {
		$dbr = wfGetDB( DB_SLAVE );
		$cTime = $dbr->selectField( 'querycache_info',
			'qci_timestamp',
			array( 'qci_type' => 'activeusers' )
		);

		if ( !wfReadOnly() ) {
			if ( !$cTime || ( time() - wfTimestamp( TS_UNIX, $cTime ) ) > $period ) {
				$dbw = wfGetDB( DB_MASTER );
				if ( $dbw->estimateRowCount( 'recentchanges' ) <= 10000 ) {
					$window = $days * 86400; // small wiki
				} else {
					$window = $period * 2;
				}
				$cTime = self::doQueryCacheUpdate( $dbw, $days, $window ) ?: $cTime;
			}
		}

		return ( time() -
			( $cTime ? wfTimestamp( TS_UNIX, $cTime ) : $days * 86400 ) );
	}

	/**
	 * @param DatabaseBase $dbw Passed in from updateSpecialPages.php
	 * @return void
	 */
	public static function cacheUpdate( DatabaseBase $dbw ) {
		global $wgActiveUserDays;

		self::doQueryCacheUpdate( $dbw, $wgActiveUserDays, $wgActiveUserDays * 86400 );
	}

	/**
	 * Update the query cache as needed
	 *
	 * @param DatabaseBase $dbw
	 * @param int $days How many days user must be idle before he is considered inactive
	 * @param int $window Maximum time range of new data to scan (in seconds)
	 * @return int|bool UNIX timestamp the cache is now up-to-date as of (false on error)
	 */
	protected static function doQueryCacheUpdate( DatabaseBase $dbw, $days, $window ) {
		$lockKey = wfWikiID() . '-activeusers';
		if ( !$dbw->lock( $lockKey, __METHOD__, 1 ) ) {
			return false; // exclusive update (avoids duplicate entries)
		}

		$now = time();
		$cTime = $dbw->selectField( 'querycache_info',
			'qci_timestamp',
			array( 'qci_type' => 'activeusers' )
		);
		$cTimeUnix = $cTime ? wfTimestamp( TS_UNIX, $cTime ) : 1;

		// Pick the date range to fetch from. This is normally from the last
		// update to till the present time, but has a limited window for sanity.
		// If the window is limited, multiple runs are need to fully populate it.
		$sTimestamp = max( $cTimeUnix, $now - $days * 86400 );
		$eTimestamp = min( $sTimestamp + $window, $now );

		// Get all the users active since the last update
		$res = $dbw->select(
			array( 'recentchanges' ),
			array( 'rc_user_text', 'lastedittime' => 'MAX(rc_timestamp)' ),
			array(
				'rc_user > 0', // actual accounts
				'rc_type != ' . $dbw->addQuotes( RC_EXTERNAL ), // no wikidata
				'rc_log_type IS NULL OR rc_log_type != ' . $dbw->addQuotes( 'newusers' ),
				'rc_timestamp >= ' . $dbw->addQuotes( $dbw->timestamp( $sTimestamp ) ),
				'rc_timestamp <= ' . $dbw->addQuotes( $dbw->timestamp( $eTimestamp ) )
			),
			__METHOD__,
			array(
				'GROUP BY' => array( 'rc_user_text' ),
				'ORDER BY' => 'NULL' // avoid filesort
			)
		);
		$names = array();
		foreach ( $res as $row ) {
			$names[$row->rc_user_text] = $row->lastedittime;
		}

		// Rotate out users that have not edited in too long (according to old data set)
		$dbw->delete( 'querycachetwo',
			array(
				'qcc_type' => 'activeusers',
				'qcc_value < ' . $dbw->addQuotes( $now - $days * 86400 ) // TS_UNIX
			),
			__METHOD__
		);

		// Find which of the recently active users are already accounted for
		if ( count( $names ) ) {
			$res = $dbw->select( 'querycachetwo',
				array( 'user_name' => 'qcc_title' ),
				array(
					'qcc_type' => 'activeusers',
					'qcc_namespace' => NS_USER,
					'qcc_title' => array_keys( $names ) ),
				__METHOD__
			);
			foreach ( $res as $row ) {
				unset( $names[$row->user_name] );
			}
		}

		// Insert the users that need to be added to the list (which their last edit time
		if ( count( $names ) ) {
			$newRows = array();
			foreach ( $names as $name => $lastEditTime ) {
				$newRows[] = array(
					'qcc_type' => 'activeusers',
					'qcc_namespace' => NS_USER,
					'qcc_title' => $name,
					'qcc_value' => wfTimestamp( TS_UNIX, $lastEditTime ),
					'qcc_namespacetwo' => 0, // unused
					'qcc_titletwo' => '' // unused
				);
			}
			foreach ( array_chunk( $newRows, 500 ) as $rowBatch ) {
				$dbw->insert( 'querycachetwo', $rowBatch, __METHOD__ );
				if ( !$dbw->trxLevel() ) {
					wfWaitForSlaves();
				}
			}
		}

		// Touch the data freshness timestamp
		$dbw->replace( 'querycache_info',
			array( 'qci_type' ),
			array( 'qci_type' => 'activeusers',
				'qci_timestamp' => $dbw->timestamp( $eTimestamp ) ), // not always $now
			__METHOD__
		);

		$dbw->unlock( $lockKey, __METHOD__ );

		return $eTimestamp;
	}
}