4 * Static accessor class for site_stats and related things
7 static $row, $loaded = false;
9 static $pageCount = array();
11 static function recache() {
15 static function load( $recache = false ) {
16 if ( self
::$loaded && !$recache ) {
20 self
::$row = self
::loadAndLazyInit();
22 # This code is somewhat schema-agnostic, because I'm changing it in a minor release -- TS
23 if ( !isset( self
::$row->ss_total_pages
) && self
::$row->ss_total_pages
== -1 ) {
25 $u = new SiteStatsUpdate( 0, 0, 0 );
27 $dbr = wfGetDB( DB_SLAVE
);
28 self
::$row = $dbr->selectRow( 'site_stats', '*', false, __METHOD__
);
34 static function loadAndLazyInit() {
35 wfDebug( __METHOD__
. ": reading site_stats from slave\n" );
36 $row = self
::doLoad( wfGetDB( DB_SLAVE
) );
38 if( !self
::isSane( $row ) ) {
39 // Might have just been initialized during this request? Underflow?
40 wfDebug( __METHOD__
. ": site_stats damaged or missing on slave\n" );
41 $row = self
::doLoad( wfGetDB( DB_MASTER
) );
44 if( !self
::isSane( $row ) ) {
45 // Normally the site_stats table is initialized at install time.
46 // Some manual construction scenarios may leave the table empty or
47 // broken, however, for instance when importing from a dump into a
48 // clean schema with mwdumper.
49 wfDebug( __METHOD__
. ": initializing damaged or missing site_stats\n" );
52 require_once "$IP/maintenance/initStats.inc";
58 $row = self
::doLoad( wfGetDB( DB_MASTER
) );
61 if( !self
::isSane( $row ) ) {
62 wfDebug( __METHOD__
. ": site_stats persistently nonsensical o_O\n" );
67 static function doLoad( $db ) {
68 return $db->selectRow( 'site_stats', '*', false, __METHOD__
);
71 static function views() {
73 return self
::$row->ss_total_views
;
76 static function edits() {
78 return self
::$row->ss_total_edits
;
81 static function articles() {
83 return self
::$row->ss_good_articles
;
86 static function pages() {
88 return self
::$row->ss_total_pages
;
91 static function users() {
93 return self
::$row->ss_users
;
96 static function images() {
98 return self
::$row->ss_images
;
101 static function admins() {
102 if ( !isset( self
::$admins ) ) {
103 $dbr = wfGetDB( DB_SLAVE
);
104 self
::$admins = $dbr->selectField( 'user_groups', 'COUNT(*)', array( 'ug_group' => 'sysop' ), __METHOD__
);
106 return self
::$admins;
109 static function jobs() {
110 if ( !isset( self
::$jobs ) ) {
111 $dbr = wfGetDB( DB_SLAVE
);
112 self
::$jobs = $dbr->estimateRowCount('job');
113 /* Zero rows still do single row read for row that doesn't exist, but people are annoyed by that */
114 if (self
::$jobs == 1) {
121 static function pagesInNs( $ns ) {
122 wfProfileIn( __METHOD__
);
123 if( !isset( self
::$pageCount[$ns] ) ) {
124 $dbr = wfGetDB( DB_SLAVE
);
125 $pageCount[$ns] = (int)$dbr->selectField( 'page', 'COUNT(*)', array( 'page_namespace' => $ns ), __METHOD__
);
127 wfProfileOut( __METHOD__
);
128 return $pageCount[$ns];
131 /** Is the provided row of site stats sane, or should it be regenerated? */
132 private static function isSane( $row ) {
135 or $row->ss_total_pages
< $row->ss_good_articles
136 or $row->ss_total_edits
< $row->ss_total_pages
137 or $row->ss_users
< $row->ss_admins
141 // Now check for underflow/overflow
142 foreach( array( 'total_views', 'total_edits', 'good_articles',
143 'total_pages', 'users', 'admins', 'images' ) as $member ) {
145 $row->{"ss_$member"} > 2000000000
146 or $row->{"ss_$member"} < 0
159 class SiteStatsUpdate
{
161 var $mViews, $mEdits, $mGood, $mPages, $mUsers;
163 function __construct( $views, $edits, $good, $pages = 0, $users = 0 ) {
164 $this->mViews
= $views;
165 $this->mEdits
= $edits;
166 $this->mGood
= $good;
167 $this->mPages
= $pages;
168 $this->mUsers
= $users;
171 function appendUpdate( &$sql, $field, $delta ) {
177 $sql .= "$field=$field-1";
179 $sql .= "$field=$field+1";
184 function doUpdate() {
185 $fname = 'SiteStatsUpdate::doUpdate';
186 $dbw = wfGetDB( DB_MASTER
);
188 # First retrieve the row just to find out which schema we're in
189 $row = $dbw->selectRow( 'site_stats', '*', false, $fname );
193 $this->appendUpdate( $updates, 'ss_total_views', $this->mViews
);
194 $this->appendUpdate( $updates, 'ss_total_edits', $this->mEdits
);
195 $this->appendUpdate( $updates, 'ss_good_articles', $this->mGood
);
197 if ( isset( $row->ss_total_pages
) ) {
198 # Update schema if required
199 if ( $row->ss_total_pages
== -1 && !$this->mViews
) {
200 $dbr = wfGetDB( DB_SLAVE
, array( 'SpecialStatistics', 'vslow') );
201 list( $page, $user ) = $dbr->tableNamesN( 'page', 'user' );
203 $sql = "SELECT COUNT(page_namespace) AS total FROM $page";
204 $res = $dbr->query( $sql, $fname );
205 $pageRow = $dbr->fetchObject( $res );
206 $pages = $pageRow->total +
$this->mPages
;
208 $sql = "SELECT COUNT(user_id) AS total FROM $user";
209 $res = $dbr->query( $sql, $fname );
210 $userRow = $dbr->fetchObject( $res );
211 $users = $userRow->total +
$this->mUsers
;
216 $updates .= "ss_total_pages=$pages, ss_users=$users";
218 $this->appendUpdate( $updates, 'ss_total_pages', $this->mPages
);
219 $this->appendUpdate( $updates, 'ss_users', $this->mUsers
);
223 $site_stats = $dbw->tableName( 'site_stats' );
224 $sql = $dbw->limitResultForUpdate("UPDATE $site_stats SET $updates", 1);
226 $dbw->query( $sql, $fname );
231 global $wgDBname, $wgTitle;
232 if ( $this->mGood && $wgDBname == 'enwiki' ) {
233 $good = $dbw->selectField( 'site_stats', 'ss_good_articles', '', $fname );
234 error_log( $good . ' ' . $wgTitle->getPrefixedDBkey() . "\n", 3, '/home/wikipedia/logs/million.log' );