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