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