diff options
Diffstat (limited to 'includes/SiteStats.php')
-rw-r--r-- | includes/SiteStats.php | 148 |
1 files changed, 83 insertions, 65 deletions
diff --git a/includes/SiteStats.php b/includes/SiteStats.php index 1c2c454d..355993c6 100644 --- a/includes/SiteStats.php +++ b/includes/SiteStats.php @@ -48,8 +48,7 @@ class SiteStats { # Update schema $u = new SiteStatsUpdate( 0, 0, 0 ); $u->doUpdate(); - $dbr = wfGetDB( DB_SLAVE ); - self::$row = $dbr->selectRow( 'site_stats', '*', false, __METHOD__ ); + self::$row = self::doLoad( wfGetDB( DB_SLAVE ) ); } self::$loaded = true; @@ -62,13 +61,13 @@ class SiteStats { wfDebug( __METHOD__ . ": reading site_stats from slave\n" ); $row = self::doLoad( wfGetDB( DB_SLAVE ) ); - if( !self::isSane( $row ) ) { + if ( !self::isSane( $row ) ) { // Might have just been initialized during this request? Underflow? wfDebug( __METHOD__ . ": site_stats damaged or missing on slave\n" ); $row = self::doLoad( wfGetDB( DB_MASTER ) ); } - if( !self::isSane( $row ) ) { + if ( !self::isSane( $row ) ) { // Normally the site_stats table is initialized at install time. // Some manual construction scenarios may leave the table empty or // broken, however, for instance when importing from a dump into a @@ -80,7 +79,7 @@ class SiteStats { $row = self::doLoad( wfGetDB( DB_MASTER ) ); } - if( !self::isSane( $row ) ) { + if ( !self::isSane( $row ) ) { wfDebug( __METHOD__ . ": site_stats persistently nonsensical o_O\n" ); } return $row; @@ -91,7 +90,16 @@ class SiteStats { * @return Bool|ResultWrapper */ static function doLoad( $db ) { - return $db->selectRow( 'site_stats', '*', false, __METHOD__ ); + return $db->selectRow( 'site_stats', array( + 'ss_row_id', + 'ss_total_views', + 'ss_total_edits', + 'ss_good_articles', + 'ss_total_pages', + 'ss_users', + 'ss_active_users', + 'ss_images', + ), false, __METHOD__ ); } /** @@ -152,7 +160,7 @@ class SiteStats { /** * Find the number of users in a given user group. - * @param $group String: name of group + * @param string $group name of group * @return Integer */ static function numberingroup( $group ) { @@ -181,7 +189,7 @@ class SiteStats { static function jobs() { if ( !isset( self::$jobs ) ) { $dbr = wfGetDB( DB_SLAVE ); - self::$jobs = $dbr->estimateRowCount( 'job' ); + self::$jobs = array_sum( JobQueueGroup::singleton()->getQueueSizes() ); /* Zero rows still do single row read for row that doesn't exist, but people are annoyed by that */ if ( self::$jobs == 1 ) { self::$jobs = 0; @@ -197,7 +205,7 @@ class SiteStats { */ static function pagesInNs( $ns ) { wfProfileIn( __METHOD__ ); - if( !isset( self::$pageCount[$ns] ) ) { + if ( !isset( self::$pageCount[$ns] ) ) { $dbr = wfGetDB( DB_SLAVE ); self::$pageCount[$ns] = (int)$dbr->selectField( 'page', @@ -218,20 +226,24 @@ class SiteStats { * @return bool */ private static function isSane( $row ) { - if( - $row === false + if ( $row === false || $row->ss_total_pages < $row->ss_good_articles || $row->ss_total_edits < $row->ss_total_pages + || $row->ss_users < $row->ss_active_users ) { return false; } // Now check for underflow/overflow - foreach( array( 'total_views', 'total_edits', 'good_articles', - 'total_pages', 'users', 'images' ) as $member ) { - if( - $row->{"ss_$member"} > 2000000000 - || $row->{"ss_$member"} < 0 - ) { + foreach ( array( + 'ss_total_views', + 'ss_total_edits', + 'ss_good_articles', + 'ss_total_pages', + 'ss_users', + 'ss_active_users', + 'ss_images', + ) as $member ) { + if ( $row->$member > 2000000000 || $row->$member < 0 ) { return false; } } @@ -250,7 +262,7 @@ class SiteStatsUpdate implements DeferrableUpdate { protected $users = 0; protected $images = 0; - // @TODO: deprecate this constructor + // @todo deprecate this constructor function __construct( $views, $edits, $good, $pages = 0, $users = 0 ) { $this->views = $views; $this->edits = $edits; @@ -285,50 +297,56 @@ class SiteStatsUpdate implements DeferrableUpdate { if ( $rate && ( $rate < 0 || mt_rand( 0, $rate - 1 ) != 0 ) ) { $this->doUpdatePendingDeltas(); } else { - $dbw = wfGetDB( DB_MASTER ); - - $lockKey = wfMemcKey( 'site_stats' ); // prepend wiki ID - if ( $rate ) { - // Lock the table so we don't have double DB/memcached updates - if ( !$dbw->lockIsFree( $lockKey, __METHOD__ ) - || !$dbw->lock( $lockKey, __METHOD__, 1 ) // 1 sec timeout - ) { - $this->doUpdatePendingDeltas(); - return; - } - $pd = $this->getPendingDeltas(); - // Piggy-back the async deltas onto those of this stats update.... - $this->views += ( $pd['ss_total_views']['+'] - $pd['ss_total_views']['-'] ); - $this->edits += ( $pd['ss_total_edits']['+'] - $pd['ss_total_edits']['-'] ); - $this->articles += ( $pd['ss_good_articles']['+'] - $pd['ss_good_articles']['-'] ); - $this->pages += ( $pd['ss_total_pages']['+'] - $pd['ss_total_pages']['-'] ); - $this->users += ( $pd['ss_users']['+'] - $pd['ss_users']['-'] ); - $this->images += ( $pd['ss_images']['+'] - $pd['ss_images']['-'] ); - } - // Need a separate transaction because this a global lock - $dbw->begin( __METHOD__ ); - - // Build up an SQL query of deltas and apply them... - $updates = ''; - $this->appendUpdate( $updates, 'ss_total_views', $this->views ); - $this->appendUpdate( $updates, 'ss_total_edits', $this->edits ); - $this->appendUpdate( $updates, 'ss_good_articles', $this->articles ); - $this->appendUpdate( $updates, 'ss_total_pages', $this->pages ); - $this->appendUpdate( $updates, 'ss_users', $this->users ); - $this->appendUpdate( $updates, 'ss_images', $this->images ); - if ( $updates != '' ) { - $dbw->update( 'site_stats', array( $updates ), array(), __METHOD__ ); - } + wfGetDB( DB_MASTER )->onTransactionIdle( array( $this, 'tryDBUpdateInternal' ) ); + } + } + + /** + * Do not call this outside of SiteStatsUpdate + * + * @return void + */ + public function tryDBUpdateInternal() { + global $wgSiteStatsAsyncFactor; - if ( $rate ) { - // Decrement the async deltas now that we applied them - $this->removePendingDeltas( $pd ); - // Commit the updates and unlock the table - $dbw->unlock( $lockKey, __METHOD__ ); + $dbw = wfGetDB( DB_MASTER ); + $lockKey = wfMemcKey( 'site_stats' ); // prepend wiki ID + if ( $wgSiteStatsAsyncFactor ) { + // Lock the table so we don't have double DB/memcached updates + if ( !$dbw->lockIsFree( $lockKey, __METHOD__ ) + || !$dbw->lock( $lockKey, __METHOD__, 1 ) // 1 sec timeout + ) { + $this->doUpdatePendingDeltas(); + return; } + $pd = $this->getPendingDeltas(); + // Piggy-back the async deltas onto those of this stats update.... + $this->views += ( $pd['ss_total_views']['+'] - $pd['ss_total_views']['-'] ); + $this->edits += ( $pd['ss_total_edits']['+'] - $pd['ss_total_edits']['-'] ); + $this->articles += ( $pd['ss_good_articles']['+'] - $pd['ss_good_articles']['-'] ); + $this->pages += ( $pd['ss_total_pages']['+'] - $pd['ss_total_pages']['-'] ); + $this->users += ( $pd['ss_users']['+'] - $pd['ss_users']['-'] ); + $this->images += ( $pd['ss_images']['+'] - $pd['ss_images']['-'] ); + } + + // Build up an SQL query of deltas and apply them... + $updates = ''; + $this->appendUpdate( $updates, 'ss_total_views', $this->views ); + $this->appendUpdate( $updates, 'ss_total_edits', $this->edits ); + $this->appendUpdate( $updates, 'ss_good_articles', $this->articles ); + $this->appendUpdate( $updates, 'ss_total_pages', $this->pages ); + $this->appendUpdate( $updates, 'ss_users', $this->users ); + $this->appendUpdate( $updates, 'ss_images', $this->images ); + if ( $updates != '' ) { + $dbw->update( 'site_stats', array( $updates ), array(), __METHOD__ ); + } - $dbw->commit( __METHOD__ ); + if ( $wgSiteStatsAsyncFactor ) { + // Decrement the async deltas now that we applied them + $this->removePendingDeltas( $pd ); + // Commit the updates and unlock the table + $dbw->unlock( $lockKey, __METHOD__ ); } } @@ -348,7 +366,7 @@ class SiteStatsUpdate implements DeferrableUpdate { 'rc_user != 0', 'rc_bot' => 0, 'rc_log_type != ' . $dbr->addQuotes( 'newusers' ) . ' OR rc_log_type IS NULL', - 'rc_timestamp >= ' . $dbr->addQuotes( $dbr->timestamp( wfTimestamp( TS_UNIX ) - $wgActiveUserDays*24*3600 ) ), + 'rc_timestamp >= ' . $dbr->addQuotes( $dbr->timestamp( wfTimestamp( TS_UNIX ) - $wgActiveUserDays * 24 * 3600 ) ), ), __METHOD__ ); @@ -390,7 +408,7 @@ class SiteStatsUpdate implements DeferrableUpdate { /** * @param $type string - * @param $sign string ('+' or '-') + * @param string $sign ('+' or '-') * @return string */ private function getTypeCacheKey( $type, $sign ) { @@ -443,7 +461,7 @@ class SiteStatsUpdate implements DeferrableUpdate { /** * Reduce pending delta counters after updates have been applied - * @param Array $pd Result of getPendingDeltas(), used for DB update + * @param array $pd Result of getPendingDeltas(), used for DB update * @return void */ protected function removePendingDeltas( array $pd ) { @@ -566,7 +584,7 @@ class SiteStatsInit { * @param $database DatabaseBase|bool * - Boolean: whether to use the master DB * - DatabaseBase: database connection to use - * @param $options Array of options, may contain the following values + * @param array $options of options, may contain the following values * - update Boolean: whether to update the current stats (true) or write fresh (false) (default: false) * - views Boolean: when true, do not update the number of page views (default: true) * - activeUsers Boolean: whether to update the number of active users (default: false) @@ -584,19 +602,19 @@ class SiteStatsInit { $counter->files(); // Only do views if we don't want to not count them - if( $options['views'] ) { + if ( $options['views'] ) { $counter->views(); } // Update/refresh - if( $options['update'] ) { + if ( $options['update'] ) { $counter->update(); } else { $counter->refresh(); } // Count active users if need be - if( $options['activeUsers'] ) { + if ( $options['activeUsers'] ) { SiteStatsUpdate::cacheUpdate( wfGetDB( DB_MASTER ) ); } } |