HomePhabricator

[ResourceLoader 2]: Add support for multiple loadScript sources

Description

[ResourceLoader 2]: Add support for multiple loadScript sources
Front-end:

  • New mw.loader method: addSource(). Call with two arguments or an object as first argument for multiple registrations
  • New property in module registry: "source". Optional for local modules (falls back to 'local'). When loading/using one or more modules, the worker will group the request by source and make separate requests to the sources as needed.
  • Re-arranging object properties in mw.loader.register to match the same order all other code parts use.
  • Adding documentation for 'source' and where missing updating it to include 'group' as well.
  • Refactor of mw.loader.work() by Roan Kattouw and Timo Tijhof:'
    • Additional splitting layer by source (in addition to splitting by group), renamed 'groups' to 'splits'
    • Clean up of the loop, and removing a no longer needed loop after the for-in-loop
    • Much more function documentation in mw.loader.work()
    • Moved caching of wgResourceLoaderMaxQueryLength out of the loop and renamed 'limit' to 'maxQueryLength

Back-end changed provided through patch by Roan Kattouw (to avoid broken code between commits):

  • New method in ResourceLoader: addSource(). During construction of ResourceLoader this will be called by default for 'local' with loadScript property set to $wgLoadScript. Additional sources can be registered through $wgResourceLoaderSources (empty array by default)
  • Calling mw.loader.addSource from the startup module
  • Passing source to mw.loader.register from startup module
  • Some new static helper methods

Use:

  • By default nothing should change in core, all modules simply default to 'local'. This info originates from the getSource()-method of the ResourceLoaderModule class, which is inherited to all core ResourceLoaderModule-implementations (none override it)
  • Third-party users and/or extensions can create new classes extending ResourceLoaderModule, re-implementing the getSource-method to return something else.

Basic example:
$wgResourceLoaderSources['mywiki'] = array( 'loadScript' => 'http://example.org/w/load.php' );
class MyCentralWikiModule extends ResourceLoaderModule {
function getSource(){

		return 'mywiki';

}
}
$wgResourceModules['cool.stuff'] => array( 'class' => 'MyCentralWikiModule' );

More complicated example
// imagine some stuff with a ForeignGadgetRepo class, putting stuff in $wgResourceLoaderSources in the __construct() method
class ForeignGadgetRepoGadget extends ResourceLoaderModule {
function getSource(){

		return $this->source;

}
}

Loading:
Loading is completely transparent, stuff like $wgOut->addModules() or mw.loader.loader/using both take it as any other module and load from the right source accordingly.

This commit is part of the ResourceLoader 2 project.