dd3fff129bd3657056a44efc153fd90f4ef6ec23
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" );
56 $row = self
::doLoad( wfGetDB( DB_MASTER
) );
59 if( !self
::isSane( $row ) ) {
60 wfDebug( __METHOD__
. ": site_stats persistently nonsensical o_O\n" );
65 static function doLoad( $db ) {
66 return $db->selectRow( 'site_stats', '*', false, __METHOD__
);
69 static function views() {
71 return self
::$row->ss_total_views
;
74 static function edits() {
76 return self
::$row->ss_total_edits
;
79 static function articles() {
81 return self
::$row->ss_good_articles
;
84 static function pages() {
86 return self
::$row->ss_total_pages
;
89 static function users() {
91 return self
::$row->ss_users
;
94 static function activeUsers() {
96 return self
::$row->ss_active_users
;
99 static function images() {
101 return self
::$row->ss_images
;
105 * @deprecated Use self::numberingroup('sysop') instead
107 static function admins() {
108 wfDeprecated(__METHOD__
);
109 return self
::numberingroup('sysop');
113 * Find the number of users in a given user group.
114 * @param string $group Name of group
117 static function numberingroup($group) {
118 if ( !isset( self
::$groupMemberCounts[$group] ) ) {
120 $key = wfMemcKey( 'SiteStats', 'groupcounts', $group );
121 $hit = $wgMemc->get( $key );
123 $dbr = wfGetDB( DB_SLAVE
);
124 $hit = $dbr->selectField( 'user_groups', 'COUNT(*)',
125 array( 'ug_group' => $group ), __METHOD__
);
126 $wgMemc->set( $key, $hit, 3600 );
128 self
::$groupMemberCounts[$group] = $hit;
130 return self
::$groupMemberCounts[$group];
133 static function jobs() {
134 if ( !isset( self
::$jobs ) ) {
135 $dbr = wfGetDB( DB_SLAVE
);
136 self
::$jobs = $dbr->estimateRowCount('job');
137 /* Zero rows still do single row read for row that doesn't exist, but people are annoyed by that */
138 if (self
::$jobs == 1) {
145 static function pagesInNs( $ns ) {
146 wfProfileIn( __METHOD__
);
147 if( !isset( self
::$pageCount[$ns] ) ) {
148 $dbr = wfGetDB( DB_SLAVE
);
149 $pageCount[$ns] = (int)$dbr->selectField( 'page', 'COUNT(*)', array( 'page_namespace' => $ns ), __METHOD__
);
151 wfProfileOut( __METHOD__
);
152 return $pageCount[$ns];
155 /** Is the provided row of site stats sane, or should it be regenerated? */
156 private static function isSane( $row ) {
159 or $row->ss_total_pages
< $row->ss_good_articles
160 or $row->ss_total_edits
< $row->ss_total_pages
161 or $row->ss_users
< $row->ss_admins
165 // Now check for underflow/overflow
166 foreach( array( 'total_views', 'total_edits', 'good_articles',
167 'total_pages', 'users', 'admins', 'images' ) as $member ) {
169 $row->{"ss_$member"} > 2000000000
170 or $row->{"ss_$member"} < 0
179 * Ported from initStats.inc.
180 * @param $update bool Whether to update the current stats write fresh
181 * @param $noViews bool When true, do not update the number of page views
183 function init( $update, $noViews = false ) {
184 $dbr = wfGetDB( DB_SLAVE
);
186 wfOut( "Counting total edits..." );
187 $edits = $dbr->selectField( 'revision', 'COUNT(*)', '', __METHOD__
);
188 $edits +
= $dbr->selectField( 'archive', 'COUNT(*)', '', __METHOD__
);
189 wfOut( "{$edits}\nCounting number of articles..." );
191 global $wgContentNamespaces;
192 $good = $dbr->selectField( 'page', 'COUNT(*)', array( 'page_namespace' => $wgContentNamespaces, 'page_is_redirect' => 0, 'page_len > 0' ), __METHOD__
);
193 wfOut( "{$good}\nCounting total pages..." );
195 $pages = $dbr->selectField( 'page', 'COUNT(*)', '', __METHOD__
);
196 wfOut( "{$pages}\nCounting number of users..." );
198 $users = $dbr->selectField( 'user', 'COUNT(*)', '', __METHOD__
);
199 wfOut( "{$users}\nCounting number of admins..." );
201 $admin = $dbr->selectField( 'user_groups', 'COUNT(*)', array( 'ug_group' => 'sysop' ), __METHOD__
);
202 wfOut( "{$admin}\nCounting number of images..." );
204 $image = $dbr->selectField( 'image', 'COUNT(*)', '', __METHOD__
);
205 wfOut( "{$image}\n" );
208 wfOut( "Counting total page views..." );
209 $views = $dbr->selectField( 'page', 'SUM(page_counter)', '', __METHOD__
);
210 wfOut( "{$views}\n" );
213 wfOut( "\nUpdating site statistics..." );
215 $dbw = wfGetDB( DB_MASTER
);
216 $values = array( 'ss_total_edits' => $edits,
217 'ss_good_articles' => $good,
218 'ss_total_pages' => $pages,
219 'ss_users' => $users,
220 'ss_admins' => $admin,
221 'ss_images' => $image );
222 $conds = array( 'ss_row_id' => 1 );
223 $views = array( 'ss_total_views' => isset( $views ) ?
$views : 0 );
226 $dbw->update( 'site_stats', $values, $conds, __METHOD__
);
228 $dbw->delete( 'site_stats', $conds, __METHOD__
);
229 $dbw->insert( 'site_stats', array_merge( $values, $conds, $views ), __METHOD__
);
240 class SiteStatsUpdate
{
242 var $mViews, $mEdits, $mGood, $mPages, $mUsers;
244 function __construct( $views, $edits, $good, $pages = 0, $users = 0 ) {
245 $this->mViews
= $views;
246 $this->mEdits
= $edits;
247 $this->mGood
= $good;
248 $this->mPages
= $pages;
249 $this->mUsers
= $users;
252 function appendUpdate( &$sql, $field, $delta ) {
258 $sql .= "$field=$field-1";
260 $sql .= "$field=$field+1";
265 function doUpdate() {
266 $fname = 'SiteStatsUpdate::doUpdate';
267 $dbw = wfGetDB( DB_MASTER
);
271 $this->appendUpdate( $updates, 'ss_total_views', $this->mViews
);
272 $this->appendUpdate( $updates, 'ss_total_edits', $this->mEdits
);
273 $this->appendUpdate( $updates, 'ss_good_articles', $this->mGood
);
274 $this->appendUpdate( $updates, 'ss_total_pages', $this->mPages
);
275 $this->appendUpdate( $updates, 'ss_users', $this->mUsers
);
278 $site_stats = $dbw->tableName( 'site_stats' );
279 $sql = "UPDATE $site_stats SET $updates";
281 # Need a separate transaction because this a global lock
283 $dbw->query( $sql, $fname );
288 public static function cacheUpdate( $dbw ) {
289 $dbr = wfGetDB( DB_SLAVE
, array( 'SpecialStatistics', 'vslow') );
290 # Get non-bot users than did some recent action other than making accounts.
291 # If account creation is included, the number gets inflated ~20+ fold on enwiki.
292 $activeUsers = $dbr->selectField( 'recentchanges', 'COUNT( DISTINCT rc_user_text )',
293 array( 'rc_user != 0', 'rc_bot' => 0, "rc_log_type != 'newusers' OR rc_log_type IS NULL" ),
295 $dbw->update( 'site_stats',
296 array( 'ss_active_users' => intval($activeUsers) ),
297 array( 'ss_row_id' => 1 ), __METHOD__