2018-02-28 02:01:02 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
use Wikimedia\TestingAccessWrapper;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @group Database
|
|
|
|
|
*/
|
2020-06-30 15:09:24 +00:00
|
|
|
class SiteStatsUpdateTest extends MediaWikiIntegrationTestCase {
|
2018-02-28 02:01:02 +00:00
|
|
|
/**
|
|
|
|
|
* @covers SiteStatsUpdate::factory
|
|
|
|
|
* @covers SiteStatsUpdate::merge
|
|
|
|
|
*/
|
|
|
|
|
public function testFactoryAndMerge() {
|
|
|
|
|
$update1 = SiteStatsUpdate::factory( [ 'pages' => 1, 'users' => 2 ] );
|
|
|
|
|
$update2 = SiteStatsUpdate::factory( [ 'users' => 1, 'images' => 1 ] );
|
|
|
|
|
|
|
|
|
|
$update1->merge( $update2 );
|
|
|
|
|
$wrapped = TestingAccessWrapper::newFromObject( $update1 );
|
|
|
|
|
|
2020-05-30 10:36:42 +00:00
|
|
|
$this->assertSame( 1, $wrapped->pages );
|
2018-02-28 02:01:02 +00:00
|
|
|
$this->assertEquals( 3, $wrapped->users );
|
2020-05-30 10:36:42 +00:00
|
|
|
$this->assertSame( 1, $wrapped->images );
|
2019-09-17 14:31:49 +00:00
|
|
|
$this->assertSame( 0, $wrapped->edits );
|
|
|
|
|
$this->assertSame( 0, $wrapped->articles );
|
2018-02-28 02:01:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers SiteStatsUpdate::doUpdate()
|
|
|
|
|
* @covers SiteStatsInit::refresh()
|
|
|
|
|
*/
|
|
|
|
|
public function testDoUpdate() {
|
|
|
|
|
$this->setMwGlobals( 'wgSiteStatsAsyncFactor', false );
|
|
|
|
|
|
2021-04-29 02:37:11 +00:00
|
|
|
$dbw = wfGetDB( DB_PRIMARY );
|
2018-02-28 02:01:02 +00:00
|
|
|
$statsInit = new SiteStatsInit( $dbw );
|
|
|
|
|
$statsInit->refresh();
|
|
|
|
|
|
|
|
|
|
$ei = SiteStats::edits(); // trigger load
|
|
|
|
|
$pi = SiteStats::pages();
|
|
|
|
|
$ui = SiteStats::users();
|
|
|
|
|
$fi = SiteStats::images();
|
|
|
|
|
$ai = SiteStats::articles();
|
|
|
|
|
|
2019-09-17 14:31:49 +00:00
|
|
|
$this->assertSame( 0, DeferredUpdates::pendingUpdatesCount() );
|
2019-07-06 20:36:43 +00:00
|
|
|
|
2018-02-28 02:01:02 +00:00
|
|
|
$dbw->begin( __METHOD__ ); // block opportunistic updates
|
|
|
|
|
|
2019-07-06 20:36:43 +00:00
|
|
|
DeferredUpdates::addUpdate(
|
|
|
|
|
SiteStatsUpdate::factory( [ 'pages' => 2, 'images' => 1, 'edits' => 2 ] )
|
|
|
|
|
);
|
2020-05-30 10:36:42 +00:00
|
|
|
$this->assertSame( 1, DeferredUpdates::pendingUpdatesCount() );
|
2018-02-28 02:01:02 +00:00
|
|
|
|
|
|
|
|
// Still the same
|
|
|
|
|
SiteStats::unload();
|
|
|
|
|
$this->assertEquals( $pi, SiteStats::pages(), 'page count' );
|
|
|
|
|
$this->assertEquals( $ei, SiteStats::edits(), 'edit count' );
|
|
|
|
|
$this->assertEquals( $ui, SiteStats::users(), 'user count' );
|
|
|
|
|
$this->assertEquals( $fi, SiteStats::images(), 'file count' );
|
|
|
|
|
$this->assertEquals( $ai, SiteStats::articles(), 'article count' );
|
2020-05-30 10:36:42 +00:00
|
|
|
$this->assertSame( 1, DeferredUpdates::pendingUpdatesCount() );
|
2018-02-28 02:01:02 +00:00
|
|
|
|
rdbms: Move setLBFactoryTriggers from doMaintenance to service wiring
This logic is not needed to run on every PHP process and was making
it difficult to run offline maintenance scripts without additional
complexity based on Maintenance::getDbType and DB_NONE.
Instead of skipping this only for DB_NONE, and establishing a pattern
that may spread to other ad-hoc places throughout the codebase, instead
remove this entirely from the eager set up code for all PHP processes
and move it to the service wiring and dependency injection.
That way, it naturally doesn't happen until and unless the DB service
is actually called upon. Scripts and entry point that need to disable
the DB service, can continue to use
MediaWikiServices::disableStorageBackend.
== Impact on SiteStatsUpdate ==
With wgCommandLineMode no longer being read at run-time from a global,
but in service wiring, this means SiteStatsUpdateTest can't change
the behaviour between CLI-like and Web-like, unless it e.g. resets
the 'DBLoadBalancerFactory' service first. Unfortunately, while most
any reset is supported, a reset of the 'DBLoadBalancerFactory' would
be unsupported as that would lose the temporary db clone context and
such, bringing us to either the developer's live db, or a broken set
up altogether. If there is a strong need for toggling oppertunistic
updates off and on at run-time, this could be supported in the
DeferredUpdates class perhaps, but we already have numerous methods
there (incl db begin/commit being a good proxy already), which this
test already used, so for now I've just removed the extra assertion
for this as it wasn't essential to that test.
Bug: T228895
Bug: T238436
Change-Id: Icf29bc484c155f52b6d8f61e5902233a15ba0c6d
2021-04-15 23:08:44 +00:00
|
|
|
// This also notifies DeferredUpdates to do an opportunistic run
|
2018-02-28 02:01:02 +00:00
|
|
|
$dbw->commit( __METHOD__ );
|
2019-09-17 14:31:49 +00:00
|
|
|
$this->assertSame( 0, DeferredUpdates::pendingUpdatesCount() );
|
2018-02-28 02:01:02 +00:00
|
|
|
|
|
|
|
|
SiteStats::unload();
|
|
|
|
|
$this->assertEquals( $pi + 2, SiteStats::pages(), 'page count' );
|
|
|
|
|
$this->assertEquals( $ei + 2, SiteStats::edits(), 'edit count' );
|
|
|
|
|
$this->assertEquals( $ui, SiteStats::users(), 'user count' );
|
|
|
|
|
$this->assertEquals( $fi + 1, SiteStats::images(), 'file count' );
|
|
|
|
|
$this->assertEquals( $ai, SiteStats::articles(), 'article count' );
|
|
|
|
|
|
|
|
|
|
$statsInit = new SiteStatsInit();
|
|
|
|
|
$statsInit->refresh();
|
|
|
|
|
}
|
|
|
|
|
}
|