*
* @par Example:
* @code
- * $wgResourceLoaderSources['foo'] = array(
- * 'loadScript' => 'http://example.org/w/load.php',
- * 'apiScript' => 'http://example.org/w/api.php'
- * );
+ * $wgResourceLoaderSources['foo'] = 'http://example.org/w/load.php';
* @endcode
*/
$wgResourceLoaderSources = array();
/** @var int */
protected static $filterCacheVersion = 7;
- /** @var array */
- protected static $requiredSourceProperties = array( 'loadScript' );
-
/** @var bool */
protected static $debugMode = null;
*/
protected $testModuleNames = array();
- /** @var array E.g. array( 'source-id' => array( 'loadScript' => 'http://.../load.php' ) ) */
+ /** @var array E.g. array( 'source-id' => 'http://.../load.php' ) */
protected $sources = array();
/** @var bool */
$this->config = $config;
// Add 'local' source first
- $this->addSource(
- 'local',
- array( 'loadScript' => wfScript( 'load' ), 'apiScript' => wfScript( 'api' ) )
- );
+ $this->addSource( 'local', wfScript( 'load' ) );
// Add other sources
$this->addSource( $config->get( 'ResourceLoaderSources' ) );
/**
* Add a foreign source of modules.
*
- * Source properties:
- * 'loadScript': URL (either fully-qualified or protocol-relative) of load.php for this source
- *
- * @param mixed $id Source ID (string), or array( id1 => props1, id2 => props2, ... )
- * @param array $properties Source properties
+ * @param array|string $id Source ID (string), or array( id1 => loadUrl, id2 => loadUrl, ... )
+ * @param string|array $loadUrl load.php url (string), or array with loadUrl key for
+ * backwards-compatability.
* @throws MWException
*/
- public function addSource( $id, $properties = null ) {
+ public function addSource( $id, $loadUrl = null ) {
// Allow multiple sources to be registered in one call
if ( is_array( $id ) ) {
foreach ( $id as $key => $value ) {
);
}
- // Validate properties
- foreach ( self::$requiredSourceProperties as $prop ) {
- if ( !isset( $properties[$prop] ) ) {
- throw new MWException( "Required property $prop missing from source ID $id" );
+ // Pre 1.24 backwards-compatability
+ if ( is_array( $loadUrl ) ) {
+ if ( !isset( $loadUrl['loadScript'] ) ) {
+ throw new MWException(
+ __METHOD__ . ' was passed an array with no "loadScript" key.'
+ );
}
+
+ $loadUrl = $loadUrl['loadScript'];
}
- $this->sources[$id] = $properties;
+ $this->sources[$id] = $loadUrl;
}
/**
/**
* Get the list of sources.
*
- * @return array Like array( id => array of properties, .. )
+ * @return array Like array( id => load.php url, .. )
*/
public function getSources() {
return $this->sources;
if ( !isset( $this->sources[$source] ) ) {
throw new MWException( "The $source source was never registered in ResourceLoader." );
}
- return $this->sources[$source]['loadScript'];
+ return $this->sources[$source];
}
/**
* - ResourceLoader::makeLoaderSourcesScript( $id, $properties ):
* Register a single source
*
- * - ResourceLoader::makeLoaderSourcesScript( array( $id1 => $props1, $id2 => $props2, ... ) );
+ * - ResourceLoader::makeLoaderSourcesScript( array( $id1 => $loadUrl, $id2 => $loadUrl, ... ) );
* Register sources with the given IDs and properties.
*
* @param string $id Source ID
*/
var registry = {},
//
- // Mapping of sources, keyed by source-id, values are objects.
+ // Mapping of sources, keyed by source-id, values are strings.
// Format:
// {
- // 'sourceId': {
- // 'loadScript': 'http://foo.bar/w/load.php'
- // }
+ // 'sourceId': 'http://foo.bar/w/load.php'
// }
//
sources = {},
for ( source in splits ) {
- sourceLoadScript = sources[source].loadScript;
+ sourceLoadScript = sources[source];
for ( group in splits[source] ) {
*
* The #work method will use this information to split up requests by source.
*
- * mw.loader.addSource( 'mediawikiwiki', { loadScript: '//www.mediawiki.org/w/load.php' } );
+ * mw.loader.addSource( 'mediawikiwiki', '//www.mediawiki.org/w/load.php' );
*
* @param {string} id Short string representing a source wiki, used internally for
* registered modules to indicate where they should be loaded from (usually lowercase a-z).
- * @param {Object} props
- * @param {string} props.loadScript Url to the load.php entry point of the source wiki.
+ * @param {Object|string} loadUrl load.php url, may be an object for backwards-compatability
* @return {boolean}
*/
- addSource: function ( id, props ) {
+ addSource: function ( id, loadUrl ) {
var source;
// Allow multiple additions
if ( typeof id === 'object' ) {
throw new Error( 'source already registered: ' + id );
}
- sources[id] = props;
+ if ( typeof loadUrl === 'object' ) {
+ loadUrl = loadUrl.loadScript;
+ }
+
+ sources[id] = loadUrl;
return true;
},
'modules' => array(),
'out' => '
mw.loader.addSource( {
- "local": {
- "loadScript": "/w/load.php",
- "apiScript": "/w/api.php"
- }
+ "local": "/w/load.php"
} );mw.loader.register( [] );'
) ),
array( array(
),
'out' => '
mw.loader.addSource( {
- "local": {
- "loadScript": "/w/load.php",
- "apiScript": "/w/api.php"
- }
+ "local": "/w/load.php"
} );mw.loader.register( [
[
"test.blank",
),
'out' => '
mw.loader.addSource( {
- "local": {
- "loadScript": "/w/load.php",
- "apiScript": "/w/api.php"
- }
+ "local": "/w/load.php"
} );mw.loader.register( [
[
"test.blank",
),
'out' => '
mw.loader.addSource( {
- "local": {
- "loadScript": "/w/load.php",
- "apiScript": "/w/api.php"
- }
+ "local": "/w/load.php"
} );mw.loader.register( [
[
"test.blank",
),
'out' => '
mw.loader.addSource( {
- "local": {
- "loadScript": "/w/load.php",
- "apiScript": "/w/api.php"
- },
- "example": {
- "loadScript": "http://example.org/w/load.php",
- "apiScript": "http://example.org/w/api.php"
- }
+ "local": "/w/load.php",
+ "example": "http://example.org/w/load.php"
} );mw.loader.register( [
[
"test.blank",
),
'out' => '
mw.loader.addSource( {
- "local": {
- "loadScript": "/w/load.php",
- "apiScript": "/w/api.php"
- }
+ "local": "/w/load.php"
} );mw.loader.register( [
[
"test.x.core",
),
'out' => '
mw.loader.addSource( {
- "local": {
- "loadScript": "/w/load.php",
- "apiScript": "/w/api.php"
- },
- "example": {
- "loadScript": "http://example.org/w/load.php",
- "apiScript": "http://example.org/w/api.php"
- }
+ "local": "/w/load.php",
+ "example": "http://example.org/w/load.php"
} );mw.loader.register( [
[
"test.blank",
$rl->register( $modules );
$module = new ResourceLoaderStartUpModule();
$this->assertEquals(
-'mw.loader.addSource({"local":{"loadScript":"/w/load.php","apiScript":"/w/api.php"}});'
+'mw.loader.addSource({"local":"/w/load.php"});'
. 'mw.loader.register(['
. '["test.blank","1388534400"],'
. '["test.min","1388534400",["test.blank"],null,"local",'
$module = new ResourceLoaderStartUpModule();
$this->assertEquals(
'mw.loader.addSource( {
- "local": {
- "loadScript": "/w/load.php",
- "apiScript": "/w/api.php"
- }
+ "local": "/w/load.php"
} );mw.loader.register( [
[
"test.blank",
);
}
+ public static function provideAddSource() {
+ return array(
+ array( 'examplewiki', '//example.org/w/load.php', 'examplewiki' ),
+ array( 'example2wiki', array( 'loadScript' => '//example.com/w/load.php' ), 'example2wiki' ),
+ array(
+ array( 'foowiki' => '//foo.org/w/load.php', 'bazwiki' => '//baz.org/w/load.php' ),
+ null,
+ array( 'foowiki', 'bazwiki' )
+ ),
+ array(
+ array( 'foowiki' => '//foo.org/w/load.php' ),
+ null,
+ false,
+ ),
+ );
+ }
+
+ /**
+ * @dataProvider provideAddSource
+ * @covers ResourceLoader::addSource
+ */
+ public function testAddSource( $name, $info, $expected ) {
+ $rl = new ResourceLoader;
+ if ( $expected === false ) {
+ $this->setExpectedException( 'MWException', 'ResourceLoader duplicate source addition error' );
+ $rl->addSource( $name, $info );
+ }
+ $rl->addSource( $name, $info );
+ if ( is_array( $expected ) ) {
+ foreach ( $expected as $source ) {
+ $this->assertArrayHasKey( $source, $rl->getSources() );
+ }
+ } else {
+ $this->assertArrayHasKey( $expected, $rl->getSources() );
+ }
+ }
+
public static function fakeSources() {
return array(
'examplewiki' => array(
mw.loader.addSource(
'testloader',
- {
- loadScript: QUnit.fixurl( mw.config.get( 'wgScriptPath' ) + '/tests/qunit/data/load.mock.php' )
- }
+ QUnit.fixurl( mw.config.get( 'wgScriptPath' ) + '/tests/qunit/data/load.mock.php' )
);
QUnit.test( 'Initial check', 8, function ( assert ) {