'ExtensionRegistry' => __DIR__ . '/includes/registration/ExtensionRegistry.php',
'ExternalStore' => __DIR__ . '/includes/externalstore/ExternalStore.php',
'ExternalStoreDB' => __DIR__ . '/includes/externalstore/ExternalStoreDB.php',
+ 'ExternalStoreFactory' => __DIR__ . '/includes/externalstore/ExternalStoreFactory.php',
'ExternalStoreHttp' => __DIR__ . '/includes/externalstore/ExternalStoreHttp.php',
'ExternalStoreMedium' => __DIR__ . '/includes/externalstore/ExternalStoreMedium.php',
'ExternalStoreMwstore' => __DIR__ . '/includes/externalstore/ExternalStoreMwstore.php',
return $this->getService( 'ShellCommandFactory' );
}
+ /**
+ * @since 1.31
+ * @return \ExternalStoreFactory
+ */
+ public function getExternalStoreFactory() {
+ return $this->getService( 'ExternalStoreFactory' );
+ }
+
///////////////////////////////////////////////////////////////////////////
// NOTE: When adding a service getter here, don't forget to add a test
// case for it in MediaWikiServicesTest::provideGetters() and in
return $factory;
},
+ 'ExternalStoreFactory' => function ( MediaWikiServices $services ) {
+ $config = $services->getMainConfig();
+
+ return new ExternalStoreFactory(
+ $config->get( 'ExternalStores' )
+ );
+ },
+
///////////////////////////////////////////////////////////////////////////
// NOTE: When adding a service here, don't forget to add a getter function
// in the MediaWikiServices class. The convenience getter should just call
* @defgroup ExternalStorage ExternalStorage
*/
+use MediaWiki\MediaWikiServices;
+
/**
* Interface for data storage in external repositories.
*
* @return ExternalStoreMedium|bool The store class or false on error
*/
public static function getStoreObject( $proto, array $params = [] ) {
- global $wgExternalStores;
-
- if ( !$wgExternalStores || !in_array( $proto, $wgExternalStores ) ) {
- return false; // protocol not enabled
- }
-
- $class = 'ExternalStore' . ucfirst( $proto );
-
- // Any custom modules should be added to $wgAutoLoadClasses for on-demand loading
- return class_exists( $class ) ? new $class( $params ) : false;
+ return MediaWikiServices::getInstance()
+ ->getExternalStoreFactory()
+ ->getStoreObject( $proto, $params );
}
/**
--- /dev/null
+<?php
+/**
+ * @defgroup ExternalStorage ExternalStorage
+ */
+
+/**
+ * @ingroup ExternalStorage
+ */
+class ExternalStoreFactory {
+
+ /**
+ * @var array
+ */
+ private $externalStores;
+
+ /**
+ * @param array $externalStores See $wgExternalStores
+ */
+ public function __construct( array $externalStores ) {
+ $this->externalStores = array_map( 'strtolower', $externalStores );
+ }
+
+ /**
+ * Get an external store object of the given type, with the given parameters
+ *
+ * @param string $proto Type of external storage, should be a value in $wgExternalStores
+ * @param array $params Associative array of ExternalStoreMedium parameters
+ * @return ExternalStoreMedium|bool The store class or false on error
+ */
+ public function getStoreObject( $proto, array $params = [] ) {
+ if ( !$this->externalStores || !in_array( strtolower( $proto ), $this->externalStores ) ) {
+ // Protocol not enabled
+ return false;
+ }
+
+ $class = 'ExternalStore' . ucfirst( $proto );
+
+ // Any custom modules should be added to $wgAutoLoadClasses for on-demand loading
+ return class_exists( $class ) ? new $class( $params ) : false;
+ }
+
+}
# tests/phpunit/includes/diff
'FakeDiffOp' => "$testDir/phpunit/includes/diff/FakeDiffOp.php",
+ # tests/phpunit/includes/externalstore
+ 'ExternalStoreForTesting' => "$testDir/phpunit/includes/externalstore/ExternalStoreForTesting.php",
+
# tests/phpunit/includes/logging
'LogFormatterTestCase' => "$testDir/phpunit/includes/logging/LogFormatterTestCase.php",
--- /dev/null
+<?php
+
+/**
+ * @covers ExternalStoreFactory
+ */
+class ExternalStoreFactoryTest extends PHPUnit_Framework_TestCase {
+
+ public function testExternalStoreFactory_noStores() {
+ $factory = new ExternalStoreFactory( [] );
+ $this->assertFalse( $factory->getStoreObject( 'ForTesting' ) );
+ $this->assertFalse( $factory->getStoreObject( 'foo' ) );
+ }
+
+ public function provideStoreNames() {
+ yield 'Same case as construction' => [ 'ForTesting' ];
+ yield 'All lower case' => [ 'fortesting' ];
+ yield 'All upper case' => [ 'FORTESTING' ];
+ yield 'Mix of cases' => [ 'FOrTEsTInG' ];
+ }
+
+ /**
+ * @dataProvider provideStoreNames
+ */
+ public function testExternalStoreFactory_someStore_protoMatch( $proto ) {
+ $factory = new ExternalStoreFactory( [ 'ForTesting' ] );
+ $store = $factory->getStoreObject( $proto );
+ $this->assertInstanceOf( ExternalStoreForTesting::class, $store );
+ }
+
+ /**
+ * @dataProvider provideStoreNames
+ */
+ public function testExternalStoreFactory_someStore_noProtoMatch( $proto ) {
+ $factory = new ExternalStoreFactory( [ 'SomeOtherClassName' ] );
+ $store = $factory->getStoreObject( $proto );
+ $this->assertFalse( $store );
+ }
+
+}
--- /dev/null
+<?php
+
+class ExternalStoreForTesting {
+
+ protected $data = [
+ 'cluster1' => [
+ '200' => 'Hello',
+ '300' => [
+ 'Hello', 'World',
+ ],
+ ],
+ ];
+
+ /**
+ * Fetch data from given URL
+ * @param string $url An url of the form FOO://cluster/id or FOO://cluster/id/itemid.
+ * @return mixed
+ */
+ public function fetchFromURL( $url ) {
+ // Based on ExternalStoreDB
+ $path = explode( '/', $url );
+ $cluster = $path[2];
+ $id = $path[3];
+ if ( isset( $path[4] ) ) {
+ $itemID = $path[4];
+ } else {
+ $itemID = false;
+ }
+
+ if ( !isset( $this->data[$cluster][$id] ) ) {
+ return null;
+ }
+
+ if ( $itemID !== false
+ && is_array( $this->data[$cluster][$id] )
+ && isset( $this->data[$cluster][$id][$itemID] )
+ ) {
+ return $this->data[$cluster][$id][$itemID];
+ }
+
+ return $this->data[$cluster][$id];
+ }
+
+}
<?php
-/**
- * External Store tests
- */
class ExternalStoreTest extends MediaWikiTestCase {
/**
* @covers ExternalStore::fetchFromURL
*/
- public function testExternalFetchFromURL() {
- $this->setMwGlobals( 'wgExternalStores', false );
+ public function testExternalFetchFromURL_noExternalStores() {
+ $this->setService(
+ 'ExternalStoreFactory',
+ new ExternalStoreFactory( [] )
+ );
$this->assertFalse(
- ExternalStore::fetchFromURL( 'FOO://cluster1/200' ),
+ ExternalStore::fetchFromURL( 'ForTesting://cluster1/200' ),
'Deny if wgExternalStores is not set to a non-empty array'
);
+ }
- $this->setMwGlobals( 'wgExternalStores', [ 'FOO' ] );
+ /**
+ * @covers ExternalStore::fetchFromURL
+ */
+ public function testExternalFetchFromURL_someExternalStore() {
+ $this->setService(
+ 'ExternalStoreFactory',
+ new ExternalStoreFactory( [ 'ForTesting' ] )
+ );
$this->assertEquals(
- ExternalStore::fetchFromURL( 'FOO://cluster1/200' ),
'Hello',
+ ExternalStore::fetchFromURL( 'ForTesting://cluster1/200' ),
'Allow FOO://cluster1/200'
);
$this->assertEquals(
- ExternalStore::fetchFromURL( 'FOO://cluster1/300/0' ),
'Hello',
+ ExternalStore::fetchFromURL( 'ForTesting://cluster1/300/0' ),
'Allow FOO://cluster1/300/0'
);
# Assertions for r68900
);
}
}
-
-class ExternalStoreFOO {
-
- protected $data = [
- 'cluster1' => [
- '200' => 'Hello',
- '300' => [
- 'Hello', 'World',
- ],
- ],
- ];
-
- /**
- * Fetch data from given URL
- * @param string $url An url of the form FOO://cluster/id or FOO://cluster/id/itemid.
- * @return mixed
- */
- function fetchFromURL( $url ) {
- // Based on ExternalStoreDB
- $path = explode( '/', $url );
- $cluster = $path[2];
- $id = $path[3];
- if ( isset( $path[4] ) ) {
- $itemID = $path[4];
- } else {
- $itemID = false;
- }
-
- if ( !isset( $this->data[$cluster][$id] ) ) {
- return null;
- }
-
- if ( $itemID !== false
- && is_array( $this->data[$cluster][$id] )
- && isset( $this->data[$cluster][$id][$itemID] )
- ) {
- return $this->data[$cluster][$id][$itemID];
- }
-
- return $this->data[$cluster][$id];
- }
-}