X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2Fdeferred%2FSiteStatsUpdate.php;h=2f882b830ec170270910c01c55918fd6809487b9;hb=b4e9b1ba621d77b580cfd5b19e927ccaec54831a;hp=8c38d8b0ca46f18b4e40da665ec6db6cc51df88d;hpb=3cb14f56bdf3271769a5866f9dcaad56bf873aea;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/deferred/SiteStatsUpdate.php b/includes/deferred/SiteStatsUpdate.php index 8c38d8b0ca..2f882b830e 100644 --- a/includes/deferred/SiteStatsUpdate.php +++ b/includes/deferred/SiteStatsUpdate.php @@ -25,6 +25,8 @@ use Wikimedia\Rdbms\IDatabase; * Class for handling updates to the site_stats table */ class SiteStatsUpdate implements DeferrableUpdate, MergeableUpdate { + /** @var BagOStuff */ + protected $stash; /** @var int */ protected $edits = 0; /** @var int */ @@ -44,6 +46,8 @@ class SiteStatsUpdate implements DeferrableUpdate, MergeableUpdate { $this->articles = $good; $this->pages = $pages; $this->users = $users; + + $this->stash = MediaWikiServices::getInstance()->getMainObjectStash(); } public function merge( MergeableUpdate $update ) { @@ -72,11 +76,9 @@ class SiteStatsUpdate implements DeferrableUpdate, MergeableUpdate { } public function doUpdate() { - global $wgSiteStatsAsyncFactor; - $this->doUpdateContextStats(); - $rate = $wgSiteStatsAsyncFactor; // convenience + $rate = MediaWikiServices::getInstance()->getMainConfig()->get( 'SiteStatsAsyncFactor' ); // If set to do so, only do actual DB updates 1 every $rate times. // The other times, just update "pending delta" values in memcached. if ( $rate && ( $rate < 0 || mt_rand( 0, $rate - 1 ) != 0 ) ) { @@ -91,16 +93,15 @@ class SiteStatsUpdate implements DeferrableUpdate, MergeableUpdate { * Do not call this outside of SiteStatsUpdate */ public function tryDBUpdateInternal() { - global $wgSiteStatsAsyncFactor; + $services = MediaWikiServices::getInstance(); + $config = $services->getMainConfig(); - $dbw = wfGetDB( DB_MASTER ); - $lockKey = wfWikiID() . ':site_stats'; // prepend wiki ID + $dbw = $services->getDBLoadBalancer()->getConnection( DB_MASTER ); + $lockKey = $dbw->getDomainID() . ':site_stats'; // prepend wiki ID $pd = []; - if ( $wgSiteStatsAsyncFactor ) { + if ( $config->get( 'SiteStatsAsyncFactor' ) ) { // 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 - ) { + if ( !$dbw->lock( $lockKey, __METHOD__, 0 ) ) { $this->doUpdatePendingDeltas(); return; @@ -125,7 +126,7 @@ class SiteStatsUpdate implements DeferrableUpdate, MergeableUpdate { $dbw->update( 'site_stats', [ $updates ], [], __METHOD__ ); } - if ( $wgSiteStatsAsyncFactor ) { + if ( $config->get( 'SiteStatsAsyncFactor' ) ) { // Decrement the async deltas now that we applied them $this->removePendingDeltas( $pd ); // Commit the updates and unlock the table @@ -140,20 +141,23 @@ class SiteStatsUpdate implements DeferrableUpdate, MergeableUpdate { * @param IDatabase $dbw * @return bool|mixed */ - public static function cacheUpdate( $dbw ) { - global $wgActiveUserDays; - $dbr = wfGetDB( DB_REPLICA, 'vslow' ); + public static function cacheUpdate( IDatabase $dbw ) { + $services = MediaWikiServices::getInstance(); + $config = $services->getMainConfig(); + + $dbr = $services->getDBLoadBalancer()->getConnection( DB_REPLICA, 'vslow' ); # Get non-bot users than did some recent action other than making accounts. # If account creation is included, the number gets inflated ~20+ fold on enwiki. $activeUsers = $dbr->selectField( 'recentchanges', 'COUNT( DISTINCT rc_user_text )', [ + 'rc_type != ' . $dbr->addQuotes( RC_EXTERNAL ), // Exclude external (Wikidata) '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( time() - $config->get( 'ActiveUserDays' ) * 24 * 3600 ) ), ], __METHOD__ ); @@ -189,7 +193,7 @@ class SiteStatsUpdate implements DeferrableUpdate, MergeableUpdate { } /** - * @param string $sql + * @param string &$sql * @param string $field * @param int $delta */ @@ -207,13 +211,13 @@ class SiteStatsUpdate implements DeferrableUpdate, MergeableUpdate { } /** - * @param BagOStuff $cache + * @param BagOStuff $stash * @param string $type * @param string $sign ('+' or '-') * @return string */ - private function getTypeCacheKey( BagOStuff $cache, $type, $sign ) { - return $cache->makeKey( 'sitestatsupdate', 'pendingdelta', $type, $sign ); + private function getTypeCacheKey( BagOStuff $stash, $type, $sign ) { + return $stash->makeKey( 'sitestatsupdate', 'pendingdelta', $type, $sign ); } /** @@ -223,15 +227,14 @@ class SiteStatsUpdate implements DeferrableUpdate, MergeableUpdate { * @param int $delta Delta (positive or negative) */ protected function adjustPending( $type, $delta ) { - $cache = MediaWikiServices::getInstance()->getMainObjectStash(); if ( $delta < 0 ) { // decrement - $key = $this->getTypeCacheKey( $cache, $type, '-' ); + $key = $this->getTypeCacheKey( $this->stash, $type, '-' ); } else { // increment - $key = $this->getTypeCacheKey( $cache, $type, '+' ); + $key = $this->getTypeCacheKey( $this->stash, $type, '+' ); } $magnitude = abs( $delta ); - $cache->incrWithInit( $key, 0, $magnitude, $magnitude ); + $this->stash->incrWithInit( $key, 0, $magnitude, $magnitude ); } /** @@ -239,16 +242,20 @@ class SiteStatsUpdate implements DeferrableUpdate, MergeableUpdate { * @return array Positive and negative deltas for each type */ protected function getPendingDeltas() { - $cache = MediaWikiServices::getInstance()->getMainObjectStash(); - $pending = []; foreach ( [ 'ss_total_edits', 'ss_good_articles', 'ss_total_pages', 'ss_users', 'ss_images' ] as $type ) { // Get pending increments and pending decrements $flg = BagOStuff::READ_LATEST; - $pending[$type]['+'] = (int)$cache->get( $this->getTypeCacheKey( $cache, $type, '+' ), $flg ); - $pending[$type]['-'] = (int)$cache->get( $this->getTypeCacheKey( $cache, $type, '-' ), $flg ); + $pending[$type]['+'] = (int)$this->stash->get( + $this->getTypeCacheKey( $this->stash, $type, '+' ), + $flg + ); + $pending[$type]['-'] = (int)$this->stash->get( + $this->getTypeCacheKey( $this->stash, $type, '-' ), + $flg + ); } return $pending; @@ -259,12 +266,11 @@ class SiteStatsUpdate implements DeferrableUpdate, MergeableUpdate { * @param array $pd Result of getPendingDeltas(), used for DB update */ protected function removePendingDeltas( array $pd ) { - $cache = MediaWikiServices::getInstance()->getMainObjectStash(); - foreach ( $pd as $type => $deltas ) { foreach ( $deltas as $sign => $magnitude ) { // Lower the pending counter now that we applied these changes - $cache->decr( $this->getTypeCacheKey( $cache, $type, $sign ), $magnitude ); + $key = $this->getTypeCacheKey( $this->stash, $type, $sign ); + $this->stash->decr( $key, $magnitude ); } } }