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