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