From 2b44fbe3eb457a28c454254fd9ebaee584267d54 Mon Sep 17 00:00:00 2001 From: Umherirrender Date: Sat, 4 Feb 2023 22:37:40 +0100 Subject: [PATCH] SiteStats: Remove unneeded SiteStats::salvageIncorrectRow The function SiteStats::salvageIncorrectRow is a noop, the variable $map is not returned, instead the unmodified $row is returned, function added in 6535091 self::doLoadFromDB on second attempt after the initialize (via doPlaceholderInit or doAllAndCommit) returns a valid row, making this extra check no longer needed (possible since 27c76fa). Fix doLoadFromDB on first attempt for empty table, broken since 5156ae0 Change-Id: Iec2b4bbdb2ca546eb377a6d12106d62b5219886f --- includes/SiteStats.php | 29 ++++-------------------- tests/phpunit/includes/SiteStatsTest.php | 13 +++++++++++ 2 files changed, 18 insertions(+), 24 deletions(-) diff --git a/includes/SiteStats.php b/includes/SiteStats.php index 04d50b6b164..813d820a811 100644 --- a/includes/SiteStats.php +++ b/includes/SiteStats.php @@ -80,12 +80,6 @@ class SiteStats { $row = self::doLoadFromDB( $lb->getConnectionRef( DB_PRIMARY ) ); } - if ( !self::isRowSensible( $row ) ) { - wfDebug( __METHOD__ . ": site_stats persistently nonsensical o_O" ); - // Always return a row-like object - $row = self::salvageIncorrectRow( $row ); - } - return $row; } @@ -238,7 +232,7 @@ class SiteStats { /** * @param IDatabase $db - * @return stdClass + * @return stdClass|false */ private static function doLoadFromDB( IDatabase $db ) { $fields = self::selectFields(); @@ -247,6 +241,9 @@ class SiteStats { ->from( 'site_stats' ) ->caller( __METHOD__ ) ->fetchResultSet(); + if ( !$rows->numRows() ) { + return false; + } $finalRow = new stdClass(); foreach ( $rows as $row ) { foreach ( $fields as $field ) { @@ -265,7 +262,7 @@ class SiteStats { * * Checks only fields which are filled by SiteStatsInit::refresh. * - * @param bool|stdClass $row + * @param stdClass|false $row * @return bool */ private static function isRowSensible( $row ) { @@ -291,22 +288,6 @@ class SiteStats { return true; } - /** - * @param stdClass|bool $row - * @return stdClass - */ - private static function salvageIncorrectRow( $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 ILoadBalancer */ diff --git a/tests/phpunit/includes/SiteStatsTest.php b/tests/phpunit/includes/SiteStatsTest.php index ecadee1343f..8aab28da0f0 100644 --- a/tests/phpunit/includes/SiteStatsTest.php +++ b/tests/phpunit/includes/SiteStatsTest.php @@ -1,5 +1,8 @@ assertSame( 0, SiteStats::jobs() ); } + /** + * @covers SiteStats + */ + public function testInit() { + $this->db->delete( 'site_stats', IDatabase::ALL_ROWS, __METHOD__ ); + SiteStats::unload(); + + SiteStats::edits(); + $this->assertNotFalse( $this->db->selectRow( 'site_stats', '1', IDatabase::ALL_ROWS, __METHOD__ ) ); + } }