/* Protected Methods */
/**
- * @param $context ResourceLoaderContext
+ * @param ResourceLoaderContext $context
* @return array
*/
protected function getConfig( $context ) {
}
global $wgLoadScript, $wgScript, $wgStylePath, $wgScriptExtension,
- $wgArticlePath, $wgScriptPath, $wgServer, $wgContLang,
- $wgVariantArticlePath, $wgActionPaths, $wgVersion,
+ $wgArticlePath, $wgScriptPath, $wgServer, $wgServerName,
+ $wgContLang, $wgVariantArticlePath, $wgActionPaths, $wgVersion,
$wgEnableAPI, $wgEnableWriteAPI, $wgDBname,
$wgSitename, $wgFileExtensions, $wgExtensionAssetsPath,
$wgCookiePrefix, $wgResourceLoaderMaxQueryLength,
// becoming [] instead of {} in JS (bug 34604)
'wgActionPaths' => (object)$wgActionPaths,
'wgServer' => $wgServer,
+ 'wgServerName' => $wgServerName,
'wgUserLanguage' => $context->getLanguage(),
'wgContentLanguage' => $wgContLang->getCode(),
'wgVersion' => $wgVersion,
}
/**
- * Gets registration code for all modules
+ * Recursively get all explicit and implicit dependencies for to the given module.
*
- * @param $context ResourceLoaderContext object
- * @return String: JavaScript code for registering all modules with the client loader
+ * @param array $registryData
+ * @param string $moduleName
+ * @return array
+ */
+ protected static function getImplicitDependencies( Array $registryData, $moduleName ) {
+ static $dependencyCache = array();
+
+ // The list of implicit dependencies won't be altered, so we can
+ // cache them without having to worry.
+ if ( !isset( $dependencyCache[$moduleName] ) ) {
+
+ if ( !isset( $registryData[$moduleName] ) ) {
+ // Dependencies may not exist
+ $dependencyCache[$moduleName] = array();
+ } else {
+ $data = $registryData[$moduleName];
+ $dependencyCache[$moduleName] = $data['dependencies'];
+
+ foreach ( $data['dependencies'] as $dependency ) {
+ // Recursively get the dependencies of the dependencies
+ $dependencyCache[$moduleName] = array_merge(
+ $dependencyCache[$moduleName],
+ self::getImplicitDependencies( $registryData, $dependency )
+ );
+ }
+ }
+ }
+
+ return $dependencyCache[$moduleName];
+ }
+
+ /**
+ * Optimize the dependency tree in $this->modules and return it.
+ *
+ * The optimization basically works like this:
+ * Given we have module A with the dependencies B and C
+ * and module B with the dependency C.
+ * Now we don't have to tell the client to explicitly fetch module
+ * C as that's already included in module B.
+ *
+ * This way we can reasonably reduce the amout of module registration
+ * data send to the client.
+ *
+ * @param Array &$registryData Modules keyed by name with properties:
+ * - string 'version'
+ * - array 'dependencies'
+ * - string|null 'group'
+ * - string 'source'
+ * - string|false 'loader'
+ */
+ public static function compileUnresolvedDependencies( Array &$registryData ) {
+ foreach ( $registryData as $name => &$data ) {
+ if ( $data['loader'] !== false ) {
+ continue;
+ }
+ $dependencies = $data['dependencies'];
+ foreach ( $data['dependencies'] as $dependency ) {
+ $implicitDependencies = self::getImplicitDependencies( $registryData, $dependency );
+ $dependencies = array_diff( $dependencies, $implicitDependencies );
+ }
+ // Rebuild keys
+ $data['dependencies'] = array_values( $dependencies );
+ }
+ }
+
+
+ /**
+ * Get registration code for all modules.
+ *
+ * @param ResourceLoaderContext $context
+ * @return string JavaScript code for registering all modules with the client loader
*/
public static function getModuleRegistrations( ResourceLoaderContext $context ) {
global $wgCacheEpoch;
wfProfileIn( __METHOD__ );
- $out = '';
- $registrations = array();
$resourceLoader = $context->getResourceLoader();
$target = $context->getRequest()->getVal( 'target', 'desktop' );
- // Register sources
- $out .= ResourceLoader::makeLoaderSourcesScript( $resourceLoader->getSources() );
+ $out = '';
+ $registryData = array();
- // Register modules
+ // Get registry data
foreach ( $resourceLoader->getModuleNames() as $name ) {
$module = $resourceLoader->getModule( $name );
$moduleTargets = $module->getTargets();
if ( !in_array( $target, $moduleTargets ) ) {
continue;
}
- $deps = $module->getDependencies();
- $group = $module->getGroup();
- $source = $module->getSource();
- // Support module loader scripts
- $loader = $module->getLoaderScript();
- if ( $loader !== false ) {
- $version = wfTimestamp( TS_ISO_8601_BASIC,
- $module->getModifiedTime( $context ) );
- $out .= ResourceLoader::makeCustomLoaderScript( $name, $version, $deps, $group, $source, $loader );
- continue;
- }
- // Automatically register module
// getModifiedTime() is supposed to return a UNIX timestamp, but it doesn't always
// seem to do that, and custom implementations might forget. Coerce it to TS_UNIX
$moduleMtime = wfTimestamp( TS_UNIX, $module->getModifiedTime( $context ) );
$mtime = max( $moduleMtime, wfTimestamp( TS_UNIX, $wgCacheEpoch ) );
- if ( !count( $deps ) && $group === null && $source === 'local' ) {
- // Modules without dependencies, a group or a foreign source pass two arguments (name, timestamp) to
- // mw.loader.register()
- $registrations[] = array( $name, $mtime );
- } elseif ( $group === null && $source === 'local' ) {
- // Modules with dependencies but no group or foreign source pass three arguments
- // (name, timestamp, dependencies) to mw.loader.register()
- $registrations[] = array( $name, $mtime, $deps );
- } elseif ( $source === 'local' ) {
- // Modules with a group but no foreign source pass four arguments (name, timestamp, dependencies, group)
- // to mw.loader.register()
- $registrations[] = array( $name, $mtime, $deps, $group );
+ // FIXME: Convert to numbers, wfTimestamp always gives us stings, even for TS_UNIX
+
+ $registryData[ $name ] = array(
+ 'version' => $mtime,
+ 'dependencies' => $module->getDependencies(),
+ 'group' => $module->getGroup(),
+ 'source' => $module->getSource(),
+ 'loader' => $module->getLoaderScript(),
+ );
+ }
+
+ self::compileUnresolvedDependencies( $registryData );
+
+ // Register sources
+ $out .= ResourceLoader::makeLoaderSourcesScript( $resourceLoader->getSources() );
+
+ // Concatenate module loader scripts and figure out the different call
+ // signatures for mw.loader.register
+ $registrations = array();
+ foreach ( $registryData as $name => $data ) {
+ if ( $data['loader'] !== false ) {
+ $out .= ResourceLoader::makeCustomLoaderScript(
+ $name,
+ wfTimestamp( TS_ISO_8601_BASIC, $data['version'] ),
+ $data['dependencies'],
+ $data['group'],
+ $data['source'],
+ $data['loader']
+ );
+ continue;
+ }
+
+ if (
+ !count( $data['dependencies'] ) &&
+ $data['group'] === null &&
+ $data['source'] === 'local'
+ ) {
+ // Modules without dependencies, a group or a foreign source;
+ // call mw.loader.register(name, timestamp)
+ $registrations[] = array( $name, $data['version'] );
+ } elseif ( $data['group'] === null && $data['source'] === 'local' ) {
+ // Modules with dependencies but no group or foreign source;
+ // call mw.loader.register(name, timestamp, dependencies)
+ $registrations[] = array( $name, $data['version'], $data['dependencies'] );
+ } elseif ( $data['source'] === 'local' ) {
+ // Modules with a group but no foreign source;
+ // call mw.loader.register(name, timestamp, dependencies, group)
+ $registrations[] = array(
+ $name,
+ $data['version'],
+ $data['dependencies'],
+ $data['group']
+ );
} else {
- // Modules with a foreign source pass five arguments (name, timestamp, dependencies, group, source)
- // to mw.loader.register()
- $registrations[] = array( $name, $mtime, $deps, $group, $source );
+ // Modules with a foreign source;
+ // call mw.loader.register(name, timestamp, dependencies, group, source)
+ $registrations[] = array(
+ $name,
+ $data['version'],
+ $data['dependencies'],
+ $data['group'],
+ $data['source']
+ );
}
}
+
+ // Register modules
$out .= ResourceLoader::makeLoaderRegisterScript( $registrations );
wfProfileOut( __METHOD__ );
* This is a helper for getScript(), but can also be called standalone, such
* as when generating an AppCache manifest.
*
- * @param $context ResourceLoaderContext
+ * @param ResourceLoaderContext $context
* @return string
*/
public static function getStartupModulesUrl( ResourceLoaderContext $context ) {
// The core modules:
$moduleNames = array( 'jquery', 'mediawiki' );
- wfRunHooks( 'ResourceLoaderGetStartupModules', array( &$moduleNames ) );
+ wfRunHooks( 'ResourceLoaderGetStartupModules', array( &$moduleNames ), '1.23' );
// Get the latest version
$loader = $context->getResourceLoader();
}
/**
- * @param $context ResourceLoaderContext
+ * @param ResourceLoaderContext $context
* @return string
*/
public function getScript( ResourceLoaderContext $context ) {
global $IP, $wgLegacyJavaScriptGlobals;
- $out = file_get_contents( "$IP/resources/startup.js" );
+ $out = file_get_contents( "$IP/resources/src/startup.js" );
if ( $context->getOnly() === 'scripts' ) {
// Startup function
// Fix indentation
$registrations = str_replace( "\n", "\n\t", trim( $registrations ) );
$out .= "var startUp = function () {\n" .
- "\tmw.config = new " . Xml::encodeJsCall( 'mw.Map', array( $wgLegacyJavaScriptGlobals ) ) . "\n" .
+ "\tmw.config = new " .
+ Xml::encodeJsCall( 'mw.Map', array( $wgLegacyJavaScriptGlobals ) ) . "\n" .
"\t$registrations\n" .
"\t" . Xml::encodeJsCall( 'mw.config.set', array( $configuration ) ) .
"};\n";
}
/**
- * @param $context ResourceLoaderContext
+ * @param ResourceLoaderContext $context
* @return array|mixed
*/
public function getModifiedTime( ResourceLoaderContext $context ) {
$time = max(
wfTimestamp( TS_UNIX, $wgCacheEpoch ),
- filemtime( "$IP/resources/startup.js" ),
+ filemtime( "$IP/resources/src/startup.js" ),
$this->getHashMtime( $context )
);
*
* Detect changes to mw.config settings embedded in #getScript (bug 28899).
*
- * @param $context ResourceLoaderContext
- * @return string: Hash
+ * @param ResourceLoaderContext $context
+ * @return string Hash
*/
public function getModifiedHash( ResourceLoaderContext $context ) {
global $wgLegacyJavaScriptGlobals;