Wikia code/includes/SiteStats.php

From mediawiki.org
--- D:\Programming\SVN\mediawiki\branches\REL1_16\phase3\includes\SiteStats.php	2011-07-18 22:31:28.008789100 +0100
+++ D:\Programming\SVN\wikia\trunk\includes\SiteStats.php	2011-08-17 15:28:46.372070300 +0100
@@ -25,7 +25,7 @@
 			# Update schema
 			$u = new SiteStatsUpdate( 0, 0, 0 );
 			$u->doUpdate();
-			$dbr = wfGetDB( DB_SLAVE );
+			$dbr = wfGetDB( DB_SLAVE, 'vslow' );
 			self::$row = $dbr->selectRow( 'site_stats', '*', false, __METHOD__ );
 		}
 
@@ -34,8 +34,9 @@
 
 	static function loadAndLazyInit() {
 		wfDebug( __METHOD__ . ": reading site_stats from slave\n" );
-		$row = self::doLoad( wfGetDB( DB_SLAVE ) );
+		$row = self::doLoad( wfGetDB( DB_SLAVE, 'vslow' ) );
 
+		/*
 		if( !self::isSane( $row ) ) {
 			// Might have just been initialized during this request? Underflow?
 			wfDebug( __METHOD__ . ": site_stats damaged or missing on slave\n" );
@@ -51,12 +52,18 @@
 
 			SiteStatsInit::doAllAndCommit( false );
 
+			ob_start();
+			wfInitStats();
+			wfGetDB( DB_MASTER )->commit();
+			ob_end_clean();
+
 			$row = self::doLoad( wfGetDB( DB_MASTER ) );
 		}
 
 		if( !self::isSane( $row ) ) {
 			wfDebug( __METHOD__ . ": site_stats persistently nonsensical o_O\n" );
 		}
+		*/
 		return $row;
 	}
 
@@ -118,7 +125,7 @@
 			$key = wfMemcKey( 'SiteStats', 'groupcounts', $group );
 			$hit = $wgMemc->get( $key );
 			if ( !$hit ) {
-				$dbr = wfGetDB( DB_SLAVE );
+				$dbr = wfGetDB( DB_SLAVE, 'vslow' );
 				$hit = $dbr->selectField( 'user_groups', 'COUNT(*)',
 					array( 'ug_group' => $group ), __METHOD__ );
 				$wgMemc->set( $key, $hit, 3600 );
@@ -130,8 +137,8 @@
 
 	static function jobs() {
 		if ( !isset( self::$jobs ) ) {
-			$dbr = wfGetDB( DB_SLAVE );
-			self::$jobs = $dbr->estimateRowCount('job');
+			$dbr = wfGetDB( DB_SLAVE, 'vslow' );
+			self::$jobs = $dbr->selectField( 'job', 'COUNT(*)', '', __METHOD__ );
 			/* Zero rows still do single row read for row that doesn't exist, but people are annoyed by that */
 			if (self::$jobs == 1) {
 				self::$jobs = 0;
@@ -143,7 +150,7 @@
 	static function pagesInNs( $ns ) {
 		wfProfileIn( __METHOD__ );
 		if( !isset( self::$pageCount[$ns] ) ) {
-			$dbr = wfGetDB( DB_SLAVE );
+			$dbr = wfGetDB( DB_SLAVE, 'vslow' );
 			$pageCount[$ns] = (int)$dbr->selectField( 'page', 'COUNT(*)', array( 'page_namespace' => $ns ), __METHOD__ );
 		}
 		wfProfileOut( __METHOD__ );
@@ -226,12 +233,20 @@
 	}
 
 	public static function cacheUpdate( $dbw ) {
+		global $wgRCMaxAge;
 		$dbr = wfGetDB( DB_SLAVE, array( 'SpecialStatistics', 'vslow') );
 		# Get non-bot users than did some recent action other than making accounts.
 		# If account creation is included, the number gets inflated ~20+ fold on enwiki.
-		$activeUsers = $dbr->selectField( 'recentchanges', 'COUNT( DISTINCT rc_user_text )',
-			array( 'rc_user != 0', 'rc_bot' => 0, "rc_log_type != 'newusers' OR rc_log_type IS NULL" ),
-			__METHOD__ );
+		$active_users_conds = array(
+			'rc_user != 0',
+			'rc_bot' => 0,
+			"rc_log_type != 'newusers' OR rc_log_type IS NULL"
+		);
+		if ( !empty($wgRCMaxAge) ) {
+			$active_users_conds[] = sprintf("rc_timestamp >= '%s'", date( 'YmdHis', time()- $wgRCMaxAge ));
+		}
+
+		$activeUsers = $dbr->selectField( 'recentchanges', 'COUNT( DISTINCT rc_user_text )', $active_users_conds, __METHOD__ );
 		$dbw->update( 'site_stats',
 			array( 'ss_active_users' => intval($activeUsers) ),
 			array( 'ss_row_id' => 1 ), __METHOD__
@@ -246,7 +261,7 @@
 class SiteStatsInit {
 
 	// Db connection
-	private $db;
+	private $db, $dbshared;
 
 	// Various stats
 	private $mEdits, $mArticles, $mPages, $mUsers, $mViews, $mFiles = 0;
@@ -256,7 +271,9 @@
 	 * @param $useMaster bool Whether to use the master db
 	 */
 	public function __construct( $useMaster = false ) {
-		$this->db = wfGetDB( $useMaster ? DB_MASTER : DB_SLAVE );
+		global $wgExternalSharedDB;
+		$this->db = wfGetDB( $useMaster ? DB_MASTER : DB_SLAVE, 'vslow' );
+		$this->dbshared = wfGetDB( $useMaster ? DB_MASTER : DB_SLAVE, 'stats', $wgExternalSharedDB );
 	}
 
 	/**
@@ -293,7 +310,17 @@
 	 * @return int
 	 */
 	public function users() {
-		$this->mUsers = $this->db->selectField( 'user', 'COUNT(*)', '', __METHOD__ );
+		/**
+		 * wikia change
+		 * cache number of users for 12hours
+		 * it's not important to have exact numbers there
+		 */
+		$cache = WF::build( "App" )->getGlobal( "wgMemc" );
+		$this->mUsers = $cache->get( wfSharedMemcKey( "registered-users-number" ) );
+		if( empty( $this->mUsers ) ) {
+			$this->mUsers = $this->dbshared->selectField( '`user`', 'COUNT(*)', '', __METHOD__ );
+			$cache->set( wfSharedMemcKey( "registered-users-number" ), $this->mUsers, 60*60*12 );
+		}
 		return $this->mUsers;
 	}