07e1be19339f2c9e90d889f96eb8b690f1e6eb78
3 * Implements Special:Activeusers
5 * Copyright © 2008 Aaron Schulz
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.
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.
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
23 * @ingroup SpecialPage
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.
31 * @ingroup SpecialPage
33 class ActiveUsersPager
extends UsersPager
{
42 protected $hideGroups = array();
47 protected $hideRights = array();
52 private $blockStatusByUid;
55 * @param IContextSource $context
56 * @param null $group Unused
57 * @param string $par Parameter passed to the page
59 function __construct( IContextSource
$context = null, $group = null, $par = null ) {
60 parent
::__construct( $context );
62 $this->RCMaxAge
= $this->getConfig()->get( 'ActiveUserDays' );
63 $un = $this->getRequest()->getText( 'username', $par );
64 $this->requestedUser
= '';
66 $username = Title
::makeTitleSafe( NS_USER
, $un );
67 if ( !is_null( $username ) ) {
68 $this->requestedUser
= $username->getText();
72 $this->setupOptions();
75 public function setupOptions() {
76 $this->opts
= new FormOptions();
78 $this->opts
->add( 'hidebots', false, FormOptions
::BOOL );
79 $this->opts
->add( 'hidesysops', false, FormOptions
::BOOL );
81 $this->opts
->fetchValuesFromRequest( $this->getRequest() );
83 if ( $this->opts
->getValue( 'hidebots' ) == 1 ) {
84 $this->hideRights
[] = 'bot';
86 if ( $this->opts
->getValue( 'hidesysops' ) == 1 ) {
87 $this->hideGroups
[] = 'sysop';
91 function getIndexField() {
95 function getQueryInfo() {
96 $dbr = $this->getDatabase();
98 $activeUserSeconds = $this->getConfig()->get( 'ActiveUserDays' ) * 86400;
99 $timestamp = $dbr->timestamp( wfTimestamp( TS_UNIX
) - $activeUserSeconds );
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_log_type IS NULL OR rc_log_type != ' . $dbr->addQuotes( 'newusers' ),
107 'rc_timestamp >= ' . $dbr->addQuotes( $timestamp ),
109 if ( $this->requestedUser
!= '' ) {
110 $conds[] = 'qcc_title >= ' . $dbr->addQuotes( $this->requestedUser
);
112 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
113 $conds[] = 'NOT EXISTS (' . $dbr->selectSQLText(
114 'ipblocks', '1', array( 'ipb_user=user_id', 'ipb_deleted' => 1 )
119 'tables' => array( 'querycachetwo', 'user', 'recentchanges' ),
120 'fields' => array( 'user_name', 'user_id', 'recentedits' => 'COUNT(*)', 'qcc_title' ),
121 'options' => array( 'GROUP BY' => array( 'qcc_title' ) ),
126 function doBatchLookups() {
128 foreach ( $this->mResult
as $row ) {
129 $uids[] = $row->user_id
;
131 // Fetch the block status of the user for showing "(blocked)" text and for
132 // striking out names of suppressed users when privileged user views the list.
133 // Although the first query already hits the block table for un-privileged, this
134 // is done in two queries to avoid huge quicksorts and to make COUNT(*) correct.
135 $dbr = $this->getDatabase();
136 $res = $dbr->select( 'ipblocks',
137 array( 'ipb_user', 'MAX(ipb_deleted) AS block_status' ),
138 array( 'ipb_user' => $uids ),
140 array( 'GROUP BY' => array( 'ipb_user' ) )
142 $this->blockStatusByUid
= array();
143 foreach ( $res as $row ) {
144 $this->blockStatusByUid
[$row->ipb_user
] = $row->block_status
; // 0 or 1
146 $this->mResult
->seek( 0 );
149 function formatRow( $row ) {
150 $userName = $row->user_name
;
152 $ulinks = Linker
::userLink( $row->user_id
, $userName );
153 $ulinks .= Linker
::userToolLinks( $row->user_id
, $userName );
155 $lang = $this->getLanguage();
158 $user = User
::newFromId( $row->user_id
);
161 foreach ( $this->hideRights
as $right ) {
162 // Calling User::getRights() within the loop so that
163 // if the hideRights() filter is empty, we don't have to
164 // trigger the lazy-init of the big userrights array in the
166 if ( in_array( $right, $user->getRights() ) ) {
172 // Note: This is a different loop than for user rights,
173 // because we're reusing it to build the group links
175 foreach ( $user->getGroups() as $group ) {
176 if ( in_array( $group, $this->hideGroups
) ) {
179 $list[] = self
::buildGroupLink( $group, $userName );
182 $groups = $lang->commaList( $list );
184 $item = $lang->specialList( $ulinks, $groups );
186 $isBlocked = isset( $this->blockStatusByUid
[$row->user_id
] );
187 if ( $isBlocked && $this->blockStatusByUid
[$row->user_id
] == 1 ) {
188 $item = "<span class=\"deleted\">$item</span>";
190 $count = $this->msg( 'activeusers-count' )->numParams( $row->recentedits
)
191 ->params( $userName )->numParams( $this->RCMaxAge
)->escaped();
192 $blocked = $isBlocked ?
' ' . $this->msg( 'listusers-blocked', $userName )->escaped() : '';
194 return Html
::rawElement( 'li', array(), "{$item} [{$count}]{$blocked}" );
197 function getPageHeader() {
198 $self = $this->getTitle();
199 $limit = $this->mLimit ? Html
::hidden( 'limit', $this->mLimit
) : '';
202 $out = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => wfScript() ) );
203 $out .= Xml
::fieldset( $this->msg( 'activeusers' )->text() ) . "\n";
204 $out .= Html
::hidden( 'title', $self->getPrefixedDBkey() ) . $limit . "\n";
207 $out .= Xml
::inputLabel( $this->msg( 'activeusers-from' )->text(),
208 'username', 'offset', 20, $this->requestedUser
,
209 array( 'class' => 'mw-ui-input-inline', 'tabindex' => 1 ) ) . '<br />';
211 $out .= Xml
::checkLabel( $this->msg( 'activeusers-hidebots' )->text(),
212 'hidebots', 'hidebots', $this->opts
->getValue( 'hidebots' ), array( 'tabindex' => 2 ) );
214 $out .= Xml
::checkLabel(
215 $this->msg( 'activeusers-hidesysops' )->text(),
218 $this->opts
->getValue( 'hidesysops' ),
219 array( 'tabindex' => 3 )
222 # Submit button and form bottom
223 $out .= Xml
::submitButton(
224 $this->msg( 'allpagessubmit' )->text(),
225 array( 'tabindex' => 4 )
227 $out .= Xml
::closeElement( 'fieldset' );
228 $out .= Xml
::closeElement( 'form' );
235 * @ingroup SpecialPage
237 class SpecialActiveUsers
extends SpecialPage
{
242 public function __construct() {
243 parent
::__construct( 'Activeusers' );
247 * Show the special page
249 * @param string $par Parameter passed to the page or null
251 public function execute( $par ) {
252 $days = $this->getConfig()->get( 'ActiveUserDays' );
255 $this->outputHeader();
257 $out = $this->getOutput();
258 $out->wrapWikiMsg( "<div class='mw-activeusers-intro'>\n$1\n</div>",
259 array( 'activeusers-intro', $this->getLanguage()->formatNum( $days ) ) );
261 // Occasionally merge in new updates
262 $seconds = min( self
::mergeActiveUsers( 600, $days ), $days * 86400 );
263 // Mention the level of staleness
264 $out->addWikiMsg( 'cachedspecial-viewing-cached-ttl',
265 $this->getLanguage()->formatDuration( $seconds ) );
267 $up = new ActiveUsersPager( $this->getContext(), null, $par );
269 # getBody() first to check, if empty
270 $usersbody = $up->getBody();
272 $out->addHTML( $up->getPageHeader() );
275 $up->getNavigationBar() .
276 Html
::rawElement( 'ul', array(), $usersbody ) .
277 $up->getNavigationBar()
280 $out->addWikiMsg( 'activeusers-noresult' );
284 protected function getGroupName() {
289 * @param int $period Seconds (do updates no more often than this)
290 * @param int $days How many days user must be idle before he is considered inactive
291 * @return int How many seconds old the cache is
293 public static function mergeActiveUsers( $period, $days ) {
294 $dbr = wfGetDB( DB_SLAVE
);
295 $cTime = $dbr->selectField( 'querycache_info',
297 array( 'qci_type' => 'activeusers' )
300 if ( !wfReadOnly() ) {
301 if ( !$cTime ||
( time() - wfTimestamp( TS_UNIX
, $cTime ) ) > $period ) {
302 $dbw = wfGetDB( DB_MASTER
);
303 if ( $dbw->estimateRowCount( 'recentchanges' ) <= 10000 ) {
304 $window = $days * 86400; // small wiki
306 $window = $period * 2;
308 $cTime = self
::doQueryCacheUpdate( $dbw, $days, $window ) ?
: $cTime;
313 ( $cTime ?
wfTimestamp( TS_UNIX
, $cTime ) : $days * 86400 ) );
317 * @param DatabaseBase $dbw Passed in from updateSpecialPages.php
320 public static function cacheUpdate( DatabaseBase
$dbw ) {
321 global $wgActiveUserDays;
323 self
::doQueryCacheUpdate( $dbw, $wgActiveUserDays, $wgActiveUserDays * 86400 );
327 * Update the query cache as needed
329 * @param DatabaseBase $dbw
330 * @param int $days How many days user must be idle before he is considered inactive
331 * @param int $window Maximum time range of new data to scan (in seconds)
332 * @return int|bool UNIX timestamp the cache is now up-to-date as of (false on error)
334 protected static function doQueryCacheUpdate( DatabaseBase
$dbw, $days, $window ) {
335 $dbw->startAtomic( __METHOD__
);
337 $lockKey = wfWikiID() . '-activeusers';
338 if ( !$dbw->lock( $lockKey, __METHOD__
, 1 ) ) {
339 return false; // exclusive update (avoids duplicate entries)
343 $cTime = $dbw->selectField( 'querycache_info',
345 array( 'qci_type' => 'activeusers' )
347 $cTimeUnix = $cTime ?
wfTimestamp( TS_UNIX
, $cTime ) : 1;
348 // If a transaction was already started, it might have an old
349 // snapshot, so kludge the timestamp range a few seconds back.
352 // Pick the date range to fetch from. This is normally from the last
353 // update to till the present time, but has a limited window for sanity.
354 // If the window is limited, multiple runs are need to fully populate it.
355 $sTimestamp = max( $cTimeUnix, $now - $days * 86400 );
356 $eTimestamp = min( $sTimestamp +
$window, $now );
358 // Get all the users active since the last update
360 array( 'recentchanges' ),
361 array( 'rc_user_text', 'lastedittime' => 'MAX(rc_timestamp)' ),
363 'rc_user > 0', // actual accounts
364 'rc_type != ' . $dbw->addQuotes( RC_EXTERNAL
), // no wikidata
365 'rc_log_type IS NULL OR rc_log_type != ' . $dbw->addQuotes( 'newusers' ),
366 'rc_timestamp >= ' . $dbw->addQuotes( $dbw->timestamp( $sTimestamp ) ),
367 'rc_timestamp <= ' . $dbw->addQuotes( $dbw->timestamp( $eTimestamp ) )
371 'GROUP BY' => array( 'rc_user_text' ),
372 'ORDER BY' => 'NULL' // avoid filesort
376 foreach ( $res as $row ) {
377 $names[$row->rc_user_text
] = $row->lastedittime
;
380 // Rotate out users that have not edited in too long (according to old data set)
381 $dbw->delete( 'querycachetwo',
383 'qcc_type' => 'activeusers',
384 'qcc_value < ' . $dbw->addQuotes( $now - $days * 86400 ) // TS_UNIX
389 // Find which of the recently active users are already accounted for
390 if ( count( $names ) ) {
391 $res = $dbw->select( 'querycachetwo',
392 array( 'user_name' => 'qcc_title' ),
394 'qcc_type' => 'activeusers',
395 'qcc_namespace' => NS_USER
,
396 'qcc_title' => array_keys( $names ) ),
398 // See the latest data (ignoring trx snapshot) to avoid
399 // duplicates if this method was called in a transaction
400 array( 'LOCK IN SHARE MODE' )
402 foreach ( $res as $row ) {
403 unset( $names[$row->user_name
] );
407 // Insert the users that need to be added to the list (which their last edit time
408 if ( count( $names ) ) {
410 foreach ( $names as $name => $lastEditTime ) {
412 'qcc_type' => 'activeusers',
413 'qcc_namespace' => NS_USER
,
414 'qcc_title' => $name,
415 'qcc_value' => wfTimestamp( TS_UNIX
, $lastEditTime ),
416 'qcc_namespacetwo' => 0, // unused
417 'qcc_titletwo' => '' // unused
420 foreach ( array_chunk( $newRows, 500 ) as $rowBatch ) {
421 $dbw->insert( 'querycachetwo', $rowBatch, __METHOD__
);
422 if ( !$dbw->trxLevel() ) {
428 // Touch the data freshness timestamp
429 $dbw->replace( 'querycache_info',
431 array( 'qci_type' => 'activeusers',
432 'qci_timestamp' => $dbw->timestamp( $eTimestamp ) ), // not always $now
436 $dbw->unlock( $lockKey, __METHOD__
);
437 $dbw->endAtomic( __METHOD__
);