3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
20 use MediaWiki\MediaWikiServices
;
21 use Wikimedia\Assert\Assert
;
24 * Class for handling updates to the site_stats table
26 class SiteStatsUpdate
implements DeferrableUpdate
, MergeableUpdate
{
32 protected $articles = 0;
36 protected $images = 0;
38 private static $counters = [ 'edits', 'pages', 'articles', 'users', 'images' ];
40 // @todo deprecate this constructor
41 function __construct( $views, $edits, $good, $pages = 0, $users = 0 ) {
42 $this->edits
= $edits;
43 $this->articles
= $good;
44 $this->pages
= $pages;
45 $this->users
= $users;
48 public function merge( MergeableUpdate
$update ) {
49 /** @var SiteStatsUpdate $update */
50 Assert
::parameterType( __CLASS__
, $update, '$update' );
52 foreach ( self
::$counters as $field ) {
53 $this->$field +
= $update->$field;
58 * @param array $deltas
59 * @return SiteStatsUpdate
61 public static function factory( array $deltas ) {
62 $update = new self( 0, 0, 0 );
64 foreach ( self
::$counters as $field ) {
65 if ( isset( $deltas[$field] ) && $deltas[$field] ) {
66 $update->$field = $deltas[$field];
73 public function doUpdate() {
74 global $wgSiteStatsAsyncFactor;
76 $this->doUpdateContextStats();
78 $rate = $wgSiteStatsAsyncFactor; // convenience
79 // If set to do so, only do actual DB updates 1 every $rate times.
80 // The other times, just update "pending delta" values in memcached.
81 if ( $rate && ( $rate < 0 ||
mt_rand( 0, $rate - 1 ) != 0 ) ) {
82 $this->doUpdatePendingDeltas();
84 // Need a separate transaction because this a global lock
85 DeferredUpdates
::addCallableUpdate( [ $this, 'tryDBUpdateInternal' ] );
90 * Do not call this outside of SiteStatsUpdate
92 public function tryDBUpdateInternal() {
93 global $wgSiteStatsAsyncFactor;
95 $dbw = wfGetDB( DB_MASTER
);
96 $lockKey = wfMemcKey( 'site_stats' ); // prepend wiki ID
98 if ( $wgSiteStatsAsyncFactor ) {
99 // Lock the table so we don't have double DB/memcached updates
100 if ( !$dbw->lockIsFree( $lockKey, __METHOD__
)
101 ||
!$dbw->lock( $lockKey, __METHOD__
, 1 ) // 1 sec timeout
103 $this->doUpdatePendingDeltas();
107 $pd = $this->getPendingDeltas();
108 // Piggy-back the async deltas onto those of this stats update....
109 $this->edits +
= ( $pd['ss_total_edits']['+'] - $pd['ss_total_edits']['-'] );
110 $this->articles +
= ( $pd['ss_good_articles']['+'] - $pd['ss_good_articles']['-'] );
111 $this->pages +
= ( $pd['ss_total_pages']['+'] - $pd['ss_total_pages']['-'] );
112 $this->users +
= ( $pd['ss_users']['+'] - $pd['ss_users']['-'] );
113 $this->images +
= ( $pd['ss_images']['+'] - $pd['ss_images']['-'] );
116 // Build up an SQL query of deltas and apply them...
118 $this->appendUpdate( $updates, 'ss_total_edits', $this->edits
);
119 $this->appendUpdate( $updates, 'ss_good_articles', $this->articles
);
120 $this->appendUpdate( $updates, 'ss_total_pages', $this->pages
);
121 $this->appendUpdate( $updates, 'ss_users', $this->users
);
122 $this->appendUpdate( $updates, 'ss_images', $this->images
);
123 if ( $updates != '' ) {
124 $dbw->update( 'site_stats', [ $updates ], [], __METHOD__
);
127 if ( $wgSiteStatsAsyncFactor ) {
128 // Decrement the async deltas now that we applied them
129 $this->removePendingDeltas( $pd );
130 // Commit the updates and unlock the table
131 $dbw->unlock( $lockKey, __METHOD__
);
134 // Invalid cache used by parser functions
139 * @param IDatabase $dbw
142 public static function cacheUpdate( $dbw ) {
143 global $wgActiveUserDays;
144 $dbr = wfGetDB( DB_REPLICA
, 'vslow' );
145 # Get non-bot users than did some recent action other than making accounts.
146 # If account creation is included, the number gets inflated ~20+ fold on enwiki.
147 $activeUsers = $dbr->selectField(
149 'COUNT( DISTINCT rc_user_text )',
153 'rc_log_type != ' . $dbr->addQuotes( 'newusers' ) . ' OR rc_log_type IS NULL',
154 'rc_timestamp >= ' . $dbr->addQuotes( $dbr->timestamp( wfTimestamp( TS_UNIX
)
155 - $wgActiveUserDays * 24 * 3600 ) ),
161 [ 'ss_active_users' => intval( $activeUsers ) ],
162 [ 'ss_row_id' => 1 ],
166 // Invalid cache used by parser functions
172 protected function doUpdateContextStats() {
173 $stats = MediaWikiServices
::getInstance()->getStatsdDataFactory();
174 foreach ( [ 'edits', 'articles', 'pages', 'users', 'images' ] as $type ) {
175 $delta = $this->$type;
176 if ( $delta !== 0 ) {
177 $stats->updateCount( "site.$type", $delta );
182 protected function doUpdatePendingDeltas() {
183 $this->adjustPending( 'ss_total_edits', $this->edits
);
184 $this->adjustPending( 'ss_good_articles', $this->articles
);
185 $this->adjustPending( 'ss_total_pages', $this->pages
);
186 $this->adjustPending( 'ss_users', $this->users
);
187 $this->adjustPending( 'ss_images', $this->images
);
192 * @param string $field
195 protected function appendUpdate( &$sql, $field, $delta ) {
201 $sql .= "$field=$field-" . abs( $delta );
203 $sql .= "$field=$field+" . abs( $delta );
209 * @param string $type
210 * @param string $sign ('+' or '-')
213 private function getTypeCacheKey( $type, $sign ) {
214 return wfMemcKey( 'sitestatsupdate', 'pendingdelta', $type, $sign );
218 * Adjust the pending deltas for a stat type.
219 * Each stat type has two pending counters, one for increments and decrements
220 * @param string $type
221 * @param int $delta Delta (positive or negative)
223 protected function adjustPending( $type, $delta ) {
224 $cache = ObjectCache
::getMainStashInstance();
225 if ( $delta < 0 ) { // decrement
226 $key = $this->getTypeCacheKey( $type, '-' );
227 } else { // increment
228 $key = $this->getTypeCacheKey( $type, '+' );
231 $magnitude = abs( $delta );
232 $cache->incrWithInit( $key, 0, $magnitude, $magnitude );
236 * Get pending delta counters for each stat type
237 * @return array Positive and negative deltas for each type
239 protected function getPendingDeltas() {
240 $cache = ObjectCache
::getMainStashInstance();
243 foreach ( [ 'ss_total_edits',
244 'ss_good_articles', 'ss_total_pages', 'ss_users', 'ss_images' ] as $type
246 // Get pending increments and pending decrements
247 $flg = BagOStuff
::READ_LATEST
;
248 $pending[$type]['+'] = (int)$cache->get( $this->getTypeCacheKey( $type, '+' ), $flg );
249 $pending[$type]['-'] = (int)$cache->get( $this->getTypeCacheKey( $type, '-' ), $flg );
256 * Reduce pending delta counters after updates have been applied
257 * @param array $pd Result of getPendingDeltas(), used for DB update
259 protected function removePendingDeltas( array $pd ) {
260 $cache = ObjectCache
::getMainStashInstance();
262 foreach ( $pd as $type => $deltas ) {
263 foreach ( $deltas as $sign => $magnitude ) {
264 // Lower the pending counter now that we applied these changes
265 $cache->decr( $this->getTypeCacheKey( $type, $sign ), $magnitude );