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 = array();
40 private static $groupMemberCounts = array();
42 static function recache() {
47 * @param bool $recache
49 static function load( $recache = false ) {
50 if ( self
::$loaded && !$recache ) {
54 self
::$row = self
::loadAndLazyInit();
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 ) {
59 $u = new SiteStatsUpdate( 0, 0, 0 );
61 self
::$row = self
::doLoad( wfGetDB( DB_SLAVE
) );
68 * @return bool|ResultWrapper
70 static function loadAndLazyInit() {
71 wfDebug( __METHOD__
. ": reading site_stats from slave\n" );
72 $row = self
::doLoad( wfGetDB( DB_SLAVE
) );
74 if ( !self
::isSane( $row ) ) {
75 // Might have just been initialized during this request? Underflow?
76 wfDebug( __METHOD__
. ": site_stats damaged or missing on slave\n" );
77 $row = self
::doLoad( wfGetDB( DB_MASTER
) );
80 if ( !self
::isSane( $row ) ) {
81 // Normally the site_stats table is initialized at install time.
82 // Some manual construction scenarios may leave the table empty or
83 // broken, however, for instance when importing from a dump into a
84 // clean schema with mwdumper.
85 wfDebug( __METHOD__
. ": initializing damaged or missing site_stats\n" );
87 SiteStatsInit
::doAllAndCommit( wfGetDB( DB_SLAVE
) );
89 $row = self
::doLoad( wfGetDB( DB_MASTER
) );
92 if ( !self
::isSane( $row ) ) {
93 wfDebug( __METHOD__
. ": site_stats persistently nonsensical o_O\n" );
99 * @param DatabaseBase $db
100 * @return bool|ResultWrapper
102 static function doLoad( $db ) {
103 return $db->selectRow( 'site_stats', array(
112 ), false, __METHOD__
);
118 static function views() {
120 return self
::$row->ss_total_views
;
126 static function edits() {
128 return self
::$row->ss_total_edits
;
134 static function articles() {
136 return self
::$row->ss_good_articles
;
142 static function pages() {
144 return self
::$row->ss_total_pages
;
150 static function users() {
152 return self
::$row->ss_users
;
158 static function activeUsers() {
160 return self
::$row->ss_active_users
;
166 static function images() {
168 return self
::$row->ss_images
;
172 * Find the number of users in a given user group.
173 * @param string $group Name of group
176 static function numberingroup( $group ) {
177 if ( !isset( self
::$groupMemberCounts[$group] ) ) {
179 $key = wfMemcKey( 'SiteStats', 'groupcounts', $group );
180 $hit = $wgMemc->get( $key );
182 $dbr = wfGetDB( DB_SLAVE
);
183 $hit = $dbr->selectField(
186 array( 'ug_group' => $group ),
189 $wgMemc->set( $key, $hit, 3600 );
191 self
::$groupMemberCounts[$group] = $hit;
193 return self
::$groupMemberCounts[$group];
199 static function jobs() {
200 if ( !isset( self
::$jobs ) ) {
201 $dbr = wfGetDB( DB_SLAVE
);
202 self
::$jobs = array_sum( JobQueueGroup
::singleton()->getQueueSizes() );
204 * Zero rows still do single row read for row that doesn't exist,
205 * but people are annoyed by that
207 if ( self
::$jobs == 1 ) {
219 static function pagesInNs( $ns ) {
220 wfProfileIn( __METHOD__
);
221 if ( !isset( self
::$pageCount[$ns] ) ) {
222 $dbr = wfGetDB( DB_SLAVE
);
223 self
::$pageCount[$ns] = (int)$dbr->selectField(
226 array( 'page_namespace' => $ns ),
230 wfProfileOut( __METHOD__
);
231 return self
::$pageCount[$ns];
235 * Is the provided row of site stats sane, or should it be regenerated?
237 * Checks only fields which are filled by SiteStatsInit::refresh.
239 * @param bool|object $row
243 private static function isSane( $row ) {
245 ||
$row->ss_total_pages
< $row->ss_good_articles
246 ||
$row->ss_total_edits
< $row->ss_total_pages
250 // Now check for underflow/overflow
259 if ( $row->$member > 2000000000 ||
$row->$member < 0 ) {
268 * Class designed for counting of stats.
270 class SiteStatsInit
{
272 // Database connection
276 private $mEdits = null, $mArticles = null, $mPages = null;
277 private $mUsers = null, $mViews = null, $mFiles = null;
281 * @param bool|DatabaseBase $database
282 * - Boolean: whether to use the master DB
283 * - DatabaseBase: database connection to use
285 public function __construct( $database = false ) {
286 if ( $database instanceof DatabaseBase
) {
287 $this->db
= $database;
289 $this->db
= wfGetDB( $database ? DB_MASTER
: DB_SLAVE
);
294 * Count the total number of edits
297 public function edits() {
298 $this->mEdits
= $this->db
->selectField( 'revision', 'COUNT(*)', '', __METHOD__
);
299 $this->mEdits +
= $this->db
->selectField( 'archive', 'COUNT(*)', '', __METHOD__
);
300 return $this->mEdits
;
304 * Count pages in article space(s)
307 public function articles() {
308 global $wgArticleCountMethod;
310 $tables = array( 'page' );
312 'page_namespace' => MWNamespace
::getContentNamespaces(),
313 'page_is_redirect' => 0,
316 if ( $wgArticleCountMethod == 'link' ) {
317 $tables[] = 'pagelinks';
318 $conds[] = 'pl_from=page_id';
319 } elseif ( $wgArticleCountMethod == 'comma' ) {
320 // To make a correct check for this, we would need, for each page,
321 // to load the text, maybe uncompress it, maybe decode it and then
322 // check if there's one comma.
323 // But one thing we are sure is that if the page is empty, it can't
324 // contain a comma :)
325 $conds[] = 'page_len > 0';
328 $this->mArticles
= $this->db
->selectField( $tables, 'COUNT(DISTINCT page_id)',
329 $conds, __METHOD__
);
330 return $this->mArticles
;
337 public function pages() {
338 $this->mPages
= $this->db
->selectField( 'page', 'COUNT(*)', '', __METHOD__
);
339 return $this->mPages
;
346 public function users() {
347 $this->mUsers
= $this->db
->selectField( 'user', 'COUNT(*)', '', __METHOD__
);
348 return $this->mUsers
;
355 public function views() {
356 $this->mViews
= $this->db
->selectField( 'page', 'SUM(page_counter)', '', __METHOD__
);
357 return $this->mViews
;
364 public function files() {
365 $this->mFiles
= $this->db
->selectField( 'image', 'COUNT(*)', '', __METHOD__
);
366 return $this->mFiles
;
370 * Do all updates and commit them. More or less a replacement
371 * for the original initStats, but without output.
373 * @param DatabaseBase|bool $database
374 * - Boolean: whether to use the master DB
375 * - DatabaseBase: database connection to use
376 * @param array $options Array of options, may contain the following values
377 * - views Boolean: when true, do not update the number of page views (default: true)
378 * - activeUsers Boolean: whether to update the number of active users (default: false)
380 public static function doAllAndCommit( $database, array $options = array() ) {
381 $options +
= array( 'update' => false, 'views' => true, 'activeUsers' => false );
383 // Grab the object and count everything
384 $counter = new SiteStatsInit( $database );
387 $counter->articles();
392 // Only do views if we don't want to not count them
393 if ( $options['views'] ) {
399 // Count active users if need be
400 if ( $options['activeUsers'] ) {
401 SiteStatsUpdate
::cacheUpdate( wfGetDB( DB_MASTER
) );
406 * Refresh site_stats. If you want ss_total_views to be updated, be sure to
407 * call views() first.
409 public function refresh() {
412 'ss_total_edits' => ( $this->mEdits
=== null ?
$this->edits() : $this->mEdits
),
413 'ss_good_articles' => ( $this->mArticles
=== null ?
$this->articles() : $this->mArticles
),
414 'ss_total_pages' => ( $this->mPages
=== null ?
$this->pages() : $this->mPages
),
415 'ss_users' => ( $this->mUsers
=== null ?
$this->users() : $this->mUsers
),
416 'ss_images' => ( $this->mFiles
=== null ?
$this->files() : $this->mFiles
),
418 $this->mViews ?
array( 'ss_total_views' => $this->mViews
) : array()
421 $dbw = wfGetDB( DB_MASTER
);
422 $dbw->upsert( 'site_stats', $values, array( 'ss_row_id' ), $values, __METHOD__
);