Merge "Add config for serving main Page from the domain root"
[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 * @ingroup ResourceLoader
43 * @internal
44 */
45 class ResourceLoaderStartUpModule extends ResourceLoaderModule {
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
57 /**
58 * @param ResourceLoaderContext $context
59 * @return array
60 */
61 private function getConfigSettings( $context ) {
62 $conf = $this->getConfig();
63
64 /**
65 * Namespace related preparation
66 * - wgNamespaceIds: Key-value pairs of all localized, canonical and aliases for namespaces.
67 * - wgCaseSensitiveNamespaces: Array of namespaces that are case-sensitive.
68 */
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
132 /**
133 * Recursively get all explicit and implicit dependencies for to the given module.
134 *
135 * @param array $registryData
136 * @param string $moduleName
137 * @param string[] $handled Internal parameter for recursion. (Optional)
138 * @return array
139 * @throws ResourceLoaderCircularDependencyError
140 */
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.
169 throw new ResourceLoaderCircularDependencyError();
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
186 /**
187 * Optimize the dependency tree in $this->modules.
188 *
189 * The optimization basically works like this:
190 * Given we have module A with the dependencies B and C
191 * and module B with the dependency C.
192 * Now we don't have to tell the client to explicitly fetch module
193 * C as that's already included in module B.
194 *
195 * This way we can reasonably reduce the amount of module registration
196 * data send to the client.
197 *
198 * @param array &$registryData Modules keyed by name with properties:
199 * - string 'version'
200 * - array 'dependencies'
201 * - string|null 'group'
202 * - string 'source'
203 */
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
222 /**
223 * Get registration code for all modules.
224 *
225 * @param ResourceLoaderContext $context
226 * @return string JavaScript code for registering all modules with the client loader
227 */
228 public function getModuleRegistrations( ResourceLoaderContext $context ) {
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 && !ResourceLoader::inDebugMode() ) {
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( $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( $registrations );
348
349 if ( $states ) {
350 $out .= "\n" . ResourceLoader::makeLoaderStateScript( $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
368 /**
369 * Base modules implicitly available to all modules.
370 *
371 * @return array
372 */
373 private function getBaseModules() {
374 $baseModules = [ 'jquery', 'mediawiki.base' ];
375 return $baseModules;
376 }
377
378 /**
379 * Get the localStorage key for the entire module store. The key references
380 * $wgDBname to prevent clashes between wikis under the same web domain.
381 *
382 * @return string localStorage item key for JavaScript
383 */
384 private function getStoreKey() {
385 return 'MediaWikiModuleStore:' . $this->getConfig()->get( 'DBname' );
386 }
387
388 /**
389 * Get the key on which the JavaScript module cache (mw.loader.store) will vary.
390 *
391 * @param ResourceLoaderContext $context
392 * @return string String of concatenated vary conditions
393 */
394 private function getStoreVary( ResourceLoaderContext $context ) {
395 return implode( ':', [
396 $context->getSkin(),
397 $this->getConfig()->get( 'ResourceLoaderStorageVersion' ),
398 $context->getLanguage(),
399 ] );
400 }
401
402 /**
403 * @param ResourceLoaderContext $context
404 * @return string JavaScript code
405 */
406 public function getScript( ResourceLoaderContext $context ) {
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' => ResourceLoader::encodeJsonForScript( $context->getReqBase() ),
430 '$VARS.baseModules' => ResourceLoader::encodeJsonForScript( $this->getBaseModules() ),
431 '$VARS.maxQueryLength' => ResourceLoader::encodeJsonForScript(
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' => ResourceLoader::encodeJsonForScript(
437 $conf->get( 'ResourceLoaderStorageEnabled' ) && !$context->getDebug()
438 ),
439 '$VARS.wgLegacyJavaScriptGlobals' => ResourceLoader::encodeJsonForScript(
440 $conf->get( 'LegacyJavaScriptGlobals' )
441 ),
442 '$VARS.storeKey' => ResourceLoader::encodeJsonForScript( $this->getStoreKey() ),
443 '$VARS.storeVary' => ResourceLoader::encodeJsonForScript( $this->getStoreVary( $context ) ),
444 '$VARS.groupUser' => ResourceLoader::encodeJsonForScript( $this->getGroupId( 'user' ) ),
445 '$VARS.groupPrivate' => ResourceLoader::encodeJsonForScript( $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' => ResourceLoader::encodeJsonForScript(
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
476 /**
477 * @return bool
478 */
479 public function supportsURLLoading() {
480 return false;
481 }
482
483 /**
484 * @return bool
485 */
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 }