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