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" );
53 require_once "$IP/maintenance/initStats.inc";
59 $row = self
::doLoad( wfGetDB( DB_MASTER
) );
62 if( !self
::isSane( $row ) ) {
63 wfDebug( __METHOD__
. ": site_stats persistently nonsensical o_O\n" );
68 static function doLoad( $db ) {
69 return $db->selectRow( 'site_stats', '*', false, __METHOD__
);
72 static function views() {
74 return self
::$row->ss_total_views
;
77 static function edits() {
79 return self
::$row->ss_total_edits
;
82 static function articles() {
84 return self
::$row->ss_good_articles
;
87 static function pages() {
89 return self
::$row->ss_total_pages
;
92 static function users() {
94 return self
::$row->ss_users
;
97 static function images() {
99 return self
::$row->ss_images
;
103 * @deprecated Use self::numberingroup('sysop') instead
105 static function admins() {
106 wfDeprecated(__METHOD__
);
107 return self
::numberingroup('sysop');
111 * Find the number of users in a given user group.
112 * @param string $group Name of group
115 static function numberingroup($group) {
116 if ( !isset( self
::$groupMemberCounts[$group] ) ) {
118 $key = wfMemcKey( 'SiteStats', 'groupcounts', $group );
119 $hit = $wgMemc->get( $key );
121 $dbr = wfGetDB( DB_SLAVE
);
122 $hit = $dbr->selectField( 'user_groups', 'COUNT(*)',
123 array( 'ug_group' => $group ), __METHOD__
);
124 $wgMemc->set( $key, $hit, 3600 );
126 self
::$groupMemberCounts[$group] = $hit;
128 return self
::$groupMemberCounts[$group];
131 static function jobs() {
132 if ( !isset( self
::$jobs ) ) {
133 $dbr = wfGetDB( DB_SLAVE
);
134 self
::$jobs = $dbr->estimateRowCount('job');
135 /* Zero rows still do single row read for row that doesn't exist, but people are annoyed by that */
136 if (self
::$jobs == 1) {
143 static function pagesInNs( $ns ) {
144 wfProfileIn( __METHOD__
);
145 if( !isset( self
::$pageCount[$ns] ) ) {
146 $dbr = wfGetDB( DB_SLAVE
);
147 $pageCount[$ns] = (int)$dbr->selectField( 'page', 'COUNT(*)', array( 'page_namespace' => $ns ), __METHOD__
);
149 wfProfileOut( __METHOD__
);
150 return $pageCount[$ns];
153 /** Is the provided row of site stats sane, or should it be regenerated? */
154 private static function isSane( $row ) {
157 or $row->ss_total_pages
< $row->ss_good_articles
158 or $row->ss_total_edits
< $row->ss_total_pages
159 or $row->ss_users
< $row->ss_admins
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 or $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 $fname = 'SiteStatsUpdate::doUpdate';
208 $dbw = wfGetDB( DB_MASTER
);
210 # First retrieve the row just to find out which schema we're in
211 $row = $dbw->selectRow( 'site_stats', '*', false, $fname );
215 $this->appendUpdate( $updates, 'ss_total_views', $this->mViews
);
216 $this->appendUpdate( $updates, 'ss_total_edits', $this->mEdits
);
217 $this->appendUpdate( $updates, 'ss_good_articles', $this->mGood
);
219 if ( isset( $row->ss_total_pages
) ) {
220 # Update schema if required
221 if ( $row->ss_total_pages
== -1 && !$this->mViews
) {
222 $dbr = wfGetDB( DB_SLAVE
, array( 'SpecialStatistics', 'vslow') );
223 list( $page, $user ) = $dbr->tableNamesN( 'page', 'user' );
225 $sql = "SELECT COUNT(page_namespace) AS total FROM $page";
226 $res = $dbr->query( $sql, $fname );
227 $pageRow = $dbr->fetchObject( $res );
228 $pages = $pageRow->total +
$this->mPages
;
230 $sql = "SELECT COUNT(user_id) AS total FROM $user";
231 $res = $dbr->query( $sql, $fname );
232 $userRow = $dbr->fetchObject( $res );
233 $users = $userRow->total +
$this->mUsers
;
238 $updates .= "ss_total_pages=$pages, ss_users=$users";
240 $this->appendUpdate( $updates, 'ss_total_pages', $this->mPages
);
241 $this->appendUpdate( $updates, 'ss_users', $this->mUsers
);
245 $site_stats = $dbw->tableName( 'site_stats' );
246 $sql = $dbw->limitResultForUpdate("UPDATE $site_stats SET $updates", 1);
248 $dbw->query( $sql, $fname );
253 global $wgDBname, $wgTitle;
254 if ( $this->mGood && $wgDBname == 'enwiki' ) {
255 $good = $dbw->selectField( 'site_stats', 'ss_good_articles', '', $fname );
256 error_log( $good . ' ' . $wgTitle->getPrefixedDBkey() . "\n", 3, '/home/wikipedia/logs/million.log' );