* (bug 24563) Entries on Special:WhatLinksHere now have a link to their history
[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 SiteStatsInit::doAllAndCommit( false );
53
54 $row = self::doLoad( wfGetDB( DB_MASTER ) );
55 }
56
57 if( !self::isSane( $row ) ) {
58 wfDebug( __METHOD__ . ": site_stats persistently nonsensical o_O\n" );
59 }
60 return $row;
61 }
62
63 static function doLoad( $db ) {
64 return $db->selectRow( 'site_stats', '*', false, __METHOD__ );
65 }
66
67 static function views() {
68 self::load();
69 return self::$row->ss_total_views;
70 }
71
72 static function edits() {
73 self::load();
74 return self::$row->ss_total_edits;
75 }
76
77 static function articles() {
78 self::load();
79 return self::$row->ss_good_articles;
80 }
81
82 static function pages() {
83 self::load();
84 return self::$row->ss_total_pages;
85 }
86
87 static function users() {
88 self::load();
89 return self::$row->ss_users;
90 }
91
92 static function activeUsers() {
93 self::load();
94 return self::$row->ss_active_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 $group String: name of group
113 * @return Integer
114 */
115 static function numberingroup( $group ) {
116 if ( !isset( self::$groupMemberCounts[$group] ) ) {
117 global $wgMemc;
118 $key = wfMemcKey( 'SiteStats', 'groupcounts', $group );
119 $hit = $wgMemc->get( $key );
120 if ( !$hit ) {
121 $dbr = wfGetDB( DB_SLAVE );
122 $hit = $dbr->selectField(
123 'user_groups',
124 'COUNT(*)',
125 array( 'ug_group' => $group ),
126 __METHOD__
127 );
128 $wgMemc->set( $key, $hit, 3600 );
129 }
130 self::$groupMemberCounts[$group] = $hit;
131 }
132 return self::$groupMemberCounts[$group];
133 }
134
135 static function jobs() {
136 if ( !isset( self::$jobs ) ) {
137 $dbr = wfGetDB( DB_SLAVE );
138 self::$jobs = $dbr->estimateRowCount( 'job' );
139 /* Zero rows still do single row read for row that doesn't exist, but people are annoyed by that */
140 if ( self::$jobs == 1 ) {
141 self::$jobs = 0;
142 }
143 }
144 return self::$jobs;
145 }
146
147 static function pagesInNs( $ns ) {
148 wfProfileIn( __METHOD__ );
149 if( !isset( self::$pageCount[$ns] ) ) {
150 $dbr = wfGetDB( DB_SLAVE );
151 $pageCount[$ns] = (int)$dbr->selectField(
152 'page',
153 'COUNT(*)',
154 array( 'page_namespace' => $ns ),
155 __METHOD__
156 );
157 }
158 wfProfileOut( __METHOD__ );
159 return $pageCount[$ns];
160 }
161
162 /** Is the provided row of site stats sane, or should it be regenerated? */
163 private static function isSane( $row ) {
164 if(
165 $row === false
166 or $row->ss_total_pages < $row->ss_good_articles
167 or $row->ss_total_edits < $row->ss_total_pages
168 ) {
169 return false;
170 }
171 // Now check for underflow/overflow
172 foreach( array( 'total_views', 'total_edits', 'good_articles',
173 'total_pages', 'users', 'admins', 'images' ) as $member ) {
174 if(
175 $row->{"ss_$member"} > 2000000000
176 or $row->{"ss_$member"} < 0
177 ) {
178 return false;
179 }
180 }
181 return true;
182 }
183 }
184
185
186 /**
187 *
188 */
189 class SiteStatsUpdate {
190
191 var $mViews, $mEdits, $mGood, $mPages, $mUsers;
192
193 function __construct( $views, $edits, $good, $pages = 0, $users = 0 ) {
194 $this->mViews = $views;
195 $this->mEdits = $edits;
196 $this->mGood = $good;
197 $this->mPages = $pages;
198 $this->mUsers = $users;
199 }
200
201 function appendUpdate( &$sql, $field, $delta ) {
202 if ( $delta ) {
203 if ( $sql ) {
204 $sql .= ',';
205 }
206 if ( $delta < 0 ) {
207 $sql .= "$field=$field-1";
208 } else {
209 $sql .= "$field=$field+1";
210 }
211 }
212 }
213
214 function doUpdate() {
215 $dbw = wfGetDB( DB_MASTER );
216
217 $updates = '';
218
219 $this->appendUpdate( $updates, 'ss_total_views', $this->mViews );
220 $this->appendUpdate( $updates, 'ss_total_edits', $this->mEdits );
221 $this->appendUpdate( $updates, 'ss_good_articles', $this->mGood );
222 $this->appendUpdate( $updates, 'ss_total_pages', $this->mPages );
223 $this->appendUpdate( $updates, 'ss_users', $this->mUsers );
224
225 if ( $updates ) {
226 $site_stats = $dbw->tableName( 'site_stats' );
227 $sql = "UPDATE $site_stats SET $updates";
228
229 # Need a separate transaction because this a global lock
230 $dbw->begin();
231 $dbw->query( $sql, __METHOD__ );
232 $dbw->commit();
233 }
234 }
235
236 public static function cacheUpdate( $dbw ) {
237 global $wgActiveUserDays;
238 $dbr = wfGetDB( DB_SLAVE, array( 'SpecialStatistics', 'vslow' ) );
239 # Get non-bot users than did some recent action other than making accounts.
240 # If account creation is included, the number gets inflated ~20+ fold on enwiki.
241 $activeUsers = $dbr->selectField(
242 'recentchanges',
243 'COUNT( DISTINCT rc_user_text )',
244 array(
245 'rc_user != 0',
246 'rc_bot' => 0,
247 "rc_log_type != 'newusers' OR rc_log_type IS NULL",
248 "rc_timestamp >= '{$dbw->timestamp( wfTimestamp( TS_UNIX ) - $wgActiveUserDays*24*3600 )}'",
249 ),
250 __METHOD__
251 );
252 $dbw->update(
253 'site_stats',
254 array( 'ss_active_users' => intval( $activeUsers ) ),
255 array( 'ss_row_id' => 1 ),
256 __METHOD__
257 );
258 return $activeUsers;
259 }
260 }
261
262 /**
263 * Class designed for counting of stats.
264 */
265 class SiteStatsInit {
266
267 // Database connection
268 private $db;
269
270 // Various stats
271 private $mEdits, $mArticles, $mPages, $mUsers, $mViews, $mFiles = 0;
272
273 /**
274 * Constructor
275 * @param $useMaster Boolean: whether to use the master DB
276 */
277 public function __construct( $useMaster = false ) {
278 $this->db = wfGetDB( $useMaster ? DB_MASTER : DB_SLAVE );
279 }
280
281 /**
282 * Count the total number of edits
283 * @return Integer
284 */
285 public function edits() {
286 $this->mEdits = $this->db->selectField( 'revision', 'COUNT(*)', '', __METHOD__ );
287 $this->mEdits += $this->db->selectField( 'archive', 'COUNT(*)', '', __METHOD__ );
288 return $this->mEdits;
289 }
290
291 /**
292 * Count pages in article space(s)
293 * @return Integer
294 */
295 public function articles() {
296 global $wgContentNamespaces;
297 $this->mArticles = $this->db->selectField(
298 'page',
299 'COUNT(*)',
300 array(
301 'page_namespace' => $wgContentNamespaces,
302 'page_is_redirect' => 0,
303 'page_len > 0'
304 ),
305 __METHOD__
306 );
307 return $this->mArticles;
308 }
309
310 /**
311 * Count total pages
312 * @return Integer
313 */
314 public function pages() {
315 $this->mPages = $this->db->selectField( 'page', 'COUNT(*)', '', __METHOD__ );
316 return $this->mPages;
317 }
318
319 /**
320 * Count total users
321 * @return Integer
322 */
323 public function users() {
324 $this->mUsers = $this->db->selectField( 'user', 'COUNT(*)', '', __METHOD__ );
325 return $this->mUsers;
326 }
327
328 /**
329 * Count views
330 * @return Integer
331 */
332 public function views() {
333 $this->mViews = $this->db->selectField( 'page', 'SUM(page_counter)', '', __METHOD__ );
334 return $this->mViews;
335 }
336
337 /**
338 * Count total files
339 * @return Integer
340 */
341 public function files() {
342 $this->mFiles = $this->db->selectField( 'image', 'COUNT(*)', '', __METHOD__ );
343 return $this->mFiles;
344 }
345
346 /**
347 * Do all updates and commit them. More or less a replacement
348 * for the original initStats, but without the calls to wfOut()
349 * @param $update Boolean: whether to update the current stats or write fresh
350 * @param $noViews Boolean: when true, do not update the number of page views
351 * @param $activeUsers Boolean: whether to update the number of active users
352 */
353 public static function doAllAndCommit( $update, $noViews = false, $activeUsers = false ) {
354 // Grab the object and count everything
355 $counter = new SiteStatsInit( false );
356 $counter->edits();
357 $counter->articles();
358 $counter->pages();
359 $counter->users();
360 $counter->files();
361
362 // Only do views if we don't want to not count them
363 if( !$noViews ) {
364 $counter->views();
365 }
366
367 // Update/refresh
368 if( $update ) {
369 $counter->update();
370 } else {
371 $counter->refresh();
372 }
373
374 // Count active users if need be
375 if( $activeUsers ) {
376 SiteStatsUpdate::cacheUpdate( wfGetDB( DB_MASTER ) );
377 }
378 }
379
380 /**
381 * Update the current row with the selected values
382 */
383 public function update() {
384 list( $values, $conds ) = $this->getDbParams();
385 $dbw = wfGetDB( DB_MASTER );
386 $dbw->update( 'site_stats', $values, $conds, __METHOD__ );
387 }
388
389 /**
390 * Refresh site_stats. Erase the current record and save all
391 * the new values.
392 */
393 public function refresh() {
394 list( $values, $conds, $views ) = $this->getDbParams();
395 $dbw = wfGetDB( DB_MASTER );
396 $dbw->delete( 'site_stats', $conds, __METHOD__ );
397 $dbw->insert( 'site_stats', array_merge( $values, $conds, $views ), __METHOD__ );
398 }
399
400 /**
401 * Return three arrays of params for the db queries
402 * @return Array
403 */
404 private function getDbParams() {
405 $values = array(
406 'ss_total_edits' => $this->mEdits,
407 'ss_good_articles' => $this->mArticles,
408 'ss_total_pages' => $this->mPages,
409 'ss_users' => $this->mUsers,
410 'ss_images' => $this->mFiles
411 );
412 $conds = array( 'ss_row_id' => 1 );
413 $views = array( 'ss_total_views' => $this->mViews );
414 return array( $values, $conds, $views );
415 }
416 }