Add a method to clear the site list programmatically.
authordaniel <daniel.kinzler@wikimedia.de>
Thu, 24 Jan 2013 14:38:52 +0000 (15:38 +0100)
committerGerrit Code Review <gerrit@wikimedia.org>
Thu, 24 Jan 2013 18:18:59 +0000 (18:18 +0000)
Change-Id: Iecee9c4d52676f95845517a5b5dbcb500ea66cc4

includes/site/SiteSQLStore.php
includes/site/SiteStore.php
tests/phpunit/includes/site/SiteSQLStoreTest.php
tests/phpunit/includes/site/TestSites.php

index 587c629..470fe26 100644 (file)
@@ -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
         *
index 0d8dcdf..52ba8fb 100644 (file)
@@ -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();
+}
index 4f7889b..5b66d29 100644 (file)
@@ -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() );
        }
 
 }
index d9861cd..b57fe9f 100644 (file)
@@ -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