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