X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2FSiteStats.php;h=580f7ccb19aa6c513a64b7401891bf62547c99fe;hb=e3bd13db0c285f312e31bb1b7271af4628cca80c;hp=15c18f359b0433cf6df334129595a4010f81e0b6;hpb=1a115f50040d1d3d2b72ca4b86c824d47fd23d5b;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/SiteStats.php b/includes/SiteStats.php index 15c18f359b..580f7ccb19 100644 --- a/includes/SiteStats.php +++ b/includes/SiteStats.php @@ -34,10 +34,10 @@ class SiteStats { private static $jobs; /** @var int[] */ - private static $pageCount = array(); + private static $pageCount = []; /** @var int[] */ - private static $groupMemberCounts = array(); + private static $groupMemberCounts = []; static function recache() { self::load( true ); @@ -68,6 +68,8 @@ class SiteStats { * @return bool|ResultWrapper */ static function loadAndLazyInit() { + global $wgMiserMode; + wfDebug( __METHOD__ . ": reading site_stats from slave\n" ); $row = self::doLoad( wfGetDB( DB_SLAVE ) ); @@ -77,7 +79,7 @@ class SiteStats { $row = self::doLoad( wfGetDB( DB_MASTER ) ); } - if ( !self::isSane( $row ) ) { + if ( !$wgMiserMode && !self::isSane( $row ) ) { // Normally the site_stats table is initialized at install time. // Some manual construction scenarios may leave the table empty or // broken, however, for instance when importing from a dump into a @@ -96,11 +98,11 @@ class SiteStats { } /** - * @param DatabaseBase $db + * @param IDatabase $db * @return bool|ResultWrapper */ static function doLoad( $db ) { - return $db->selectRow( 'site_stats', array( + return $db->selectRow( 'site_stats', [ 'ss_row_id', 'ss_total_edits', 'ss_good_articles', @@ -108,7 +110,7 @@ class SiteStats { 'ss_users', 'ss_active_users', 'ss_images', - ), false, __METHOD__ ); + ], false, __METHOD__ ); } /** @@ -178,23 +180,24 @@ class SiteStats { * @return int */ static function numberingroup( $group ) { - if ( !isset( self::$groupMemberCounts[$group] ) ) { - global $wgMemc; - $key = wfMemcKey( 'SiteStats', 'groupcounts', $group ); - $hit = $wgMemc->get( $key ); - if ( !$hit ) { + $cache = ObjectCache::getMainWANInstance(); + return $cache->getWithSetCallback( + wfMemcKey( 'SiteStats', 'groupcounts', $group ), + $cache::TTL_HOUR, + function ( $oldValue, &$ttl, array &$setOpts ) use ( $group ) { $dbr = wfGetDB( DB_SLAVE ); - $hit = $dbr->selectField( + + $setOpts += Database::getCacheSetOptions( $dbr ); + + return $dbr->selectField( 'user_groups', 'COUNT(*)', - array( 'ug_group' => $group ), + [ 'ug_group' => $group ], __METHOD__ ); - $wgMemc->set( $key, $hit, 3600 ); - } - self::$groupMemberCounts[$group] = $hit; - } - return self::$groupMemberCounts[$group]; + }, + [ 'pcTTL' => 10 ] + ); } /** @@ -226,7 +229,7 @@ class SiteStats { self::$pageCount[$ns] = (int)$dbr->selectField( 'page', 'COUNT(*)', - array( 'page_namespace' => $ns ), + [ 'page_namespace' => $ns ], __METHOD__ ); } @@ -250,13 +253,13 @@ class SiteStats { return false; } // Now check for underflow/overflow - foreach ( array( + foreach ( [ 'ss_total_edits', 'ss_good_articles', 'ss_total_pages', 'ss_users', 'ss_images', - ) as $member ) { + ] as $member ) { if ( $row->$member > 2000000000 || $row->$member < 0 ) { return false; } @@ -279,12 +282,12 @@ class SiteStatsInit { /** * Constructor - * @param bool|DatabaseBase $database - * - Boolean: whether to use the master DB - * - DatabaseBase: database connection to use + * @param bool|IDatabase $database + * - boolean: Whether to use the master DB + * - IDatabase: Database connection to use */ public function __construct( $database = false ) { - if ( $database instanceof DatabaseBase ) { + if ( $database instanceof IDatabase ) { $this->db = $database; } else { $this->db = wfGetDB( $database ? DB_MASTER : DB_SLAVE ); @@ -308,11 +311,11 @@ class SiteStatsInit { public function articles() { global $wgArticleCountMethod; - $tables = array( 'page' ); - $conds = array( + $tables = [ 'page' ]; + $conds = [ 'page_namespace' => MWNamespace::getContentNamespaces(), 'page_is_redirect' => 0, - ); + ]; if ( $wgArticleCountMethod == 'link' ) { $tables[] = 'pagelinks'; @@ -362,14 +365,14 @@ class SiteStatsInit { * Do all updates and commit them. More or less a replacement * for the original initStats, but without output. * - * @param DatabaseBase|bool $database - * - Boolean: whether to use the master DB - * - DatabaseBase: database connection to use + * @param IDatabase|bool $database + * - boolean: Whether to use the master DB + * - IDatabase: Database connection to use * @param array $options Array of options, may contain the following values - * - activeUsers Boolean: whether to update the number of active users (default: false) + * - activeUsers boolean: Whether to update the number of active users (default: false) */ - public static function doAllAndCommit( $database, array $options = array() ) { - $options += array( 'update' => false, 'activeUsers' => false ); + public static function doAllAndCommit( $database, array $options = [] ) { + $options += [ 'update' => false, 'activeUsers' => false ]; // Grab the object and count everything $counter = new SiteStatsInit( $database ); @@ -392,16 +395,16 @@ class SiteStatsInit { * Refresh site_stats */ public function refresh() { - $values = array( + $values = [ 'ss_row_id' => 1, 'ss_total_edits' => ( $this->mEdits === null ? $this->edits() : $this->mEdits ), 'ss_good_articles' => ( $this->mArticles === null ? $this->articles() : $this->mArticles ), 'ss_total_pages' => ( $this->mPages === null ? $this->pages() : $this->mPages ), 'ss_users' => ( $this->mUsers === null ? $this->users() : $this->mUsers ), 'ss_images' => ( $this->mFiles === null ? $this->files() : $this->mFiles ), - ); + ]; $dbw = wfGetDB( DB_MASTER ); - $dbw->upsert( 'site_stats', $values, array( 'ss_row_id' ), $values, __METHOD__ ); + $dbw->upsert( 'site_stats', $values, [ 'ss_row_id' ], $values, __METHOD__ ); } }