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;
37 private static $pageCount = [];
39 static function unload() {
40 self
::$loaded = false;
43 static function recache() {
48 * @param bool $recache
50 static function load( $recache = false ) {
51 if ( self
::$loaded && !$recache ) {
55 self
::$row = self
::loadAndLazyInit();
61 * @return bool|stdClass
63 static function loadAndLazyInit() {
66 wfDebug( __METHOD__
. ": reading site_stats from replica DB\n" );
67 $row = self
::doLoad( wfGetDB( DB_REPLICA
) );
69 if ( !self
::isSane( $row ) ) {
70 $lb = MediaWikiServices
::getInstance()->getDBLoadBalancer();
71 if ( $lb->hasOrMadeRecentMasterChanges() ) {
72 // Might have just been initialized during this request? Underflow?
73 wfDebug( __METHOD__
. ": site_stats damaged or missing on replica DB\n" );
74 $row = self
::doLoad( wfGetDB( DB_MASTER
) );
78 if ( !self
::isSane( $row ) ) {
80 // Start off with all zeroes, assuming that this is a new wiki or any
81 // repopulations where done manually via script.
82 SiteStatsInit
::doPlaceholderInit();
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 SiteStatsInit
::doAllAndCommit( wfGetDB( DB_REPLICA
) );
92 $row = self
::doLoad( wfGetDB( DB_MASTER
) );
95 if ( !self
::isSane( $row ) ) {
96 wfDebug( __METHOD__
. ": site_stats persistently nonsensical o_O\n" );
97 $row = (object)array_fill_keys( self
::selectFields(), 0 );
104 * @param IDatabase $db
105 * @return bool|stdClass
107 static function doLoad( $db ) {
108 return $db->selectRow(
110 self
::selectFields(),
111 [ 'ss_row_id' => 1 ],
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.
121 * @deprecated since 1.25
124 static function views() {
125 wfDeprecated( __METHOD__
, '1.25' );
132 static function edits() {
134 return self
::$row->ss_total_edits
;
140 static function articles() {
142 return self
::$row->ss_good_articles
;
148 static function pages() {
150 return self
::$row->ss_total_pages
;
156 static function users() {
158 return self
::$row->ss_users
;
164 static function activeUsers() {
166 return self
::$row->ss_active_users
;
172 static function images() {
174 return self
::$row->ss_images
;
178 * Find the number of users in a given user group.
179 * @param string $group Name of group
182 static function numberingroup( $group ) {
183 $cache = MediaWikiServices
::getInstance()->getMainWANObjectCache();
184 return $cache->getWithSetCallback(
185 $cache->makeKey( 'SiteStats', 'groupcounts', $group ),
187 function ( $oldValue, &$ttl, array &$setOpts ) use ( $group ) {
188 $dbr = wfGetDB( DB_REPLICA
);
190 $setOpts +
= Database
::getCacheSetOptions( $dbr );
192 return $dbr->selectField(
196 'ug_group' => $group,
197 'ug_expiry IS NULL OR ug_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() )
202 [ 'pcTTL' => $cache::TTL_PROC_LONG
]
207 * Total number of jobs in the job queue.
210 static function jobs() {
211 $cache = MediaWikiServices
::getInstance()->getMainWANObjectCache();
212 return $cache->getWithSetCallback(
213 $cache->makeKey( 'SiteStats', 'jobscount' ),
215 function ( $oldValue, &$ttl, array &$setOpts ) {
217 $jobs = array_sum( JobQueueGroup
::singleton()->getQueueSizes() );
218 } catch ( JobQueueError
$e ) {
223 [ 'pcTTL' => $cache::TTL_PROC_LONG
]
232 static function pagesInNs( $ns ) {
233 if ( !isset( self
::$pageCount[$ns] ) ) {
234 $dbr = wfGetDB( DB_REPLICA
);
235 self
::$pageCount[$ns] = (int)$dbr->selectField(
238 [ 'page_namespace' => $ns ],
242 return self
::$pageCount[$ns];
248 public static function selectFields() {
260 * Is the provided row of site stats sane, or should it be regenerated?
262 * Checks only fields which are filled by SiteStatsInit::refresh.
264 * @param bool|object $row
267 private static function isSane( $row ) {
269 ||
$row->ss_total_pages
< $row->ss_good_articles
270 ||
$row->ss_total_edits
< $row->ss_total_pages
274 // Now check for underflow/overflow
282 if ( $row->$member > 2000000000 ||
$row->$member < 0 ) {