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 static $row, $loaded = false;
29 static $pageCount = array();
30 static $groupMemberCounts = array();
32 static function recache() {
37 * @param bool $recache
39 static function load( $recache = false ) {
40 if ( self
::$loaded && !$recache ) {
44 self
::$row = self
::loadAndLazyInit();
46 # This code is somewhat schema-agnostic, because I'm changing it in a minor release -- TS
47 if ( !isset( self
::$row->ss_total_pages
) && self
::$row->ss_total_pages
== -1 ) {
49 $u = new SiteStatsUpdate( 0, 0, 0 );
51 self
::$row = self
::doLoad( wfGetDB( DB_SLAVE
) );
58 * @return bool|ResultWrapper
60 static function loadAndLazyInit() {
61 wfDebug( __METHOD__
. ": reading site_stats from slave\n" );
62 $row = self
::doLoad( wfGetDB( DB_SLAVE
) );
64 if ( !self
::isSane( $row ) ) {
65 // Might have just been initialized during this request? Underflow?
66 wfDebug( __METHOD__
. ": site_stats damaged or missing on slave\n" );
67 $row = self
::doLoad( wfGetDB( DB_MASTER
) );
70 if ( !self
::isSane( $row ) ) {
71 // Normally the site_stats table is initialized at install time.
72 // Some manual construction scenarios may leave the table empty or
73 // broken, however, for instance when importing from a dump into a
74 // clean schema with mwdumper.
75 wfDebug( __METHOD__
. ": initializing damaged or missing site_stats\n" );
77 SiteStatsInit
::doAllAndCommit( wfGetDB( DB_SLAVE
) );
79 $row = self
::doLoad( wfGetDB( DB_MASTER
) );
82 if ( !self
::isSane( $row ) ) {
83 wfDebug( __METHOD__
. ": site_stats persistently nonsensical o_O\n" );
89 * @param DatabaseBase $db
90 * @return bool|ResultWrapper
92 static function doLoad( $db ) {
93 return $db->selectRow( 'site_stats', array(
102 ), false, __METHOD__
);
108 static function views() {
110 return self
::$row->ss_total_views
;
116 static function edits() {
118 return self
::$row->ss_total_edits
;
124 static function articles() {
126 return self
::$row->ss_good_articles
;
132 static function pages() {
134 return self
::$row->ss_total_pages
;
140 static function users() {
142 return self
::$row->ss_users
;
148 static function activeUsers() {
150 return self
::$row->ss_active_users
;
156 static function images() {
158 return self
::$row->ss_images
;
162 * Find the number of users in a given user group.
163 * @param string $group name of group
166 static function numberingroup( $group ) {
167 if ( !isset( self
::$groupMemberCounts[$group] ) ) {
169 $key = wfMemcKey( 'SiteStats', 'groupcounts', $group );
170 $hit = $wgMemc->get( $key );
172 $dbr = wfGetDB( DB_SLAVE
);
173 $hit = $dbr->selectField(
176 array( 'ug_group' => $group ),
179 $wgMemc->set( $key, $hit, 3600 );
181 self
::$groupMemberCounts[$group] = $hit;
183 return self
::$groupMemberCounts[$group];
189 static function jobs() {
190 if ( !isset( self
::$jobs ) ) {
191 $dbr = wfGetDB( DB_SLAVE
);
192 self
::$jobs = array_sum( JobQueueGroup
::singleton()->getQueueSizes() );
193 /* Zero rows still do single row read for row that doesn't exist, but people are annoyed by that */
194 if ( self
::$jobs == 1 ) {
206 static function pagesInNs( $ns ) {
207 wfProfileIn( __METHOD__
);
208 if ( !isset( self
::$pageCount[$ns] ) ) {
209 $dbr = wfGetDB( DB_SLAVE
);
210 self
::$pageCount[$ns] = (int)$dbr->selectField(
213 array( 'page_namespace' => $ns ),
217 wfProfileOut( __METHOD__
);
218 return self
::$pageCount[$ns];
222 * Is the provided row of site stats sane, or should it be regenerated?
224 * Checks only fields which are filled by SiteStatsInit::refresh.
226 * @param bool|object $row
230 private static function isSane( $row ) {
232 ||
$row->ss_total_pages
< $row->ss_good_articles
233 ||
$row->ss_total_edits
< $row->ss_total_pages
237 // Now check for underflow/overflow
246 if ( $row->$member > 2000000000 ||
$row->$member < 0 ) {
255 * Class designed for counting of stats.
257 class SiteStatsInit
{
259 // Database connection
263 private $mEdits, $mArticles, $mPages, $mUsers, $mViews, $mFiles = 0;
267 * @param bool|DatabaseBase $database
268 * - Boolean: whether to use the master DB
269 * - DatabaseBase: database connection to use
271 public function __construct( $database = false ) {
272 if ( $database instanceof DatabaseBase
) {
273 $this->db
= $database;
275 $this->db
= wfGetDB( $database ? DB_MASTER
: DB_SLAVE
);
280 * Count the total number of edits
283 public function edits() {
284 $this->mEdits
= $this->db
->selectField( 'revision', 'COUNT(*)', '', __METHOD__
);
285 $this->mEdits +
= $this->db
->selectField( 'archive', 'COUNT(*)', '', __METHOD__
);
286 return $this->mEdits
;
290 * Count pages in article space(s)
293 public function articles() {
294 global $wgArticleCountMethod;
296 $tables = array( 'page' );
298 'page_namespace' => MWNamespace
::getContentNamespaces(),
299 'page_is_redirect' => 0,
302 if ( $wgArticleCountMethod == 'link' ) {
303 $tables[] = 'pagelinks';
304 $conds[] = 'pl_from=page_id';
305 } elseif ( $wgArticleCountMethod == 'comma' ) {
306 // To make a correct check for this, we would need, for each page,
307 // to load the text, maybe uncompress it, maybe decode it and then
308 // check if there's one comma.
309 // But one thing we are sure is that if the page is empty, it can't
310 // contain a comma :)
311 $conds[] = 'page_len > 0';
314 $this->mArticles
= $this->db
->selectField( $tables, 'COUNT(DISTINCT page_id)',
315 $conds, __METHOD__
);
316 return $this->mArticles
;
323 public function pages() {
324 $this->mPages
= $this->db
->selectField( 'page', 'COUNT(*)', '', __METHOD__
);
325 return $this->mPages
;
332 public function users() {
333 $this->mUsers
= $this->db
->selectField( 'user', 'COUNT(*)', '', __METHOD__
);
334 return $this->mUsers
;
341 public function views() {
342 $this->mViews
= $this->db
->selectField( 'page', 'SUM(page_counter)', '', __METHOD__
);
343 return $this->mViews
;
350 public function files() {
351 $this->mFiles
= $this->db
->selectField( 'image', 'COUNT(*)', '', __METHOD__
);
352 return $this->mFiles
;
356 * Do all updates and commit them. More or less a replacement
357 * for the original initStats, but without output.
359 * @param DatabaseBase|bool $database
360 * - Boolean: whether to use the master DB
361 * - DatabaseBase: database connection to use
362 * @param array $options of options, may contain the following values
363 * - views Boolean: when true, do not update the number of page views (default: true)
364 * - activeUsers Boolean: whether to update the number of active users (default: false)
366 public static function doAllAndCommit( $database, array $options = array() ) {
367 $options +
= array( 'update' => false, 'views' => true, 'activeUsers' => false );
369 // Grab the object and count everything
370 $counter = new SiteStatsInit( $database );
373 $counter->articles();
378 // Only do views if we don't want to not count them
379 if ( $options['views'] ) {
385 // Count active users if need be
386 if ( $options['activeUsers'] ) {
387 SiteStatsUpdate
::cacheUpdate( wfGetDB( DB_MASTER
) );
392 * Refresh site_stats.
394 protected function refresh() {
397 'ss_total_edits' => $this->mEdits
,
398 'ss_good_articles' => $this->mArticles
,
399 'ss_total_pages' => $this->mPages
,
400 'ss_users' => $this->mUsers
,
401 'ss_images' => $this->mFiles
,
403 $this->mViews ?
array( 'ss_total_views' => $this->mViews
) : array()
406 $dbw = wfGetDB( DB_MASTER
);
407 $dbw->upsert( 'site_stats', $values, array( 'ss_row_id' ), $values, __METHOD__
);