99ffcd2a947e228f8958ae8d475b646a9fe6d9c7
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderStartUpModule.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @author Trevor Parscal
20 * @author Roan Kattouw
21 */
22
23 use MediaWiki\MediaWikiServices;
24
25 /**
26 * Module for ResourceLoader initialization.
27 *
28 * See also <https://www.mediawiki.org/wiki/ResourceLoader/Features#Startup_Module>
29 *
30 * The startup module, as being called only from ResourceLoaderClientHtml, has
31 * the ability to vary based extra query parameters, in addition to those
32 * from ResourceLoaderContext:
33 *
34 * - target: Only register modules in the client intended for this target.
35 * Default: "desktop".
36 * See also: OutputPage::setTarget(), ResourceLoaderModule::getTargets().
37 *
38 * - safemode: Only register modules that have ORIGIN_CORE as their origin.
39 * This effectively disables ORIGIN_USER modules. (T185303)
40 * See also: OutputPage::disallowUserJs()
41 */
42 class ResourceLoaderStartUpModule extends ResourceLoaderModule {
43
44 // Cache for getConfigSettings() as it's called by multiple methods
45 protected $configVars = [];
46 protected $targets = [ 'desktop', 'mobile' ];
47
48 /**
49 * @param ResourceLoaderContext $context
50 * @return array
51 */
52 protected function getConfigSettings( $context ) {
53 $hash = $context->getHash();
54 if ( isset( $this->configVars[$hash] ) ) {
55 return $this->configVars[$hash];
56 }
57
58 $conf = $this->getConfig();
59
60 // We can't use Title::newMainPage() if 'mainpage' is in
61 // $wgForceUIMsgAsContentMsg because that will try to use the session
62 // user's language and we have no session user. This does the
63 // equivalent but falling back to our ResourceLoaderContext language
64 // instead.
65 $mainPage = Title::newFromText( $context->msg( 'mainpage' )->inContentLanguage()->text() );
66 if ( !$mainPage ) {
67 $mainPage = Title::newFromText( 'Main Page' );
68 }
69
70 /**
71 * Namespace related preparation
72 * - wgNamespaceIds: Key-value pairs of all localized, canonical and aliases for namespaces.
73 * - wgCaseSensitiveNamespaces: Array of namespaces that are case-sensitive.
74 */
75 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
76 $namespaceIds = $contLang->getNamespaceIds();
77 $caseSensitiveNamespaces = [];
78 foreach ( MWNamespace::getCanonicalNamespaces() as $index => $name ) {
79 $namespaceIds[$contLang->lc( $name )] = $index;
80 if ( !MWNamespace::isCapitalized( $index ) ) {
81 $caseSensitiveNamespaces[] = $index;
82 }
83 }
84
85 $illegalFileChars = $conf->get( 'IllegalFileChars' );
86 $oldCommentSchema = $conf->get( 'CommentTableSchemaMigrationStage' ) === MIGRATION_OLD;
87
88 // Build list of variables
89 $vars = [
90 'wgLoadScript' => wfScript( 'load' ),
91 'debug' => $context->getDebug(),
92 'skin' => $context->getSkin(),
93 'stylepath' => $conf->get( 'StylePath' ),
94 'wgUrlProtocols' => wfUrlProtocols(),
95 'wgArticlePath' => $conf->get( 'ArticlePath' ),
96 'wgScriptPath' => $conf->get( 'ScriptPath' ),
97 'wgScript' => wfScript(),
98 'wgSearchType' => $conf->get( 'SearchType' ),
99 'wgVariantArticlePath' => $conf->get( 'VariantArticlePath' ),
100 // Force object to avoid "empty" associative array from
101 // becoming [] instead of {} in JS (T36604)
102 'wgActionPaths' => (object)$conf->get( 'ActionPaths' ),
103 'wgServer' => $conf->get( 'Server' ),
104 'wgServerName' => $conf->get( 'ServerName' ),
105 'wgUserLanguage' => $context->getLanguage(),
106 'wgContentLanguage' => $contLang->getCode(),
107 'wgTranslateNumerals' => $conf->get( 'TranslateNumerals' ),
108 'wgVersion' => $conf->get( 'Version' ),
109 'wgEnableAPI' => true, // Deprecated since MW 1.32
110 'wgEnableWriteAPI' => true, // Deprecated since MW 1.32
111 'wgMainPageTitle' => $mainPage->getPrefixedText(),
112 'wgFormattedNamespaces' => $contLang->getFormattedNamespaces(),
113 'wgNamespaceIds' => $namespaceIds,
114 'wgContentNamespaces' => MWNamespace::getContentNamespaces(),
115 'wgSiteName' => $conf->get( 'Sitename' ),
116 'wgDBname' => $conf->get( 'DBname' ),
117 'wgExtraSignatureNamespaces' => $conf->get( 'ExtraSignatureNamespaces' ),
118 'wgAvailableSkins' => Skin::getSkinNames(),
119 'wgExtensionAssetsPath' => $conf->get( 'ExtensionAssetsPath' ),
120 // MediaWiki sets cookies to have this prefix by default
121 'wgCookiePrefix' => $conf->get( 'CookiePrefix' ),
122 'wgCookieDomain' => $conf->get( 'CookieDomain' ),
123 'wgCookiePath' => $conf->get( 'CookiePath' ),
124 'wgCookieExpiration' => $conf->get( 'CookieExpiration' ),
125 'wgResourceLoaderMaxQueryLength' => $conf->get( 'ResourceLoaderMaxQueryLength' ),
126 'wgCaseSensitiveNamespaces' => $caseSensitiveNamespaces,
127 'wgLegalTitleChars' => Title::convertByteClassToUnicodeClass( Title::legalChars() ),
128 'wgIllegalFileChars' => Title::convertByteClassToUnicodeClass( $illegalFileChars ),
129 'wgResourceLoaderStorageVersion' => $conf->get( 'ResourceLoaderStorageVersion' ),
130 'wgResourceLoaderStorageEnabled' => $conf->get( 'ResourceLoaderStorageEnabled' ),
131 'wgForeignUploadTargets' => $conf->get( 'ForeignUploadTargets' ),
132 'wgEnableUploads' => $conf->get( 'EnableUploads' ),
133 'wgCommentByteLimit' => $oldCommentSchema ? 255 : null,
134 'wgCommentCodePointLimit' => $oldCommentSchema ? null : CommentStore::COMMENT_CHARACTER_LIMIT,
135 ];
136
137 Hooks::run( 'ResourceLoaderGetConfigVars', [ &$vars ] );
138
139 $this->configVars[$hash] = $vars;
140 return $this->configVars[$hash];
141 }
142
143 /**
144 * Recursively get all explicit and implicit dependencies for to the given module.
145 *
146 * @param array $registryData
147 * @param string $moduleName
148 * @return array
149 */
150 protected static function getImplicitDependencies( array $registryData, $moduleName ) {
151 static $dependencyCache = [];
152
153 // The list of implicit dependencies won't be altered, so we can
154 // cache them without having to worry.
155 if ( !isset( $dependencyCache[$moduleName] ) ) {
156 if ( !isset( $registryData[$moduleName] ) ) {
157 // Dependencies may not exist
158 $dependencyCache[$moduleName] = [];
159 } else {
160 $data = $registryData[$moduleName];
161 $dependencyCache[$moduleName] = $data['dependencies'];
162
163 foreach ( $data['dependencies'] as $dependency ) {
164 // Recursively get the dependencies of the dependencies
165 $dependencyCache[$moduleName] = array_merge(
166 $dependencyCache[$moduleName],
167 self::getImplicitDependencies( $registryData, $dependency )
168 );
169 }
170 }
171 }
172
173 return $dependencyCache[$moduleName];
174 }
175
176 /**
177 * Optimize the dependency tree in $this->modules.
178 *
179 * The optimization basically works like this:
180 * Given we have module A with the dependencies B and C
181 * and module B with the dependency C.
182 * Now we don't have to tell the client to explicitly fetch module
183 * C as that's already included in module B.
184 *
185 * This way we can reasonably reduce the amount of module registration
186 * data send to the client.
187 *
188 * @param array &$registryData Modules keyed by name with properties:
189 * - string 'version'
190 * - array 'dependencies'
191 * - string|null 'group'
192 * - string 'source'
193 */
194 public static function compileUnresolvedDependencies( array &$registryData ) {
195 foreach ( $registryData as $name => &$data ) {
196 $dependencies = $data['dependencies'];
197 foreach ( $data['dependencies'] as $dependency ) {
198 $implicitDependencies = self::getImplicitDependencies( $registryData, $dependency );
199 $dependencies = array_diff( $dependencies, $implicitDependencies );
200 }
201 // Rebuild keys
202 $data['dependencies'] = array_values( $dependencies );
203 }
204 }
205
206 /**
207 * Get registration code for all modules.
208 *
209 * @param ResourceLoaderContext $context
210 * @return string JavaScript code for registering all modules with the client loader
211 */
212 public function getModuleRegistrations( ResourceLoaderContext $context ) {
213 $resourceLoader = $context->getResourceLoader();
214 // Future developers: Use WebRequest::getRawVal() instead getVal().
215 // The getVal() method performs slow Language+UTF logic. (f303bb9360)
216 $target = $context->getRequest()->getRawVal( 'target', 'desktop' );
217 $safemode = $context->getRequest()->getRawVal( 'safemode' ) === '1';
218 // Bypass target filter if this request is Special:JavaScriptTest.
219 // To prevent misuse in production, this is only allowed if testing is enabled server-side.
220 $byPassTargetFilter = $this->getConfig()->get( 'EnableJavaScriptTest' ) && $target === 'test';
221
222 $out = '';
223 $states = [];
224 $registryData = [];
225
226 // Get registry data
227 foreach ( $resourceLoader->getModuleNames() as $name ) {
228 $module = $resourceLoader->getModule( $name );
229 $moduleTargets = $module->getTargets();
230 if (
231 ( !$byPassTargetFilter && !in_array( $target, $moduleTargets ) )
232 || ( $safemode && $module->getOrigin() > ResourceLoaderModule::ORIGIN_CORE_INDIVIDUAL )
233 ) {
234 continue;
235 }
236
237 if ( $module->isRaw() ) {
238 // Don't register "raw" modules (like 'jquery' and 'mediawiki') client-side because
239 // depending on them is illegal anyway and would only lead to them being reloaded
240 // causing any state to be lost (like jQuery plugins, mw.config etc.)
241 continue;
242 }
243
244 try {
245 $versionHash = $module->getVersionHash( $context );
246 } catch ( Exception $e ) {
247 // See also T152266 and ResourceLoader::getCombinedVersion()
248 MWExceptionHandler::logException( $e );
249 $context->getLogger()->warning(
250 'Calculating version for "{module}" failed: {exception}',
251 [
252 'module' => $name,
253 'exception' => $e,
254 ]
255 );
256 $versionHash = '';
257 $states[$name] = 'error';
258 }
259
260 if ( $versionHash !== '' && strlen( $versionHash ) !== 7 ) {
261 $context->getLogger()->warning(
262 "Module '{module}' produced an invalid version hash: '{version}'.",
263 [
264 'module' => $name,
265 'version' => $versionHash,
266 ]
267 );
268 // Module implementation either broken or deviated from ResourceLoader::makeHash
269 // Asserted by tests/phpunit/structure/ResourcesTest.
270 $versionHash = ResourceLoader::makeHash( $versionHash );
271 }
272
273 $skipFunction = $module->getSkipFunction();
274 if ( $skipFunction !== null && !ResourceLoader::inDebugMode() ) {
275 $skipFunction = ResourceLoader::filter( 'minify-js', $skipFunction );
276 }
277
278 $registryData[$name] = [
279 'version' => $versionHash,
280 'dependencies' => $module->getDependencies( $context ),
281 'group' => $module->getGroup(),
282 'source' => $module->getSource(),
283 'skip' => $skipFunction,
284 ];
285 }
286
287 self::compileUnresolvedDependencies( $registryData );
288
289 // Register sources
290 $out .= ResourceLoader::makeLoaderSourcesScript( $resourceLoader->getSources() );
291
292 // Figure out the different call signatures for mw.loader.register
293 $registrations = [];
294 foreach ( $registryData as $name => $data ) {
295 // Call mw.loader.register(name, version, dependencies, group, source, skip)
296 $registrations[] = [
297 $name,
298 $data['version'],
299 $data['dependencies'],
300 $data['group'],
301 // Swap default (local) for null
302 $data['source'] === 'local' ? null : $data['source'],
303 $data['skip']
304 ];
305 }
306
307 // Register modules
308 $out .= "\n" . ResourceLoader::makeLoaderRegisterScript( $registrations );
309
310 if ( $states ) {
311 $out .= "\n" . ResourceLoader::makeLoaderStateScript( $states );
312 }
313
314 return $out;
315 }
316
317 /**
318 * @return bool
319 */
320 public function isRaw() {
321 return true;
322 }
323
324 /**
325 * Internal modules used by ResourceLoader that cannot be depended on.
326 *
327 * These module(s) should have isRaw() return true, and are not
328 * legal dependencies (enforced by structure/ResourcesTest).
329 *
330 * @deprecated since 1.32 No longer used.
331 * @return array
332 */
333 public static function getStartupModules() {
334 wfDeprecated( __METHOD__, '1.32' );
335 return [];
336 }
337
338 /**
339 * @deprecated since 1.32 No longer used.
340 * @return array
341 */
342 public static function getLegacyModules() {
343 wfDeprecated( __METHOD__, '1.32' );
344 return [];
345 }
346
347 /**
348 * @private For internal use by SpecialJavaScriptTest
349 * @since 1.32
350 * @return array
351 */
352 public function getBaseModulesInternal() {
353 return $this->getBaseModules();
354 }
355
356 /**
357 * Base modules implicitly available to all modules.
358 *
359 * @return array
360 */
361 private function getBaseModules() {
362 global $wgIncludeLegacyJavaScript;
363
364 $baseModules = [ 'jquery', 'mediawiki.base' ];
365 if ( $wgIncludeLegacyJavaScript ) {
366 $baseModules[] = 'mediawiki.legacy.wikibits';
367 }
368
369 return $baseModules;
370 }
371
372 /**
373 * @param ResourceLoaderContext $context
374 * @return string JavaScript code
375 */
376 public function getScript( ResourceLoaderContext $context ) {
377 global $IP;
378 if ( $context->getOnly() !== 'scripts' ) {
379 return '/* Requires only=script */';
380 }
381
382 $startupCode = file_get_contents( "$IP/resources/src/startup/startup.js" );
383
384 // The files read here MUST be kept in sync with maintenance/jsduck/eg-iframe.html,
385 // and MUST be considered by 'fileHashes' in StartUpModule::getDefinitionSummary().
386 $mwLoaderCode = file_get_contents( "$IP/resources/src/startup/mediawiki.js" ) .
387 file_get_contents( "$IP/resources/src/startup/mediawiki.requestIdleCallback.js" );
388 if ( $context->getDebug() ) {
389 $mwLoaderCode .= file_get_contents( "$IP/resources/src/startup/mediawiki.log.js" );
390 }
391 if ( $this->getConfig()->get( 'ResourceLoaderEnableJSProfiler' ) ) {
392 $mwLoaderCode .= file_get_contents( "$IP/resources/src/startup/profiler.js" );
393 }
394
395 $mapToJson = function ( $value ) {
396 $value = FormatJson::encode( $value, ResourceLoader::inDebugMode(), FormatJson::ALL_OK );
397 // Fix indentation
398 $value = str_replace( "\n", "\n\t", $value );
399 return $value;
400 };
401
402 // Perform replacements for mediawiki.js
403 $mwLoaderPairs = [
404 '$VARS.baseModules' => $mapToJson( $this->getBaseModules() ),
405 ];
406 $profilerStubs = [
407 '$CODE.profileExecuteStart();' => 'mw.loader.profiler.onExecuteStart( module );',
408 '$CODE.profileExecuteEnd();' => 'mw.loader.profiler.onExecuteEnd( module );',
409 '$CODE.profileScriptStart();' => 'mw.loader.profiler.onScriptStart( module );',
410 '$CODE.profileScriptEnd();' => 'mw.loader.profiler.onScriptEnd( module );',
411 ];
412 if ( $this->getConfig()->get( 'ResourceLoaderEnableJSProfiler' ) ) {
413 // When profiling is enabled, insert the calls.
414 $mwLoaderPairs += $profilerStubs;
415 } else {
416 // When disabled (by default), insert nothing.
417 $mwLoaderPairs += array_fill_keys( array_keys( $profilerStubs ), '' );
418 }
419 $mwLoaderCode = strtr( $mwLoaderCode, $mwLoaderPairs );
420
421 // Perform replacements for startup.js
422 $pairs = array_map( $mapToJson, [
423 '$VARS.wgLegacyJavaScriptGlobals' => $this->getConfig()->get( 'LegacyJavaScriptGlobals' ),
424 '$VARS.configuration' => $this->getConfigSettings( $context ),
425 ] );
426 // Raw JavaScript code (not for JSON)
427 $pairs['$CODE.registrations();'] = str_replace(
428 "\n",
429 "\n\t",
430 trim( $this->getModuleRegistrations( $context ) )
431 );
432 $pairs['$CODE.defineLoader();'] = $mwLoaderCode;
433 $startupCode = strtr( $startupCode, $pairs );
434
435 return $startupCode;
436 }
437
438 /**
439 * @return bool
440 */
441 public function supportsURLLoading() {
442 return false;
443 }
444
445 /**
446 * Get the definition summary for this module.
447 *
448 * @param ResourceLoaderContext $context
449 * @return array
450 */
451 public function getDefinitionSummary( ResourceLoaderContext $context ) {
452 global $IP;
453 $summary = parent::getDefinitionSummary( $context );
454 $startup = [
455 // getScript() exposes these variables to mw.config (T30899).
456 'vars' => $this->getConfigSettings( $context ),
457 // getScript() uses this to decide how configure mw.Map for mw.config.
458 'wgLegacyJavaScriptGlobals' => $this->getConfig()->get( 'LegacyJavaScriptGlobals' ),
459 // Detect changes to the module registrations output by getScript().
460 'moduleHashes' => $this->getAllModuleHashes( $context ),
461 // Detect changes to base modules listed by getScript().
462 'baseModules' => $this->getBaseModules(),
463
464 'fileHashes' => [
465 $this->safeFileHash( "$IP/resources/src/startup/startup.js" ),
466 $this->safeFileHash( "$IP/resources/src/startup/mediawiki.js" ),
467 $this->safeFileHash( "$IP/resources/src/startup/mediawiki.requestIdleCallback.js" ),
468 ],
469 ];
470 if ( $context->getDebug() ) {
471 $startup['fileHashes'][] = $this->safeFileHash( "$IP/resources/src/startup/mediawiki.log.js" );
472 }
473 if ( $this->getConfig()->get( 'ResourceLoaderEnableJSProfiler' ) ) {
474 $startup['fileHashes'][] = $this->safeFileHash( "$IP/resources/src/startup/profiling.js" );
475 }
476 $summary[] = $startup;
477 return $summary;
478 }
479
480 /**
481 * Helper method for getDefinitionSummary().
482 *
483 * @param ResourceLoaderContext $context
484 * @return string SHA-1
485 */
486 protected function getAllModuleHashes( ResourceLoaderContext $context ) {
487 $rl = $context->getResourceLoader();
488 // Preload for getCombinedVersion()
489 $rl->preloadModuleInfo( $rl->getModuleNames(), $context );
490
491 // ATTENTION: Because of the line below, this is not going to cause infinite recursion.
492 // Think carefully before making changes to this code!
493 // Pre-populate versionHash with something because the loop over all modules below includes
494 // the startup module (this module).
495 // See ResourceLoaderModule::getVersionHash() for usage of this cache.
496 $this->versionHash[$context->getHash()] = null;
497
498 return $rl->getCombinedVersion( $context, $rl->getModuleNames() );
499 }
500
501 /**
502 * @return string
503 */
504 public function getGroup() {
505 return 'startup';
506 }
507 }