3 * Accessors and mutators for the site-wide statistics.
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.
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.
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
23 use Wikimedia\Rdbms\Database
;
24 use Wikimedia\Rdbms\IDatabase
;
25 use MediaWiki\MediaWikiServices
;
28 * Static accessor class for site_stats and related things
31 /** @var bool|stdClass */
35 private static $loaded = false;
41 private static $pageCount = [];
43 static function unload() {
44 self
::$loaded = false;
47 static function recache() {
52 * @param bool $recache
54 static function load( $recache = false ) {
55 if ( self
::$loaded && !$recache ) {
59 self
::$row = self
::loadAndLazyInit();
61 # This code is somewhat schema-agnostic, because I'm changing it in a minor release -- TS
62 if ( !isset( self
::$row->ss_total_pages
) && self
::$row->ss_total_pages
== -1 ) {
64 $u = new SiteStatsUpdate( 0, 0, 0 );
66 self
::$row = self
::doLoad( wfGetDB( DB_REPLICA
) );
73 * @return bool|stdClass
75 static function loadAndLazyInit() {
78 wfDebug( __METHOD__
. ": reading site_stats from replica DB\n" );
79 $row = self
::doLoad( wfGetDB( DB_REPLICA
) );
81 if ( !self
::isSane( $row ) ) {
82 $lb = MediaWikiServices
::getInstance()->getDBLoadBalancer();
83 if ( $lb->hasOrMadeRecentMasterChanges() ) {
84 // Might have just been initialized during this request? Underflow?
85 wfDebug( __METHOD__
. ": site_stats damaged or missing on replica DB\n" );
86 $row = self
::doLoad( wfGetDB( DB_MASTER
) );
90 if ( !$wgMiserMode && !self
::isSane( $row ) ) {
91 // Normally the site_stats table is initialized at install time.
92 // Some manual construction scenarios may leave the table empty or
93 // broken, however, for instance when importing from a dump into a
94 // clean schema with mwdumper.
95 wfDebug( __METHOD__
. ": initializing damaged or missing site_stats\n" );
97 SiteStatsInit
::doAllAndCommit( wfGetDB( DB_REPLICA
) );
99 $row = self
::doLoad( wfGetDB( DB_MASTER
) );
102 if ( !self
::isSane( $row ) ) {
103 wfDebug( __METHOD__
. ": site_stats persistently nonsensical o_O\n" );
110 * @param IDatabase $db
111 * @return bool|stdClass
113 static function doLoad( $db ) {
114 return $db->selectRow( 'site_stats', [
126 * Return the total number of page views. Except we don't track those anymore.
127 * Stop calling this function, it will be removed some time in the future. It's
128 * kept here simply to prevent fatal errors.
130 * @deprecated since 1.25
133 static function views() {
134 wfDeprecated( __METHOD__
, '1.25' );
141 static function edits() {
143 return self
::$row->ss_total_edits
;
149 static function articles() {
151 return self
::$row->ss_good_articles
;
157 static function pages() {
159 return self
::$row->ss_total_pages
;
165 static function users() {
167 return self
::$row->ss_users
;
173 static function activeUsers() {
175 return self
::$row->ss_active_users
;
181 static function images() {
183 return self
::$row->ss_images
;
187 * Find the number of users in a given user group.
188 * @param string $group Name of group
191 static function numberingroup( $group ) {
192 $cache = ObjectCache
::getMainWANInstance();
193 return $cache->getWithSetCallback(
194 wfMemcKey( 'SiteStats', 'groupcounts', $group ),
196 function ( $oldValue, &$ttl, array &$setOpts ) use ( $group ) {
197 $dbr = wfGetDB( DB_REPLICA
);
199 $setOpts +
= Database
::getCacheSetOptions( $dbr );
201 return $dbr->selectField(
205 'ug_group' => $group,
206 'ug_expiry IS NULL OR ug_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() )
211 [ 'pcTTL' => $cache::TTL_PROC_LONG
]
218 static function jobs() {
219 if ( !isset( self
::$jobs ) ) {
221 self
::$jobs = array_sum( JobQueueGroup
::singleton()->getQueueSizes() );
222 } catch ( JobQueueError
$e ) {
226 * Zero rows still do single row read for row that doesn't exist,
227 * but people are annoyed by that
229 if ( self
::$jobs == 1 ) {
241 static function pagesInNs( $ns ) {
242 if ( !isset( self
::$pageCount[$ns] ) ) {
243 $dbr = wfGetDB( DB_REPLICA
);
244 self
::$pageCount[$ns] = (int)$dbr->selectField(
247 [ 'page_namespace' => $ns ],
251 return self
::$pageCount[$ns];
255 * Is the provided row of site stats sane, or should it be regenerated?
257 * Checks only fields which are filled by SiteStatsInit::refresh.
259 * @param bool|object $row
263 private static function isSane( $row ) {
265 ||
$row->ss_total_pages
< $row->ss_good_articles
266 ||
$row->ss_total_edits
< $row->ss_total_pages
270 // Now check for underflow/overflow
278 if ( $row->$member > 2000000000 ||
$row->$member < 0 ) {
287 * Class designed for counting of stats.
289 class SiteStatsInit
{
291 // Database connection
295 private $mEdits = null, $mArticles = null, $mPages = null;
296 private $mUsers = null, $mFiles = null;
300 * @param bool|IDatabase $database
301 * - boolean: Whether to use the master DB
302 * - IDatabase: Database connection to use
304 public function __construct( $database = false ) {
305 if ( $database instanceof IDatabase
) {
306 $this->db
= $database;
307 } elseif ( $database ) {
308 $this->db
= wfGetDB( DB_MASTER
);
310 $this->db
= wfGetDB( DB_REPLICA
, 'vslow' );
315 * Count the total number of edits
318 public function edits() {
319 $this->mEdits
= $this->db
->selectField( 'revision', 'COUNT(*)', '', __METHOD__
);
320 $this->mEdits +
= $this->db
->selectField( 'archive', 'COUNT(*)', '', __METHOD__
);
321 return $this->mEdits
;
325 * Count pages in article space(s)
328 public function articles() {
329 global $wgArticleCountMethod;
331 $tables = [ 'page' ];
333 'page_namespace' => MWNamespace
::getContentNamespaces(),
334 'page_is_redirect' => 0,
337 if ( $wgArticleCountMethod == 'link' ) {
338 $tables[] = 'pagelinks';
339 $conds[] = 'pl_from=page_id';
340 } elseif ( $wgArticleCountMethod == 'comma' ) {
341 // To make a correct check for this, we would need, for each page,
342 // to load the text, maybe uncompress it, maybe decode it and then
343 // check if there's one comma.
344 // But one thing we are sure is that if the page is empty, it can't
345 // contain a comma :)
346 $conds[] = 'page_len > 0';
349 $this->mArticles
= $this->db
->selectField( $tables, 'COUNT(DISTINCT page_id)',
350 $conds, __METHOD__
);
351 return $this->mArticles
;
358 public function pages() {
359 $this->mPages
= $this->db
->selectField( 'page', 'COUNT(*)', '', __METHOD__
);
360 return $this->mPages
;
367 public function users() {
368 $this->mUsers
= $this->db
->selectField( 'user', 'COUNT(*)', '', __METHOD__
);
369 return $this->mUsers
;
376 public function files() {
377 $this->mFiles
= $this->db
->selectField( 'image', 'COUNT(*)', '', __METHOD__
);
378 return $this->mFiles
;
382 * Do all updates and commit them. More or less a replacement
383 * for the original initStats, but without output.
385 * @param IDatabase|bool $database
386 * - boolean: Whether to use the master DB
387 * - IDatabase: Database connection to use
388 * @param array $options Array of options, may contain the following values
389 * - activeUsers boolean: Whether to update the number of active users (default: false)
391 public static function doAllAndCommit( $database, array $options = [] ) {
392 $options +
= [ 'update' => false, 'activeUsers' => false ];
394 // Grab the object and count everything
395 $counter = new SiteStatsInit( $database );
398 $counter->articles();
405 // Count active users if need be
406 if ( $options['activeUsers'] ) {
407 SiteStatsUpdate
::cacheUpdate( wfGetDB( DB_MASTER
) );
414 public function refresh() {
417 'ss_total_edits' => ( $this->mEdits
=== null ?
$this->edits() : $this->mEdits
),
418 'ss_good_articles' => ( $this->mArticles
=== null ?
$this->articles() : $this->mArticles
),
419 'ss_total_pages' => ( $this->mPages
=== null ?
$this->pages() : $this->mPages
),
420 'ss_users' => ( $this->mUsers
=== null ?
$this->users() : $this->mUsers
),
421 'ss_images' => ( $this->mFiles
=== null ?
$this->files() : $this->mFiles
),
424 $dbw = wfGetDB( DB_MASTER
);
425 $dbw->upsert( 'site_stats', $values, [ 'ss_row_id' ], $values, __METHOD__
);