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