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