Merge maintenance-work branch:
[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 ob_start();
53 self::init( false );
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 activeUsers() {
95 self::load();
96 return self::$row->ss_active_users;
97 }
98
99 static function images() {
100 self::load();
101 return self::$row->ss_images;
102 }
103
104 /**
105 * @deprecated Use self::numberingroup('sysop') instead
106 */
107 static function admins() {
108 wfDeprecated(__METHOD__);
109 return self::numberingroup('sysop');
110 }
111
112 /**
113 * Find the number of users in a given user group.
114 * @param string $group Name of group
115 * @return int
116 */
117 static function numberingroup($group) {
118 if ( !isset( self::$groupMemberCounts[$group] ) ) {
119 global $wgMemc;
120 $key = wfMemcKey( 'SiteStats', 'groupcounts', $group );
121 $hit = $wgMemc->get( $key );
122 if ( !$hit ) {
123 $dbr = wfGetDB( DB_SLAVE );
124 $hit = $dbr->selectField( 'user_groups', 'COUNT(*)',
125 array( 'ug_group' => $group ), __METHOD__ );
126 $wgMemc->set( $key, $hit, 3600 );
127 }
128 self::$groupMemberCounts[$group] = $hit;
129 }
130 return self::$groupMemberCounts[$group];
131 }
132
133 static function jobs() {
134 if ( !isset( self::$jobs ) ) {
135 $dbr = wfGetDB( DB_SLAVE );
136 self::$jobs = $dbr->estimateRowCount('job');
137 /* Zero rows still do single row read for row that doesn't exist, but people are annoyed by that */
138 if (self::$jobs == 1) {
139 self::$jobs = 0;
140 }
141 }
142 return self::$jobs;
143 }
144
145 static function pagesInNs( $ns ) {
146 wfProfileIn( __METHOD__ );
147 if( !isset( self::$pageCount[$ns] ) ) {
148 $dbr = wfGetDB( DB_SLAVE );
149 $pageCount[$ns] = (int)$dbr->selectField( 'page', 'COUNT(*)', array( 'page_namespace' => $ns ), __METHOD__ );
150 }
151 wfProfileOut( __METHOD__ );
152 return $pageCount[$ns];
153 }
154
155 /** Is the provided row of site stats sane, or should it be regenerated? */
156 private static function isSane( $row ) {
157 if(
158 $row === false
159 or $row->ss_total_pages < $row->ss_good_articles
160 or $row->ss_total_edits < $row->ss_total_pages
161 or $row->ss_users < $row->ss_admins
162 ) {
163 return false;
164 }
165 // Now check for underflow/overflow
166 foreach( array( 'total_views', 'total_edits', 'good_articles',
167 'total_pages', 'users', 'admins', 'images' ) as $member ) {
168 if(
169 $row->{"ss_$member"} > 2000000000
170 or $row->{"ss_$member"} < 0
171 ) {
172 return false;
173 }
174 }
175 return true;
176 }
177
178 /**
179 * Ported from initStats.inc.
180 * @param $update bool Whether to update the current stats write fresh
181 * @param $noViews bool When true, do not update the number of page views
182 */
183 function init( $update, $noViews = false ) {
184 $dbr = wfGetDB( DB_SLAVE );
185
186 wfOut( "Counting total edits..." );
187 $edits = $dbr->selectField( 'revision', 'COUNT(*)', '', __METHOD__ );
188 $edits += $dbr->selectField( 'archive', 'COUNT(*)', '', __METHOD__ );
189 wfOut( "{$edits}\nCounting number of articles..." );
190
191 global $wgContentNamespaces;
192 $good = $dbr->selectField( 'page', 'COUNT(*)', array( 'page_namespace' => $wgContentNamespaces, 'page_is_redirect' => 0, 'page_len > 0' ), __METHOD__ );
193 wfOut( "{$good}\nCounting total pages..." );
194
195 $pages = $dbr->selectField( 'page', 'COUNT(*)', '', __METHOD__ );
196 wfOut( "{$pages}\nCounting number of users..." );
197
198 $users = $dbr->selectField( 'user', 'COUNT(*)', '', __METHOD__ );
199 wfOut( "{$users}\nCounting number of admins..." );
200
201 $admin = $dbr->selectField( 'user_groups', 'COUNT(*)', array( 'ug_group' => 'sysop' ), __METHOD__ );
202 wfOut( "{$admin}\nCounting number of images..." );
203
204 $image = $dbr->selectField( 'image', 'COUNT(*)', '', __METHOD__ );
205 wfOut( "{$image}\n" );
206
207 if( !$noViews ) {
208 wfOut( "Counting total page views..." );
209 $views = $dbr->selectField( 'page', 'SUM(page_counter)', '', __METHOD__ );
210 wfOut( "{$views}\n" );
211 }
212
213 wfOut( "\nUpdating site statistics..." );
214
215 $dbw = wfGetDB( DB_MASTER );
216 $values = array( 'ss_total_edits' => $edits,
217 'ss_good_articles' => $good,
218 'ss_total_pages' => $pages,
219 'ss_users' => $users,
220 'ss_admins' => $admin,
221 'ss_images' => $image );
222 $conds = array( 'ss_row_id' => 1 );
223 $views = array( 'ss_total_views' => isset( $views ) ? $views : 0 );
224
225 if( $update ) {
226 $dbw->update( 'site_stats', $values, $conds, __METHOD__ );
227 } else {
228 $dbw->delete( 'site_stats', $conds, __METHOD__ );
229 $dbw->insert( 'site_stats', array_merge( $values, $conds, $views ), __METHOD__ );
230 }
231
232 wfOut( "done.\n" );
233 }
234 }
235
236
237 /**
238 *
239 */
240 class SiteStatsUpdate {
241
242 var $mViews, $mEdits, $mGood, $mPages, $mUsers;
243
244 function __construct( $views, $edits, $good, $pages = 0, $users = 0 ) {
245 $this->mViews = $views;
246 $this->mEdits = $edits;
247 $this->mGood = $good;
248 $this->mPages = $pages;
249 $this->mUsers = $users;
250 }
251
252 function appendUpdate( &$sql, $field, $delta ) {
253 if ( $delta ) {
254 if ( $sql ) {
255 $sql .= ',';
256 }
257 if ( $delta < 0 ) {
258 $sql .= "$field=$field-1";
259 } else {
260 $sql .= "$field=$field+1";
261 }
262 }
263 }
264
265 function doUpdate() {
266 $fname = 'SiteStatsUpdate::doUpdate';
267 $dbw = wfGetDB( DB_MASTER );
268
269 $updates = '';
270
271 $this->appendUpdate( $updates, 'ss_total_views', $this->mViews );
272 $this->appendUpdate( $updates, 'ss_total_edits', $this->mEdits );
273 $this->appendUpdate( $updates, 'ss_good_articles', $this->mGood );
274 $this->appendUpdate( $updates, 'ss_total_pages', $this->mPages );
275 $this->appendUpdate( $updates, 'ss_users', $this->mUsers );
276
277 if ( $updates ) {
278 $site_stats = $dbw->tableName( 'site_stats' );
279 $sql = "UPDATE $site_stats SET $updates";
280
281 # Need a separate transaction because this a global lock
282 $dbw->begin();
283 $dbw->query( $sql, $fname );
284 $dbw->commit();
285 }
286 }
287
288 public static function cacheUpdate( $dbw ) {
289 $dbr = wfGetDB( DB_SLAVE, array( 'SpecialStatistics', 'vslow') );
290 # Get non-bot users than did some recent action other than making accounts.
291 # If account creation is included, the number gets inflated ~20+ fold on enwiki.
292 $activeUsers = $dbr->selectField( 'recentchanges', 'COUNT( DISTINCT rc_user_text )',
293 array( 'rc_user != 0', 'rc_bot' => 0, "rc_log_type != 'newusers' OR rc_log_type IS NULL" ),
294 __METHOD__ );
295 $dbw->update( 'site_stats',
296 array( 'ss_active_users' => intval($activeUsers) ),
297 array( 'ss_row_id' => 1 ), __METHOD__
298 );
299 }
300 }