46700ccd9e1098f872c0eadf8411e85f97e79985
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 $dbr = wfGetDB( DB_SLAVE
);
21 self
::$row = $dbr->selectRow( 'site_stats', '*', false, __METHOD__
);
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 self
::$row = $dbr->selectRow( 'site_stats', '*', false, __METHOD__
);
32 static function views() {
34 return self
::$row->ss_total_views
;
37 static function edits() {
39 return self
::$row->ss_total_edits
;
42 static function articles() {
44 return self
::$row->ss_good_articles
;
47 static function pages() {
49 return self
::$row->ss_total_pages
;
52 static function users() {
54 return self
::$row->ss_users
;
57 static function images() {
59 return self
::$row->ss_images
;
62 static function admins() {
63 if ( !isset( self
::$admins ) ) {
64 $dbr = wfGetDB( DB_SLAVE
);
65 self
::$admins = $dbr->selectField( 'user_groups', 'COUNT(*)', array( 'ug_group' => 'sysop' ), __METHOD__
);
70 static function pagesInNs( $ns ) {
71 wfProfileIn( __METHOD__
);
72 if( !isset( self
::$pageCount[$ns] ) ) {
73 $dbr = wfGetDB( DB_SLAVE
);
74 $pageCount[$ns] = (int)$dbr->selectField( 'page', 'COUNT(*)', array( 'page_namespace' => $ns ), __METHOD__
);
76 wfProfileOut( __METHOD__
);
77 return $pageCount[$ns];
86 class SiteStatsUpdate
{
88 var $mViews, $mEdits, $mGood, $mPages, $mUsers;
90 function __construct( $views, $edits, $good, $pages = 0, $users = 0 ) {
91 $this->mViews
= $views;
92 $this->mEdits
= $edits;
94 $this->mPages
= $pages;
95 $this->mUsers
= $users;
98 function appendUpdate( &$sql, $field, $delta ) {
104 $sql .= "$field=$field-1";
106 $sql .= "$field=$field+1";
111 function doUpdate() {
112 $fname = 'SiteStatsUpdate::doUpdate';
113 $dbw = wfGetDB( DB_MASTER
);
115 # First retrieve the row just to find out which schema we're in
116 $row = $dbw->selectRow( 'site_stats', '*', false, $fname );
120 $this->appendUpdate( $updates, 'ss_total_views', $this->mViews
);
121 $this->appendUpdate( $updates, 'ss_total_edits', $this->mEdits
);
122 $this->appendUpdate( $updates, 'ss_good_articles', $this->mGood
);
124 if ( isset( $row->ss_total_pages
) ) {
125 # Update schema if required
126 if ( $row->ss_total_pages
== -1 && !$this->mViews
) {
127 $dbr = wfGetDB( DB_SLAVE
, array( 'SpecialStatistics', 'vslow') );
128 list( $page, $user ) = $dbr->tableNamesN( 'page', 'user' );
130 $sql = "SELECT COUNT(page_namespace) AS total FROM $page";
131 $res = $dbr->query( $sql, $fname );
132 $pageRow = $dbr->fetchObject( $res );
133 $pages = $pageRow->total +
$this->mPages
;
135 $sql = "SELECT COUNT(user_id) AS total FROM $user";
136 $res = $dbr->query( $sql, $fname );
137 $userRow = $dbr->fetchObject( $res );
138 $users = $userRow->total +
$this->mUsers
;
143 $updates .= "ss_total_pages=$pages, ss_users=$users";
145 $this->appendUpdate( $updates, 'ss_total_pages', $this->mPages
);
146 $this->appendUpdate( $updates, 'ss_users', $this->mUsers
);
150 $site_stats = $dbw->tableName( 'site_stats' );
151 $sql = $dbw->limitResultForUpdate("UPDATE $site_stats SET $updates", 1);
153 $dbw->query( $sql, $fname );
158 global $wgDBname, $wgTitle;
159 if ( $this->mGood && $wgDBname == 'enwiki' ) {
160 $good = $dbw->selectField( 'site_stats', 'ss_good_articles', '', $fname );
161 error_log( $good . ' ' . $wgTitle->getPrefixedDBkey() . "\n", 3, '/home/wikipedia/logs/million.log' );