4 * Static accessor class for site_stats and related things
7 static $row, $loaded = false;
9 static $pageCount = array();
10 static $groupMemberCounts = array();
12 static function recache() {
16 static function load( $recache = false ) {
17 if ( self
::$loaded && !$recache ) {
21 self
::$row = self
::loadAndLazyInit();
23 # This code is somewhat schema-agnostic, because I'm changing it in a minor release -- TS
24 if ( !isset( self
::$row->ss_total_pages
) && self
::$row->ss_total_pages
== -1 ) {
26 $u = new SiteStatsUpdate( 0, 0, 0 );
28 $dbr = wfGetDB( DB_SLAVE
);
29 self
::$row = $dbr->selectRow( 'site_stats', '*', false, __METHOD__
);
35 static function loadAndLazyInit() {
36 wfDebug( __METHOD__
. ": reading site_stats from slave\n" );
37 $row = self
::doLoad( wfGetDB( DB_SLAVE
) );
39 if( !self
::isSane( $row ) ) {
40 // Might have just been initialized during this request? Underflow?
41 wfDebug( __METHOD__
. ": site_stats damaged or missing on slave\n" );
42 $row = self
::doLoad( wfGetDB( DB_MASTER
) );
45 if( !self
::isSane( $row ) ) {
46 // Normally the site_stats table is initialized at install time.
47 // Some manual construction scenarios may leave the table empty or
48 // broken, however, for instance when importing from a dump into a
49 // clean schema with mwdumper.
50 wfDebug( __METHOD__
. ": initializing damaged or missing site_stats\n" );
52 SiteStatsInit
::doAllAndCommit( false );
54 $row = self
::doLoad( wfGetDB( DB_MASTER
) );
57 if( !self
::isSane( $row ) ) {
58 wfDebug( __METHOD__
. ": site_stats persistently nonsensical o_O\n" );
63 static function doLoad( $db ) {
64 return $db->selectRow( 'site_stats', '*', false, __METHOD__
);
67 static function views() {
69 return self
::$row->ss_total_views
;
72 static function edits() {
74 return self
::$row->ss_total_edits
;
77 static function articles() {
79 return self
::$row->ss_good_articles
;
82 static function pages() {
84 return self
::$row->ss_total_pages
;
87 static function users() {
89 return self
::$row->ss_users
;
92 static function activeUsers() {
94 return self
::$row->ss_active_users
;
97 static function images() {
99 return self
::$row->ss_images
;
103 * Find the number of users in a given user group.
104 * @param $group String: name of group
107 static function numberingroup( $group ) {
108 if ( !isset( self
::$groupMemberCounts[$group] ) ) {
110 $key = wfMemcKey( 'SiteStats', 'groupcounts', $group );
111 $hit = $wgMemc->get( $key );
113 $dbr = wfGetDB( DB_SLAVE
);
114 $hit = $dbr->selectField(
117 array( 'ug_group' => $group ),
120 $wgMemc->set( $key, $hit, 3600 );
122 self
::$groupMemberCounts[$group] = $hit;
124 return self
::$groupMemberCounts[$group];
127 static function jobs() {
128 if ( !isset( self
::$jobs ) ) {
129 $dbr = wfGetDB( DB_SLAVE
);
130 self
::$jobs = $dbr->estimateRowCount( 'job' );
131 /* Zero rows still do single row read for row that doesn't exist, but people are annoyed by that */
132 if ( self
::$jobs == 1 ) {
139 static function pagesInNs( $ns ) {
140 wfProfileIn( __METHOD__
);
141 if( !isset( self
::$pageCount[$ns] ) ) {
142 $dbr = wfGetDB( DB_SLAVE
);
143 $pageCount[$ns] = (int)$dbr->selectField(
146 array( 'page_namespace' => $ns ),
150 wfProfileOut( __METHOD__
);
151 return $pageCount[$ns];
154 /** Is the provided row of site stats sane, or should it be regenerated? */
155 private static function isSane( $row ) {
158 ||
$row->ss_total_pages
< $row->ss_good_articles
159 ||
$row->ss_total_edits
< $row->ss_total_pages
163 // Now check for underflow/overflow
164 foreach( array( 'total_views', 'total_edits', 'good_articles',
165 'total_pages', 'users', 'admins', 'images' ) as $member ) {
167 $row->{"ss_$member"} > 2000000000
168 ||
$row->{"ss_$member"} < 0
181 class SiteStatsUpdate
{
183 var $mViews, $mEdits, $mGood, $mPages, $mUsers;
185 function __construct( $views, $edits, $good, $pages = 0, $users = 0 ) {
186 $this->mViews
= $views;
187 $this->mEdits
= $edits;
188 $this->mGood
= $good;
189 $this->mPages
= $pages;
190 $this->mUsers
= $users;
193 function appendUpdate( &$sql, $field, $delta ) {
199 $sql .= "$field=$field-1";
201 $sql .= "$field=$field+1";
206 function doUpdate() {
207 $dbw = wfGetDB( DB_MASTER
);
211 $this->appendUpdate( $updates, 'ss_total_views', $this->mViews
);
212 $this->appendUpdate( $updates, 'ss_total_edits', $this->mEdits
);
213 $this->appendUpdate( $updates, 'ss_good_articles', $this->mGood
);
214 $this->appendUpdate( $updates, 'ss_total_pages', $this->mPages
);
215 $this->appendUpdate( $updates, 'ss_users', $this->mUsers
);
218 $site_stats = $dbw->tableName( 'site_stats' );
219 $sql = "UPDATE $site_stats SET $updates";
221 # Need a separate transaction because this a global lock
223 $dbw->query( $sql, __METHOD__
);
228 public static function cacheUpdate( $dbw ) {
229 global $wgActiveUserDays;
230 $dbr = wfGetDB( DB_SLAVE
, array( 'SpecialStatistics', 'vslow' ) );
231 # Get non-bot users than did some recent action other than making accounts.
232 # If account creation is included, the number gets inflated ~20+ fold on enwiki.
233 $activeUsers = $dbr->selectField(
235 'COUNT( DISTINCT rc_user_text )',
239 "rc_log_type != 'newusers' OR rc_log_type IS NULL",
240 "rc_timestamp >= '{$dbw->timestamp( wfTimestamp( TS_UNIX ) - $wgActiveUserDays*24*3600 )}'",
246 array( 'ss_active_users' => intval( $activeUsers ) ),
247 array( 'ss_row_id' => 1 ),
255 * Class designed for counting of stats.
257 class SiteStatsInit
{
259 // Database connection
263 private $mEdits, $mArticles, $mPages, $mUsers, $mViews, $mFiles = 0;
267 * @param $useMaster Boolean: whether to use the master DB
269 public function __construct( $useMaster = false ) {
270 $this->db
= wfGetDB( $useMaster ? DB_MASTER
: DB_SLAVE
);
274 * Count the total number of edits
277 public function edits() {
278 $this->mEdits
= $this->db
->selectField( 'revision', 'COUNT(*)', '', __METHOD__
);
279 $this->mEdits +
= $this->db
->selectField( 'archive', 'COUNT(*)', '', __METHOD__
);
280 return $this->mEdits
;
284 * Count pages in article space(s)
287 public function articles() {
288 global $wgArticleCountMethod;
290 $tables = array( 'page' );
292 'page_namespace' => MWNamespace
::getContentNamespaces(),
293 'page_is_redirect' => 0,
296 if ( $wgArticleCountMethod == 'link' ) {
297 $tables[] = 'pagelinks';
298 $conds[] = 'pl_from=page_id';
299 } elseif ( $wgArticleCountMethod == 'comma' ) {
300 // To make a correct check for this, we would need, for each page,
301 // to load the text, maybe uncompress it, maybe decode it and then
302 // check if there's one comma.
303 // But one thing we are sure is that if the page is empty, it can't
304 // contain a comma :)
305 $conds[] = 'page_len > 0';
308 $this->mArticles
= $this->db
->selectField( $tables, 'COUNT(DISTINCT page_id)',
309 $conds, __METHOD__
);
310 return $this->mArticles
;
317 public function pages() {
318 $this->mPages
= $this->db
->selectField( 'page', 'COUNT(*)', '', __METHOD__
);
319 return $this->mPages
;
326 public function users() {
327 $this->mUsers
= $this->db
->selectField( 'user', 'COUNT(*)', '', __METHOD__
);
328 return $this->mUsers
;
335 public function views() {
336 $this->mViews
= $this->db
->selectField( 'page', 'SUM(page_counter)', '', __METHOD__
);
337 return $this->mViews
;
344 public function files() {
345 $this->mFiles
= $this->db
->selectField( 'image', 'COUNT(*)', '', __METHOD__
);
346 return $this->mFiles
;
350 * Do all updates and commit them. More or less a replacement
351 * for the original initStats, but without the calls to wfOut()
352 * @param $update Boolean: whether to update the current stats or write fresh
353 * @param $noViews Boolean: when true, do not update the number of page views
354 * @param $activeUsers Boolean: whether to update the number of active users
356 public static function doAllAndCommit( $update, $noViews = false, $activeUsers = false ) {
357 // Grab the object and count everything
358 $counter = new SiteStatsInit( false );
360 $counter->articles();
365 // Only do views if we don't want to not count them
377 // Count active users if need be
379 SiteStatsUpdate
::cacheUpdate( wfGetDB( DB_MASTER
) );
384 * Update the current row with the selected values
386 public function update() {
387 list( $values, $conds ) = $this->getDbParams();
388 $dbw = wfGetDB( DB_MASTER
);
389 $dbw->update( 'site_stats', $values, $conds, __METHOD__
);
393 * Refresh site_stats. Erase the current record and save all
396 public function refresh() {
397 list( $values, $conds, $views ) = $this->getDbParams();
398 $dbw = wfGetDB( DB_MASTER
);
399 $dbw->delete( 'site_stats', $conds, __METHOD__
);
400 $dbw->insert( 'site_stats', array_merge( $values, $conds, $views ), __METHOD__
);
404 * Return three arrays of params for the db queries
407 private function getDbParams() {
409 'ss_total_edits' => $this->mEdits
,
410 'ss_good_articles' => $this->mArticles
,
411 'ss_total_pages' => $this->mPages
,
412 'ss_users' => $this->mUsers
,
413 'ss_images' => $this->mFiles
415 $conds = array( 'ss_row_id' => 1 );
416 $views = array( 'ss_total_views' => $this->mViews
);
417 return array( $values, $conds, $views );