From 93be12f54d05bd45550cfa82ad3bb5afa3330ced Mon Sep 17 00:00:00 2001 From: daniel Date: Thu, 24 Jan 2013 15:38:52 +0100 Subject: [PATCH] Add a method to clear the site list programmatically. Change-Id: Iecee9c4d52676f95845517a5b5dbcb500ea66cc4 --- includes/site/SiteSQLStore.php | 28 +++++++++++++++++++ includes/site/SiteStore.php | 7 ++++- .../includes/site/SiteSQLStoreTest.php | 28 +++++++++++++------ tests/phpunit/includes/site/TestSites.php | 16 +---------- 4 files changed, 55 insertions(+), 24 deletions(-) diff --git a/includes/site/SiteSQLStore.php b/includes/site/SiteSQLStore.php index 587c629501..470fe261a6 100644 --- a/includes/site/SiteSQLStore.php +++ b/includes/site/SiteSQLStore.php @@ -339,6 +339,34 @@ class SiteSQLStore implements SiteStore { $this->sites = null; } + /** + * Clears the list of sites stored in the database. + * + * @see SiteStore::clear() + * + * @return bool success + */ + public function clear() { + $dbw = $this->sitesTable->getWriteDbConnection(); + + $trx = $dbw->trxLevel(); + + if ( $trx == 0 ) { + $dbw->begin( __METHOD__ ); + } + + $ok = $dbw->delete( 'sites', '*', __METHOD__ ); + $ok = $dbw->delete( 'site_identifiers', '*', __METHOD__ ) && $ok; + + if ( $trx == 0 ) { + $dbw->commit( __METHOD__ ); + } + + $this->reset(); + + return $ok; + } + /** * @since 1.21 * diff --git a/includes/site/SiteStore.php b/includes/site/SiteStore.php index 0d8dcdf6bd..52ba8fbffe 100644 --- a/includes/site/SiteStore.php +++ b/includes/site/SiteStore.php @@ -77,4 +77,9 @@ interface SiteStore { */ public function getSites( $source = 'cache' ); -} \ No newline at end of file + /** + * Deletes all sites from the database. After calling clear(), getSites() will return an empty + * list and getSite() will return null until saveSite() or saveSites() is called. + */ + public function clear(); +} diff --git a/tests/phpunit/includes/site/SiteSQLStoreTest.php b/tests/phpunit/includes/site/SiteSQLStoreTest.php index 4f7889bff1..5b66d29b35 100644 --- a/tests/phpunit/includes/site/SiteSQLStoreTest.php +++ b/tests/phpunit/includes/site/SiteSQLStoreTest.php @@ -83,25 +83,37 @@ class SiteSQLStoreTest extends MediaWikiTestCase { } public function testReset() { - $store = SiteSQLStore::newInstance(); + $store1 = SiteSQLStore::newInstance(); + $store2 = SiteSQLStore::newInstance(); // initialize internal cache - $this->assertGreaterThan( 0, $store->getSites()->count() ); + $this->assertGreaterThan( 0, $store1->getSites()->count() ); + $this->assertGreaterThan( 0, $store2->getSites()->count() ); - // Clear actual data. Will not purge the internal cache in store2. - $dbw = wfGetDB( DB_MASTER ); - $dbw->delete( 'sites', '*', __METHOD__ ); - $dbw->delete( 'site_identifiers', '*', __METHOD__ ); + // Clear actual data. Will purge the external cache and reset the internal + // cache in $store1, but not the internal cache in store2. + $this->assertTrue( $store1->clear() ); // sanity check: $store2 should have a stale cache now - $this->assertNotNull( $store->getSite( 'enwiki' ) ); + $this->assertNotNull( $store2->getSite( 'enwiki' ) ); // purge cache - $store->reset(); + $store2->reset(); // ...now the internal cache of $store2 should be updated and thus empty. + $site = $store2->getSite( 'enwiki' ); + $this->assertNull( $site ); + } + + public function testClear() { + $store = SiteSQLStore::newInstance(); + $this->assertTrue( $store->clear() ); + $site = $store->getSite( 'enwiki' ); $this->assertNull( $site ); + + $sites = $store->getSites(); + $this->assertEquals( 0, $sites->count() ); } } diff --git a/tests/phpunit/includes/site/TestSites.php b/tests/phpunit/includes/site/TestSites.php index d9861cda91..b57fe9f76e 100644 --- a/tests/phpunit/includes/site/TestSites.php +++ b/tests/phpunit/includes/site/TestSites.php @@ -93,23 +93,9 @@ class TestSites { * @since 0.1 */ public static function insertIntoDb() { - $dbw = wfGetDB( DB_MASTER ); - - $trx = $dbw->trxLevel(); - - if ( $trx == 0 ) { - $dbw->begin( __METHOD__ ); - } - - $dbw->delete( 'sites', '*', __METHOD__ ); - $dbw->delete( 'site_identifiers', '*', __METHOD__ ); - $sitesTable = SiteSQLStore::newInstance(); + $sitesTable->clear(); $sitesTable->saveSites( TestSites::getSites() ); - - if ( $trx == 0 ) { - $dbw->commit( __METHOD__ ); - } } } \ No newline at end of file -- 2.20.1