Replace deprecated Context::getStats() with MWServices::getStatsdDataFactory()
[lhc/web/wiklou.git] / includes / deferred / SiteStatsUpdate.php
1 <?php
2 /**
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.
7 *
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.
12 *
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
17 *
18 * @file
19 */
20 use MediaWiki\MediaWikiServices;
21 use Wikimedia\Assert\Assert;
22
23 /**
24 * Class for handling updates to the site_stats table
25 */
26 class SiteStatsUpdate implements DeferrableUpdate, MergeableUpdate {
27 /** @var int */
28 protected $edits = 0;
29 /** @var int */
30 protected $pages = 0;
31 /** @var int */
32 protected $articles = 0;
33 /** @var int */
34 protected $users = 0;
35 /** @var int */
36 protected $images = 0;
37
38 private static $counters = [ 'edits', 'pages', 'articles', 'users', 'images' ];
39
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;
46 }
47
48 public function merge( MergeableUpdate $update ) {
49 /** @var SiteStatsUpdate $update */
50 Assert::parameterType( __CLASS__, $update, '$update' );
51
52 foreach ( self::$counters as $field ) {
53 $this->$field += $update->$field;
54 }
55 }
56
57 /**
58 * @param array $deltas
59 * @return SiteStatsUpdate
60 */
61 public static function factory( array $deltas ) {
62 $update = new self( 0, 0, 0 );
63
64 foreach ( self::$counters as $field ) {
65 if ( isset( $deltas[$field] ) && $deltas[$field] ) {
66 $update->$field = $deltas[$field];
67 }
68 }
69
70 return $update;
71 }
72
73 public function doUpdate() {
74 global $wgSiteStatsAsyncFactor;
75
76 $this->doUpdateContextStats();
77
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();
83 } else {
84 // Need a separate transaction because this a global lock
85 DeferredUpdates::addCallableUpdate( [ $this, 'tryDBUpdateInternal' ] );
86 }
87 }
88
89 /**
90 * Do not call this outside of SiteStatsUpdate
91 */
92 public function tryDBUpdateInternal() {
93 global $wgSiteStatsAsyncFactor;
94
95 $dbw = wfGetDB( DB_MASTER );
96 $lockKey = wfMemcKey( 'site_stats' ); // prepend wiki ID
97 $pd = [];
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
102 ) {
103 $this->doUpdatePendingDeltas();
104
105 return;
106 }
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']['-'] );
114 }
115
116 // Build up an SQL query of deltas and apply them...
117 $updates = '';
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__ );
125 }
126
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__ );
132 }
133
134 // Invalid cache used by parser functions
135 SiteStats::unload();
136 }
137
138 /**
139 * @param IDatabase $dbw
140 * @return bool|mixed
141 */
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(
148 'recentchanges',
149 'COUNT( DISTINCT rc_user_text )',
150 [
151 'rc_user != 0',
152 'rc_bot' => 0,
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 ) ),
156 ],
157 __METHOD__
158 );
159 $dbw->update(
160 'site_stats',
161 [ 'ss_active_users' => intval( $activeUsers ) ],
162 [ 'ss_row_id' => 1 ],
163 __METHOD__
164 );
165
166 // Invalid cache used by parser functions
167 SiteStats::unload();
168
169 return $activeUsers;
170 }
171
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 );
178 }
179 }
180 }
181
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 );
188 }
189
190 /**
191 * @param string $sql
192 * @param string $field
193 * @param int $delta
194 */
195 protected function appendUpdate( &$sql, $field, $delta ) {
196 if ( $delta ) {
197 if ( $sql ) {
198 $sql .= ',';
199 }
200 if ( $delta < 0 ) {
201 $sql .= "$field=$field-" . abs( $delta );
202 } else {
203 $sql .= "$field=$field+" . abs( $delta );
204 }
205 }
206 }
207
208 /**
209 * @param string $type
210 * @param string $sign ('+' or '-')
211 * @return string
212 */
213 private function getTypeCacheKey( $type, $sign ) {
214 return wfMemcKey( 'sitestatsupdate', 'pendingdelta', $type, $sign );
215 }
216
217 /**
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)
222 */
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, '+' );
229 }
230
231 $magnitude = abs( $delta );
232 $cache->incrWithInit( $key, 0, $magnitude, $magnitude );
233 }
234
235 /**
236 * Get pending delta counters for each stat type
237 * @return array Positive and negative deltas for each type
238 */
239 protected function getPendingDeltas() {
240 $cache = ObjectCache::getMainStashInstance();
241
242 $pending = [];
243 foreach ( [ 'ss_total_edits',
244 'ss_good_articles', 'ss_total_pages', 'ss_users', 'ss_images' ] as $type
245 ) {
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 );
250 }
251
252 return $pending;
253 }
254
255 /**
256 * Reduce pending delta counters after updates have been applied
257 * @param array $pd Result of getPendingDeltas(), used for DB update
258 */
259 protected function removePendingDeltas( array $pd ) {
260 $cache = ObjectCache::getMainStashInstance();
261
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 );
266 }
267 }
268 }
269 }