Lowered ActiveUsers query period to reduce timeouts
[lhc/web/wiklou.git] / includes / specials / SpecialActiveusers.php
index 6db9e5f..6219fc4 100644 (file)
@@ -46,17 +46,20 @@ class ActiveUsersPager extends UsersPager {
         */
        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 ) {
-               global $wgActiveUserDays;
-
                parent::__construct( $context );
 
-               $this->RCMaxAge = $wgActiveUserDays;
+               $this->RCMaxAge = $this->getConfig()->get( 'ActiveUserDays' );
                $un = $this->getRequest()->getText( 'username', $par );
                $this->requestedUser = '';
                if ( $un != '' ) {
@@ -192,19 +195,18 @@ class ActiveUsersPager extends UsersPager {
        }
 
        function getPageHeader() {
-               global $wgScript;
-
                $self = $this->getTitle();
                $limit = $this->mLimit ? Html::hidden( 'limit', $this->mLimit ) : '';
 
                # Form tag
-               $out = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
+               $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 />';
+                       'username', 'offset', 20, $this->requestedUser,
+                       array( 'class' => 'mw-ui-input-inline', 'tabindex' => 1 ) ) . '<br />';
 
                $out .= Xml::checkLabel( $this->msg( 'activeusers-hidebots' )->text(),
                        'hidebots', 'hidebots', $this->opts->getValue( 'hidebots' ), array( 'tabindex' => 2 ) );
@@ -247,17 +249,17 @@ class SpecialActiveUsers extends SpecialPage {
         * @param string $par Parameter passed to the page or null
         */
        public function execute( $par ) {
-               global $wgActiveUserDays;
+               $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( $wgActiveUserDays ) ) );
+                       array( 'activeusers-intro', $this->getLanguage()->formatNum( $days ) ) );
 
                // Occasionally merge in new updates
-               $seconds = min( self::mergeActiveUsers( 600 ), $wgActiveUserDays * 86400 );
+               $seconds = min( self::mergeActiveUsers( 300, $days ), $days * 86400 );
                // Mention the level of staleness
                $out->addWikiMsg( 'cachedspecial-viewing-cached-ttl',
                        $this->getLanguage()->formatDuration( $seconds ) );
@@ -284,12 +286,11 @@ class SpecialActiveUsers extends SpecialPage {
        }
 
        /**
-        * @param intr $period Seconds (do updates no more often than this)
+        * @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 ) {
-               global $wgActiveUserDays;
-
+       public static function mergeActiveUsers( $period, $days ) {
                $dbr = wfGetDB( DB_SLAVE );
                $cTime = $dbr->selectField( 'querycache_info',
                        'qci_timestamp',
@@ -300,16 +301,16 @@ class SpecialActiveUsers extends SpecialPage {
                        if ( !$cTime || ( time() - wfTimestamp( TS_UNIX, $cTime ) ) > $period ) {
                                $dbw = wfGetDB( DB_MASTER );
                                if ( $dbw->estimateRowCount( 'recentchanges' ) <= 10000 ) {
-                                       $window = $wgActiveUserDays * 86400; // small wiki
+                                       $window = $days * 86400; // small wiki
                                } else {
                                        $window = $period * 2;
                                }
-                               $cTime = self::doQueryCacheUpdate( $dbw, $window ) ?: $cTime;
+                               $cTime = self::doQueryCacheUpdate( $dbw, $days, $window ) ?: $cTime;
                        }
                }
 
                return ( time() -
-                       ( $cTime ? wfTimestamp( TS_UNIX, $cTime ) : $wgActiveUserDays * 86400 ) );
+                       ( $cTime ? wfTimestamp( TS_UNIX, $cTime ) : $days * 86400 ) );
        }
 
        /**
@@ -319,25 +320,27 @@ class SpecialActiveUsers extends SpecialPage {
        public static function cacheUpdate( DatabaseBase $dbw ) {
                global $wgActiveUserDays;
 
-               self::doQueryCacheUpdate( $dbw, $wgActiveUserDays * 86400 );
+               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, $window ) {
-               global $wgActiveUserDays;
+       protected static function doQueryCacheUpdate( DatabaseBase $dbw, $days, $window ) {
+               $dbw->startAtomic( __METHOD__ );
 
                $lockKey = wfWikiID() . '-activeusers';
                if ( !$dbw->lock( $lockKey, __METHOD__, 1 ) ) {
                        return false; // exclusive update (avoids duplicate entries)
                }
 
-               $now = time();
+               $nowUnix = time();
+               // Get the last-updated timestamp for the cache
                $cTime = $dbw->selectField( 'querycache_info',
                        'qci_timestamp',
                        array( 'qci_type' => 'activeusers' )
@@ -347,8 +350,8 @@ class SpecialActiveUsers extends SpecialPage {
                // 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 - $wgActiveUserDays * 86400 );
-               $eTimestamp = min( $sTimestamp + $window, $now );
+               $sTimestamp = max( $cTimeUnix, $nowUnix - $days * 86400 );
+               $eTimestamp = min( $sTimestamp + $window, $nowUnix );
 
                // Get all the users active since the last update
                $res = $dbw->select(
@@ -376,7 +379,7 @@ class SpecialActiveUsers extends SpecialPage {
                $dbw->delete( 'querycachetwo',
                        array(
                                'qcc_type' => 'activeusers',
-                               'qcc_value < ' . $dbw->addQuotes( $now - $wgActiveUserDays * 86400 ) // TS_UNIX
+                               'qcc_value < ' . $dbw->addQuotes( $nowUnix - $days * 86400 ) // TS_UNIX
                        ),
                        __METHOD__
                );
@@ -389,7 +392,10 @@ class SpecialActiveUsers extends SpecialPage {
                                        'qcc_type' => 'activeusers',
                                        'qcc_namespace' => NS_USER,
                                        'qcc_title' => array_keys( $names ) ),
-                               __METHOD__
+                               __METHOD__,
+                               // See the latest data (ignoring trx snapshot) to avoid
+                               // duplicates if this method was called in a transaction
+                               array( 'LOCK IN SHARE MODE' )
                        );
                        foreach ( $res as $row ) {
                                unset( $names[$row->user_name] );
@@ -417,15 +423,20 @@ class SpecialActiveUsers extends SpecialPage {
                        }
                }
 
+               // If a transaction was already started, it might have an old
+               // snapshot, so kludge the timestamp range back as needed.
+               $asOfTimestamp = min( $eTimestamp, (int)$dbw->trxTimestamp() );
+
                // Touch the data freshness timestamp
                $dbw->replace( 'querycache_info',
                        array( 'qci_type' ),
                        array( 'qci_type' => 'activeusers',
-                               'qci_timestamp' => $dbw->timestamp( $eTimestamp ) ), // not always $now
+                               'qci_timestamp' => $dbw->timestamp( $asOfTimestamp ) ), // not always $now
                        __METHOD__
                );
 
                $dbw->unlock( $lockKey, __METHOD__ );
+               $dbw->endAtomic( __METHOD__ );
 
                return $eTimestamp;
        }