MediaWiki REL1_34
ResourceLoaderStartUpModule.php
Go to the documentation of this file.
1<?php
24
46 protected $targets = [ 'desktop', 'mobile' ];
47
48 private $groupIds = [
49 // These reserved numbers MUST start at 0 and not skip any. These are preset
50 // for forward compatiblity so that they can be safely referenced by mediawiki.js,
51 // even when the code is cached and the order of registrations (and implicit
52 // group ids) changes between versions of the software.
53 'user' => 0,
54 'private' => 1,
55 ];
56
62 $conf = $this->getConfig();
63
69 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
70 $namespaceIds = $contLang->getNamespaceIds();
71 $caseSensitiveNamespaces = [];
72 $nsInfo = MediaWikiServices::getInstance()->getNamespaceInfo();
73 foreach ( $nsInfo->getCanonicalNamespaces() as $index => $name ) {
74 $namespaceIds[$contLang->lc( $name )] = $index;
75 if ( !$nsInfo->isCapitalized( $index ) ) {
76 $caseSensitiveNamespaces[] = $index;
77 }
78 }
79
80 $illegalFileChars = $conf->get( 'IllegalFileChars' );
81
82 // Build list of variables
83 $skin = $context->getSkin();
84 $vars = [
85 'debug' => $context->getDebug(),
86 'skin' => $skin,
87 'stylepath' => $conf->get( 'StylePath' ),
88 'wgUrlProtocols' => wfUrlProtocols(),
89 'wgArticlePath' => $conf->get( 'ArticlePath' ),
90 'wgScriptPath' => $conf->get( 'ScriptPath' ),
91 'wgScript' => $conf->get( 'Script' ),
92 'wgSearchType' => $conf->get( 'SearchType' ),
93 'wgVariantArticlePath' => $conf->get( 'VariantArticlePath' ),
94 // Force object to avoid "empty" associative array from
95 // becoming [] instead of {} in JS (T36604)
96 'wgActionPaths' => (object)$conf->get( 'ActionPaths' ),
97 'wgServer' => $conf->get( 'Server' ),
98 'wgServerName' => $conf->get( 'ServerName' ),
99 'wgUserLanguage' => $context->getLanguage(),
100 'wgContentLanguage' => $contLang->getCode(),
101 'wgTranslateNumerals' => $conf->get( 'TranslateNumerals' ),
102 'wgVersion' => $conf->get( 'Version' ),
103 'wgEnableAPI' => true, // Deprecated since MW 1.32
104 'wgEnableWriteAPI' => true, // Deprecated since MW 1.32
105 'wgFormattedNamespaces' => $contLang->getFormattedNamespaces(),
106 'wgNamespaceIds' => $namespaceIds,
107 'wgContentNamespaces' => $nsInfo->getContentNamespaces(),
108 'wgSiteName' => $conf->get( 'Sitename' ),
109 'wgDBname' => $conf->get( 'DBname' ),
110 'wgWikiID' => WikiMap::getWikiIdFromDbDomain( WikiMap::getCurrentWikiDbDomain() ),
111 'wgExtraSignatureNamespaces' => $conf->get( 'ExtraSignatureNamespaces' ),
112 'wgExtensionAssetsPath' => $conf->get( 'ExtensionAssetsPath' ),
113 // MediaWiki sets cookies to have this prefix by default
114 'wgCookiePrefix' => $conf->get( 'CookiePrefix' ),
115 'wgCookieDomain' => $conf->get( 'CookieDomain' ),
116 'wgCookiePath' => $conf->get( 'CookiePath' ),
117 'wgCookieExpiration' => $conf->get( 'CookieExpiration' ),
118 'wgCaseSensitiveNamespaces' => $caseSensitiveNamespaces,
119 'wgLegalTitleChars' => Title::convertByteClassToUnicodeClass( Title::legalChars() ),
120 'wgIllegalFileChars' => Title::convertByteClassToUnicodeClass( $illegalFileChars ),
121 'wgForeignUploadTargets' => $conf->get( 'ForeignUploadTargets' ),
122 'wgEnableUploads' => $conf->get( 'EnableUploads' ),
123 'wgCommentByteLimit' => null,
124 'wgCommentCodePointLimit' => CommentStore::COMMENT_CHARACTER_LIMIT,
125 ];
126
127 Hooks::run( 'ResourceLoaderGetConfigVars', [ &$vars, $skin, $conf ] );
128
129 return $vars;
130 }
131
141 protected static function getImplicitDependencies(
142 array $registryData,
143 $moduleName,
144 array $handled = []
145 ) {
146 static $dependencyCache = [];
147
148 // No modules will be added or changed server-side after this point,
149 // so we can safely cache parts of the tree for re-use.
150 if ( !isset( $dependencyCache[$moduleName] ) ) {
151 if ( !isset( $registryData[$moduleName] ) ) {
152 // Unknown module names are allowed here, this is only an optimisation.
153 // Checks for illegal and unknown dependencies happen as PHPUnit structure tests,
154 // and also client-side at run-time.
155 $flat = [];
156 } else {
157 $data = $registryData[$moduleName];
158 $flat = $data['dependencies'];
159
160 // Prevent recursion
161 $handled[] = $moduleName;
162 foreach ( $data['dependencies'] as $dependency ) {
163 if ( in_array( $dependency, $handled, true ) ) {
164 // If we encounter a circular dependency, then stop the optimiser and leave the
165 // original dependencies array unmodified. Circular dependencies are not
166 // supported in ResourceLoader. Awareness of them exists here so that we can
167 // optimise the registry when it isn't broken, and otherwise transport the
168 // registry unchanged. The client will handle this further.
170 } else {
171 // Recursively add the dependencies of the dependencies
172 $flat = array_merge(
173 $flat,
174 self::getImplicitDependencies( $registryData, $dependency, $handled )
175 );
176 }
177 }
178 }
179
180 $dependencyCache[$moduleName] = $flat;
181 }
182
183 return $dependencyCache[$moduleName];
184 }
185
204 public static function compileUnresolvedDependencies( array &$registryData ) {
205 foreach ( $registryData as $name => &$data ) {
206 $dependencies = $data['dependencies'];
207 try {
208 foreach ( $data['dependencies'] as $dependency ) {
209 $implicitDependencies = self::getImplicitDependencies( $registryData, $dependency );
210 $dependencies = array_diff( $dependencies, $implicitDependencies );
211 }
213 // Leave unchanged
214 $dependencies = $data['dependencies'];
215 }
216
217 // Rebuild keys
218 $data['dependencies'] = array_values( $dependencies );
219 }
220 }
221
229 $resourceLoader = $context->getResourceLoader();
230 // Future developers: Use WebRequest::getRawVal() instead getVal().
231 // The getVal() method performs slow Language+UTF logic. (f303bb9360)
232 $target = $context->getRequest()->getRawVal( 'target', 'desktop' );
233 $safemode = $context->getRequest()->getRawVal( 'safemode' ) === '1';
234 // Bypass target filter if this request is Special:JavaScriptTest.
235 // To prevent misuse in production, this is only allowed if testing is enabled server-side.
236 $byPassTargetFilter = $this->getConfig()->get( 'EnableJavaScriptTest' ) && $target === 'test';
237
238 $out = '';
239 $states = [];
240 $registryData = [];
241 $moduleNames = $resourceLoader->getModuleNames();
242
243 // Preload with a batch so that the below calls to getVersionHash() for each module
244 // don't require on-demand loading of more information.
245 try {
246 $resourceLoader->preloadModuleInfo( $moduleNames, $context );
247 } catch ( Exception $e ) {
248 // Don't fail the request (T152266)
249 // Also print the error in the main output
250 $resourceLoader->outputErrorAndLog( $e,
251 'Preloading module info from startup failed: {exception}',
252 [ 'exception' => $e ]
253 );
254 }
255
256 // Get registry data
257 foreach ( $moduleNames as $name ) {
258 $module = $resourceLoader->getModule( $name );
259 $moduleTargets = $module->getTargets();
260 if (
261 ( !$byPassTargetFilter && !in_array( $target, $moduleTargets ) )
262 || ( $safemode && $module->getOrigin() > ResourceLoaderModule::ORIGIN_CORE_INDIVIDUAL )
263 ) {
264 continue;
265 }
266
267 if ( $module instanceof ResourceLoaderStartUpModule ) {
268 // Don't register 'startup' to the client because loading it lazily or depending
269 // on it doesn't make sense, because the startup module *is* the client.
270 // Registering would be a waste of bandwidth and memory and risks somehow causing
271 // it to load a second time.
272
273 // ATTENTION: Because of the line below, this is not going to cause infinite recursion.
274 // Think carefully before making changes to this code!
275 // The below code is going to call ResourceLoaderModule::getVersionHash() for every module.
276 // For StartUpModule (this module) the hash is computed based on the manifest content,
277 // which is the very thing we are computing right here. As such, this must skip iterating
278 // over 'startup' itself.
279 continue;
280 }
281
282 try {
283 $versionHash = $module->getVersionHash( $context );
284 } catch ( Exception $e ) {
285 // Don't fail the request (T152266)
286 // Also print the error in the main output
287 $resourceLoader->outputErrorAndLog( $e,
288 'Calculating version for "{module}" failed: {exception}',
289 [
290 'module' => $name,
291 'exception' => $e,
292 ]
293 );
294 $versionHash = '';
295 $states[$name] = 'error';
296 }
297
298 if ( $versionHash !== '' && strlen( $versionHash ) !== ResourceLoader::HASH_LENGTH ) {
299 $e = new RuntimeException( "Badly formatted module version hash" );
300 $resourceLoader->outputErrorAndLog( $e,
301 "Module '{module}' produced an invalid version hash: '{version}'.",
302 [
303 'module' => $name,
304 'version' => $versionHash,
305 ]
306 );
307 // Module implementation either broken or deviated from ResourceLoader::makeHash
308 // Asserted by tests/phpunit/structure/ResourcesTest.
309 $versionHash = ResourceLoader::makeHash( $versionHash );
310 }
311
312 $skipFunction = $module->getSkipFunction();
313 if ( $skipFunction !== null && !$context->getDebug() ) {
314 $skipFunction = ResourceLoader::filter( 'minify-js', $skipFunction );
315 }
316
317 $registryData[$name] = [
318 'version' => $versionHash,
319 'dependencies' => $module->getDependencies( $context ),
320 'group' => $this->getGroupId( $module->getGroup() ),
321 'source' => $module->getSource(),
322 'skip' => $skipFunction,
323 ];
324 }
325
327
328 // Register sources
329 $out .= ResourceLoader::makeLoaderSourcesScript( $context, $resourceLoader->getSources() );
330
331 // Figure out the different call signatures for mw.loader.register
332 $registrations = [];
333 foreach ( $registryData as $name => $data ) {
334 // Call mw.loader.register(name, version, dependencies, group, source, skip)
335 $registrations[] = [
336 $name,
337 $data['version'],
338 $data['dependencies'],
339 $data['group'],
340 // Swap default (local) for null
341 $data['source'] === 'local' ? null : $data['source'],
342 $data['skip']
343 ];
344 }
345
346 // Register modules
347 $out .= "\n" . ResourceLoader::makeLoaderRegisterScript( $context, $registrations );
348
349 if ( $states ) {
350 $out .= "\n" . ResourceLoader::makeLoaderStateScript( $context, $states );
351 }
352
353 return $out;
354 }
355
356 private function getGroupId( $groupName ) {
357 if ( $groupName === null ) {
358 return null;
359 }
360
361 if ( !array_key_exists( $groupName, $this->groupIds ) ) {
362 $this->groupIds[$groupName] = count( $this->groupIds );
363 }
364
365 return $this->groupIds[$groupName];
366 }
367
373 private function getBaseModules() {
374 $baseModules = [ 'jquery', 'mediawiki.base' ];
375 return $baseModules;
376 }
377
384 private function getStoreKey() {
385 return 'MediaWikiModuleStore:' . $this->getConfig()->get( 'DBname' );
386 }
387
392 private function getMaxQueryLength() : int {
393 $len = $this->getConfig()->get( 'ResourceLoaderMaxQueryLength' );
394 // - Ignore -1, which in MW 1.34 and earlier was used to mean "unlimited".
395 // - Ignore invalid values, e.g. non-int or other negative values.
396 if ( $len === false || $len < 0 ) {
397 // Default
398 $len = 2000;
399 }
400 return $len;
401 }
402
410 return implode( ':', [
411 $context->getSkin(),
412 $this->getConfig()->get( 'ResourceLoaderStorageVersion' ),
413 $context->getLanguage(),
414 ] );
415 }
416
422 global $IP;
423 $conf = $this->getConfig();
424
425 if ( $context->getOnly() !== 'scripts' ) {
426 return '/* Requires only=script */';
427 }
428
429 $startupCode = file_get_contents( "$IP/resources/src/startup/startup.js" );
430
431 // The files read here MUST be kept in sync with maintenance/jsduck/eg-iframe.html,
432 // and MUST be considered by 'fileHashes' in StartUpModule::getDefinitionSummary().
433 $mwLoaderCode = file_get_contents( "$IP/resources/src/startup/mediawiki.js" ) .
434 file_get_contents( "$IP/resources/src/startup/mediawiki.requestIdleCallback.js" );
435 if ( $context->getDebug() ) {
436 $mwLoaderCode .= file_get_contents( "$IP/resources/src/startup/mediawiki.log.js" );
437 }
438 if ( $conf->get( 'ResourceLoaderEnableJSProfiler' ) ) {
439 $mwLoaderCode .= file_get_contents( "$IP/resources/src/startup/profiler.js" );
440 }
441
442 // Perform replacements for mediawiki.js
443 $mwLoaderPairs = [
444 '$VARS.reqBase' => $context->encodeJson( $context->getReqBase() ),
445 '$VARS.baseModules' => $context->encodeJson( $this->getBaseModules() ),
446 '$VARS.maxQueryLength' => $context->encodeJson( $this->getMaxQueryLength() ),
447 // The client-side module cache can be disabled by site configuration.
448 // It is also always disabled in debug mode.
449 '$VARS.storeEnabled' => $context->encodeJson(
450 $conf->get( 'ResourceLoaderStorageEnabled' ) && !$context->getDebug()
451 ),
452 '$VARS.wgLegacyJavaScriptGlobals' => $context->encodeJson(
453 $conf->get( 'LegacyJavaScriptGlobals' )
454 ),
455 '$VARS.storeKey' => $context->encodeJson( $this->getStoreKey() ),
456 '$VARS.storeVary' => $context->encodeJson( $this->getStoreVary( $context ) ),
457 '$VARS.groupUser' => $context->encodeJson( $this->getGroupId( 'user' ) ),
458 '$VARS.groupPrivate' => $context->encodeJson( $this->getGroupId( 'private' ) ),
459 ];
460 $profilerStubs = [
461 '$CODE.profileExecuteStart();' => 'mw.loader.profiler.onExecuteStart( module );',
462 '$CODE.profileExecuteEnd();' => 'mw.loader.profiler.onExecuteEnd( module );',
463 '$CODE.profileScriptStart();' => 'mw.loader.profiler.onScriptStart( module );',
464 '$CODE.profileScriptEnd();' => 'mw.loader.profiler.onScriptEnd( module );',
465 ];
466 if ( $conf->get( 'ResourceLoaderEnableJSProfiler' ) ) {
467 // When profiling is enabled, insert the calls.
468 $mwLoaderPairs += $profilerStubs;
469 } else {
470 // When disabled (by default), insert nothing.
471 $mwLoaderPairs += array_fill_keys( array_keys( $profilerStubs ), '' );
472 }
473 $mwLoaderCode = strtr( $mwLoaderCode, $mwLoaderPairs );
474
475 // Perform string replacements for startup.js
476 $pairs = [
477 '$VARS.configuration' => $context->encodeJson(
478 $this->getConfigSettings( $context )
479 ),
480 // Raw JavaScript code (not JSON)
481 '$CODE.registrations();' => trim( $this->getModuleRegistrations( $context ) ),
482 '$CODE.defineLoader();' => $mwLoaderCode,
483 ];
484 $startupCode = strtr( $startupCode, $pairs );
485
486 return $startupCode;
487 }
488
492 public function supportsURLLoading() {
493 return false;
494 }
495
499 public function enableModuleContentVersion() {
500 // Enabling this means that ResourceLoader::getVersionHash will simply call getScript()
501 // and hash it to determine the version (as used by E-Tag HTTP response header).
502 return true;
503 }
504}
wfUrlProtocols( $includeProtocolRelative=true)
Returns a regular expression of url protocols.
$IP
Definition WebStart.php:41
MediaWikiServices is the service locator for the application scope of MediaWiki.
Context object that contains information about the state of a specific ResourceLoader web request.
Abstraction for ResourceLoader modules, with name registration and maxage functionality.
array $versionHash
Map of (context hash => cached module version hash)
string null $name
Module name.
Module for ResourceLoader initialization.
getStoreVary(ResourceLoaderContext $context)
Get the key on which the JavaScript module cache (mw.loader.store) will vary.
getScript(ResourceLoaderContext $context)
static compileUnresolvedDependencies(array &$registryData)
Optimize the dependency tree in $this->modules.
getBaseModules()
Base modules implicitly available to all modules.
getConfigSettings(ResourceLoaderContext $context)
getModuleRegistrations(ResourceLoaderContext $context)
Get registration code for all modules.
static getImplicitDependencies(array $registryData, $moduleName, array $handled=[])
Recursively get all explicit and implicit dependencies for to the given module.
getStoreKey()
Get the localStorage key for the entire module store.
$resourceLoader
Definition load.php:44
$context
Definition load.php:45