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