Merge "Fix ParserOutput::getText 'unwrap' flag for end-of-doc comment"
[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 use Wikimedia\Rdbms\Database;
24 use Wikimedia\Rdbms\IDatabase;
25 use MediaWiki\MediaWikiServices;
26
27 /**
28 * Static accessor class for site_stats and related things
29 */
30 class SiteStats {
31 /** @var bool|stdClass */
32 private static $row;
33
34 /** @var bool */
35 private static $loaded = false;
36 /** @var int[] */
37 private static $pageCount = [];
38
39 static function unload() {
40 self::$loaded = false;
41 }
42
43 static function recache() {
44 self::load( true );
45 }
46
47 /**
48 * @param bool $recache
49 */
50 static function load( $recache = false ) {
51 if ( self::$loaded && !$recache ) {
52 return;
53 }
54
55 self::$row = self::loadAndLazyInit();
56
57 self::$loaded = true;
58 }
59
60 /**
61 * @return bool|stdClass
62 */
63 static function loadAndLazyInit() {
64 global $wgMiserMode;
65
66 wfDebug( __METHOD__ . ": reading site_stats from replica DB\n" );
67 $row = self::doLoad( wfGetDB( DB_REPLICA ) );
68
69 if ( !self::isSane( $row ) ) {
70 $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
71 if ( $lb->hasOrMadeRecentMasterChanges() ) {
72 // Might have just been initialized during this request? Underflow?
73 wfDebug( __METHOD__ . ": site_stats damaged or missing on replica DB\n" );
74 $row = self::doLoad( wfGetDB( DB_MASTER ) );
75 }
76 }
77
78 if ( !self::isSane( $row ) ) {
79 if ( $wgMiserMode ) {
80 // Start off with all zeroes, assuming that this is a new wiki or any
81 // repopulations where done manually via script.
82 SiteStatsInit::doPlaceholderInit();
83 } else {
84 // Normally the site_stats table is initialized at install time.
85 // Some manual construction scenarios may leave the table empty or
86 // broken, however, for instance when importing from a dump into a
87 // clean schema with mwdumper.
88 wfDebug( __METHOD__ . ": initializing damaged or missing site_stats\n" );
89 SiteStatsInit::doAllAndCommit( wfGetDB( DB_REPLICA ) );
90 }
91
92 $row = self::doLoad( wfGetDB( DB_MASTER ) );
93 }
94
95 if ( !self::isSane( $row ) ) {
96 wfDebug( __METHOD__ . ": site_stats persistently nonsensical o_O\n" );
97
98 $row = (object)array_fill_keys( self::selectFields(), 0 );
99 }
100
101 return $row;
102 }
103
104 /**
105 * @param IDatabase $db
106 * @return bool|stdClass
107 */
108 static function doLoad( $db ) {
109 return $db->selectRow( 'site_stats', self::selectFields(), [], __METHOD__ );
110 }
111
112 /**
113 * Return the total number of page views. Except we don't track those anymore.
114 * Stop calling this function, it will be removed some time in the future. It's
115 * kept here simply to prevent fatal errors.
116 *
117 * @deprecated since 1.25
118 * @return int
119 */
120 static function views() {
121 wfDeprecated( __METHOD__, '1.25' );
122 return 0;
123 }
124
125 /**
126 * @return int
127 */
128 static function edits() {
129 self::load();
130 return self::$row->ss_total_edits;
131 }
132
133 /**
134 * @return int
135 */
136 static function articles() {
137 self::load();
138 return self::$row->ss_good_articles;
139 }
140
141 /**
142 * @return int
143 */
144 static function pages() {
145 self::load();
146 return self::$row->ss_total_pages;
147 }
148
149 /**
150 * @return int
151 */
152 static function users() {
153 self::load();
154 return self::$row->ss_users;
155 }
156
157 /**
158 * @return int
159 */
160 static function activeUsers() {
161 self::load();
162 return self::$row->ss_active_users;
163 }
164
165 /**
166 * @return int
167 */
168 static function images() {
169 self::load();
170 return self::$row->ss_images;
171 }
172
173 /**
174 * Find the number of users in a given user group.
175 * @param string $group Name of group
176 * @return int
177 */
178 static function numberingroup( $group ) {
179 $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
180 return $cache->getWithSetCallback(
181 $cache->makeKey( 'SiteStats', 'groupcounts', $group ),
182 $cache::TTL_HOUR,
183 function ( $oldValue, &$ttl, array &$setOpts ) use ( $group ) {
184 $dbr = wfGetDB( DB_REPLICA );
185
186 $setOpts += Database::getCacheSetOptions( $dbr );
187
188 return $dbr->selectField(
189 'user_groups',
190 'COUNT(*)',
191 [
192 'ug_group' => $group,
193 'ug_expiry IS NULL OR ug_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() )
194 ],
195 __METHOD__
196 );
197 },
198 [ 'pcTTL' => $cache::TTL_PROC_LONG ]
199 );
200 }
201
202 /**
203 * Total number of jobs in the job queue.
204 * @return int
205 */
206 static function jobs() {
207 $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
208 return $cache->getWithSetCallback(
209 $cache->makeKey( 'SiteStats', 'jobscount' ),
210 $cache::TTL_MINUTE,
211 function ( $oldValue, &$ttl, array &$setOpts ) {
212 try{
213 $jobs = array_sum( JobQueueGroup::singleton()->getQueueSizes() );
214 } catch ( JobQueueError $e ) {
215 $jobs = 0;
216 }
217 return $jobs;
218 },
219 [ 'pcTTL' => $cache::TTL_PROC_LONG ]
220 );
221 }
222
223 /**
224 * @param int $ns
225 *
226 * @return int
227 */
228 static function pagesInNs( $ns ) {
229 if ( !isset( self::$pageCount[$ns] ) ) {
230 $dbr = wfGetDB( DB_REPLICA );
231 self::$pageCount[$ns] = (int)$dbr->selectField(
232 'page',
233 'COUNT(*)',
234 [ 'page_namespace' => $ns ],
235 __METHOD__
236 );
237 }
238 return self::$pageCount[$ns];
239 }
240
241 /**
242 * @return array
243 */
244 public static function selectFields() {
245 return [
246 'ss_row_id',
247 'ss_total_edits',
248 'ss_good_articles',
249 'ss_total_pages',
250 'ss_users',
251 'ss_active_users',
252 'ss_images',
253 ];
254 }
255
256 /**
257 * Is the provided row of site stats sane, or should it be regenerated?
258 *
259 * Checks only fields which are filled by SiteStatsInit::refresh.
260 *
261 * @param bool|object $row
262 *
263 * @return bool
264 */
265 private static function isSane( $row ) {
266 if ( $row === false
267 || $row->ss_total_pages < $row->ss_good_articles
268 || $row->ss_total_edits < $row->ss_total_pages
269 ) {
270 return false;
271 }
272 // Now check for underflow/overflow
273 foreach ( [
274 'ss_total_edits',
275 'ss_good_articles',
276 'ss_total_pages',
277 'ss_users',
278 'ss_images',
279 ] as $member ) {
280 if ( $row->$member > 2000000000 || $row->$member < 0 ) {
281 return false;
282 }
283 }
284 return true;
285 }
286 }
287
288 /**
289 * Class designed for counting of stats.
290 */
291 class SiteStatsInit {
292
293 // Database connection
294 private $db;
295
296 // Various stats
297 private $mEdits = null, $mArticles = null, $mPages = null;
298 private $mUsers = null, $mFiles = null;
299
300 /**
301 * @param bool|IDatabase $database
302 * - bool: Whether to use the master DB
303 * - IDatabase: Database connection to use
304 */
305 public function __construct( $database = false ) {
306 if ( $database instanceof IDatabase ) {
307 $this->db = $database;
308 } elseif ( $database ) {
309 $this->db = wfGetDB( DB_MASTER );
310 } else {
311 $this->db = wfGetDB( DB_REPLICA, 'vslow' );
312 }
313 }
314
315 /**
316 * Count the total number of edits
317 * @return int
318 */
319 public function edits() {
320 $this->mEdits = $this->db->selectField( 'revision', 'COUNT(*)', '', __METHOD__ );
321 $this->mEdits += $this->db->selectField( 'archive', 'COUNT(*)', '', __METHOD__ );
322 return $this->mEdits;
323 }
324
325 /**
326 * Count pages in article space(s)
327 * @return int
328 */
329 public function articles() {
330 global $wgArticleCountMethod;
331
332 $tables = [ 'page' ];
333 $conds = [
334 'page_namespace' => MWNamespace::getContentNamespaces(),
335 'page_is_redirect' => 0,
336 ];
337
338 if ( $wgArticleCountMethod == 'link' ) {
339 $tables[] = 'pagelinks';
340 $conds[] = 'pl_from=page_id';
341 } elseif ( $wgArticleCountMethod == 'comma' ) {
342 // To make a correct check for this, we would need, for each page,
343 // to load the text, maybe uncompress it, maybe decode it and then
344 // check if there's one comma.
345 // But one thing we are sure is that if the page is empty, it can't
346 // contain a comma :)
347 $conds[] = 'page_len > 0';
348 }
349
350 $this->mArticles = $this->db->selectField( $tables, 'COUNT(DISTINCT page_id)',
351 $conds, __METHOD__ );
352 return $this->mArticles;
353 }
354
355 /**
356 * Count total pages
357 * @return int
358 */
359 public function pages() {
360 $this->mPages = $this->db->selectField( 'page', 'COUNT(*)', '', __METHOD__ );
361 return $this->mPages;
362 }
363
364 /**
365 * Count total users
366 * @return int
367 */
368 public function users() {
369 $this->mUsers = $this->db->selectField( 'user', 'COUNT(*)', '', __METHOD__ );
370 return $this->mUsers;
371 }
372
373 /**
374 * Count total files
375 * @return int
376 */
377 public function files() {
378 $this->mFiles = $this->db->selectField( 'image', 'COUNT(*)', '', __METHOD__ );
379 return $this->mFiles;
380 }
381
382 /**
383 * Do all updates and commit them. More or less a replacement
384 * for the original initStats, but without output.
385 *
386 * @param IDatabase|bool $database
387 * - bool: Whether to use the master DB
388 * - IDatabase: Database connection to use
389 * @param array $options Array of options, may contain the following values
390 * - activeUsers bool: Whether to update the number of active users (default: false)
391 */
392 public static function doAllAndCommit( $database, array $options = [] ) {
393 $options += [ 'update' => false, 'activeUsers' => false ];
394
395 // Grab the object and count everything
396 $counter = new SiteStatsInit( $database );
397
398 $counter->edits();
399 $counter->articles();
400 $counter->pages();
401 $counter->users();
402 $counter->files();
403
404 $counter->refresh();
405
406 // Count active users if need be
407 if ( $options['activeUsers'] ) {
408 SiteStatsUpdate::cacheUpdate( wfGetDB( DB_MASTER ) );
409 }
410 }
411
412 /**
413 * Insert a dummy row with all zeroes if no row is present
414 */
415 public static function doPlaceholderInit() {
416 $dbw = wfGetDB( DB_MASTER );
417 if ( $dbw->selectRow( 'site_stats', '1', [], __METHOD__ ) === false ) {
418 $dbw->insert(
419 'site_stats',
420 array_fill_keys( SiteStats::selectFields(), 0 ),
421 __METHOD__,
422 [ 'IGNORE' ]
423 );
424 }
425 }
426
427 /**
428 * Refresh site_stats
429 */
430 public function refresh() {
431 $values = [
432 'ss_row_id' => 1,
433 'ss_total_edits' => ( $this->mEdits === null ? $this->edits() : $this->mEdits ),
434 'ss_good_articles' => ( $this->mArticles === null ? $this->articles() : $this->mArticles ),
435 'ss_total_pages' => ( $this->mPages === null ? $this->pages() : $this->mPages ),
436 'ss_users' => ( $this->mUsers === null ? $this->users() : $this->mUsers ),
437 'ss_images' => ( $this->mFiles === null ? $this->files() : $this->mFiles ),
438 ];
439
440 $dbw = wfGetDB( DB_MASTER );
441 $dbw->upsert( 'site_stats', $values, [ 'ss_row_id' ], $values, __METHOD__ );
442 }
443 }