-
- protected function doUpdateContextStats() {
- $stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
- foreach ( [ 'edits', 'articles', 'pages', 'users', 'images' ] as $type ) {
- $delta = $this->$type;
- if ( $delta !== 0 ) {
- $stats->updateCount( "site.$type", $delta );
- }
- }
- }
-
- protected function doUpdatePendingDeltas() {
- $this->adjustPending( 'ss_total_edits', $this->edits );
- $this->adjustPending( 'ss_good_articles', $this->articles );
- $this->adjustPending( 'ss_total_pages', $this->pages );
- $this->adjustPending( 'ss_users', $this->users );
- $this->adjustPending( 'ss_images', $this->images );
- }
-
- /**
- * @param string &$sql
- * @param string $field
- * @param int $delta
- */
- protected function appendUpdate( &$sql, $field, $delta ) {
- if ( $delta ) {
- if ( $sql ) {
- $sql .= ',';
- }
- if ( $delta < 0 ) {
- $sql .= "$field=$field-" . abs( $delta );
- } else {
- $sql .= "$field=$field+" . abs( $delta );
- }
- }
- }
-
- /**
- * @param BagOStuff $stash
- * @param string $type
- * @param string $sign ('+' or '-')
- * @return string
- */
- private function getTypeCacheKey( BagOStuff $stash, $type, $sign ) {
- return $stash->makeKey( 'sitestatsupdate', 'pendingdelta', $type, $sign );
- }
-
- /**
- * Adjust the pending deltas for a stat type.
- * Each stat type has two pending counters, one for increments and decrements
- * @param string $type
- * @param int $delta Delta (positive or negative)
- */
- protected function adjustPending( $type, $delta ) {
- if ( $delta < 0 ) { // decrement
- $key = $this->getTypeCacheKey( $this->stash, $type, '-' );
- } else { // increment
- $key = $this->getTypeCacheKey( $this->stash, $type, '+' );
- }
-
- $magnitude = abs( $delta );
- $this->stash->incrWithInit( $key, 0, $magnitude, $magnitude );
- }
-
- /**
- * Get pending delta counters for each stat type
- * @return array Positive and negative deltas for each type
- */
- protected function getPendingDeltas() {
- $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)$this->stash->get(
- $this->getTypeCacheKey( $this->stash, $type, '+' ),
- $flg
- );
- $pending[$type]['-'] = (int)$this->stash->get(
- $this->getTypeCacheKey( $this->stash, $type, '-' ),
- $flg
- );
- }
-
- return $pending;
- }
-
- /**
- * Reduce pending delta counters after updates have been applied
- * @param array $pd Result of getPendingDeltas(), used for DB update
- */
- protected function removePendingDeltas( array $pd ) {
- foreach ( $pd as $type => $deltas ) {
- foreach ( $deltas as $sign => $magnitude ) {
- // Lower the pending counter now that we applied these changes
- $key = $this->getTypeCacheKey( $this->stash, $type, $sign );
- $this->stash->decr( $key, $magnitude );
- }
- }
- }