* Revert revert r39662 of my parser changes.
[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, $jobs;
9 static $pageCount = array();
10 static $groupMemberCounts = array();
11
12 static function recache() {
13 self::load( true );
14 }
15
16 static function load( $recache = false ) {
17 if ( self::$loaded && !$recache ) {
18 return;
19 }
20
21 self::$row = self::loadAndLazyInit();
22
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 ) {
25 # Update schema
26 $u = new SiteStatsUpdate( 0, 0, 0 );
27 $u->doUpdate();
28 $dbr = wfGetDB( DB_SLAVE );
29 self::$row = $dbr->selectRow( 'site_stats', '*', false, __METHOD__ );
30 }
31
32 self::$loaded = true;
33 }
34
35 static function loadAndLazyInit() {
36 wfDebug( __METHOD__ . ": reading site_stats from slave\n" );
37 $row = self::doLoad( wfGetDB( DB_SLAVE ) );
38
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 ) );
43 }
44
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" );
51
52 global $IP;
53 require_once "$IP/maintenance/initStats.inc";
54
55 ob_start();
56 wfInitStats();
57 ob_end_clean();
58
59 $row = self::doLoad( wfGetDB( DB_MASTER ) );
60 }
61
62 if( !self::isSane( $row ) ) {
63 wfDebug( __METHOD__ . ": site_stats persistently nonsensical o_O\n" );
64 }
65 return $row;
66 }
67
68 static function doLoad( $db ) {
69 return $db->selectRow( 'site_stats', '*', false, __METHOD__ );
70 }
71
72 static function views() {
73 self::load();
74 return self::$row->ss_total_views;
75 }
76
77 static function edits() {
78 self::load();
79 return self::$row->ss_total_edits;
80 }
81
82 static function articles() {
83 self::load();
84 return self::$row->ss_good_articles;
85 }
86
87 static function pages() {
88 self::load();
89 return self::$row->ss_total_pages;
90 }
91
92 static function users() {
93 self::load();
94 return self::$row->ss_users;
95 }
96
97 static function images() {
98 self::load();
99 return self::$row->ss_images;
100 }
101
102 /**
103 * @deprecated Use self::numberingroup('sysop') instead
104 */
105 static function admins() {
106 wfDeprecated(__METHOD__);
107 return self::numberingroup('sysop');
108 }
109
110 /**
111 * Find the number of users in a given user group.
112 * @param string $group Name of group
113 * @return int
114 */
115 static function numberingroup($group) {
116 if ( !isset( self::$groupMemberCounts[$group] ) ) {
117 $dbr = wfGetDB( DB_SLAVE );
118 self::$groupMemberCounts[$group] = $dbr->selectField( 'user_groups', 'COUNT(*)',
119 array( 'ug_group' => $group ), __METHOD__ );
120 }
121 return self::$groupMemberCounts[$group];
122 }
123
124 static function jobs() {
125 if ( !isset( self::$jobs ) ) {
126 $dbr = wfGetDB( DB_SLAVE );
127 self::$jobs = $dbr->estimateRowCount('job');
128 /* Zero rows still do single row read for row that doesn't exist, but people are annoyed by that */
129 if (self::$jobs == 1) {
130 self::$jobs = 0;
131 }
132 }
133 return self::$jobs;
134 }
135
136 static function pagesInNs( $ns ) {
137 wfProfileIn( __METHOD__ );
138 if( !isset( self::$pageCount[$ns] ) ) {
139 $dbr = wfGetDB( DB_SLAVE );
140 $pageCount[$ns] = (int)$dbr->selectField( 'page', 'COUNT(*)', array( 'page_namespace' => $ns ), __METHOD__ );
141 }
142 wfProfileOut( __METHOD__ );
143 return $pageCount[$ns];
144 }
145
146 /** Is the provided row of site stats sane, or should it be regenerated? */
147 private static function isSane( $row ) {
148 if(
149 $row === false
150 or $row->ss_total_pages < $row->ss_good_articles
151 or $row->ss_total_edits < $row->ss_total_pages
152 or $row->ss_users < $row->ss_admins
153 ) {
154 return false;
155 }
156 // Now check for underflow/overflow
157 foreach( array( 'total_views', 'total_edits', 'good_articles',
158 'total_pages', 'users', 'admins', 'images' ) as $member ) {
159 if(
160 $row->{"ss_$member"} > 2000000000
161 or $row->{"ss_$member"} < 0
162 ) {
163 return false;
164 }
165 }
166 return true;
167 }
168 }
169
170
171 /**
172 *
173 */
174 class SiteStatsUpdate {
175
176 var $mViews, $mEdits, $mGood, $mPages, $mUsers;
177
178 function __construct( $views, $edits, $good, $pages = 0, $users = 0 ) {
179 $this->mViews = $views;
180 $this->mEdits = $edits;
181 $this->mGood = $good;
182 $this->mPages = $pages;
183 $this->mUsers = $users;
184 }
185
186 function appendUpdate( &$sql, $field, $delta ) {
187 if ( $delta ) {
188 if ( $sql ) {
189 $sql .= ',';
190 }
191 if ( $delta < 0 ) {
192 $sql .= "$field=$field-1";
193 } else {
194 $sql .= "$field=$field+1";
195 }
196 }
197 }
198
199 function doUpdate() {
200 $fname = 'SiteStatsUpdate::doUpdate';
201 $dbw = wfGetDB( DB_MASTER );
202
203 # First retrieve the row just to find out which schema we're in
204 $row = $dbw->selectRow( 'site_stats', '*', false, $fname );
205
206 $updates = '';
207
208 $this->appendUpdate( $updates, 'ss_total_views', $this->mViews );
209 $this->appendUpdate( $updates, 'ss_total_edits', $this->mEdits );
210 $this->appendUpdate( $updates, 'ss_good_articles', $this->mGood );
211
212 if ( isset( $row->ss_total_pages ) ) {
213 # Update schema if required
214 if ( $row->ss_total_pages == -1 && !$this->mViews ) {
215 $dbr = wfGetDB( DB_SLAVE, array( 'SpecialStatistics', 'vslow') );
216 list( $page, $user ) = $dbr->tableNamesN( 'page', 'user' );
217
218 $sql = "SELECT COUNT(page_namespace) AS total FROM $page";
219 $res = $dbr->query( $sql, $fname );
220 $pageRow = $dbr->fetchObject( $res );
221 $pages = $pageRow->total + $this->mPages;
222
223 $sql = "SELECT COUNT(user_id) AS total FROM $user";
224 $res = $dbr->query( $sql, $fname );
225 $userRow = $dbr->fetchObject( $res );
226 $users = $userRow->total + $this->mUsers;
227
228 if ( $updates ) {
229 $updates .= ',';
230 }
231 $updates .= "ss_total_pages=$pages, ss_users=$users";
232 } else {
233 $this->appendUpdate( $updates, 'ss_total_pages', $this->mPages );
234 $this->appendUpdate( $updates, 'ss_users', $this->mUsers );
235 }
236 }
237 if ( $updates ) {
238 $site_stats = $dbw->tableName( 'site_stats' );
239 $sql = $dbw->limitResultForUpdate("UPDATE $site_stats SET $updates", 1);
240 $dbw->begin();
241 $dbw->query( $sql, $fname );
242 $dbw->commit();
243 }
244
245 /*
246 global $wgDBname, $wgTitle;
247 if ( $this->mGood && $wgDBname == 'enwiki' ) {
248 $good = $dbw->selectField( 'site_stats', 'ss_good_articles', '', $fname );
249 error_log( $good . ' ' . $wgTitle->getPrefixedDBkey() . "\n", 3, '/home/wikipedia/logs/million.log' );
250 }
251 */
252 }
253 }