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
24 * Static accessor class for site_stats and related things
27 /** @var bool|ResultWrapper */
31 private static $loaded = false;
37 private static $pageCount = [];
39 static function recache() {
44 * @param bool $recache
46 static function load( $recache = false ) {
47 if ( self
::$loaded && !$recache ) {
51 self
::$row = self
::loadAndLazyInit();
53 # This code is somewhat schema-agnostic, because I'm changing it in a minor release -- TS
54 if ( !isset( self
::$row->ss_total_pages
) && self
::$row->ss_total_pages
== -1 ) {
56 $u = new SiteStatsUpdate( 0, 0, 0 );
58 self
::$row = self
::doLoad( wfGetDB( DB_SLAVE
) );
65 * @return bool|ResultWrapper
67 static function loadAndLazyInit() {
70 wfDebug( __METHOD__
. ": reading site_stats from slave\n" );
71 $row = self
::doLoad( wfGetDB( DB_SLAVE
) );
73 if ( !self
::isSane( $row ) ) {
74 // Might have just been initialized during this request? Underflow?
75 wfDebug( __METHOD__
. ": site_stats damaged or missing on slave\n" );
76 $row = self
::doLoad( wfGetDB( DB_MASTER
) );
79 if ( !$wgMiserMode && !self
::isSane( $row ) ) {
80 // Normally the site_stats table is initialized at install time.
81 // Some manual construction scenarios may leave the table empty or
82 // broken, however, for instance when importing from a dump into a
83 // clean schema with mwdumper.
84 wfDebug( __METHOD__
. ": initializing damaged or missing site_stats\n" );
86 SiteStatsInit
::doAllAndCommit( wfGetDB( DB_SLAVE
) );
88 $row = self
::doLoad( wfGetDB( DB_MASTER
) );
91 if ( !self
::isSane( $row ) ) {
92 wfDebug( __METHOD__
. ": site_stats persistently nonsensical o_O\n" );
98 * @param IDatabase $db
99 * @return bool|ResultWrapper
101 static function doLoad( $db ) {
102 return $db->selectRow( 'site_stats', [
110 ], false, __METHOD__
);
114 * Return the total number of page views. Except we don't track those anymore.
115 * Stop calling this function, it will be removed some time in the future. It's
116 * kept here simply to prevent fatal errors.
118 * @deprecated since 1.25
121 static function views() {
122 wfDeprecated( __METHOD__
, '1.25' );
129 static function edits() {
131 return self
::$row->ss_total_edits
;
137 static function articles() {
139 return self
::$row->ss_good_articles
;
145 static function pages() {
147 return self
::$row->ss_total_pages
;
153 static function users() {
155 return self
::$row->ss_users
;
161 static function activeUsers() {
163 return self
::$row->ss_active_users
;
169 static function images() {
171 return self
::$row->ss_images
;
175 * Find the number of users in a given user group.
176 * @param string $group Name of group
179 static function numberingroup( $group ) {
180 $cache = ObjectCache
::getMainWANInstance();
181 return $cache->getWithSetCallback(
182 wfMemcKey( 'SiteStats', 'groupcounts', $group ),
184 function ( $oldValue, &$ttl, array &$setOpts ) use ( $group ) {
185 $dbr = wfGetDB( DB_SLAVE
);
187 $setOpts +
= Database
::getCacheSetOptions( $dbr );
189 return $dbr->selectField(
192 [ 'ug_group' => $group ],
203 static function jobs() {
204 if ( !isset( self
::$jobs ) ) {
205 $dbr = wfGetDB( DB_SLAVE
);
206 self
::$jobs = array_sum( JobQueueGroup
::singleton()->getQueueSizes() );
208 * Zero rows still do single row read for row that doesn't exist,
209 * but people are annoyed by that
211 if ( self
::$jobs == 1 ) {
223 static function pagesInNs( $ns ) {
224 if ( !isset( self
::$pageCount[$ns] ) ) {
225 $dbr = wfGetDB( DB_SLAVE
);
226 self
::$pageCount[$ns] = (int)$dbr->selectField(
229 [ 'page_namespace' => $ns ],
233 return self
::$pageCount[$ns];
237 * Is the provided row of site stats sane, or should it be regenerated?
239 * Checks only fields which are filled by SiteStatsInit::refresh.
241 * @param bool|object $row
245 private static function isSane( $row ) {
247 ||
$row->ss_total_pages
< $row->ss_good_articles
248 ||
$row->ss_total_edits
< $row->ss_total_pages
252 // Now check for underflow/overflow
260 if ( $row->$member > 2000000000 ||
$row->$member < 0 ) {
269 * Class designed for counting of stats.
271 class SiteStatsInit
{
273 // Database connection
277 private $mEdits = null, $mArticles = null, $mPages = null;
278 private $mUsers = null, $mFiles = null;
282 * @param bool|IDatabase $database
283 * - boolean: Whether to use the master DB
284 * - IDatabase: Database connection to use
286 public function __construct( $database = false ) {
287 if ( $database instanceof IDatabase
) {
288 $this->db
= $database;
289 } elseif ( $database ) {
290 $this->db
= wfGetDB( DB_MASTER
);
292 $this->db
= wfGetDB( DB_SLAVE
, 'vslow' );
297 * Count the total number of edits
300 public function edits() {
301 $this->mEdits
= $this->db
->selectField( 'revision', 'COUNT(*)', '', __METHOD__
);
302 $this->mEdits +
= $this->db
->selectField( 'archive', 'COUNT(*)', '', __METHOD__
);
303 return $this->mEdits
;
307 * Count pages in article space(s)
310 public function articles() {
311 global $wgArticleCountMethod;
313 $tables = [ 'page' ];
315 'page_namespace' => MWNamespace
::getContentNamespaces(),
316 'page_is_redirect' => 0,
319 if ( $wgArticleCountMethod == 'link' ) {
320 $tables[] = 'pagelinks';
321 $conds[] = 'pl_from=page_id';
322 } elseif ( $wgArticleCountMethod == 'comma' ) {
323 // To make a correct check for this, we would need, for each page,
324 // to load the text, maybe uncompress it, maybe decode it and then
325 // check if there's one comma.
326 // But one thing we are sure is that if the page is empty, it can't
327 // contain a comma :)
328 $conds[] = 'page_len > 0';
331 $this->mArticles
= $this->db
->selectField( $tables, 'COUNT(DISTINCT page_id)',
332 $conds, __METHOD__
);
333 return $this->mArticles
;
340 public function pages() {
341 $this->mPages
= $this->db
->selectField( 'page', 'COUNT(*)', '', __METHOD__
);
342 return $this->mPages
;
349 public function users() {
350 $this->mUsers
= $this->db
->selectField( 'user', 'COUNT(*)', '', __METHOD__
);
351 return $this->mUsers
;
358 public function files() {
359 $this->mFiles
= $this->db
->selectField( 'image', 'COUNT(*)', '', __METHOD__
);
360 return $this->mFiles
;
364 * Do all updates and commit them. More or less a replacement
365 * for the original initStats, but without output.
367 * @param IDatabase|bool $database
368 * - boolean: Whether to use the master DB
369 * - IDatabase: Database connection to use
370 * @param array $options Array of options, may contain the following values
371 * - activeUsers boolean: Whether to update the number of active users (default: false)
373 public static function doAllAndCommit( $database, array $options = [] ) {
374 $options +
= [ 'update' => false, 'activeUsers' => false ];
376 // Grab the object and count everything
377 $counter = new SiteStatsInit( $database );
380 $counter->articles();
387 // Count active users if need be
388 if ( $options['activeUsers'] ) {
389 SiteStatsUpdate
::cacheUpdate( wfGetDB( DB_MASTER
) );
396 public function refresh() {
399 'ss_total_edits' => ( $this->mEdits
=== null ?
$this->edits() : $this->mEdits
),
400 'ss_good_articles' => ( $this->mArticles
=== null ?
$this->articles() : $this->mArticles
),
401 'ss_total_pages' => ( $this->mPages
=== null ?
$this->pages() : $this->mPages
),
402 'ss_users' => ( $this->mUsers
=== null ?
$this->users() : $this->mUsers
),
403 'ss_images' => ( $this->mFiles
=== null ?
$this->files() : $this->mFiles
),
406 $dbw = wfGetDB( DB_MASTER
);
407 $dbw->upsert( 'site_stats', $values, [ 'ss_row_id' ], $values, __METHOD__
);