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
This commit is contained in:
Umherirrender 2023-02-04 22:37:40 +01:00
parent e6f4dc96fe
commit 2b44fbe3eb
2 changed files with 18 additions and 24 deletions

View file

@ -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
*/

View file

@ -1,5 +1,8 @@
<?php
/**
* @group Database
*/
class SiteStatsTest extends MediaWikiIntegrationTestCase {
/**
@ -32,4 +35,14 @@ class SiteStatsTest extends MediaWikiIntegrationTestCase {
$this->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__ ) );
}
}