MediaWiki  1.34.0
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' ),
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  }
212  } catch ( ResourceLoaderCircularDependencyError $err ) {
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 
326  self::compileUnresolvedDependencies( $registryData );
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 
395  return implode( ':', [
396  $context->getSkin(),
397  $this->getConfig()->get( 'ResourceLoaderStorageVersion' ),
398  $context->getLanguage(),
399  ] );
400  }
401 
407  global $IP;
408  $conf = $this->getConfig();
409 
410  if ( $context->getOnly() !== 'scripts' ) {
411  return '/* Requires only=script */';
412  }
413 
414  $startupCode = file_get_contents( "$IP/resources/src/startup/startup.js" );
415 
416  // The files read here MUST be kept in sync with maintenance/jsduck/eg-iframe.html,
417  // and MUST be considered by 'fileHashes' in StartUpModule::getDefinitionSummary().
418  $mwLoaderCode = file_get_contents( "$IP/resources/src/startup/mediawiki.js" ) .
419  file_get_contents( "$IP/resources/src/startup/mediawiki.requestIdleCallback.js" );
420  if ( $context->getDebug() ) {
421  $mwLoaderCode .= file_get_contents( "$IP/resources/src/startup/mediawiki.log.js" );
422  }
423  if ( $conf->get( 'ResourceLoaderEnableJSProfiler' ) ) {
424  $mwLoaderCode .= file_get_contents( "$IP/resources/src/startup/profiler.js" );
425  }
426 
427  // Perform replacements for mediawiki.js
428  $mwLoaderPairs = [
429  '$VARS.reqBase' => $context->encodeJson( $context->getReqBase() ),
430  '$VARS.baseModules' => $context->encodeJson( $this->getBaseModules() ),
431  '$VARS.maxQueryLength' => $context->encodeJson(
432  $conf->get( 'ResourceLoaderMaxQueryLength' )
433  ),
434  // The client-side module cache can be disabled by site configuration.
435  // It is also always disabled in debug mode.
436  '$VARS.storeEnabled' => $context->encodeJson(
437  $conf->get( 'ResourceLoaderStorageEnabled' ) && !$context->getDebug()
438  ),
439  '$VARS.wgLegacyJavaScriptGlobals' => $context->encodeJson(
440  $conf->get( 'LegacyJavaScriptGlobals' )
441  ),
442  '$VARS.storeKey' => $context->encodeJson( $this->getStoreKey() ),
443  '$VARS.storeVary' => $context->encodeJson( $this->getStoreVary( $context ) ),
444  '$VARS.groupUser' => $context->encodeJson( $this->getGroupId( 'user' ) ),
445  '$VARS.groupPrivate' => $context->encodeJson( $this->getGroupId( 'private' ) ),
446  ];
447  $profilerStubs = [
448  '$CODE.profileExecuteStart();' => 'mw.loader.profiler.onExecuteStart( module );',
449  '$CODE.profileExecuteEnd();' => 'mw.loader.profiler.onExecuteEnd( module );',
450  '$CODE.profileScriptStart();' => 'mw.loader.profiler.onScriptStart( module );',
451  '$CODE.profileScriptEnd();' => 'mw.loader.profiler.onScriptEnd( module );',
452  ];
453  if ( $conf->get( 'ResourceLoaderEnableJSProfiler' ) ) {
454  // When profiling is enabled, insert the calls.
455  $mwLoaderPairs += $profilerStubs;
456  } else {
457  // When disabled (by default), insert nothing.
458  $mwLoaderPairs += array_fill_keys( array_keys( $profilerStubs ), '' );
459  }
460  $mwLoaderCode = strtr( $mwLoaderCode, $mwLoaderPairs );
461 
462  // Perform string replacements for startup.js
463  $pairs = [
464  '$VARS.configuration' => $context->encodeJson(
465  $this->getConfigSettings( $context )
466  ),
467  // Raw JavaScript code (not JSON)
468  '$CODE.registrations();' => trim( $this->getModuleRegistrations( $context ) ),
469  '$CODE.defineLoader();' => $mwLoaderCode,
470  ];
471  $startupCode = strtr( $startupCode, $pairs );
472 
473  return $startupCode;
474  }
475 
479  public function supportsURLLoading() {
480  return false;
481  }
482 
486  public function enableModuleContentVersion() {
487  // Enabling this means that ResourceLoader::getVersionHash will simply call getScript()
488  // and hash it to determine the version (as used by E-Tag HTTP response header).
489  return true;
490  }
491 }
ResourceLoaderStartUpModule\$targets
$targets
Definition: ResourceLoaderStartUpModule.php:46
ResourceLoaderContext
Context object that contains information about the state of a specific ResourceLoader web request.
Definition: ResourceLoaderContext.php:33
WikiMap\getCurrentWikiDbDomain
static getCurrentWikiDbDomain()
Definition: WikiMap.php:292
ResourceLoaderStartUpModule\getConfigSettings
getConfigSettings(ResourceLoaderContext $context)
Definition: ResourceLoaderStartUpModule.php:61
ResourceLoaderStartUpModule\getModuleRegistrations
getModuleRegistrations(ResourceLoaderContext $context)
Get registration code for all modules.
Definition: ResourceLoaderStartUpModule.php:228
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:117
$resourceLoader
$resourceLoader
Definition: load.php:44
ResourceLoaderContext\getOnly
getOnly()
Definition: ResourceLoaderContext.php:261
Title\convertByteClassToUnicodeClass
static convertByteClassToUnicodeClass( $byteClass)
Utility method for converting a character sequence from bytes to Unicode.
Definition: Title.php:709
ResourceLoaderStartUpModule\getGroupId
getGroupId( $groupName)
Definition: ResourceLoaderStartUpModule.php:356
ResourceLoaderStartUpModule\supportsURLLoading
supportsURLLoading()
Definition: ResourceLoaderStartUpModule.php:479
WikiMap\getWikiIdFromDbDomain
static getWikiIdFromDbDomain( $domain)
Get the wiki ID of a database domain.
Definition: WikiMap.php:268
$IP
$IP
Definition: update.php:3
ResourceLoaderContext\getLanguage
getLanguage()
Definition: ResourceLoaderContext.php:166
ResourceLoaderModule\$versionHash
array $versionHash
Map of (context hash => cached module version hash)
Definition: ResourceLoaderModule.php:62
ResourceLoaderStartUpModule\getScript
getScript(ResourceLoaderContext $context)
Definition: ResourceLoaderStartUpModule.php:406
wfUrlProtocols
wfUrlProtocols( $includeProtocolRelative=true)
Returns a regular expression of url protocols.
Definition: GlobalFunctions.php:719
ResourceLoaderStartUpModule\getStoreKey
getStoreKey()
Get the localStorage key for the entire module store.
Definition: ResourceLoaderStartUpModule.php:384
ResourceLoaderModule\$name
string null $name
Module name.
Definition: ResourceLoaderModule.php:53
ResourceLoaderStartUpModule
Module for ResourceLoader initialization.
Definition: ResourceLoaderStartUpModule.php:45
ResourceLoaderStartUpModule\getBaseModules
getBaseModules()
Base modules implicitly available to all modules.
Definition: ResourceLoaderStartUpModule.php:373
$context
$context
Definition: load.php:45
CommentStore\COMMENT_CHARACTER_LIMIT
const COMMENT_CHARACTER_LIMIT
Maximum length of a comment in UTF-8 characters.
Definition: CommentStore.php:37
ResourceLoaderStartUpModule\getStoreVary
getStoreVary(ResourceLoaderContext $context)
Get the key on which the JavaScript module cache (mw.loader.store) will vary.
Definition: ResourceLoaderStartUpModule.php:394
ResourceLoaderStartUpModule\$groupIds
$groupIds
Definition: ResourceLoaderStartUpModule.php:48
ResourceLoaderModule
Abstraction for ResourceLoader modules, with name registration and maxage functionality.
Definition: ResourceLoaderModule.php:37
ResourceLoaderStartUpModule\enableModuleContentVersion
enableModuleContentVersion()
Definition: ResourceLoaderStartUpModule.php:486
Title\legalChars
static legalChars()
Get a regex character class describing the legal characters in a link.
Definition: Title.php:695
ResourceLoaderCircularDependencyError
Definition: ResourceLoaderCircularDependencyError.php:25
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:200
ResourceLoaderStartUpModule\compileUnresolvedDependencies
static compileUnresolvedDependencies(array &$registryData)
Optimize the dependency tree in $this->modules.
Definition: ResourceLoaderStartUpModule.php:204
ResourceLoaderStartUpModule\getImplicitDependencies
static getImplicitDependencies(array $registryData, $moduleName, array $handled=[])
Recursively get all explicit and implicit dependencies for to the given module.
Definition: ResourceLoaderStartUpModule.php:141
ResourceLoaderModule\getConfig
getConfig()
Definition: ResourceLoaderModule.php:200