Whoops, reversed inequality. Forgot to check whether this is being run *too* often.
[lhc/web/wiklou.git] / includes / SiteStats.php
1 <?php
2
3 /**
4 * Static accessor class for site_stats and related things
5 */
6 class SiteStats {
7 static $row, $loaded = false;
8 static $admins;
9 static $pageCount = array();
10
11 static function recache() {
12 self::load( true );
13 }
14
15 static function load( $recache = false ) {
16 if ( self::$loaded && !$recache ) {
17 return;
18 }
19
20 self::$row = self::loadAndLazyInit();
21
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 ) {
24 # Update schema
25 $u = new SiteStatsUpdate( 0, 0, 0 );
26 $u->doUpdate();
27 $dbr = wfGetDB( DB_SLAVE );
28 self::$row = $dbr->selectRow( 'site_stats', '*', false, __METHOD__ );
29 }
30 }
31
32 static function loadAndLazyInit() {
33 wfDebug( __METHOD__ . ": reading site_stats from slave\n" );
34 $row = self::doLoad( wfGetDB( DB_SLAVE ) );
35
36 if( !self::isSane( $row ) ) {
37 // Might have just been initialized during this request? Underflow?
38 wfDebug( __METHOD__ . ": site_stats damaged or missing on slave\n" );
39 $row = self::doLoad( wfGetDB( DB_MASTER ) );
40 }
41
42 if( !self::isSane( $row ) ) {
43 // Normally the site_stats table is initialized at install time.
44 // Some manual construction scenarios may leave the table empty or
45 // broken, however, for instance when importing from a dump into a
46 // clean schema with mwdumper.
47 wfDebug( __METHOD__ . ": initializing damaged or missing site_stats\n" );
48
49 global $IP;
50 require_once "$IP/maintenance/initStats.inc";
51
52 ob_start();
53 wfInitStats();
54 ob_end_clean();
55
56 $row = self::doLoad( wfGetDB( DB_MASTER ) );
57 }
58
59 if( !self::isSane( $row ) ) {
60 wfDebug( __METHOD__ . ": site_stats persistently nonsensical o_O\n" );
61 }
62 return $row;
63 }
64
65 static function doLoad( $db ) {
66 return $db->selectRow( 'site_stats', '*', false, __METHOD__ );
67 }
68
69 static function views() {
70 self::load();
71 return self::$row->ss_total_views;
72 }
73
74 static function edits() {
75 self::load();
76 return self::$row->ss_total_edits;
77 }
78
79 static function articles() {
80 self::load();
81 return self::$row->ss_good_articles;
82 }
83
84 static function pages() {
85 self::load();
86 return self::$row->ss_total_pages;
87 }
88
89 static function users() {
90 self::load();
91 return self::$row->ss_users;
92 }
93
94 static function images() {
95 self::load();
96 return self::$row->ss_images;
97 }
98
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__ );
103 }
104 return self::$admins;
105 }
106
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__ );
112 }
113 wfProfileOut( __METHOD__ );
114 return $pageCount[$ns];
115 }
116
117 /** Is the provided row of site stats sane, or should it be regenerated? */
118 private static function isSane( $row ) {
119 if(
120 $row === false
121 or $row->ss_total_pages < $row->ss_good_articles
122 or $row->ss_total_edits < $row->ss_total_pages
123 or $row->ss_users < $row->ss_admins
124 ) {
125 return false;
126 }
127 // Now check for underflow/overflow
128 foreach( array( 'total_views', 'total_edits', 'good_articles',
129 'total_pages', 'users', 'admins', 'images' ) as $member ) {
130 if(
131 $row->{"ss_$member"} > 2000000000
132 or $row->{"ss_$member"} < 0
133 ) {
134 return false;
135 }
136 }
137 }
138 }
139
140
141 /**
142 *
143 */
144 class SiteStatsUpdate {
145
146 var $mViews, $mEdits, $mGood, $mPages, $mUsers;
147
148 function __construct( $views, $edits, $good, $pages = 0, $users = 0 ) {
149 $this->mViews = $views;
150 $this->mEdits = $edits;
151 $this->mGood = $good;
152 $this->mPages = $pages;
153 $this->mUsers = $users;
154 }
155
156 function appendUpdate( &$sql, $field, $delta ) {
157 if ( $delta ) {
158 if ( $sql ) {
159 $sql .= ',';
160 }
161 if ( $delta < 0 ) {
162 $sql .= "$field=$field-1";
163 } else {
164 $sql .= "$field=$field+1";
165 }
166 }
167 }
168
169 function doUpdate() {
170 $fname = 'SiteStatsUpdate::doUpdate';
171 $dbw = wfGetDB( DB_MASTER );
172
173 # First retrieve the row just to find out which schema we're in
174 $row = $dbw->selectRow( 'site_stats', '*', false, $fname );
175
176 $updates = '';
177
178 $this->appendUpdate( $updates, 'ss_total_views', $this->mViews );
179 $this->appendUpdate( $updates, 'ss_total_edits', $this->mEdits );
180 $this->appendUpdate( $updates, 'ss_good_articles', $this->mGood );
181
182 if ( isset( $row->ss_total_pages ) ) {
183 # Update schema if required
184 if ( $row->ss_total_pages == -1 && !$this->mViews ) {
185 $dbr = wfGetDB( DB_SLAVE, array( 'SpecialStatistics', 'vslow') );
186 list( $page, $user ) = $dbr->tableNamesN( 'page', 'user' );
187
188 $sql = "SELECT COUNT(page_namespace) AS total FROM $page";
189 $res = $dbr->query( $sql, $fname );
190 $pageRow = $dbr->fetchObject( $res );
191 $pages = $pageRow->total + $this->mPages;
192
193 $sql = "SELECT COUNT(user_id) AS total FROM $user";
194 $res = $dbr->query( $sql, $fname );
195 $userRow = $dbr->fetchObject( $res );
196 $users = $userRow->total + $this->mUsers;
197
198 if ( $updates ) {
199 $updates .= ',';
200 }
201 $updates .= "ss_total_pages=$pages, ss_users=$users";
202 } else {
203 $this->appendUpdate( $updates, 'ss_total_pages', $this->mPages );
204 $this->appendUpdate( $updates, 'ss_users', $this->mUsers );
205 }
206 }
207 if ( $updates ) {
208 $site_stats = $dbw->tableName( 'site_stats' );
209 $sql = $dbw->limitResultForUpdate("UPDATE $site_stats SET $updates", 1);
210 $dbw->begin();
211 $dbw->query( $sql, $fname );
212 $dbw->commit();
213 }
214
215 /*
216 global $wgDBname, $wgTitle;
217 if ( $this->mGood && $wgDBname == 'enwiki' ) {
218 $good = $dbw->selectField( 'site_stats', 'ss_good_articles', '', $fname );
219 error_log( $good . ' ' . $wgTitle->getPrefixedDBkey() . "\n", 3, '/home/wikipedia/logs/million.log' );
220 }
221 */
222 }
223 }
224