[SiteStatsUpdate] Added support for memcached staging of stats updates.
[lhc/web/wiklou.git] / includes / SiteStats.php
1 <?php
2
3 /**
4 * Static accessor class for site_stats and related things
5 */
6 class SiteStats {
7 static $row, $loaded = false;
8 static $jobs;
9 static $pageCount = array();
10 static $groupMemberCounts = array();
11
12 static function recache() {
13 self::load( true );
14 }
15
16 /**
17 * @param $recache bool
18 */
19 static function load( $recache = false ) {
20 if ( self::$loaded && !$recache ) {
21 return;
22 }
23
24 self::$row = self::loadAndLazyInit();
25
26 # This code is somewhat schema-agnostic, because I'm changing it in a minor release -- TS
27 if ( !isset( self::$row->ss_total_pages ) && self::$row->ss_total_pages == -1 ) {
28 # Update schema
29 $u = new SiteStatsUpdate( 0, 0, 0 );
30 $u->doUpdate();
31 $dbr = wfGetDB( DB_SLAVE );
32 self::$row = $dbr->selectRow( 'site_stats', '*', false, __METHOD__ );
33 }
34
35 self::$loaded = true;
36 }
37
38 /**
39 * @return Bool|ResultWrapper
40 */
41 static function loadAndLazyInit() {
42 wfDebug( __METHOD__ . ": reading site_stats from slave\n" );
43 $row = self::doLoad( wfGetDB( DB_SLAVE ) );
44
45 if( !self::isSane( $row ) ) {
46 // Might have just been initialized during this request? Underflow?
47 wfDebug( __METHOD__ . ": site_stats damaged or missing on slave\n" );
48 $row = self::doLoad( wfGetDB( DB_MASTER ) );
49 }
50
51 if( !self::isSane( $row ) ) {
52 // Normally the site_stats table is initialized at install time.
53 // Some manual construction scenarios may leave the table empty or
54 // broken, however, for instance when importing from a dump into a
55 // clean schema with mwdumper.
56 wfDebug( __METHOD__ . ": initializing damaged or missing site_stats\n" );
57
58 SiteStatsInit::doAllAndCommit( wfGetDB( DB_SLAVE ) );
59
60 $row = self::doLoad( wfGetDB( DB_MASTER ) );
61 }
62
63 if( !self::isSane( $row ) ) {
64 wfDebug( __METHOD__ . ": site_stats persistently nonsensical o_O\n" );
65 }
66 return $row;
67 }
68
69 /**
70 * @param $db DatabaseBase
71 * @return Bool|ResultWrapper
72 */
73 static function doLoad( $db ) {
74 return $db->selectRow( 'site_stats', '*', false, __METHOD__ );
75 }
76
77 /**
78 * @return int
79 */
80 static function views() {
81 self::load();
82 return self::$row->ss_total_views;
83 }
84
85 /**
86 * @return int
87 */
88 static function edits() {
89 self::load();
90 return self::$row->ss_total_edits;
91 }
92
93 /**
94 * @return int
95 */
96 static function articles() {
97 self::load();
98 return self::$row->ss_good_articles;
99 }
100
101 /**
102 * @return int
103 */
104 static function pages() {
105 self::load();
106 return self::$row->ss_total_pages;
107 }
108
109 /**
110 * @return int
111 */
112 static function users() {
113 self::load();
114 return self::$row->ss_users;
115 }
116
117 /**
118 * @return int
119 */
120 static function activeUsers() {
121 self::load();
122 return self::$row->ss_active_users;
123 }
124
125 /**
126 * @return int
127 */
128 static function images() {
129 self::load();
130 return self::$row->ss_images;
131 }
132
133 /**
134 * Find the number of users in a given user group.
135 * @param $group String: name of group
136 * @return Integer
137 */
138 static function numberingroup( $group ) {
139 if ( !isset( self::$groupMemberCounts[$group] ) ) {
140 global $wgMemc;
141 $key = wfMemcKey( 'SiteStats', 'groupcounts', $group );
142 $hit = $wgMemc->get( $key );
143 if ( !$hit ) {
144 $dbr = wfGetDB( DB_SLAVE );
145 $hit = $dbr->selectField(
146 'user_groups',
147 'COUNT(*)',
148 array( 'ug_group' => $group ),
149 __METHOD__
150 );
151 $wgMemc->set( $key, $hit, 3600 );
152 }
153 self::$groupMemberCounts[$group] = $hit;
154 }
155 return self::$groupMemberCounts[$group];
156 }
157
158 /**
159 * @return int
160 */
161 static function jobs() {
162 if ( !isset( self::$jobs ) ) {
163 $dbr = wfGetDB( DB_SLAVE );
164 self::$jobs = $dbr->estimateRowCount( 'job' );
165 /* Zero rows still do single row read for row that doesn't exist, but people are annoyed by that */
166 if ( self::$jobs == 1 ) {
167 self::$jobs = 0;
168 }
169 }
170 return self::$jobs;
171 }
172
173 /**
174 * @param $ns int
175 *
176 * @return int
177 */
178 static function pagesInNs( $ns ) {
179 wfProfileIn( __METHOD__ );
180 if( !isset( self::$pageCount[$ns] ) ) {
181 $dbr = wfGetDB( DB_SLAVE );
182 self::$pageCount[$ns] = (int)$dbr->selectField(
183 'page',
184 'COUNT(*)',
185 array( 'page_namespace' => $ns ),
186 __METHOD__
187 );
188 }
189 wfProfileOut( __METHOD__ );
190 return self::$pageCount[$ns];
191 }
192
193 /**
194 * Is the provided row of site stats sane, or should it be regenerated?
195 *
196 * @param $row
197 *
198 * @return bool
199 */
200 private static function isSane( $row ) {
201 if(
202 $row === false
203 || $row->ss_total_pages < $row->ss_good_articles
204 || $row->ss_total_edits < $row->ss_total_pages
205 ) {
206 return false;
207 }
208 // Now check for underflow/overflow
209 foreach( array( 'total_views', 'total_edits', 'good_articles',
210 'total_pages', 'users', 'images' ) as $member ) {
211 if(
212 $row->{"ss_$member"} > 2000000000
213 || $row->{"ss_$member"} < 0
214 ) {
215 return false;
216 }
217 }
218 return true;
219 }
220 }
221
222 /**
223 * Class for handling updates to the site_stats table
224 */
225 class SiteStatsUpdate implements DeferrableUpdate {
226 protected $views = 0;
227 protected $edits = 0;
228 protected $pages = 0;
229 protected $articles = 0;
230 protected $users = 0;
231 protected $images = 0;
232
233 // @TODO: deprecate this constructor
234 function __construct( $views, $edits, $good, $pages = 0, $users = 0 ) {
235 $this->views = $views;
236 $this->edits = $edits;
237 $this->articles = $good;
238 $this->pages = $pages;
239 $this->users = $users;
240 }
241
242 /**
243 * @param $deltas Array
244 * @return SiteStatsUpdate
245 */
246 public static function factory( array $deltas ) {
247 $update = new self( 0, 0, 0 );
248
249 $fields = array( 'views', 'edits', 'pages', 'articles', 'users', 'images' );
250 foreach ( $fields as $field ) {
251 if ( isset( $deltas[$field] ) && $deltas[$field] ) {
252 $update->$field = $deltas[$field];
253 }
254 }
255
256 return $update;
257 }
258
259 public function doUpdate() {
260 global $wgSiteStatsAsyncFactor;
261
262 $rate = $wgSiteStatsAsyncFactor; // convenience
263 // If set to do so, only do actual DB updates 1 every $rate times.
264 // The other times, just update "pending delta" values in memcached.
265 if ( $rate && ( $rate < 0 || mt_rand( 0, $rate - 1 ) != 0 ) ) {
266 $this->doUpdatePendingDeltas();
267 } else {
268 $dbw = wfGetDB( DB_MASTER );
269 // Need a separate transaction because this a global lock
270 $dbw->begin( __METHOD__ );
271
272 $lockKey = wfMemcKey( 'site_stats' ); // prepend wiki ID
273 if ( $rate ) {
274 // Lock the table so we don't have double DB/memcached updates
275 if ( !$dbw->lock( $lockKey, __METHOD__, 1 ) ) {
276 $dbw->commit( __METHOD__ );
277 $this->doUpdatePendingDeltas();
278 return;
279 }
280 $pd = $this->getPendingDeltas();
281 // Piggy-back the async deltas onto those of this stats update....
282 $this->views += ( $pd['ss_total_views']['+'] - $pd['ss_total_views']['-'] );
283 $this->edits += ( $pd['ss_total_edits']['+'] - $pd['ss_total_edits']['-'] );
284 $this->articles += ( $pd['ss_good_articles']['+'] - $pd['ss_good_articles']['-'] );
285 $this->pages += ( $pd['ss_total_pages']['+'] - $pd['ss_total_pages']['-'] );
286 $this->users += ( $pd['ss_users']['+'] - $pd['ss_users']['-'] );
287 $this->images += ( $pd['ss_images']['+'] - $pd['ss_images']['-'] );
288 }
289
290 // Build up an SQL query of deltas and apply them...
291 $updates = '';
292 $this->appendUpdate( $updates, 'ss_total_views', $this->views );
293 $this->appendUpdate( $updates, 'ss_total_edits', $this->edits );
294 $this->appendUpdate( $updates, 'ss_good_articles', $this->articles );
295 $this->appendUpdate( $updates, 'ss_total_pages', $this->pages );
296 $this->appendUpdate( $updates, 'ss_users', $this->users );
297 $this->appendUpdate( $updates, 'ss_images', $this->images );
298 if ( $updates != '' ) {
299 $dbw->update( 'site_stats', array( $updates ), array(), __METHOD__ );
300 }
301
302 if ( $rate ) {
303 // Decrement the async deltas now that we applied them
304 $this->removePendingDeltas( $pd );
305 // Commit the updates and unlock the table
306 $dbw->unlock( $lockKey, __METHOD__ );
307 }
308
309 $dbw->commit( __METHOD__ );
310 }
311 }
312
313 /**
314 * @param $dbw DatabaseBase
315 * @return bool|mixed
316 */
317 public static function cacheUpdate( $dbw ) {
318 global $wgActiveUserDays;
319 $dbr = wfGetDB( DB_SLAVE, array( 'SpecialStatistics', 'vslow' ) );
320 # Get non-bot users than did some recent action other than making accounts.
321 # If account creation is included, the number gets inflated ~20+ fold on enwiki.
322 $activeUsers = $dbr->selectField(
323 'recentchanges',
324 'COUNT( DISTINCT rc_user_text )',
325 array(
326 'rc_user != 0',
327 'rc_bot' => 0,
328 "rc_log_type != 'newusers' OR rc_log_type IS NULL",
329 "rc_timestamp >= '{$dbw->timestamp( wfTimestamp( TS_UNIX ) - $wgActiveUserDays*24*3600 )}'",
330 ),
331 __METHOD__
332 );
333 $dbw->update(
334 'site_stats',
335 array( 'ss_active_users' => intval( $activeUsers ) ),
336 array( 'ss_row_id' => 1 ),
337 __METHOD__
338 );
339 return $activeUsers;
340 }
341
342 protected function doUpdatePendingDeltas() {
343 $this->adjustPending( 'ss_total_views', $this->views );
344 $this->adjustPending( 'ss_total_edits', $this->edits );
345 $this->adjustPending( 'ss_good_articles', $this->articles );
346 $this->adjustPending( 'ss_total_pages', $this->pages );
347 $this->adjustPending( 'ss_users', $this->users );
348 $this->adjustPending( 'ss_images', $this->images );
349 }
350
351 /**
352 * @param $sql string
353 * @param $field string
354 * @param $delta integer
355 */
356 protected function appendUpdate( &$sql, $field, $delta ) {
357 if ( $delta ) {
358 if ( $sql ) {
359 $sql .= ',';
360 }
361 if ( $delta < 0 ) {
362 $sql .= "$field=$field-" . abs( $delta );
363 } else {
364 $sql .= "$field=$field+" . abs( $delta );
365 }
366 }
367 }
368
369 /**
370 * @param $type string
371 * @param $sign string ('+' or '-')
372 * @return void
373 */
374 private function getTypeCacheKey( $type, $sign ) {
375 return wfMemcKey( 'sitestatsupdate', 'pendingdelta', $type, $sign );
376 }
377
378 /**
379 * Adjust the pending deltas for a stat type.
380 * Each stat type has two pending counters, one for increments and decrements
381 * @param $type string
382 * @param $delta integer Delta (positive or negative)
383 * @return void
384 */
385 protected function adjustPending( $type, $delta ) {
386 global $wgMemc;
387
388 if ( $delta < 0 ) { // decrement
389 $key = $this->getTypeCacheKey( $type, '-' );
390 } else { // increment
391 $key = $this->getTypeCacheKey( $type, '+' );
392 }
393
394 $magnitude = abs( $delta );
395 if ( !$wgMemc->incr( $key, $magnitude ) ) { // not there?
396 if ( !$wgMemc->add( $key, $magnitude ) ) { // race?
397 $wgMemc->incr( $key, $magnitude );
398 }
399 }
400 }
401
402 /**
403 * Get pending delta counters for each stat type
404 * @return Array Positive and negative deltas for each type
405 * @return void
406 */
407 protected function getPendingDeltas() {
408 global $wgMemc;
409
410 $pending = array();
411 foreach ( array( 'ss_total_views', 'ss_total_edits',
412 'ss_good_articles', 'ss_total_pages', 'ss_users', 'ss_images' ) as $type )
413 {
414 // Get pending increments and pending decrements
415 $pending[$type]['+'] = (int)$wgMemc->get( $this->getTypeCacheKey( $type, '+' ) );
416 $pending[$type]['-'] = (int)$wgMemc->get( $this->getTypeCacheKey( $type, '-' ) );
417 }
418
419 return $pending;
420 }
421
422 /**
423 * Reduce pending delta counters after updates have been applied
424 * @param Array Result of getPendingDeltas(), used for DB update
425 * @return void
426 */
427 protected function removePendingDeltas( array $pd ) {
428 global $wgMemc;
429
430 foreach ( $pd as $type => $deltas ) {
431 foreach ( $deltas as $sign => $magnitude ) {
432 // Lower the pending counter now that we applied these changes
433 $wgMemc->decr( $this->getTypeCacheKey( $type, $sign ), $magnitude );
434 }
435 }
436 }
437 }
438
439 /**
440 * Class designed for counting of stats.
441 */
442 class SiteStatsInit {
443
444 // Database connection
445 private $db;
446
447 // Various stats
448 private $mEdits, $mArticles, $mPages, $mUsers, $mViews, $mFiles = 0;
449
450 /**
451 * Constructor
452 * @param $database Boolean or DatabaseBase:
453 * - Boolean: whether to use the master DB
454 * - DatabaseBase: database connection to use
455 */
456 public function __construct( $database = false ) {
457 if ( $database instanceof DatabaseBase ) {
458 $this->db = $database;
459 } else {
460 $this->db = wfGetDB( $database ? DB_MASTER : DB_SLAVE );
461 }
462 }
463
464 /**
465 * Count the total number of edits
466 * @return Integer
467 */
468 public function edits() {
469 $this->mEdits = $this->db->selectField( 'revision', 'COUNT(*)', '', __METHOD__ );
470 $this->mEdits += $this->db->selectField( 'archive', 'COUNT(*)', '', __METHOD__ );
471 return $this->mEdits;
472 }
473
474 /**
475 * Count pages in article space(s)
476 * @return Integer
477 */
478 public function articles() {
479 global $wgArticleCountMethod;
480
481 $tables = array( 'page' );
482 $conds = array(
483 'page_namespace' => MWNamespace::getContentNamespaces(),
484 'page_is_redirect' => 0,
485 );
486
487 if ( $wgArticleCountMethod == 'link' ) {
488 $tables[] = 'pagelinks';
489 $conds[] = 'pl_from=page_id';
490 } elseif ( $wgArticleCountMethod == 'comma' ) {
491 // To make a correct check for this, we would need, for each page,
492 // to load the text, maybe uncompress it, maybe decode it and then
493 // check if there's one comma.
494 // But one thing we are sure is that if the page is empty, it can't
495 // contain a comma :)
496 $conds[] = 'page_len > 0';
497 }
498
499 $this->mArticles = $this->db->selectField( $tables, 'COUNT(DISTINCT page_id)',
500 $conds, __METHOD__ );
501 return $this->mArticles;
502 }
503
504 /**
505 * Count total pages
506 * @return Integer
507 */
508 public function pages() {
509 $this->mPages = $this->db->selectField( 'page', 'COUNT(*)', '', __METHOD__ );
510 return $this->mPages;
511 }
512
513 /**
514 * Count total users
515 * @return Integer
516 */
517 public function users() {
518 $this->mUsers = $this->db->selectField( 'user', 'COUNT(*)', '', __METHOD__ );
519 return $this->mUsers;
520 }
521
522 /**
523 * Count views
524 * @return Integer
525 */
526 public function views() {
527 $this->mViews = $this->db->selectField( 'page', 'SUM(page_counter)', '', __METHOD__ );
528 return $this->mViews;
529 }
530
531 /**
532 * Count total files
533 * @return Integer
534 */
535 public function files() {
536 $this->mFiles = $this->db->selectField( 'image', 'COUNT(*)', '', __METHOD__ );
537 return $this->mFiles;
538 }
539
540 /**
541 * Do all updates and commit them. More or less a replacement
542 * for the original initStats, but without output.
543 *
544 * @param $database DatabaseBase|bool
545 * - Boolean: whether to use the master DB
546 * - DatabaseBase: database connection to use
547 * @param $options Array of options, may contain the following values
548 * - update Boolean: whether to update the current stats (true) or write fresh (false) (default: false)
549 * - views Boolean: when true, do not update the number of page views (default: true)
550 * - activeUsers Boolean: whether to update the number of active users (default: false)
551 */
552 public static function doAllAndCommit( $database, array $options = array() ) {
553 $options += array( 'update' => false, 'views' => true, 'activeUsers' => false );
554
555 // Grab the object and count everything
556 $counter = new SiteStatsInit( $database );
557
558 $counter->edits();
559 $counter->articles();
560 $counter->pages();
561 $counter->users();
562 $counter->files();
563
564 // Only do views if we don't want to not count them
565 if( $options['views'] ) {
566 $counter->views();
567 }
568
569 // Update/refresh
570 if( $options['update'] ) {
571 $counter->update();
572 } else {
573 $counter->refresh();
574 }
575
576 // Count active users if need be
577 if( $options['activeUsers'] ) {
578 SiteStatsUpdate::cacheUpdate( wfGetDB( DB_MASTER ) );
579 }
580 }
581
582 /**
583 * Update the current row with the selected values
584 */
585 public function update() {
586 list( $values, $conds ) = $this->getDbParams();
587 $dbw = wfGetDB( DB_MASTER );
588 $dbw->update( 'site_stats', $values, $conds, __METHOD__ );
589 }
590
591 /**
592 * Refresh site_stats. Erase the current record and save all
593 * the new values.
594 */
595 public function refresh() {
596 list( $values, $conds, $views ) = $this->getDbParams();
597 $dbw = wfGetDB( DB_MASTER );
598 $dbw->delete( 'site_stats', $conds, __METHOD__ );
599 $dbw->insert( 'site_stats', array_merge( $values, $conds, $views ), __METHOD__ );
600 }
601
602 /**
603 * Return three arrays of params for the db queries
604 * @return Array
605 */
606 private function getDbParams() {
607 $values = array(
608 'ss_total_edits' => $this->mEdits,
609 'ss_good_articles' => $this->mArticles,
610 'ss_total_pages' => $this->mPages,
611 'ss_users' => $this->mUsers,
612 'ss_images' => $this->mFiles
613 );
614 $conds = array( 'ss_row_id' => 1 );
615 $views = array( 'ss_total_views' => $this->mViews );
616 return array( $values, $conds, $views );
617 }
618 }