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