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