X-Git-Url: https://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=blobdiff_plain;f=includes%2FSiteStats.php;h=cf3a1ebdf7adfbb60e59837af84174996bd0d037;hp=7b2b8d38e26c21831184418ca560a618011852b2;hb=7afced64454ad30d688540f7626448ac2faefebb;hpb=595a108b36b0ee96e0a9b6719024670362b7a839 diff --git a/includes/SiteStats.php b/includes/SiteStats.php index 7b2b8d38e2..cf3a1ebdf7 100644 --- a/includes/SiteStats.php +++ b/includes/SiteStats.php @@ -52,17 +52,17 @@ class SiteStats { $config = MediaWikiServices::getInstance()->getMainConfig(); $lb = self::getLB(); - $dbr = $lb->getConnection( DB_REPLICA ); + $dbr = $lb->getConnectionRef( DB_REPLICA ); wfDebug( __METHOD__ . ": reading site_stats from replica DB\n" ); $row = self::doLoadFromDB( $dbr ); - if ( !self::isSane( $row ) && $lb->hasOrMadeRecentMasterChanges() ) { + if ( !self::isRowSane( $row ) && $lb->hasOrMadeRecentMasterChanges() ) { // Might have just been initialized during this request? Underflow? wfDebug( __METHOD__ . ": site_stats damaged or missing on replica DB\n" ); - $row = self::doLoadFromDB( $lb->getConnection( DB_MASTER ) ); + $row = self::doLoadFromDB( $lb->getConnectionRef( DB_MASTER ) ); } - if ( !self::isSane( $row ) ) { + if ( !self::isRowSane( $row ) ) { if ( $config->get( 'MiserMode' ) ) { // Start off with all zeroes, assuming that this is a new wiki or any // repopulations where done manually via script. @@ -76,38 +76,25 @@ class SiteStats { SiteStatsInit::doAllAndCommit( $dbr ); } - $row = self::doLoadFromDB( $lb->getConnection( DB_MASTER ) ); + $row = self::doLoadFromDB( $lb->getConnectionRef( DB_MASTER ) ); } - if ( !self::isSane( $row ) ) { + if ( !self::isRowSane( $row ) ) { wfDebug( __METHOD__ . ": site_stats persistently nonsensical o_O\n" ); // Always return a row-like object - $row = (object)array_fill_keys( self::selectFields(), 0 ); + $row = self::salvageInsaneRow( $row ); } return $row; } - /** - * @param IDatabase $db - * @return stdClass|bool - */ - private static function doLoadFromDB( IDatabase $db ) { - return $db->selectRow( - 'site_stats', - self::selectFields(), - [ 'ss_row_id' => 1 ], - __METHOD__ - ); - } - /** * @return int */ public static function edits() { self::load(); - return self::$row->ss_total_edits; + return (int)self::$row->ss_total_edits; } /** @@ -116,7 +103,7 @@ class SiteStats { public static function articles() { self::load(); - return self::$row->ss_good_articles; + return (int)self::$row->ss_good_articles; } /** @@ -125,7 +112,7 @@ class SiteStats { public static function pages() { self::load(); - return self::$row->ss_total_pages; + return (int)self::$row->ss_total_pages; } /** @@ -134,7 +121,7 @@ class SiteStats { public static function users() { self::load(); - return self::$row->ss_users; + return (int)self::$row->ss_users; } /** @@ -143,7 +130,7 @@ class SiteStats { public static function activeUsers() { self::load(); - return self::$row->ss_active_users; + return (int)self::$row->ss_active_users; } /** @@ -152,7 +139,7 @@ class SiteStats { public static function images() { self::load(); - return self::$row->ss_images; + return (int)self::$row->ss_images; } /** @@ -162,12 +149,13 @@ class SiteStats { */ public static function numberingroup( $group ) { $cache = MediaWikiServices::getInstance()->getMainWANObjectCache(); + $fname = __METHOD__; return $cache->getWithSetCallback( $cache->makeKey( 'SiteStats', 'groupcounts', $group ), $cache::TTL_HOUR, - function ( $oldValue, &$ttl, array &$setOpts ) use ( $group ) { - $dbr = self::getLB()->getConnection( DB_REPLICA ); + function ( $oldValue, &$ttl, array &$setOpts ) use ( $group, $fname ) { + $dbr = self::getLB()->getConnectionRef( DB_REPLICA ); $setOpts += Database::getCacheSetOptions( $dbr ); return (int)$dbr->selectField( @@ -177,7 +165,7 @@ class SiteStats { 'ug_group' => $group, 'ug_expiry IS NULL OR ug_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() ) ], - __METHOD__ + $fname ); }, [ 'pcTTL' => $cache::TTL_PROC_LONG ] @@ -212,19 +200,20 @@ class SiteStats { */ public static function pagesInNs( $ns ) { $cache = MediaWikiServices::getInstance()->getMainWANObjectCache(); + $fname = __METHOD__; return $cache->getWithSetCallback( $cache->makeKey( 'SiteStats', 'page-in-namespace', $ns ), $cache::TTL_HOUR, - function ( $oldValue, &$ttl, array &$setOpts ) use ( $ns ) { - $dbr = self::getLB()->getConnection( DB_REPLICA ); + function ( $oldValue, &$ttl, array &$setOpts ) use ( $ns, $fname ) { + $dbr = self::getLB()->getConnectionRef( DB_REPLICA ); $setOpts += Database::getCacheSetOptions( $dbr ); return (int)$dbr->selectField( 'page', 'COUNT(*)', [ 'page_namespace' => $ns ], - __METHOD__ + $fname ); }, [ 'pcTTL' => $cache::TTL_PROC_LONG ] @@ -245,6 +234,19 @@ class SiteStats { ]; } + /** + * @param IDatabase $db + * @return stdClass|bool + */ + private static function doLoadFromDB( IDatabase $db ) { + return $db->selectRow( + 'site_stats', + self::selectFields(), + [ 'ss_row_id' => 1 ], + __METHOD__ + ); + } + /** * Is the provided row of site stats sane, or should it be regenerated? * @@ -253,7 +255,7 @@ class SiteStats { * @param bool|object $row * @return bool */ - private static function isSane( $row ) { + private static function isRowSane( $row ) { if ( $row === false || $row->ss_total_pages < $row->ss_good_articles || $row->ss_total_edits < $row->ss_total_pages @@ -268,7 +270,7 @@ class SiteStats { 'ss_users', 'ss_images', ] as $member ) { - if ( $row->$member > 2000000000 || $row->$member < 0 ) { + if ( $row->$member < 0 ) { return false; } } @@ -276,6 +278,22 @@ class SiteStats { return true; } + /** + * @param stdClass|bool $row + * @return stdClass + */ + private static function salvageInsaneRow( $row ) { + $map = $row ? (array)$row : []; + // Fill in any missing values with zero + $map += array_fill_keys( self::selectFields(), 0 ); + // Convert negative values to zero + foreach ( $map as $field => $value ) { + $map[$field] = max( 0, $value ); + } + + return (object)$row; + } + /** * @return LoadBalancer */