Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PLATFORM-8076 | SiteStats #81

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 5 additions & 24 deletions includes/SiteStats.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,6 @@ protected static function loadAndLazyInit() {
$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;
}

Expand Down Expand Up @@ -238,7 +232,7 @@ public static function selectFields() {

/**
* @param IDatabase $db
* @return stdClass
* @return stdClass|false
*/
private static function doLoadFromDB( IDatabase $db ) {
$fields = self::selectFields();
Expand All @@ -247,6 +241,9 @@ private static function doLoadFromDB( IDatabase $db ) {
->from( 'site_stats' )
->caller( __METHOD__ )
->fetchResultSet();
if ( !$rows->numRows() ) {
return false;
}
$finalRow = new stdClass();
foreach ( $rows as $row ) {
foreach ( $fields as $field ) {
Expand All @@ -265,7 +262,7 @@ private static function doLoadFromDB( IDatabase $db ) {
*
* Checks only fields which are filled by SiteStatsInit::refresh.
*
* @param bool|stdClass $row
* @param stdClass|false $row
* @return bool
*/
private static function isRowSensible( $row ) {
Expand All @@ -291,22 +288,6 @@ private static function isRowSensible( $row ) {
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
*/
Expand Down
13 changes: 13 additions & 0 deletions tests/phpunit/includes/SiteStatsTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

/**
* @group Database
*/
class SiteStatsTest extends MediaWikiIntegrationTestCase {

/**
Expand Down Expand Up @@ -32,4 +35,14 @@ public function testJobsCountGetCached() {
$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__ ) );
}
}