resourceloader: Simplify StringSet fallback
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderClientHtml.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 */
20
21 use Wikimedia\WrappedString;
22 use Wikimedia\WrappedStringList;
23
24 /**
25 * Bootstrap a ResourceLoader client on an HTML page.
26 *
27 * @since 1.28
28 */
29 class ResourceLoaderClientHtml {
30
31 /** @var ResourceLoaderContext */
32 private $context;
33
34 /** @var ResourceLoader */
35 private $resourceLoader;
36
37 /** @var array */
38 private $options;
39
40 /** @var array */
41 private $config = [];
42
43 /** @var array */
44 private $modules = [];
45
46 /** @var array */
47 private $moduleStyles = [];
48
49 /** @var array */
50 private $moduleScripts = [];
51
52 /** @var array */
53 private $exemptStates = [];
54
55 /** @var array */
56 private $data;
57
58 /**
59 * @param ResourceLoaderContext $context
60 * @param array $options [optional] Array of options
61 * - 'target': Parameter for modules=startup request, see ResourceLoaderStartUpModule.
62 * - 'safemode': Parameter for modules=startup request, see ResourceLoaderStartUpModule.
63 * - 'nonce': From OutputPage::getCSPNonce().
64 */
65 public function __construct( ResourceLoaderContext $context, array $options = [] ) {
66 $this->context = $context;
67 $this->resourceLoader = $context->getResourceLoader();
68 $this->options = $options + [
69 'target' => null,
70 'safemode' => null,
71 'nonce' => null,
72 ];
73 }
74
75 /**
76 * Set mw.config variables.
77 *
78 * @param array $vars Array of key/value pairs
79 */
80 public function setConfig( array $vars ) {
81 foreach ( $vars as $key => $value ) {
82 $this->config[$key] = $value;
83 }
84 }
85
86 /**
87 * Ensure one or more modules are loaded.
88 *
89 * @param array $modules Array of module names
90 */
91 public function setModules( array $modules ) {
92 $this->modules = $modules;
93 }
94
95 /**
96 * Ensure the styles of one or more modules are loaded.
97 *
98 * @param array $modules Array of module names
99 */
100 public function setModuleStyles( array $modules ) {
101 $this->moduleStyles = $modules;
102 }
103
104 /**
105 * Ensure the scripts of one or more modules are loaded.
106 *
107 * @deprecated since 1.28
108 * @param array $modules Array of module names
109 */
110 public function setModuleScripts( array $modules ) {
111 $this->moduleScripts = $modules;
112 }
113
114 /**
115 * Set state of special modules that are handled by the caller manually.
116 *
117 * See OutputPage::buildExemptModules() for use cases.
118 *
119 * @param array $states Module state keyed by module name
120 */
121 public function setExemptStates( array $states ) {
122 $this->exemptStates = $states;
123 }
124
125 /**
126 * @return array
127 */
128 private function getData() {
129 if ( $this->data ) {
130 // @codeCoverageIgnoreStart
131 return $this->data;
132 // @codeCoverageIgnoreEnd
133 }
134
135 $rl = $this->resourceLoader;
136 $data = [
137 'states' => [
138 // moduleName => state
139 ],
140 'general' => [],
141 'styles' => [],
142 'scripts' => [],
143 // Embedding for private modules
144 'embed' => [
145 'styles' => [],
146 'general' => [],
147 ],
148 // Deprecations for style-only modules
149 'styleDeprecations' => [],
150 ];
151
152 foreach ( $this->modules as $name ) {
153 $module = $rl->getModule( $name );
154 if ( !$module ) {
155 continue;
156 }
157
158 $group = $module->getGroup();
159 $context = $this->getContext( $group, ResourceLoaderModule::TYPE_COMBINED );
160 if ( $module->isKnownEmpty( $context ) ) {
161 // Avoid needless request or embed for empty module
162 $data['states'][$name] = 'ready';
163 continue;
164 }
165
166 if ( $group === 'user' || $module->shouldEmbedModule( $this->context ) ) {
167 // Call makeLoad() to decide how to load these, instead of
168 // loading via mw.loader.load().
169 // - For group=user: We need to provide a pre-generated load.php
170 // url to the client that has the 'user' and 'version' parameters
171 // filled in. Without this, the client would wrongly use the static
172 // version hash, per T64602.
173 // - For shouldEmbed=true: Embed via mw.loader.implement, per T36907.
174 $data['embed']['general'][] = $name;
175 // Avoid duplicate request from mw.loader
176 $data['states'][$name] = 'loading';
177 } else {
178 // Load via mw.loader.load()
179 $data['general'][] = $name;
180 }
181 }
182
183 foreach ( $this->moduleStyles as $name ) {
184 $module = $rl->getModule( $name );
185 if ( !$module ) {
186 continue;
187 }
188
189 if ( $module->getType() !== ResourceLoaderModule::LOAD_STYLES ) {
190 $logger = $rl->getLogger();
191 $logger->error( 'Unexpected general module "{module}" in styles queue.', [
192 'module' => $name,
193 ] );
194 continue;
195 }
196
197 // Stylesheet doesn't trigger mw.loader callback.
198 // Set "ready" state to allow script modules to depend on this module (T87871).
199 // And to avoid duplicate requests at run-time from mw.loader.
200 $data['states'][$name] = 'ready';
201
202 $group = $module->getGroup();
203 $context = $this->getContext( $group, ResourceLoaderModule::TYPE_STYLES );
204 // Avoid needless request for empty module
205 if ( !$module->isKnownEmpty( $context ) ) {
206 if ( $module->shouldEmbedModule( $this->context ) ) {
207 // Embed via style element
208 $data['embed']['styles'][] = $name;
209 } else {
210 // Load from load.php?only=styles via <link rel=stylesheet>
211 $data['styles'][] = $name;
212 }
213 }
214 $deprecation = $module->getDeprecationInformation();
215 if ( $deprecation ) {
216 $data['styleDeprecations'][] = $deprecation;
217 }
218 }
219
220 foreach ( $this->moduleScripts as $name ) {
221 $module = $rl->getModule( $name );
222 if ( !$module ) {
223 continue;
224 }
225
226 $group = $module->getGroup();
227 $context = $this->getContext( $group, ResourceLoaderModule::TYPE_SCRIPTS );
228 if ( $module->isKnownEmpty( $context ) ) {
229 // Avoid needless request for empty module
230 $data['states'][$name] = 'ready';
231 } else {
232 // Load from load.php?only=scripts via <script src></script>
233 $data['scripts'][] = $name;
234
235 // Avoid duplicate request from mw.loader
236 $data['states'][$name] = 'loading';
237 }
238 }
239
240 return $data;
241 }
242
243 /**
244 * @return array Attribute key-value pairs for the HTML document element
245 */
246 public function getDocumentAttributes() {
247 return [ 'class' => 'client-nojs' ];
248 }
249
250 /**
251 * The order of elements in the head is as follows:
252 * - Inline scripts.
253 * - Stylesheets.
254 * - Async external script-src.
255 *
256 * Reasons:
257 * - Script execution may be blocked on preceeding stylesheets.
258 * - Async scripts are not blocked on stylesheets.
259 * - Inline scripts can't be asynchronous.
260 * - For styles, earlier is better.
261 *
262 * @return string|WrappedStringList HTML
263 */
264 public function getHeadHtml() {
265 $nonce = $this->options['nonce'];
266 $data = $this->getData();
267 $chunks = [];
268
269 // Change "client-nojs" class to client-js. This allows easy toggling of UI components.
270 // This happens synchronously on every page view to avoid flashes of wrong content.
271 // See also #getDocumentAttributes() and /resources/src/startup.js.
272 $chunks[] = Html::inlineScript(
273 'document.documentElement.className = document.documentElement.className'
274 . '.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );',
275 $nonce
276 );
277
278 // Inline RLQ: Set page variables
279 if ( $this->config ) {
280 $chunks[] = ResourceLoader::makeInlineScript(
281 ResourceLoader::makeConfigSetScript( $this->config ),
282 $nonce
283 );
284 }
285
286 // Inline RLQ: Initial module states
287 $states = array_merge( $this->exemptStates, $data['states'] );
288 if ( $states ) {
289 $chunks[] = ResourceLoader::makeInlineScript(
290 ResourceLoader::makeLoaderStateScript( $states ),
291 $nonce
292 );
293 }
294
295 // Inline RLQ: Embedded modules
296 if ( $data['embed']['general'] ) {
297 $chunks[] = $this->getLoad(
298 $data['embed']['general'],
299 ResourceLoaderModule::TYPE_COMBINED,
300 $nonce
301 );
302 }
303
304 // Inline RLQ: Load general modules
305 if ( $data['general'] ) {
306 $chunks[] = ResourceLoader::makeInlineScript(
307 Xml::encodeJsCall( 'mw.loader.load', [ $data['general'] ] ),
308 $nonce
309 );
310 }
311
312 // Inline RLQ: Load only=scripts
313 if ( $data['scripts'] ) {
314 $chunks[] = $this->getLoad(
315 $data['scripts'],
316 ResourceLoaderModule::TYPE_SCRIPTS,
317 $nonce
318 );
319 }
320
321 // External stylesheets (only=styles)
322 if ( $data['styles'] ) {
323 $chunks[] = $this->getLoad(
324 $data['styles'],
325 ResourceLoaderModule::TYPE_STYLES,
326 $nonce
327 );
328 }
329
330 // Inline stylesheets (embedded only=styles)
331 if ( $data['embed']['styles'] ) {
332 $chunks[] = $this->getLoad(
333 $data['embed']['styles'],
334 ResourceLoaderModule::TYPE_STYLES,
335 $nonce
336 );
337 }
338
339 // Async scripts. Once the startup is loaded, inline RLQ scripts will run.
340 // Pass-through a custom 'target' from OutputPage (T143066).
341 $startupQuery = [];
342 foreach ( [ 'target', 'safemode' ] as $param ) {
343 if ( $this->options[$param] !== null ) {
344 $startupQuery[$param] = (string)$this->options[$param];
345 }
346 }
347 $chunks[] = $this->getLoad(
348 'startup',
349 ResourceLoaderModule::TYPE_SCRIPTS,
350 $nonce,
351 $startupQuery
352 );
353
354 return WrappedString::join( "\n", $chunks );
355 }
356
357 /**
358 * @return string|WrappedStringList HTML
359 */
360 public function getBodyHtml() {
361 $data = $this->getData();
362 $chunks = [];
363
364 // Deprecations for only=styles modules
365 if ( $data['styleDeprecations'] ) {
366 $chunks[] = ResourceLoader::makeInlineScript(
367 implode( '', $data['styleDeprecations'] ),
368 $this->options['nonce']
369 );
370 }
371
372 return WrappedString::join( "\n", $chunks );
373 }
374
375 private function getContext( $group, $type ) {
376 return self::makeContext( $this->context, $group, $type );
377 }
378
379 private function getLoad( $modules, $only, $nonce, array $extraQuery = [] ) {
380 return self::makeLoad( $this->context, (array)$modules, $only, $extraQuery, $nonce );
381 }
382
383 private static function makeContext( ResourceLoaderContext $mainContext, $group, $type,
384 array $extraQuery = []
385 ) {
386 // Create new ResourceLoaderContext so that $extraQuery may trigger isRaw().
387 $req = new FauxRequest( array_merge( $mainContext->getRequest()->getValues(), $extraQuery ) );
388 // Set 'only' if not combined
389 $req->setVal( 'only', $type === ResourceLoaderModule::TYPE_COMBINED ? null : $type );
390 // Remove user parameter in most cases
391 if ( $group !== 'user' && $group !== 'private' ) {
392 $req->setVal( 'user', null );
393 }
394 $context = new ResourceLoaderContext( $mainContext->getResourceLoader(), $req );
395 // Allow caller to setVersion() and setModules()
396 $ret = new DerivativeResourceLoaderContext( $context );
397 $ret->setContentOverrideCallback( $mainContext->getContentOverrideCallback() );
398 return $ret;
399 }
400
401 /**
402 * Explicily load or embed modules on a page.
403 *
404 * @param ResourceLoaderContext $mainContext
405 * @param array $modules One or more module names
406 * @param string $only ResourceLoaderModule TYPE_ class constant
407 * @param array $extraQuery [optional] Array with extra query parameters for the request
408 * @param string|null $nonce [optional] Content-Security-Policy nonce
409 * (from OutputPage::getCSPNonce)
410 * @return string|WrappedStringList HTML
411 */
412 public static function makeLoad( ResourceLoaderContext $mainContext, array $modules, $only,
413 array $extraQuery = [], $nonce = null
414 ) {
415 $rl = $mainContext->getResourceLoader();
416 $chunks = [];
417
418 // Sort module names so requests are more uniform
419 sort( $modules );
420
421 if ( $mainContext->getDebug() && count( $modules ) > 1 ) {
422 $chunks = [];
423 // Recursively call us for every item
424 foreach ( $modules as $name ) {
425 $chunks[] = self::makeLoad( $mainContext, [ $name ], $only, $extraQuery, $nonce );
426 }
427 return new WrappedStringList( "\n", $chunks );
428 }
429
430 // Create keyed-by-source and then keyed-by-group list of module objects from modules list
431 $sortedModules = [];
432 foreach ( $modules as $name ) {
433 $module = $rl->getModule( $name );
434 if ( !$module ) {
435 $rl->getLogger()->warning( 'Unknown module "{module}"', [ 'module' => $name ] );
436 continue;
437 }
438 $sortedModules[$module->getSource()][$module->getGroup()][$name] = $module;
439 }
440
441 foreach ( $sortedModules as $source => $groups ) {
442 foreach ( $groups as $group => $grpModules ) {
443 $context = self::makeContext( $mainContext, $group, $only, $extraQuery );
444
445 // Separate sets of linked and embedded modules while preserving order
446 $moduleSets = [];
447 $idx = -1;
448 foreach ( $grpModules as $name => $module ) {
449 $shouldEmbed = $module->shouldEmbedModule( $context );
450 if ( !$moduleSets || $moduleSets[$idx][0] !== $shouldEmbed ) {
451 $moduleSets[++$idx] = [ $shouldEmbed, [] ];
452 }
453 $moduleSets[$idx][1][$name] = $module;
454 }
455
456 // Link/embed each set
457 foreach ( $moduleSets as list( $embed, $moduleSet ) ) {
458 $context->setModules( array_keys( $moduleSet ) );
459 if ( $embed ) {
460 // Decide whether to use style or script element
461 if ( $only == ResourceLoaderModule::TYPE_STYLES ) {
462 $chunks[] = Html::inlineStyle(
463 $rl->makeModuleResponse( $context, $moduleSet )
464 );
465 } else {
466 $chunks[] = ResourceLoader::makeInlineScript(
467 $rl->makeModuleResponse( $context, $moduleSet ),
468 $nonce
469 );
470 }
471 } else {
472 // See if we have one or more raw modules
473 $isRaw = false;
474 foreach ( $moduleSet as $key => $module ) {
475 $isRaw |= $module->isRaw();
476 }
477
478 // Special handling for the user group; because users might change their stuff
479 // on-wiki like user pages, or user preferences; we need to find the highest
480 // timestamp of these user-changeable modules so we can ensure cache misses on change
481 // This should NOT be done for the site group (T29564) because anons get that too
482 // and we shouldn't be putting timestamps in CDN-cached HTML
483 if ( $group === 'user' ) {
484 // Must setModules() before makeVersionQuery()
485 $context->setVersion( $rl->makeVersionQuery( $context ) );
486 }
487
488 $url = $rl->createLoaderURL( $source, $context, $extraQuery );
489
490 // Decide whether to use 'style' or 'script' element
491 if ( $only === ResourceLoaderModule::TYPE_STYLES ) {
492 $chunk = Html::linkedStyle( $url );
493 } else {
494 if ( $context->getRaw() || $isRaw ) {
495 $chunk = Html::element( 'script', [
496 // In SpecialJavaScriptTest, QUnit must load synchronous
497 'async' => !isset( $extraQuery['sync'] ),
498 'src' => $url
499 ] );
500 } else {
501 $chunk = ResourceLoader::makeInlineScript(
502 Xml::encodeJsCall( 'mw.loader.load', [ $url ] ),
503 $nonce
504 );
505 }
506 }
507
508 if ( $group == 'noscript' ) {
509 $chunks[] = Html::rawElement( 'noscript', [], $chunk );
510 } else {
511 $chunks[] = $chunk;
512 }
513 }
514 }
515 }
516 }
517
518 return new WrappedStringList( "\n", $chunks );
519 }
520 }