3bf309d9eaa7a3f653d7bc9c4118c247dee6ee6b
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderModule.php
1 <?php
2 /**
3 * Abstraction for ResourceLoader modules.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @author Trevor Parscal
22 * @author Roan Kattouw
23 */
24
25 use MediaWiki\MediaWikiServices;
26 use Psr\Log\LoggerAwareInterface;
27 use Psr\Log\LoggerInterface;
28 use Psr\Log\NullLogger;
29 use Wikimedia\RelPath;
30 use Wikimedia\ScopedCallback;
31
32 /**
33 * Abstraction for ResourceLoader modules, with name registration and maxage functionality.
34 */
35 abstract class ResourceLoaderModule implements LoggerAwareInterface {
36 # Type of resource
37 const TYPE_SCRIPTS = 'scripts';
38 const TYPE_STYLES = 'styles';
39 const TYPE_COMBINED = 'combined';
40
41 # Desired load type
42 // Module only has styles (loaded via <style> or <link rel=stylesheet>)
43 const LOAD_STYLES = 'styles';
44 // Module may have other resources (loaded via mw.loader from a script)
45 const LOAD_GENERAL = 'general';
46
47 # sitewide core module like a skin file or jQuery component
48 const ORIGIN_CORE_SITEWIDE = 1;
49
50 # per-user module generated by the software
51 const ORIGIN_CORE_INDIVIDUAL = 2;
52
53 # sitewide module generated from user-editable files, like MediaWiki:Common.js, or
54 # modules accessible to multiple users, such as those generated by the Gadgets extension.
55 const ORIGIN_USER_SITEWIDE = 3;
56
57 # per-user module generated from user-editable files, like User:Me/vector.js
58 const ORIGIN_USER_INDIVIDUAL = 4;
59
60 # an access constant; make sure this is kept as the largest number in this group
61 const ORIGIN_ALL = 10;
62
63 # script and style modules form a hierarchy of trustworthiness, with core modules like
64 # skins and jQuery as most trustworthy, and user scripts as least trustworthy. We can
65 # limit the types of scripts and styles we allow to load on, say, sensitive special
66 # pages like Special:UserLogin and Special:Preferences
67 protected $origin = self::ORIGIN_CORE_SITEWIDE;
68
69 protected $name = null;
70 protected $targets = [ 'desktop' ];
71
72 // In-object cache for file dependencies
73 protected $fileDeps = [];
74 // In-object cache for message blob (keyed by language)
75 protected $msgBlobs = [];
76 // In-object cache for version hash
77 protected $versionHash = [];
78 // In-object cache for module content
79 protected $contents = [];
80
81 /**
82 * @var Config
83 */
84 protected $config;
85
86 /**
87 * @var array|bool
88 */
89 protected $deprecated = false;
90
91 /**
92 * @var LoggerInterface
93 */
94 protected $logger;
95
96 /**
97 * Get this module's name. This is set when the module is registered
98 * with ResourceLoader::register()
99 *
100 * @return string|null Name (string) or null if no name was set
101 */
102 public function getName() {
103 return $this->name;
104 }
105
106 /**
107 * Set this module's name. This is called by ResourceLoader::register()
108 * when registering the module. Other code should not call this.
109 *
110 * @param string $name
111 */
112 public function setName( $name ) {
113 $this->name = $name;
114 }
115
116 /**
117 * Get this module's origin. This is set when the module is registered
118 * with ResourceLoader::register()
119 *
120 * @return int ResourceLoaderModule class constant, the subclass default
121 * if not set manually
122 */
123 public function getOrigin() {
124 return $this->origin;
125 }
126
127 /**
128 * @param ResourceLoaderContext $context
129 * @return bool
130 */
131 public function getFlip( $context ) {
132 return MediaWikiServices::getInstance()->getContentLanguage()->getDir() !==
133 $context->getDirection();
134 }
135
136 /**
137 * Get JS representing deprecation information for the current module if available
138 *
139 * @return string JavaScript code
140 */
141 public function getDeprecationInformation() {
142 $deprecationInfo = $this->deprecated;
143 if ( $deprecationInfo ) {
144 $name = $this->getName();
145 $warning = 'This page is using the deprecated ResourceLoader module "' . $name . '".';
146 if ( is_string( $deprecationInfo ) ) {
147 $warning .= "\n" . $deprecationInfo;
148 }
149 return Xml::encodeJsCall(
150 'mw.log.warn',
151 [ $warning ]
152 );
153 } else {
154 return '';
155 }
156 }
157
158 /**
159 * Get all JS for this module for a given language and skin.
160 * Includes all relevant JS except loader scripts.
161 *
162 * @param ResourceLoaderContext $context
163 * @return string JavaScript code
164 */
165 public function getScript( ResourceLoaderContext $context ) {
166 // Stub, override expected
167 return '';
168 }
169
170 /**
171 * Takes named templates by the module and returns an array mapping.
172 *
173 * @return array of templates mapping template alias to content
174 */
175 public function getTemplates() {
176 // Stub, override expected.
177 return [];
178 }
179
180 /**
181 * @return Config
182 * @since 1.24
183 */
184 public function getConfig() {
185 if ( $this->config === null ) {
186 // Ugh, fall back to default
187 $this->config = MediaWikiServices::getInstance()->getMainConfig();
188 }
189
190 return $this->config;
191 }
192
193 /**
194 * @param Config $config
195 * @since 1.24
196 */
197 public function setConfig( Config $config ) {
198 $this->config = $config;
199 }
200
201 /**
202 * @since 1.27
203 * @param LoggerInterface $logger
204 * @return null
205 */
206 public function setLogger( LoggerInterface $logger ) {
207 $this->logger = $logger;
208 }
209
210 /**
211 * @since 1.27
212 * @return LoggerInterface
213 */
214 protected function getLogger() {
215 if ( !$this->logger ) {
216 $this->logger = new NullLogger();
217 }
218 return $this->logger;
219 }
220
221 /**
222 * Get the URL or URLs to load for this module's JS in debug mode.
223 * The default behavior is to return a load.php?only=scripts URL for
224 * the module, but file-based modules will want to override this to
225 * load the files directly.
226 *
227 * This function is called only when 1) we're in debug mode, 2) there
228 * is no only= parameter and 3) supportsURLLoading() returns true.
229 * #2 is important to prevent an infinite loop, therefore this function
230 * MUST return either an only= URL or a non-load.php URL.
231 *
232 * @param ResourceLoaderContext $context
233 * @return array Array of URLs
234 */
235 public function getScriptURLsForDebug( ResourceLoaderContext $context ) {
236 $resourceLoader = $context->getResourceLoader();
237 $derivative = new DerivativeResourceLoaderContext( $context );
238 $derivative->setModules( [ $this->getName() ] );
239 $derivative->setOnly( 'scripts' );
240 $derivative->setDebug( true );
241
242 $url = $resourceLoader->createLoaderURL(
243 $this->getSource(),
244 $derivative
245 );
246
247 return [ $url ];
248 }
249
250 /**
251 * Whether this module supports URL loading. If this function returns false,
252 * getScript() will be used even in cases (debug mode, no only param) where
253 * getScriptURLsForDebug() would normally be used instead.
254 * @return bool
255 */
256 public function supportsURLLoading() {
257 return true;
258 }
259
260 /**
261 * Get all CSS for this module for a given skin.
262 *
263 * @param ResourceLoaderContext $context
264 * @return array List of CSS strings or array of CSS strings keyed by media type.
265 * like [ 'screen' => '.foo { width: 0 }' ];
266 * or [ 'screen' => [ '.foo { width: 0 }' ] ];
267 */
268 public function getStyles( ResourceLoaderContext $context ) {
269 // Stub, override expected
270 return [];
271 }
272
273 /**
274 * Get the URL or URLs to load for this module's CSS in debug mode.
275 * The default behavior is to return a load.php?only=styles URL for
276 * the module, but file-based modules will want to override this to
277 * load the files directly. See also getScriptURLsForDebug()
278 *
279 * @param ResourceLoaderContext $context
280 * @return array [ mediaType => [ URL1, URL2, ... ], ... ]
281 */
282 public function getStyleURLsForDebug( ResourceLoaderContext $context ) {
283 $resourceLoader = $context->getResourceLoader();
284 $derivative = new DerivativeResourceLoaderContext( $context );
285 $derivative->setModules( [ $this->getName() ] );
286 $derivative->setOnly( 'styles' );
287 $derivative->setDebug( true );
288
289 $url = $resourceLoader->createLoaderURL(
290 $this->getSource(),
291 $derivative
292 );
293
294 return [ 'all' => [ $url ] ];
295 }
296
297 /**
298 * Get the messages needed for this module.
299 *
300 * To get a JSON blob with messages, use MessageBlobStore::get()
301 *
302 * @return array List of message keys. Keys may occur more than once
303 */
304 public function getMessages() {
305 // Stub, override expected
306 return [];
307 }
308
309 /**
310 * Get the group this module is in.
311 *
312 * @return string Group name
313 */
314 public function getGroup() {
315 // Stub, override expected
316 return null;
317 }
318
319 /**
320 * Get the source of this module. Should only be overridden for foreign modules.
321 *
322 * @return string Source name, 'local' for local modules
323 */
324 public function getSource() {
325 // Stub, override expected
326 return 'local';
327 }
328
329 /**
330 * Whether this module's JS expects to work without the client-side ResourceLoader module.
331 * Returning true from this function will prevent mw.loader.state() call from being
332 * appended to the bottom of the script.
333 *
334 * @return bool
335 */
336 public function isRaw() {
337 return false;
338 }
339
340 /**
341 * Get a list of modules this module depends on.
342 *
343 * Dependency information is taken into account when loading a module
344 * on the client side.
345 *
346 * Note: It is expected that $context will be made non-optional in the near
347 * future.
348 *
349 * @param ResourceLoaderContext|null $context
350 * @return array List of module names as strings
351 */
352 public function getDependencies( ResourceLoaderContext $context = null ) {
353 // Stub, override expected
354 return [];
355 }
356
357 /**
358 * Get target(s) for the module, eg ['desktop'] or ['desktop', 'mobile']
359 *
360 * @return array Array of strings
361 */
362 public function getTargets() {
363 return $this->targets;
364 }
365
366 /**
367 * Get the module's load type.
368 *
369 * @since 1.28
370 * @return string ResourceLoaderModule LOAD_* constant
371 */
372 public function getType() {
373 return self::LOAD_GENERAL;
374 }
375
376 /**
377 * Get the skip function.
378 *
379 * Modules that provide fallback functionality can provide a "skip function". This
380 * function, if provided, will be passed along to the module registry on the client.
381 * When this module is loaded (either directly or as a dependency of another module),
382 * then this function is executed first. If the function returns true, the module will
383 * instantly be considered "ready" without requesting the associated module resources.
384 *
385 * The value returned here must be valid javascript for execution in a private function.
386 * It must not contain the "function () {" and "}" wrapper though.
387 *
388 * @return string|null A JavaScript function body returning a boolean value, or null
389 */
390 public function getSkipFunction() {
391 return null;
392 }
393
394 /**
395 * Get the files this module depends on indirectly for a given skin.
396 *
397 * These are only image files referenced by the module's stylesheet.
398 *
399 * @param ResourceLoaderContext $context
400 * @return array List of files
401 */
402 protected function getFileDependencies( ResourceLoaderContext $context ) {
403 $vary = $context->getSkin() . '|' . $context->getLanguage();
404
405 // Try in-object cache first
406 if ( !isset( $this->fileDeps[$vary] ) ) {
407 $dbr = wfGetDB( DB_REPLICA );
408 $deps = $dbr->selectField( 'module_deps',
409 'md_deps',
410 [
411 'md_module' => $this->getName(),
412 'md_skin' => $vary,
413 ],
414 __METHOD__
415 );
416
417 if ( !is_null( $deps ) ) {
418 $this->fileDeps[$vary] = self::expandRelativePaths(
419 (array)FormatJson::decode( $deps, true )
420 );
421 } else {
422 $this->fileDeps[$vary] = [];
423 }
424 }
425 return $this->fileDeps[$vary];
426 }
427
428 /**
429 * Set in-object cache for file dependencies.
430 *
431 * This is used to retrieve data in batches. See ResourceLoader::preloadModuleInfo().
432 * To save the data, use saveFileDependencies().
433 *
434 * @param ResourceLoaderContext $context
435 * @param string[] $files Array of file names
436 */
437 public function setFileDependencies( ResourceLoaderContext $context, $files ) {
438 $vary = $context->getSkin() . '|' . $context->getLanguage();
439 $this->fileDeps[$vary] = $files;
440 }
441
442 /**
443 * Set the files this module depends on indirectly for a given skin.
444 *
445 * @since 1.27
446 * @param ResourceLoaderContext $context
447 * @param array $localFileRefs List of files
448 */
449 protected function saveFileDependencies( ResourceLoaderContext $context, $localFileRefs ) {
450 try {
451 // Related bugs and performance considerations:
452 // 1. Don't needlessly change the database value with the same list in a
453 // different order or with duplicates.
454 // 2. Use relative paths to avoid ghost entries when $IP changes. (T111481)
455 // 3. Don't needlessly replace the database with the same value
456 // just because $IP changed (e.g. when upgrading a wiki).
457 // 4. Don't create an endless replace loop on every request for this
458 // module when '../' is used anywhere. Even though both are expanded
459 // (one expanded by getFileDependencies from the DB, the other is
460 // still raw as originally read by RL), the latter has not
461 // been normalized yet.
462
463 // Normalise
464 $localFileRefs = array_values( array_unique( $localFileRefs ) );
465 sort( $localFileRefs );
466 $localPaths = self::getRelativePaths( $localFileRefs );
467
468 $storedPaths = self::getRelativePaths( $this->getFileDependencies( $context ) );
469 // If the list has been modified since last time we cached it, update the cache
470 if ( $localPaths !== $storedPaths ) {
471 $vary = $context->getSkin() . '|' . $context->getLanguage();
472 $cache = ObjectCache::getLocalClusterInstance();
473 $key = $cache->makeKey( __METHOD__, $this->getName(), $vary );
474 $scopeLock = $cache->getScopedLock( $key, 0 );
475 if ( !$scopeLock ) {
476 return; // T124649; avoid write slams
477 }
478
479 $deps = FormatJson::encode( $localPaths );
480 $dbw = wfGetDB( DB_MASTER );
481 $dbw->upsert( 'module_deps',
482 [
483 'md_module' => $this->getName(),
484 'md_skin' => $vary,
485 'md_deps' => $deps,
486 ],
487 [ 'md_module', 'md_skin' ],
488 [
489 'md_deps' => $deps,
490 ]
491 );
492
493 if ( $dbw->trxLevel() ) {
494 $dbw->onTransactionResolution(
495 function () use ( &$scopeLock ) {
496 ScopedCallback::consume( $scopeLock ); // release after commit
497 },
498 __METHOD__
499 );
500 }
501 }
502 } catch ( Exception $e ) {
503 wfDebugLog( 'resourceloader', __METHOD__ . ": failed to update DB: $e" );
504 }
505 }
506
507 /**
508 * Make file paths relative to MediaWiki directory.
509 *
510 * This is used to make file paths safe for storing in a database without the paths
511 * becoming stale or incorrect when MediaWiki is moved or upgraded (T111481).
512 *
513 * @since 1.27
514 * @param array $filePaths
515 * @return array
516 */
517 public static function getRelativePaths( array $filePaths ) {
518 global $IP;
519 return array_map( function ( $path ) use ( $IP ) {
520 return RelPath::getRelativePath( $path, $IP );
521 }, $filePaths );
522 }
523
524 /**
525 * Expand directories relative to $IP.
526 *
527 * @since 1.27
528 * @param array $filePaths
529 * @return array
530 */
531 public static function expandRelativePaths( array $filePaths ) {
532 global $IP;
533 return array_map( function ( $path ) use ( $IP ) {
534 return RelPath::joinPath( $IP, $path );
535 }, $filePaths );
536 }
537
538 /**
539 * Get the hash of the message blob.
540 *
541 * @since 1.27
542 * @param ResourceLoaderContext $context
543 * @return string|null JSON blob or null if module has no messages
544 */
545 protected function getMessageBlob( ResourceLoaderContext $context ) {
546 if ( !$this->getMessages() ) {
547 // Don't bother consulting MessageBlobStore
548 return null;
549 }
550 // Message blobs may only vary language, not by context keys
551 $lang = $context->getLanguage();
552 if ( !isset( $this->msgBlobs[$lang] ) ) {
553 $this->getLogger()->warning( 'Message blob for {module} should have been preloaded', [
554 'module' => $this->getName(),
555 ] );
556 $store = $context->getResourceLoader()->getMessageBlobStore();
557 $this->msgBlobs[$lang] = $store->getBlob( $this, $lang );
558 }
559 return $this->msgBlobs[$lang];
560 }
561
562 /**
563 * Set in-object cache for message blobs.
564 *
565 * Used to allow fetching of message blobs in batches. See ResourceLoader::preloadModuleInfo().
566 *
567 * @since 1.27
568 * @param string|null $blob JSON blob or null
569 * @param string $lang Language code
570 */
571 public function setMessageBlob( $blob, $lang ) {
572 $this->msgBlobs[$lang] = $blob;
573 }
574
575 /**
576 * Get headers to send as part of a module web response.
577 *
578 * It is not supported to send headers through this method that are
579 * required to be unique or otherwise sent once in an HTTP response
580 * because clients may make batch requests for multiple modules (as
581 * is the default behaviour for ResourceLoader clients).
582 *
583 * For exclusive or aggregated headers, see ResourceLoader::sendResponseHeaders().
584 *
585 * @since 1.30
586 * @param ResourceLoaderContext $context
587 * @return string[] Array of HTTP response headers
588 */
589 final public function getHeaders( ResourceLoaderContext $context ) {
590 $headers = [];
591
592 $formattedLinks = [];
593 foreach ( $this->getPreloadLinks( $context ) as $url => $attribs ) {
594 $link = "<{$url}>;rel=preload";
595 foreach ( $attribs as $key => $val ) {
596 $link .= ";{$key}={$val}";
597 }
598 $formattedLinks[] = $link;
599 }
600 if ( $formattedLinks ) {
601 $headers[] = 'Link: ' . implode( ',', $formattedLinks );
602 }
603
604 return $headers;
605 }
606
607 /**
608 * Get a list of resources that web browsers may preload.
609 *
610 * Behaviour of rel=preload link is specified at <https://www.w3.org/TR/preload/>.
611 *
612 * Use case for ResourceLoader originally part of T164299.
613 *
614 * @par Example
615 * @code
616 * protected function getPreloadLinks() {
617 * return [
618 * 'https://example.org/script.js' => [ 'as' => 'script' ],
619 * 'https://example.org/image.png' => [ 'as' => 'image' ],
620 * ];
621 * }
622 * @endcode
623 *
624 * @par Example using HiDPI image variants
625 * @code
626 * protected function getPreloadLinks() {
627 * return [
628 * 'https://example.org/logo.png' => [
629 * 'as' => 'image',
630 * 'media' => 'not all and (min-resolution: 2dppx)',
631 * ],
632 * 'https://example.org/logo@2x.png' => [
633 * 'as' => 'image',
634 * 'media' => '(min-resolution: 2dppx)',
635 * ],
636 * ];
637 * }
638 * @endcode
639 *
640 * @see ResourceLoaderModule::getHeaders
641 * @since 1.30
642 * @param ResourceLoaderContext $context
643 * @return array Keyed by url, values must be an array containing
644 * at least an 'as' key. Optionally a 'media' key as well.
645 */
646 protected function getPreloadLinks( ResourceLoaderContext $context ) {
647 return [];
648 }
649
650 /**
651 * Get module-specific LESS variables, if any.
652 *
653 * @since 1.27
654 * @param ResourceLoaderContext $context
655 * @return array Module-specific LESS variables.
656 */
657 protected function getLessVars( ResourceLoaderContext $context ) {
658 return [];
659 }
660
661 /**
662 * Get an array of this module's resources. Ready for serving to the web.
663 *
664 * @since 1.26
665 * @param ResourceLoaderContext $context
666 * @return array
667 */
668 public function getModuleContent( ResourceLoaderContext $context ) {
669 $contextHash = $context->getHash();
670 // Cache this expensive operation. This calls builds the scripts, styles, and messages
671 // content which typically involves filesystem and/or database access.
672 if ( !array_key_exists( $contextHash, $this->contents ) ) {
673 $this->contents[$contextHash] = $this->buildContent( $context );
674 }
675 return $this->contents[$contextHash];
676 }
677
678 /**
679 * Bundle all resources attached to this module into an array.
680 *
681 * @since 1.26
682 * @param ResourceLoaderContext $context
683 * @return array
684 */
685 final protected function buildContent( ResourceLoaderContext $context ) {
686 $rl = $context->getResourceLoader();
687 $stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
688 $statStart = microtime( true );
689
690 // Only include properties that are relevant to this context (e.g. only=scripts)
691 // and that are non-empty (e.g. don't include "templates" for modules without
692 // templates). This helps prevent invalidating cache for all modules when new
693 // optional properties are introduced.
694 $content = [];
695
696 // Scripts
697 if ( $context->shouldIncludeScripts() ) {
698 // If we are in debug mode, we'll want to return an array of URLs if possible
699 // However, we can't do this if the module doesn't support it
700 // We also can't do this if there is an only= parameter, because we have to give
701 // the module a way to return a load.php URL without causing an infinite loop
702 if ( $context->getDebug() && !$context->getOnly() && $this->supportsURLLoading() ) {
703 $scripts = $this->getScriptURLsForDebug( $context );
704 } else {
705 $scripts = $this->getScript( $context );
706 // Make the script safe to concatenate by making sure there is at least one
707 // trailing new line at the end of the content. Previously, this looked for
708 // a semi-colon instead, but that breaks concatenation if the semicolon
709 // is inside a comment like "// foo();". Instead, simply use a
710 // line break as separator which matches JavaScript native logic for implicitly
711 // ending statements even if a semi-colon is missing.
712 // Bugs: T29054, T162719.
713 if ( is_string( $scripts )
714 && strlen( $scripts )
715 && substr( $scripts, -1 ) !== "\n"
716 ) {
717 $scripts .= "\n";
718 }
719 }
720 $content['scripts'] = $scripts;
721 }
722
723 // Styles
724 if ( $context->shouldIncludeStyles() ) {
725 $styles = [];
726 // Don't create empty stylesheets like [ '' => '' ] for modules
727 // that don't *have* any stylesheets (T40024).
728 $stylePairs = $this->getStyles( $context );
729 if ( count( $stylePairs ) ) {
730 // If we are in debug mode without &only= set, we'll want to return an array of URLs
731 // See comment near shouldIncludeScripts() for more details
732 if ( $context->getDebug() && !$context->getOnly() && $this->supportsURLLoading() ) {
733 $styles = [
734 'url' => $this->getStyleURLsForDebug( $context )
735 ];
736 } else {
737 // Minify CSS before embedding in mw.loader.implement call
738 // (unless in debug mode)
739 if ( !$context->getDebug() ) {
740 foreach ( $stylePairs as $media => $style ) {
741 // Can be either a string or an array of strings.
742 if ( is_array( $style ) ) {
743 $stylePairs[$media] = [];
744 foreach ( $style as $cssText ) {
745 if ( is_string( $cssText ) ) {
746 $stylePairs[$media][] =
747 ResourceLoader::filter( 'minify-css', $cssText );
748 }
749 }
750 } elseif ( is_string( $style ) ) {
751 $stylePairs[$media] = ResourceLoader::filter( 'minify-css', $style );
752 }
753 }
754 }
755 // Wrap styles into @media groups as needed and flatten into a numerical array
756 $styles = [
757 'css' => $rl->makeCombinedStyles( $stylePairs )
758 ];
759 }
760 }
761 $content['styles'] = $styles;
762 }
763
764 // Messages
765 $blob = $this->getMessageBlob( $context );
766 if ( $blob ) {
767 $content['messagesBlob'] = $blob;
768 }
769
770 $templates = $this->getTemplates();
771 if ( $templates ) {
772 $content['templates'] = $templates;
773 }
774
775 $headers = $this->getHeaders( $context );
776 if ( $headers ) {
777 $content['headers'] = $headers;
778 }
779
780 $statTiming = microtime( true ) - $statStart;
781 $statName = strtr( $this->getName(), '.', '_' );
782 $stats->timing( "resourceloader_build.all", 1000 * $statTiming );
783 $stats->timing( "resourceloader_build.$statName", 1000 * $statTiming );
784
785 return $content;
786 }
787
788 /**
789 * Get a string identifying the current version of this module in a given context.
790 *
791 * Whenever anything happens that changes the module's response (e.g. scripts, styles, and
792 * messages) this value must change. This value is used to store module responses in cache.
793 * (Both client-side and server-side.)
794 *
795 * It is not recommended to override this directly. Use getDefinitionSummary() instead.
796 * If overridden, one must call the parent getVersionHash(), append data and re-hash.
797 *
798 * This method should be quick because it is frequently run by ResourceLoaderStartUpModule to
799 * propagate changes to the client and effectively invalidate cache.
800 *
801 * @since 1.26
802 * @param ResourceLoaderContext $context
803 * @return string Hash (should use ResourceLoader::makeHash)
804 */
805 public function getVersionHash( ResourceLoaderContext $context ) {
806 // The startup module produces a manifest with versions representing the entire module.
807 // Typically, the request for the startup module itself has only=scripts. That must apply
808 // only to the startup module content, and not to the module version computed here.
809 $context = new DerivativeResourceLoaderContext( $context );
810 $context->setModules( [] );
811 // Version hash must cover all resources, regardless of startup request itself.
812 $context->setOnly( null );
813 // Compute version hash based on content, not debug urls.
814 $context->setDebug( false );
815
816 // Cache this somewhat expensive operation. Especially because some classes
817 // (e.g. startup module) iterate more than once over all modules to get versions.
818 $contextHash = $context->getHash();
819 if ( !array_key_exists( $contextHash, $this->versionHash ) ) {
820 if ( $this->enableModuleContentVersion() ) {
821 // Detect changes directly
822 $str = json_encode( $this->getModuleContent( $context ) );
823 } else {
824 // Infer changes based on definition and other metrics
825 $summary = $this->getDefinitionSummary( $context );
826 if ( !isset( $summary['_cacheEpoch'] ) ) {
827 throw new LogicException( 'getDefinitionSummary must call parent method' );
828 }
829 $str = json_encode( $summary );
830 }
831
832 $this->versionHash[$contextHash] = ResourceLoader::makeHash( $str );
833 }
834 return $this->versionHash[$contextHash];
835 }
836
837 /**
838 * Whether to generate version hash based on module content.
839 *
840 * If a module requires database or file system access to build the module
841 * content, consider disabling this in favour of manually tracking relevant
842 * aspects in getDefinitionSummary(). See getVersionHash() for how this is used.
843 *
844 * @return bool
845 */
846 public function enableModuleContentVersion() {
847 return false;
848 }
849
850 /**
851 * Get the definition summary for this module.
852 *
853 * This is the method subclasses are recommended to use to track values in their
854 * version hash. Call this in getVersionHash() and pass it to e.g. json_encode.
855 *
856 * Subclasses must call the parent getDefinitionSummary() and build on that.
857 * It is recommended that each subclass appends its own new array. This prevents
858 * clashes or accidental overwrites of existing keys and gives each subclass
859 * its own scope for simple array keys.
860 *
861 * @code
862 * $summary = parent::getDefinitionSummary( $context );
863 * $summary[] = [
864 * 'foo' => 123,
865 * 'bar' => 'quux',
866 * ];
867 * return $summary;
868 * @endcode
869 *
870 * Return an array containing values from all significant properties of this
871 * module's definition.
872 *
873 * Be careful not to normalise too much. Especially preserve the order of things
874 * that carry significance in getScript and getStyles (T39812).
875 *
876 * Avoid including things that are insiginificant (e.g. order of message keys is
877 * insignificant and should be sorted to avoid unnecessary cache invalidation).
878 *
879 * This data structure must exclusively contain arrays and scalars as values (avoid
880 * object instances) to allow simple serialisation using json_encode.
881 *
882 * If modules have a hash or timestamp from another source, that may be incuded as-is.
883 *
884 * A number of utility methods are available to help you gather data. These are not
885 * called by default and must be included by the subclass' getDefinitionSummary().
886 *
887 * - getMessageBlob()
888 *
889 * @since 1.23
890 * @param ResourceLoaderContext $context
891 * @return array|null
892 */
893 public function getDefinitionSummary( ResourceLoaderContext $context ) {
894 return [
895 '_class' => static::class,
896 '_cacheEpoch' => $this->getConfig()->get( 'CacheEpoch' ),
897 ];
898 }
899
900 /**
901 * Check whether this module is known to be empty. If a child class
902 * has an easy and cheap way to determine that this module is
903 * definitely going to be empty, it should override this method to
904 * return true in that case. Callers may optimize the request for this
905 * module away if this function returns true.
906 * @param ResourceLoaderContext $context
907 * @return bool
908 */
909 public function isKnownEmpty( ResourceLoaderContext $context ) {
910 return false;
911 }
912
913 /**
914 * Check whether this module should be embeded rather than linked
915 *
916 * Modules returning true here will be embedded rather than loaded by
917 * ResourceLoaderClientHtml.
918 *
919 * @since 1.30
920 * @param ResourceLoaderContext $context
921 * @return bool
922 */
923 public function shouldEmbedModule( ResourceLoaderContext $context ) {
924 return $this->getGroup() === 'private';
925 }
926
927 /** @var JSParser Lazy-initialized; use self::javaScriptParser() */
928 private static $jsParser;
929 private static $parseCacheVersion = 1;
930
931 /**
932 * Validate a given script file; if valid returns the original source.
933 * If invalid, returns replacement JS source that throws an exception.
934 *
935 * @param string $fileName
936 * @param string $contents
937 * @return string JS with the original, or a replacement error
938 */
939 protected function validateScriptFile( $fileName, $contents ) {
940 if ( !$this->getConfig()->get( 'ResourceLoaderValidateJS' ) ) {
941 return $contents;
942 }
943 $cache = ObjectCache::getMainWANInstance();
944 return $cache->getWithSetCallback(
945 $cache->makeGlobalKey(
946 'resourceloader',
947 'jsparse',
948 self::$parseCacheVersion,
949 md5( $contents ),
950 $fileName
951 ),
952 $cache::TTL_WEEK,
953 function () use ( $contents, $fileName ) {
954 $parser = self::javaScriptParser();
955 try {
956 $parser->parse( $contents, $fileName, 1 );
957 $result = $contents;
958 } catch ( Exception $e ) {
959 // We'll save this to cache to avoid having to re-validate broken JS
960 $err = $e->getMessage();
961 $result = "mw.log.error(" .
962 Xml::encodeJsVar( "JavaScript parse error: $err" ) . ");";
963 }
964 return $result;
965 }
966 );
967 }
968
969 /**
970 * @return JSParser
971 */
972 protected static function javaScriptParser() {
973 if ( !self::$jsParser ) {
974 self::$jsParser = new JSParser();
975 }
976 return self::$jsParser;
977 }
978
979 /**
980 * Safe version of filemtime(), which doesn't throw a PHP warning if the file doesn't exist.
981 * Defaults to 1.
982 *
983 * @param string $filePath File path
984 * @return int UNIX timestamp
985 */
986 protected static function safeFilemtime( $filePath ) {
987 Wikimedia\suppressWarnings();
988 $mtime = filemtime( $filePath ) ?: 1;
989 Wikimedia\restoreWarnings();
990 return $mtime;
991 }
992
993 /**
994 * Compute a non-cryptographic string hash of a file's contents.
995 * If the file does not exist or cannot be read, returns an empty string.
996 *
997 * @since 1.26 Uses MD4 instead of SHA1.
998 * @param string $filePath File path
999 * @return string Hash
1000 */
1001 protected static function safeFileHash( $filePath ) {
1002 return FileContentsHasher::getFileContentsHash( $filePath );
1003 }
1004 }