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__
);
32 static function loadAndLazyInit() {
33 wfDebug( __METHOD__
. ": reading site_stats from slave\n" );
34 $row = self
::doLoad( wfGetDB( DB_SLAVE
) );
36 if( $row === false ) {
37 // Might have just been initialzed during this request?
38 wfDebug( __METHOD__
. ": site_stats missing on slave\n" );
39 $row = self
::doLoad( wfGetDB( DB_MASTER
) );
42 if( $row === false ) {
43 // Normally the site_stats table is initialized at install time.
44 // Some manual construction scenarios may leave the table empty,
45 // however, for instance when importing from a dump into a clean
46 // schema with mwdumper.
47 wfDebug( __METHOD__
. ": initializing empty site_stats\n" );
50 require_once "$IP/maintenance/initStats.inc";
56 $row = self
::doLoad( wfGetDB( DB_MASTER
) );
59 if( $row === false ) {
60 wfDebug( __METHOD__
. ": init of site_stats failed 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 images() {
96 return self
::$row->ss_images
;
99 static function admins() {
100 if ( !isset( self
::$admins ) ) {
101 $dbr = wfGetDB( DB_SLAVE
);
102 self
::$admins = $dbr->selectField( 'user_groups', 'COUNT(*)', array( 'ug_group' => 'sysop' ), __METHOD__
);
104 return self
::$admins;
107 static function pagesInNs( $ns ) {
108 wfProfileIn( __METHOD__
);
109 if( !isset( self
::$pageCount[$ns] ) ) {
110 $dbr = wfGetDB( DB_SLAVE
);
111 $pageCount[$ns] = (int)$dbr->selectField( 'page', 'COUNT(*)', array( 'page_namespace' => $ns ), __METHOD__
);
113 wfProfileOut( __METHOD__
);
114 return $pageCount[$ns];
123 class SiteStatsUpdate
{
125 var $mViews, $mEdits, $mGood, $mPages, $mUsers;
127 function __construct( $views, $edits, $good, $pages = 0, $users = 0 ) {
128 $this->mViews
= $views;
129 $this->mEdits
= $edits;
130 $this->mGood
= $good;
131 $this->mPages
= $pages;
132 $this->mUsers
= $users;
135 function appendUpdate( &$sql, $field, $delta ) {
141 $sql .= "$field=$field-1";
143 $sql .= "$field=$field+1";
148 function doUpdate() {
149 $fname = 'SiteStatsUpdate::doUpdate';
150 $dbw = wfGetDB( DB_MASTER
);
152 # First retrieve the row just to find out which schema we're in
153 $row = $dbw->selectRow( 'site_stats', '*', false, $fname );
157 $this->appendUpdate( $updates, 'ss_total_views', $this->mViews
);
158 $this->appendUpdate( $updates, 'ss_total_edits', $this->mEdits
);
159 $this->appendUpdate( $updates, 'ss_good_articles', $this->mGood
);
161 if ( isset( $row->ss_total_pages
) ) {
162 # Update schema if required
163 if ( $row->ss_total_pages
== -1 && !$this->mViews
) {
164 $dbr = wfGetDB( DB_SLAVE
, array( 'SpecialStatistics', 'vslow') );
165 list( $page, $user ) = $dbr->tableNamesN( 'page', 'user' );
167 $sql = "SELECT COUNT(page_namespace) AS total FROM $page";
168 $res = $dbr->query( $sql, $fname );
169 $pageRow = $dbr->fetchObject( $res );
170 $pages = $pageRow->total +
$this->mPages
;
172 $sql = "SELECT COUNT(user_id) AS total FROM $user";
173 $res = $dbr->query( $sql, $fname );
174 $userRow = $dbr->fetchObject( $res );
175 $users = $userRow->total +
$this->mUsers
;
180 $updates .= "ss_total_pages=$pages, ss_users=$users";
182 $this->appendUpdate( $updates, 'ss_total_pages', $this->mPages
);
183 $this->appendUpdate( $updates, 'ss_users', $this->mUsers
);
187 $site_stats = $dbw->tableName( 'site_stats' );
188 $sql = $dbw->limitResultForUpdate("UPDATE $site_stats SET $updates", 1);
190 $dbw->query( $sql, $fname );
195 global $wgDBname, $wgTitle;
196 if ( $this->mGood && $wgDBname == 'enwiki' ) {
197 $good = $dbw->selectField( 'site_stats', 'ss_good_articles', '', $fname );
198 error_log( $good . ' ' . $wgTitle->getPrefixedDBkey() . "\n", 3, '/home/wikipedia/logs/million.log' );