3 * Accessors and mutators for the site-wide statistics.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 * Static accessor class for site_stats and related things
27 static $row, $loaded = false;
29 static $pageCount = array();
30 static $groupMemberCounts = array();
32 static function recache() {
37 * @param $recache bool
39 static function load( $recache = false ) {
40 if ( self
::$loaded && !$recache ) {
44 self
::$row = self
::loadAndLazyInit();
46 # This code is somewhat schema-agnostic, because I'm changing it in a minor release -- TS
47 if ( !isset( self
::$row->ss_total_pages
) && self
::$row->ss_total_pages
== -1 ) {
49 $u = new SiteStatsUpdate( 0, 0, 0 );
51 $dbr = wfGetDB( DB_SLAVE
);
52 self
::$row = $dbr->selectRow( 'site_stats', '*', false, __METHOD__
);
59 * @return Bool|ResultWrapper
61 static function loadAndLazyInit() {
62 wfDebug( __METHOD__
. ": reading site_stats from slave\n" );
63 $row = self
::doLoad( wfGetDB( DB_SLAVE
) );
65 if( !self
::isSane( $row ) ) {
66 // Might have just been initialized during this request? Underflow?
67 wfDebug( __METHOD__
. ": site_stats damaged or missing on slave\n" );
68 $row = self
::doLoad( wfGetDB( DB_MASTER
) );
71 if( !self
::isSane( $row ) ) {
72 // Normally the site_stats table is initialized at install time.
73 // Some manual construction scenarios may leave the table empty or
74 // broken, however, for instance when importing from a dump into a
75 // clean schema with mwdumper.
76 wfDebug( __METHOD__
. ": initializing damaged or missing site_stats\n" );
78 SiteStatsInit
::doAllAndCommit( wfGetDB( DB_SLAVE
) );
80 $row = self
::doLoad( wfGetDB( DB_MASTER
) );
83 if( !self
::isSane( $row ) ) {
84 wfDebug( __METHOD__
. ": site_stats persistently nonsensical o_O\n" );
90 * @param $db DatabaseBase
91 * @return Bool|ResultWrapper
93 static function doLoad( $db ) {
94 return $db->selectRow( 'site_stats', '*', false, __METHOD__
);
100 static function views() {
102 return self
::$row->ss_total_views
;
108 static function edits() {
110 return self
::$row->ss_total_edits
;
116 static function articles() {
118 return self
::$row->ss_good_articles
;
124 static function pages() {
126 return self
::$row->ss_total_pages
;
132 static function users() {
134 return self
::$row->ss_users
;
140 static function activeUsers() {
142 return self
::$row->ss_active_users
;
148 static function images() {
150 return self
::$row->ss_images
;
154 * Find the number of users in a given user group.
155 * @param $group String: name of group
158 static function numberingroup( $group ) {
159 if ( !isset( self
::$groupMemberCounts[$group] ) ) {
161 $key = wfMemcKey( 'SiteStats', 'groupcounts', $group );
162 $hit = $wgMemc->get( $key );
164 $dbr = wfGetDB( DB_SLAVE
);
165 $hit = $dbr->selectField(
168 array( 'ug_group' => $group ),
171 $wgMemc->set( $key, $hit, 3600 );
173 self
::$groupMemberCounts[$group] = $hit;
175 return self
::$groupMemberCounts[$group];
181 static function jobs() {
182 if ( !isset( self
::$jobs ) ) {
183 $dbr = wfGetDB( DB_SLAVE
);
184 self
::$jobs = $dbr->estimateRowCount( 'job' );
185 /* Zero rows still do single row read for row that doesn't exist, but people are annoyed by that */
186 if ( self
::$jobs == 1 ) {
198 static function pagesInNs( $ns ) {
199 wfProfileIn( __METHOD__
);
200 if( !isset( self
::$pageCount[$ns] ) ) {
201 $dbr = wfGetDB( DB_SLAVE
);
202 self
::$pageCount[$ns] = (int)$dbr->selectField(
205 array( 'page_namespace' => $ns ),
209 wfProfileOut( __METHOD__
);
210 return self
::$pageCount[$ns];
214 * Is the provided row of site stats sane, or should it be regenerated?
220 private static function isSane( $row ) {
223 ||
$row->ss_total_pages
< $row->ss_good_articles
224 ||
$row->ss_total_edits
< $row->ss_total_pages
228 // Now check for underflow/overflow
229 foreach( array( 'total_views', 'total_edits', 'good_articles',
230 'total_pages', 'users', 'images' ) as $member ) {
232 $row->{"ss_$member"} > 2000000000
233 ||
$row->{"ss_$member"} < 0
243 * Class for handling updates to the site_stats table
245 class SiteStatsUpdate
implements DeferrableUpdate
{
246 protected $views = 0;
247 protected $edits = 0;
248 protected $pages = 0;
249 protected $goodPages = 0;
250 protected $users = 0;
251 protected $images = 0;
253 // @TODO: deprecate this
254 function __construct( $views, $edits, $good, $pages = 0, $users = 0 ) {
255 $this->views
= $views;
256 $this->edits
= $edits;
257 $this->goodPages
= $good;
258 $this->pages
= $pages;
259 $this->users
= $users;
263 * @param $deltas Array
264 * @return SiteStatsUpdate
266 public static function factory( array $deltas ) {
267 $update = new self( 0, 0, 0 );
269 $fields = array( 'views', 'edits', 'pages', 'goodPages', 'users', 'images' );
270 foreach ( $fields as $field ) {
271 if ( isset( $deltas[$field] ) && $deltas[$field] ) {
272 $update->$field = $deltas[$field];
284 function appendUpdate( &$sql, $field, $delta ) {
290 $sql .= "$field=$field-1";
292 $sql .= "$field=$field+1";
297 public function doUpdate() {
298 $dbw = wfGetDB( DB_MASTER
);
302 $this->appendUpdate( $updates, 'ss_total_views', $this->views
);
303 $this->appendUpdate( $updates, 'ss_total_edits', $this->edits
);
304 $this->appendUpdate( $updates, 'ss_good_articles', $this->goodPages
);
305 $this->appendUpdate( $updates, 'ss_total_pages', $this->pages
);
306 $this->appendUpdate( $updates, 'ss_users', $this->users
);
307 $this->appendUpdate( $updates, 'ss_images', $this->images
);
310 $site_stats = $dbw->tableName( 'site_stats' );
311 $sql = "UPDATE $site_stats SET $updates";
313 # Need a separate transaction because this a global lock
314 $dbw->begin( __METHOD__
);
315 $dbw->query( $sql, __METHOD__
);
316 $dbw->commit( __METHOD__
);
321 * @param $dbw DatabaseBase
324 public static function cacheUpdate( $dbw ) {
325 global $wgActiveUserDays;
326 $dbr = wfGetDB( DB_SLAVE
, array( 'SpecialStatistics', 'vslow' ) );
327 # Get non-bot users than did some recent action other than making accounts.
328 # If account creation is included, the number gets inflated ~20+ fold on enwiki.
329 $activeUsers = $dbr->selectField(
331 'COUNT( DISTINCT rc_user_text )',
335 "rc_log_type != 'newusers' OR rc_log_type IS NULL",
336 "rc_timestamp >= '{$dbw->timestamp( wfTimestamp( TS_UNIX ) - $wgActiveUserDays*24*3600 )}'",
342 array( 'ss_active_users' => intval( $activeUsers ) ),
343 array( 'ss_row_id' => 1 ),
351 * Class designed for counting of stats.
353 class SiteStatsInit
{
355 // Database connection
359 private $mEdits, $mArticles, $mPages, $mUsers, $mViews, $mFiles = 0;
363 * @param $database Boolean or DatabaseBase:
364 * - Boolean: whether to use the master DB
365 * - DatabaseBase: database connection to use
367 public function __construct( $database = false ) {
368 if ( $database instanceof DatabaseBase
) {
369 $this->db
= $database;
371 $this->db
= wfGetDB( $database ? DB_MASTER
: DB_SLAVE
);
376 * Count the total number of edits
379 public function edits() {
380 $this->mEdits
= $this->db
->selectField( 'revision', 'COUNT(*)', '', __METHOD__
);
381 $this->mEdits +
= $this->db
->selectField( 'archive', 'COUNT(*)', '', __METHOD__
);
382 return $this->mEdits
;
386 * Count pages in article space(s)
389 public function articles() {
390 global $wgArticleCountMethod;
392 $tables = array( 'page' );
394 'page_namespace' => MWNamespace
::getContentNamespaces(),
395 'page_is_redirect' => 0,
398 if ( $wgArticleCountMethod == 'link' ) {
399 $tables[] = 'pagelinks';
400 $conds[] = 'pl_from=page_id';
401 } elseif ( $wgArticleCountMethod == 'comma' ) {
402 // To make a correct check for this, we would need, for each page,
403 // to load the text, maybe uncompress it, maybe decode it and then
404 // check if there's one comma.
405 // But one thing we are sure is that if the page is empty, it can't
406 // contain a comma :)
407 $conds[] = 'page_len > 0';
410 $this->mArticles
= $this->db
->selectField( $tables, 'COUNT(DISTINCT page_id)',
411 $conds, __METHOD__
);
412 return $this->mArticles
;
419 public function pages() {
420 $this->mPages
= $this->db
->selectField( 'page', 'COUNT(*)', '', __METHOD__
);
421 return $this->mPages
;
428 public function users() {
429 $this->mUsers
= $this->db
->selectField( 'user', 'COUNT(*)', '', __METHOD__
);
430 return $this->mUsers
;
437 public function views() {
438 $this->mViews
= $this->db
->selectField( 'page', 'SUM(page_counter)', '', __METHOD__
);
439 return $this->mViews
;
446 public function files() {
447 $this->mFiles
= $this->db
->selectField( 'image', 'COUNT(*)', '', __METHOD__
);
448 return $this->mFiles
;
452 * Do all updates and commit them. More or less a replacement
453 * for the original initStats, but without output.
455 * @param $database DatabaseBase|bool
456 * - Boolean: whether to use the master DB
457 * - DatabaseBase: database connection to use
458 * @param $options Array of options, may contain the following values
459 * - update Boolean: whether to update the current stats (true) or write fresh (false) (default: false)
460 * - views Boolean: when true, do not update the number of page views (default: true)
461 * - activeUsers Boolean: whether to update the number of active users (default: false)
463 public static function doAllAndCommit( $database, array $options = array() ) {
464 $options +
= array( 'update' => false, 'views' => true, 'activeUsers' => false );
466 // Grab the object and count everything
467 $counter = new SiteStatsInit( $database );
470 $counter->articles();
475 // Only do views if we don't want to not count them
476 if( $options['views'] ) {
481 if( $options['update'] ) {
487 // Count active users if need be
488 if( $options['activeUsers'] ) {
489 SiteStatsUpdate
::cacheUpdate( wfGetDB( DB_MASTER
) );
494 * Update the current row with the selected values
496 public function update() {
497 list( $values, $conds ) = $this->getDbParams();
498 $dbw = wfGetDB( DB_MASTER
);
499 $dbw->update( 'site_stats', $values, $conds, __METHOD__
);
503 * Refresh site_stats. Erase the current record and save all
506 public function refresh() {
507 list( $values, $conds, $views ) = $this->getDbParams();
508 $dbw = wfGetDB( DB_MASTER
);
509 $dbw->delete( 'site_stats', $conds, __METHOD__
);
510 $dbw->insert( 'site_stats', array_merge( $values, $conds, $views ), __METHOD__
);
514 * Return three arrays of params for the db queries
517 private function getDbParams() {
519 'ss_total_edits' => $this->mEdits
,
520 'ss_good_articles' => $this->mArticles
,
521 'ss_total_pages' => $this->mPages
,
522 'ss_users' => $this->mUsers
,
523 'ss_images' => $this->mFiles
525 $conds = array( 'ss_row_id' => 1 );
526 $views = array( 'ss_total_views' => $this->mViews
);
527 return array( $values, $conds, $views );