Merge "Revert "Hide HHVM tag on Special:{Contributions,RecentChanges,...}""
[lhc/web/wiklou.git] / includes / SiteStats.php
1 <?php
2 /**
3 * Accessors and mutators for the site-wide statistics.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * Static accessor class for site_stats and related things
25 */
26 class SiteStats {
27 /** @var bool|ResultWrapper */
28 private static $row;
29
30 /** @var bool */
31 private static $loaded = false;
32
33 /** @var int */
34 private static $jobs;
35
36 /** @var int[] */
37 private static $pageCount = array();
38
39 /** @var int[] */
40 private static $groupMemberCounts = array();
41
42 static function recache() {
43 self::load( true );
44 }
45
46 /**
47 * @param bool $recache
48 */
49 static function load( $recache = false ) {
50 if ( self::$loaded && !$recache ) {
51 return;
52 }
53
54 self::$row = self::loadAndLazyInit();
55
56 # This code is somewhat schema-agnostic, because I'm changing it in a minor release -- TS
57 if ( !isset( self::$row->ss_total_pages ) && self::$row->ss_total_pages == -1 ) {
58 # Update schema
59 $u = new SiteStatsUpdate( 0, 0, 0 );
60 $u->doUpdate();
61 self::$row = self::doLoad( wfGetDB( DB_SLAVE ) );
62 }
63
64 self::$loaded = true;
65 }
66
67 /**
68 * @return bool|ResultWrapper
69 */
70 static function loadAndLazyInit() {
71 wfDebug( __METHOD__ . ": reading site_stats from slave\n" );
72 $row = self::doLoad( wfGetDB( DB_SLAVE ) );
73
74 if ( !self::isSane( $row ) ) {
75 // Might have just been initialized during this request? Underflow?
76 wfDebug( __METHOD__ . ": site_stats damaged or missing on slave\n" );
77 $row = self::doLoad( wfGetDB( DB_MASTER ) );
78 }
79
80 if ( !self::isSane( $row ) ) {
81 // Normally the site_stats table is initialized at install time.
82 // Some manual construction scenarios may leave the table empty or
83 // broken, however, for instance when importing from a dump into a
84 // clean schema with mwdumper.
85 wfDebug( __METHOD__ . ": initializing damaged or missing site_stats\n" );
86
87 SiteStatsInit::doAllAndCommit( wfGetDB( DB_SLAVE ) );
88
89 $row = self::doLoad( wfGetDB( DB_MASTER ) );
90 }
91
92 if ( !self::isSane( $row ) ) {
93 wfDebug( __METHOD__ . ": site_stats persistently nonsensical o_O\n" );
94 }
95 return $row;
96 }
97
98 /**
99 * @param DatabaseBase $db
100 * @return bool|ResultWrapper
101 */
102 static function doLoad( $db ) {
103 return $db->selectRow( 'site_stats', array(
104 'ss_row_id',
105 'ss_total_edits',
106 'ss_good_articles',
107 'ss_total_pages',
108 'ss_users',
109 'ss_active_users',
110 'ss_images',
111 ), false, __METHOD__ );
112 }
113
114 /**
115 * Return the total number of page views. Except we don't track those anymore.
116 * Stop calling this function, it will be removed some time in the future. It's
117 * kept here simply to prevent fatal errors.
118 *
119 * @deprecated since 1.25
120 * @return int
121 */
122 static function views() {
123 wfDeprecated( __METHOD__, '1.25' );
124 return 0;
125 }
126
127 /**
128 * @return int
129 */
130 static function edits() {
131 self::load();
132 return self::$row->ss_total_edits;
133 }
134
135 /**
136 * @return int
137 */
138 static function articles() {
139 self::load();
140 return self::$row->ss_good_articles;
141 }
142
143 /**
144 * @return int
145 */
146 static function pages() {
147 self::load();
148 return self::$row->ss_total_pages;
149 }
150
151 /**
152 * @return int
153 */
154 static function users() {
155 self::load();
156 return self::$row->ss_users;
157 }
158
159 /**
160 * @return int
161 */
162 static function activeUsers() {
163 self::load();
164 return self::$row->ss_active_users;
165 }
166
167 /**
168 * @return int
169 */
170 static function images() {
171 self::load();
172 return self::$row->ss_images;
173 }
174
175 /**
176 * Find the number of users in a given user group.
177 * @param string $group Name of group
178 * @return int
179 */
180 static function numberingroup( $group ) {
181 if ( !isset( self::$groupMemberCounts[$group] ) ) {
182 global $wgMemc;
183 $key = wfMemcKey( 'SiteStats', 'groupcounts', $group );
184 $hit = $wgMemc->get( $key );
185 if ( !$hit ) {
186 $dbr = wfGetDB( DB_SLAVE );
187 $hit = $dbr->selectField(
188 'user_groups',
189 'COUNT(*)',
190 array( 'ug_group' => $group ),
191 __METHOD__
192 );
193 $wgMemc->set( $key, $hit, 3600 );
194 }
195 self::$groupMemberCounts[$group] = $hit;
196 }
197 return self::$groupMemberCounts[$group];
198 }
199
200 /**
201 * @return int
202 */
203 static function jobs() {
204 if ( !isset( self::$jobs ) ) {
205 $dbr = wfGetDB( DB_SLAVE );
206 self::$jobs = array_sum( JobQueueGroup::singleton()->getQueueSizes() );
207 /**
208 * Zero rows still do single row read for row that doesn't exist,
209 * but people are annoyed by that
210 */
211 if ( self::$jobs == 1 ) {
212 self::$jobs = 0;
213 }
214 }
215 return self::$jobs;
216 }
217
218 /**
219 * @param int $ns
220 *
221 * @return int
222 */
223 static function pagesInNs( $ns ) {
224 if ( !isset( self::$pageCount[$ns] ) ) {
225 $dbr = wfGetDB( DB_SLAVE );
226 self::$pageCount[$ns] = (int)$dbr->selectField(
227 'page',
228 'COUNT(*)',
229 array( 'page_namespace' => $ns ),
230 __METHOD__
231 );
232 }
233 return self::$pageCount[$ns];
234 }
235
236 /**
237 * Is the provided row of site stats sane, or should it be regenerated?
238 *
239 * Checks only fields which are filled by SiteStatsInit::refresh.
240 *
241 * @param bool|object $row
242 *
243 * @return bool
244 */
245 private static function isSane( $row ) {
246 if ( $row === false
247 || $row->ss_total_pages < $row->ss_good_articles
248 || $row->ss_total_edits < $row->ss_total_pages
249 ) {
250 return false;
251 }
252 // Now check for underflow/overflow
253 foreach ( array(
254 'ss_total_edits',
255 'ss_good_articles',
256 'ss_total_pages',
257 'ss_users',
258 'ss_images',
259 ) as $member ) {
260 if ( $row->$member > 2000000000 || $row->$member < 0 ) {
261 return false;
262 }
263 }
264 return true;
265 }
266 }
267
268 /**
269 * Class designed for counting of stats.
270 */
271 class SiteStatsInit {
272
273 // Database connection
274 private $db;
275
276 // Various stats
277 private $mEdits = null, $mArticles = null, $mPages = null;
278 private $mUsers = null, $mFiles = null;
279
280 /**
281 * Constructor
282 * @param bool|DatabaseBase $database
283 * - Boolean: whether to use the master DB
284 * - DatabaseBase: database connection to use
285 */
286 public function __construct( $database = false ) {
287 if ( $database instanceof DatabaseBase ) {
288 $this->db = $database;
289 } else {
290 $this->db = wfGetDB( $database ? DB_MASTER : DB_SLAVE );
291 }
292 }
293
294 /**
295 * Count the total number of edits
296 * @return int
297 */
298 public function edits() {
299 $this->mEdits = $this->db->selectField( 'revision', 'COUNT(*)', '', __METHOD__ );
300 $this->mEdits += $this->db->selectField( 'archive', 'COUNT(*)', '', __METHOD__ );
301 return $this->mEdits;
302 }
303
304 /**
305 * Count pages in article space(s)
306 * @return int
307 */
308 public function articles() {
309 global $wgArticleCountMethod;
310
311 $tables = array( 'page' );
312 $conds = array(
313 'page_namespace' => MWNamespace::getContentNamespaces(),
314 'page_is_redirect' => 0,
315 );
316
317 if ( $wgArticleCountMethod == 'link' ) {
318 $tables[] = 'pagelinks';
319 $conds[] = 'pl_from=page_id';
320 } elseif ( $wgArticleCountMethod == 'comma' ) {
321 // To make a correct check for this, we would need, for each page,
322 // to load the text, maybe uncompress it, maybe decode it and then
323 // check if there's one comma.
324 // But one thing we are sure is that if the page is empty, it can't
325 // contain a comma :)
326 $conds[] = 'page_len > 0';
327 }
328
329 $this->mArticles = $this->db->selectField( $tables, 'COUNT(DISTINCT page_id)',
330 $conds, __METHOD__ );
331 return $this->mArticles;
332 }
333
334 /**
335 * Count total pages
336 * @return int
337 */
338 public function pages() {
339 $this->mPages = $this->db->selectField( 'page', 'COUNT(*)', '', __METHOD__ );
340 return $this->mPages;
341 }
342
343 /**
344 * Count total users
345 * @return int
346 */
347 public function users() {
348 $this->mUsers = $this->db->selectField( 'user', 'COUNT(*)', '', __METHOD__ );
349 return $this->mUsers;
350 }
351
352 /**
353 * Count total files
354 * @return int
355 */
356 public function files() {
357 $this->mFiles = $this->db->selectField( 'image', 'COUNT(*)', '', __METHOD__ );
358 return $this->mFiles;
359 }
360
361 /**
362 * Do all updates and commit them. More or less a replacement
363 * for the original initStats, but without output.
364 *
365 * @param DatabaseBase|bool $database
366 * - Boolean: whether to use the master DB
367 * - DatabaseBase: database connection to use
368 * @param array $options Array of options, may contain the following values
369 * - activeUsers Boolean: whether to update the number of active users (default: false)
370 */
371 public static function doAllAndCommit( $database, array $options = array() ) {
372 $options += array( 'update' => false, 'activeUsers' => false );
373
374 // Grab the object and count everything
375 $counter = new SiteStatsInit( $database );
376
377 $counter->edits();
378 $counter->articles();
379 $counter->pages();
380 $counter->users();
381 $counter->files();
382
383 $counter->refresh();
384
385 // Count active users if need be
386 if ( $options['activeUsers'] ) {
387 SiteStatsUpdate::cacheUpdate( wfGetDB( DB_MASTER ) );
388 }
389 }
390
391 /**
392 * Refresh site_stats
393 */
394 public function refresh() {
395 $values = array(
396 'ss_row_id' => 1,
397 'ss_total_edits' => ( $this->mEdits === null ? $this->edits() : $this->mEdits ),
398 'ss_good_articles' => ( $this->mArticles === null ? $this->articles() : $this->mArticles ),
399 'ss_total_pages' => ( $this->mPages === null ? $this->pages() : $this->mPages ),
400 'ss_users' => ( $this->mUsers === null ? $this->users() : $this->mUsers ),
401 'ss_images' => ( $this->mFiles === null ? $this->files() : $this->mFiles ),
402 );
403
404 $dbw = wfGetDB( DB_MASTER );
405 $dbw->upsert( 'site_stats', $values, array( 'ss_row_id' ), $values, __METHOD__ );
406 }
407 }