Disregard expired user_group rows in special page and API DB queries
[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|stdClass */
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 = [];
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 # This code is somewhat schema-agnostic, because I'm changing it in a minor release -- TS
58 if ( !isset( self::$row->ss_total_pages ) && self::$row->ss_total_pages == -1 ) {
59 # Update schema
60 $u = new SiteStatsUpdate( 0, 0, 0 );
61 $u->doUpdate();
62 self::$row = self::doLoad( wfGetDB( DB_REPLICA ) );
63 }
64
65 self::$loaded = true;
66 }
67
68 /**
69 * @return bool|stdClass
70 */
71 static function loadAndLazyInit() {
72 global $wgMiserMode;
73
74 wfDebug( __METHOD__ . ": reading site_stats from replica DB\n" );
75 $row = self::doLoad( wfGetDB( DB_REPLICA ) );
76
77 if ( !self::isSane( $row ) ) {
78 // Might have just been initialized during this request? Underflow?
79 wfDebug( __METHOD__ . ": site_stats damaged or missing on replica DB\n" );
80 $row = self::doLoad( wfGetDB( DB_MASTER ) );
81 }
82
83 if ( !$wgMiserMode && !self::isSane( $row ) ) {
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
90 SiteStatsInit::doAllAndCommit( wfGetDB( DB_REPLICA ) );
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 return $row;
99 }
100
101 /**
102 * @param IDatabase $db
103 * @return bool|stdClass
104 */
105 static function doLoad( $db ) {
106 return $db->selectRow( 'site_stats', [
107 'ss_row_id',
108 'ss_total_edits',
109 'ss_good_articles',
110 'ss_total_pages',
111 'ss_users',
112 'ss_active_users',
113 'ss_images',
114 ], [], __METHOD__ );
115 }
116
117 /**
118 * Return the total number of page views. Except we don't track those anymore.
119 * Stop calling this function, it will be removed some time in the future. It's
120 * kept here simply to prevent fatal errors.
121 *
122 * @deprecated since 1.25
123 * @return int
124 */
125 static function views() {
126 wfDeprecated( __METHOD__, '1.25' );
127 return 0;
128 }
129
130 /**
131 * @return int
132 */
133 static function edits() {
134 self::load();
135 return self::$row->ss_total_edits;
136 }
137
138 /**
139 * @return int
140 */
141 static function articles() {
142 self::load();
143 return self::$row->ss_good_articles;
144 }
145
146 /**
147 * @return int
148 */
149 static function pages() {
150 self::load();
151 return self::$row->ss_total_pages;
152 }
153
154 /**
155 * @return int
156 */
157 static function users() {
158 self::load();
159 return self::$row->ss_users;
160 }
161
162 /**
163 * @return int
164 */
165 static function activeUsers() {
166 self::load();
167 return self::$row->ss_active_users;
168 }
169
170 /**
171 * @return int
172 */
173 static function images() {
174 self::load();
175 return self::$row->ss_images;
176 }
177
178 /**
179 * Find the number of users in a given user group.
180 * @param string $group Name of group
181 * @return int
182 */
183 static function numberingroup( $group ) {
184 $cache = ObjectCache::getMainWANInstance();
185 return $cache->getWithSetCallback(
186 wfMemcKey( 'SiteStats', 'groupcounts', $group ),
187 $cache::TTL_HOUR,
188 function ( $oldValue, &$ttl, array &$setOpts ) use ( $group ) {
189 $dbr = wfGetDB( DB_REPLICA );
190
191 $setOpts += Database::getCacheSetOptions( $dbr );
192
193 return $dbr->selectField(
194 'user_groups',
195 'COUNT(*)',
196 [
197 'ug_group' => $group,
198 'ug_expiry IS NULL OR ug_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() )
199 ],
200 __METHOD__
201 );
202 },
203 [ 'pcTTL' => $cache::TTL_PROC_LONG ]
204 );
205 }
206
207 /**
208 * @return int
209 */
210 static function jobs() {
211 if ( !isset( self::$jobs ) ) {
212 try{
213 self::$jobs = array_sum( JobQueueGroup::singleton()->getQueueSizes() );
214 } catch ( JobQueueError $e ) {
215 self::$jobs = 0;
216 }
217 /**
218 * Zero rows still do single row read for row that doesn't exist,
219 * but people are annoyed by that
220 */
221 if ( self::$jobs == 1 ) {
222 self::$jobs = 0;
223 }
224 }
225 return self::$jobs;
226 }
227
228 /**
229 * @param int $ns
230 *
231 * @return int
232 */
233 static function pagesInNs( $ns ) {
234 if ( !isset( self::$pageCount[$ns] ) ) {
235 $dbr = wfGetDB( DB_REPLICA );
236 self::$pageCount[$ns] = (int)$dbr->selectField(
237 'page',
238 'COUNT(*)',
239 [ 'page_namespace' => $ns ],
240 __METHOD__
241 );
242 }
243 return self::$pageCount[$ns];
244 }
245
246 /**
247 * Is the provided row of site stats sane, or should it be regenerated?
248 *
249 * Checks only fields which are filled by SiteStatsInit::refresh.
250 *
251 * @param bool|object $row
252 *
253 * @return bool
254 */
255 private static function isSane( $row ) {
256 if ( $row === false
257 || $row->ss_total_pages < $row->ss_good_articles
258 || $row->ss_total_edits < $row->ss_total_pages
259 ) {
260 return false;
261 }
262 // Now check for underflow/overflow
263 foreach ( [
264 'ss_total_edits',
265 'ss_good_articles',
266 'ss_total_pages',
267 'ss_users',
268 'ss_images',
269 ] as $member ) {
270 if ( $row->$member > 2000000000 || $row->$member < 0 ) {
271 return false;
272 }
273 }
274 return true;
275 }
276 }
277
278 /**
279 * Class designed for counting of stats.
280 */
281 class SiteStatsInit {
282
283 // Database connection
284 private $db;
285
286 // Various stats
287 private $mEdits = null, $mArticles = null, $mPages = null;
288 private $mUsers = null, $mFiles = null;
289
290 /**
291 * Constructor
292 * @param bool|IDatabase $database
293 * - boolean: Whether to use the master DB
294 * - IDatabase: Database connection to use
295 */
296 public function __construct( $database = false ) {
297 if ( $database instanceof IDatabase ) {
298 $this->db = $database;
299 } elseif ( $database ) {
300 $this->db = wfGetDB( DB_MASTER );
301 } else {
302 $this->db = wfGetDB( DB_REPLICA, 'vslow' );
303 }
304 }
305
306 /**
307 * Count the total number of edits
308 * @return int
309 */
310 public function edits() {
311 $this->mEdits = $this->db->selectField( 'revision', 'COUNT(*)', '', __METHOD__ );
312 $this->mEdits += $this->db->selectField( 'archive', 'COUNT(*)', '', __METHOD__ );
313 return $this->mEdits;
314 }
315
316 /**
317 * Count pages in article space(s)
318 * @return int
319 */
320 public function articles() {
321 global $wgArticleCountMethod;
322
323 $tables = [ 'page' ];
324 $conds = [
325 'page_namespace' => MWNamespace::getContentNamespaces(),
326 'page_is_redirect' => 0,
327 ];
328
329 if ( $wgArticleCountMethod == 'link' ) {
330 $tables[] = 'pagelinks';
331 $conds[] = 'pl_from=page_id';
332 } elseif ( $wgArticleCountMethod == 'comma' ) {
333 // To make a correct check for this, we would need, for each page,
334 // to load the text, maybe uncompress it, maybe decode it and then
335 // check if there's one comma.
336 // But one thing we are sure is that if the page is empty, it can't
337 // contain a comma :)
338 $conds[] = 'page_len > 0';
339 }
340
341 $this->mArticles = $this->db->selectField( $tables, 'COUNT(DISTINCT page_id)',
342 $conds, __METHOD__ );
343 return $this->mArticles;
344 }
345
346 /**
347 * Count total pages
348 * @return int
349 */
350 public function pages() {
351 $this->mPages = $this->db->selectField( 'page', 'COUNT(*)', '', __METHOD__ );
352 return $this->mPages;
353 }
354
355 /**
356 * Count total users
357 * @return int
358 */
359 public function users() {
360 $this->mUsers = $this->db->selectField( 'user', 'COUNT(*)', '', __METHOD__ );
361 return $this->mUsers;
362 }
363
364 /**
365 * Count total files
366 * @return int
367 */
368 public function files() {
369 $this->mFiles = $this->db->selectField( 'image', 'COUNT(*)', '', __METHOD__ );
370 return $this->mFiles;
371 }
372
373 /**
374 * Do all updates and commit them. More or less a replacement
375 * for the original initStats, but without output.
376 *
377 * @param IDatabase|bool $database
378 * - boolean: Whether to use the master DB
379 * - IDatabase: Database connection to use
380 * @param array $options Array of options, may contain the following values
381 * - activeUsers boolean: Whether to update the number of active users (default: false)
382 */
383 public static function doAllAndCommit( $database, array $options = [] ) {
384 $options += [ 'update' => false, 'activeUsers' => false ];
385
386 // Grab the object and count everything
387 $counter = new SiteStatsInit( $database );
388
389 $counter->edits();
390 $counter->articles();
391 $counter->pages();
392 $counter->users();
393 $counter->files();
394
395 $counter->refresh();
396
397 // Count active users if need be
398 if ( $options['activeUsers'] ) {
399 SiteStatsUpdate::cacheUpdate( wfGetDB( DB_MASTER ) );
400 }
401 }
402
403 /**
404 * Refresh site_stats
405 */
406 public function refresh() {
407 $values = [
408 'ss_row_id' => 1,
409 'ss_total_edits' => ( $this->mEdits === null ? $this->edits() : $this->mEdits ),
410 'ss_good_articles' => ( $this->mArticles === null ? $this->articles() : $this->mArticles ),
411 'ss_total_pages' => ( $this->mPages === null ? $this->pages() : $this->mPages ),
412 'ss_users' => ( $this->mUsers === null ? $this->users() : $this->mUsers ),
413 'ss_images' => ( $this->mFiles === null ? $this->files() : $this->mFiles ),
414 ];
415
416 $dbw = wfGetDB( DB_MASTER );
417 $dbw->upsert( 'site_stats', $values, [ 'ss_row_id' ], $values, __METHOD__ );
418 }
419 }