*
* @file
*/
+use MediaWiki\MediaWikiServices;
+use Wikimedia\Assert\Assert;
+use Wikimedia\Rdbms\IDatabase;
/**
* Class for handling updates to the site_stats table
*/
-class SiteStatsUpdate implements DeferrableUpdate {
+class SiteStatsUpdate implements DeferrableUpdate, MergeableUpdate {
/** @var int */
protected $edits = 0;
-
/** @var int */
protected $pages = 0;
-
/** @var int */
protected $articles = 0;
-
/** @var int */
protected $users = 0;
-
/** @var int */
protected $images = 0;
+ private static $counters = [ 'edits', 'pages', 'articles', 'users', 'images' ];
+
// @todo deprecate this constructor
function __construct( $views, $edits, $good, $pages = 0, $users = 0 ) {
$this->edits = $edits;
$this->users = $users;
}
+ public function merge( MergeableUpdate $update ) {
+ /** @var SiteStatsUpdate $update */
+ Assert::parameterType( __CLASS__, $update, '$update' );
+
+ foreach ( self::$counters as $field ) {
+ $this->$field += $update->$field;
+ }
+ }
+
/**
* @param array $deltas
* @return SiteStatsUpdate
public static function factory( array $deltas ) {
$update = new self( 0, 0, 0 );
- $fields = [ 'views', 'edits', 'pages', 'articles', 'users', 'images' ];
- foreach ( $fields as $field ) {
+ foreach ( self::$counters as $field ) {
if ( isset( $deltas[$field] ) && $deltas[$field] ) {
$update->$field = $deltas[$field];
}
global $wgSiteStatsAsyncFactor;
$dbw = wfGetDB( DB_MASTER );
- $lockKey = wfMemcKey( 'site_stats' ); // prepend wiki ID
+ $lockKey = wfWikiID() . ':site_stats'; // prepend wiki ID
$pd = [];
if ( $wgSiteStatsAsyncFactor ) {
// Lock the table so we don't have double DB/memcached updates
}
protected function doUpdateContextStats() {
- $stats = RequestContext::getMain()->getStats();
+ $stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
foreach ( [ 'edits', 'articles', 'pages', 'users', 'images' ] as $type ) {
$delta = $this->$type;
if ( $delta !== 0 ) {
}
/**
+ * @param BagOStuff $cache
* @param string $type
* @param string $sign ('+' or '-')
* @return string
*/
- private function getTypeCacheKey( $type, $sign ) {
- return wfMemcKey( 'sitestatsupdate', 'pendingdelta', $type, $sign );
+ private function getTypeCacheKey( BagOStuff $cache, $type, $sign ) {
+ return $cache->makeKey( 'sitestatsupdate', 'pendingdelta', $type, $sign );
}
/**
* @param int $delta Delta (positive or negative)
*/
protected function adjustPending( $type, $delta ) {
- $cache = ObjectCache::getMainStashInstance();
+ $cache = MediaWikiServices::getInstance()->getMainObjectStash();
if ( $delta < 0 ) { // decrement
- $key = $this->getTypeCacheKey( $type, '-' );
+ $key = $this->getTypeCacheKey( $cache, $type, '-' );
} else { // increment
- $key = $this->getTypeCacheKey( $type, '+' );
+ $key = $this->getTypeCacheKey( $cache, $type, '+' );
}
$magnitude = abs( $delta );
* @return array Positive and negative deltas for each type
*/
protected function getPendingDeltas() {
- $cache = ObjectCache::getMainStashInstance();
+ $cache = MediaWikiServices::getInstance()->getMainObjectStash();
$pending = [];
foreach ( [ 'ss_total_edits',
) {
// Get pending increments and pending decrements
$flg = BagOStuff::READ_LATEST;
- $pending[$type]['+'] = (int)$cache->get( $this->getTypeCacheKey( $type, '+' ), $flg );
- $pending[$type]['-'] = (int)$cache->get( $this->getTypeCacheKey( $type, '-' ), $flg );
+ $pending[$type]['+'] = (int)$cache->get( $this->getTypeCacheKey( $cache, $type, '+' ), $flg );
+ $pending[$type]['-'] = (int)$cache->get( $this->getTypeCacheKey( $cache, $type, '-' ), $flg );
}
return $pending;
* @param array $pd Result of getPendingDeltas(), used for DB update
*/
protected function removePendingDeltas( array $pd ) {
- $cache = ObjectCache::getMainStashInstance();
+ $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( $type, $sign ), $magnitude );
+ $cache->decr( $this->getTypeCacheKey( $cache, $type, $sign ), $magnitude );
}
}
}