2004-02-18 02:15:00 +00:00
|
|
|
<?php
|
2012-05-10 15:51:44 +00:00
|
|
|
/**
|
|
|
|
|
* Accessors and mutators for the site-wide statistics.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
2006-11-21 09:53:45 +00:00
|
|
|
|
2017-02-07 04:49:57 +00:00
|
|
|
use Wikimedia\Rdbms\Database;
|
2017-02-10 18:09:05 +00:00
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
2017-04-29 05:10:43 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2017-02-10 18:09:05 +00:00
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2006-11-21 09:53:45 +00:00
|
|
|
* Static accessor class for site_stats and related things
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2006-11-21 09:53:45 +00:00
|
|
|
class SiteStats {
|
2016-08-09 22:09:45 +00:00
|
|
|
/** @var bool|stdClass */
|
2014-05-11 15:59:57 +00:00
|
|
|
private static $row;
|
|
|
|
|
|
|
|
|
|
/** @var bool */
|
|
|
|
|
private static $loaded = false;
|
|
|
|
|
/** @var int[] */
|
2016-02-17 09:09:32 +00:00
|
|
|
private static $pageCount = [];
|
2014-05-11 15:59:57 +00:00
|
|
|
|
2016-08-25 01:37:38 +00:00
|
|
|
static function unload() {
|
|
|
|
|
self::$loaded = false;
|
|
|
|
|
}
|
|
|
|
|
|
2006-11-21 09:53:45 +00:00
|
|
|
static function recache() {
|
|
|
|
|
self::load( true );
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-21 20:06:57 +00:00
|
|
|
/**
|
2014-04-22 11:07:02 +00:00
|
|
|
* @param bool $recache
|
2011-05-21 20:06:57 +00:00
|
|
|
*/
|
2006-11-21 09:53:45 +00:00
|
|
|
static function load( $recache = false ) {
|
|
|
|
|
if ( self::$loaded && !$recache ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2007-02-21 00:04:21 +00:00
|
|
|
self::$row = self::loadAndLazyInit();
|
2006-11-21 09:53:45 +00:00
|
|
|
|
2007-08-09 12:27:50 +00:00
|
|
|
self::$loaded = true;
|
2006-11-21 09:53:45 +00:00
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2011-05-21 20:06:57 +00:00
|
|
|
/**
|
2016-08-09 22:09:45 +00:00
|
|
|
* @return bool|stdClass
|
2011-05-21 20:06:57 +00:00
|
|
|
*/
|
2007-02-21 00:04:21 +00:00
|
|
|
static function loadAndLazyInit() {
|
2015-04-08 16:35:56 +00:00
|
|
|
global $wgMiserMode;
|
|
|
|
|
|
2016-09-05 20:21:26 +00:00
|
|
|
wfDebug( __METHOD__ . ": reading site_stats from replica DB\n" );
|
2016-09-05 19:55:19 +00:00
|
|
|
$row = self::doLoad( wfGetDB( DB_REPLICA ) );
|
2007-07-16 19:48:18 +00:00
|
|
|
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( !self::isSane( $row ) ) {
|
2017-04-29 05:10:43 +00:00
|
|
|
$lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
|
|
|
|
|
if ( $lb->hasOrMadeRecentMasterChanges() ) {
|
|
|
|
|
// Might have just been initialized during this request? Underflow?
|
|
|
|
|
wfDebug( __METHOD__ . ": site_stats damaged or missing on replica DB\n" );
|
|
|
|
|
$row = self::doLoad( wfGetDB( DB_MASTER ) );
|
|
|
|
|
}
|
2007-02-21 00:04:21 +00:00
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2018-01-31 21:39:44 +00:00
|
|
|
if ( !self::isSane( $row ) ) {
|
|
|
|
|
if ( $wgMiserMode ) {
|
|
|
|
|
// Start off with all zeroes, assuming that this is a new wiki or any
|
|
|
|
|
// repopulations where done manually via script.
|
|
|
|
|
SiteStatsInit::doPlaceholderInit();
|
|
|
|
|
} else {
|
|
|
|
|
// Normally the site_stats table is initialized at install time.
|
|
|
|
|
// Some manual construction scenarios may leave the table empty or
|
|
|
|
|
// broken, however, for instance when importing from a dump into a
|
|
|
|
|
// clean schema with mwdumper.
|
|
|
|
|
wfDebug( __METHOD__ . ": initializing damaged or missing site_stats\n" );
|
|
|
|
|
SiteStatsInit::doAllAndCommit( wfGetDB( DB_REPLICA ) );
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-02-21 00:04:21 +00:00
|
|
|
$row = self::doLoad( wfGetDB( DB_MASTER ) );
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( !self::isSane( $row ) ) {
|
2007-07-16 19:48:18 +00:00
|
|
|
wfDebug( __METHOD__ . ": site_stats persistently nonsensical o_O\n" );
|
2018-01-31 21:39:44 +00:00
|
|
|
$row = (object)array_fill_keys( self::selectFields(), 0 );
|
2007-02-21 00:04:21 +00:00
|
|
|
}
|
2017-04-29 05:10:43 +00:00
|
|
|
|
2007-02-21 00:04:21 +00:00
|
|
|
return $row;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-21 20:06:57 +00:00
|
|
|
/**
|
2015-10-04 09:07:25 +00:00
|
|
|
* @param IDatabase $db
|
2016-08-09 22:09:45 +00:00
|
|
|
* @return bool|stdClass
|
2011-05-21 20:06:57 +00:00
|
|
|
*/
|
2007-02-21 00:04:21 +00:00
|
|
|
static function doLoad( $db ) {
|
2018-02-10 08:37:42 +00:00
|
|
|
return $db->selectRow(
|
|
|
|
|
'site_stats',
|
|
|
|
|
self::selectFields(),
|
|
|
|
|
[ 'ss_row_id' => 1 ],
|
|
|
|
|
__METHOD__
|
|
|
|
|
);
|
2007-02-21 00:04:21 +00:00
|
|
|
}
|
2006-11-21 09:53:45 +00:00
|
|
|
|
2011-05-21 20:06:57 +00:00
|
|
|
/**
|
2014-07-30 20:56:20 +00:00
|
|
|
* Return the total number of page views. Except we don't track those anymore.
|
|
|
|
|
* Stop calling this function, it will be removed some time in the future. It's
|
|
|
|
|
* kept here simply to prevent fatal errors.
|
|
|
|
|
*
|
|
|
|
|
* @deprecated since 1.25
|
2011-05-21 20:06:57 +00:00
|
|
|
* @return int
|
|
|
|
|
*/
|
2006-11-21 09:53:45 +00:00
|
|
|
static function views() {
|
2014-07-30 20:56:20 +00:00
|
|
|
wfDeprecated( __METHOD__, '1.25' );
|
|
|
|
|
return 0;
|
2006-11-21 09:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
2011-05-21 20:06:57 +00:00
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
2006-11-21 09:53:45 +00:00
|
|
|
static function edits() {
|
|
|
|
|
self::load();
|
|
|
|
|
return self::$row->ss_total_edits;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-21 20:06:57 +00:00
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
2006-11-21 09:53:45 +00:00
|
|
|
static function articles() {
|
|
|
|
|
self::load();
|
|
|
|
|
return self::$row->ss_good_articles;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-21 20:06:57 +00:00
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
2006-11-21 09:53:45 +00:00
|
|
|
static function pages() {
|
|
|
|
|
self::load();
|
|
|
|
|
return self::$row->ss_total_pages;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-21 20:06:57 +00:00
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
2006-11-21 09:53:45 +00:00
|
|
|
static function users() {
|
|
|
|
|
self::load();
|
|
|
|
|
return self::$row->ss_users;
|
|
|
|
|
}
|
2009-07-18 07:44:57 +00:00
|
|
|
|
2011-05-21 20:06:57 +00:00
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
2008-08-31 19:29:37 +00:00
|
|
|
static function activeUsers() {
|
|
|
|
|
self::load();
|
|
|
|
|
return self::$row->ss_active_users;
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2011-05-21 20:06:57 +00:00
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
2006-11-21 09:53:45 +00:00
|
|
|
static function images() {
|
|
|
|
|
self::load();
|
|
|
|
|
return self::$row->ss_images;
|
|
|
|
|
}
|
|
|
|
|
|
2008-07-28 15:49:44 +00:00
|
|
|
/**
|
|
|
|
|
* Find the number of users in a given user group.
|
2014-07-24 17:42:24 +00:00
|
|
|
* @param string $group Name of group
|
2014-04-22 11:07:02 +00:00
|
|
|
* @return int
|
2008-07-28 15:49:44 +00:00
|
|
|
*/
|
2010-05-29 23:45:02 +00:00
|
|
|
static function numberingroup( $group ) {
|
2017-05-25 07:46:22 +00:00
|
|
|
$cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
|
2015-10-19 17:52:19 +00:00
|
|
|
return $cache->getWithSetCallback(
|
2017-05-25 07:46:22 +00:00
|
|
|
$cache->makeKey( 'SiteStats', 'groupcounts', $group ),
|
2015-10-19 17:52:19 +00:00
|
|
|
$cache::TTL_HOUR,
|
2015-10-15 02:45:03 +00:00
|
|
|
function ( $oldValue, &$ttl, array &$setOpts ) use ( $group ) {
|
2016-09-05 19:55:19 +00:00
|
|
|
$dbr = wfGetDB( DB_REPLICA );
|
2015-10-15 02:45:03 +00:00
|
|
|
|
2015-10-19 19:57:34 +00:00
|
|
|
$setOpts += Database::getCacheSetOptions( $dbr );
|
2015-10-15 02:45:03 +00:00
|
|
|
|
|
|
|
|
return $dbr->selectField(
|
2010-05-29 23:45:02 +00:00
|
|
|
'user_groups',
|
|
|
|
|
'COUNT(*)',
|
2016-12-23 02:07:59 +00:00
|
|
|
[
|
|
|
|
|
'ug_group' => $group,
|
2017-04-11 02:32:02 +00:00
|
|
|
'ug_expiry IS NULL OR ug_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() )
|
2016-12-23 02:07:59 +00:00
|
|
|
],
|
2010-05-29 23:45:02 +00:00
|
|
|
__METHOD__
|
|
|
|
|
);
|
2015-10-15 02:45:03 +00:00
|
|
|
},
|
2016-05-05 23:40:26 +00:00
|
|
|
[ 'pcTTL' => $cache::TTL_PROC_LONG ]
|
2015-10-15 02:45:03 +00:00
|
|
|
);
|
2006-11-21 09:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
2011-05-21 20:06:57 +00:00
|
|
|
/**
|
2017-05-29 21:58:33 +00:00
|
|
|
* Total number of jobs in the job queue.
|
2011-05-21 20:06:57 +00:00
|
|
|
* @return int
|
|
|
|
|
*/
|
2007-08-09 12:27:50 +00:00
|
|
|
static function jobs() {
|
2017-05-29 21:58:33 +00:00
|
|
|
$cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
|
|
|
|
|
return $cache->getWithSetCallback(
|
|
|
|
|
$cache->makeKey( 'SiteStats', 'jobscount' ),
|
|
|
|
|
$cache::TTL_MINUTE,
|
|
|
|
|
function ( $oldValue, &$ttl, array &$setOpts ) {
|
|
|
|
|
try{
|
|
|
|
|
$jobs = array_sum( JobQueueGroup::singleton()->getQueueSizes() );
|
|
|
|
|
} catch ( JobQueueError $e ) {
|
|
|
|
|
$jobs = 0;
|
|
|
|
|
}
|
|
|
|
|
return $jobs;
|
|
|
|
|
},
|
|
|
|
|
[ 'pcTTL' => $cache::TTL_PROC_LONG ]
|
|
|
|
|
);
|
2007-08-09 12:27:50 +00:00
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2011-05-21 20:06:57 +00:00
|
|
|
/**
|
2014-04-22 11:07:02 +00:00
|
|
|
* @param int $ns
|
2011-05-21 20:06:57 +00:00
|
|
|
*
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
2006-11-21 09:53:45 +00:00
|
|
|
static function pagesInNs( $ns ) {
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( !isset( self::$pageCount[$ns] ) ) {
|
2016-09-05 19:55:19 +00:00
|
|
|
$dbr = wfGetDB( DB_REPLICA );
|
2011-05-25 20:57:15 +00:00
|
|
|
self::$pageCount[$ns] = (int)$dbr->selectField(
|
2010-05-29 23:45:02 +00:00
|
|
|
'page',
|
|
|
|
|
'COUNT(*)',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'page_namespace' => $ns ],
|
2010-05-29 23:45:02 +00:00
|
|
|
__METHOD__
|
|
|
|
|
);
|
2006-11-21 09:53:45 +00:00
|
|
|
}
|
2011-05-25 20:57:15 +00:00
|
|
|
return self::$pageCount[$ns];
|
2006-11-21 09:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
2018-01-31 21:39:44 +00:00
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public static function selectFields() {
|
|
|
|
|
return [
|
|
|
|
|
'ss_total_edits',
|
|
|
|
|
'ss_good_articles',
|
|
|
|
|
'ss_total_pages',
|
|
|
|
|
'ss_users',
|
|
|
|
|
'ss_active_users',
|
|
|
|
|
'ss_images',
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-21 20:06:57 +00:00
|
|
|
/**
|
|
|
|
|
* Is the provided row of site stats sane, or should it be regenerated?
|
|
|
|
|
*
|
2014-02-24 17:17:35 +00:00
|
|
|
* Checks only fields which are filled by SiteStatsInit::refresh.
|
|
|
|
|
*
|
2014-04-22 11:07:02 +00:00
|
|
|
* @param bool|object $row
|
2011-05-21 20:06:57 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2007-07-16 19:48:18 +00:00
|
|
|
private static function isSane( $row ) {
|
2013-05-23 17:05:13 +00:00
|
|
|
if ( $row === false
|
2011-01-31 16:41:19 +00:00
|
|
|
|| $row->ss_total_pages < $row->ss_good_articles
|
|
|
|
|
|| $row->ss_total_edits < $row->ss_total_pages
|
2007-07-16 19:48:18 +00:00
|
|
|
) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
// Now check for underflow/overflow
|
2016-02-17 09:09:32 +00:00
|
|
|
foreach ( [
|
2013-05-23 17:05:13 +00:00
|
|
|
'ss_total_edits',
|
|
|
|
|
'ss_good_articles',
|
|
|
|
|
'ss_total_pages',
|
|
|
|
|
'ss_users',
|
|
|
|
|
'ss_images',
|
2016-02-17 09:09:32 +00:00
|
|
|
] as $member ) {
|
2013-05-23 17:05:13 +00:00
|
|
|
if ( $row->$member > 2000000000 || $row->$member < 0 ) {
|
2007-07-16 19:48:18 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-07-16 20:05:32 +00:00
|
|
|
return true;
|
2007-07-16 19:48:18 +00:00
|
|
|
}
|
2006-11-21 09:53:45 +00:00
|
|
|
}
|