Merge "Add link trail for Azerbaijani language"
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderFileModule.php
1 <?php
2 /**
3 * ResourceLoader module based on local JavaScript/CSS files.
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 /**
26 * ResourceLoader module based on local JavaScript/CSS files.
27 */
28 class ResourceLoaderFileModule extends ResourceLoaderModule {
29
30 /** @var string Local base path, see __construct() */
31 protected $localBasePath = '';
32
33 /** @var string Remote base path, see __construct() */
34 protected $remoteBasePath = '';
35
36 /** @var array Saves a list of the templates named by the modules. */
37 protected $templates = [];
38
39 /**
40 * @var array List of paths to JavaScript files to always include
41 * @par Usage:
42 * @code
43 * [ [file-path], [file-path], ... ]
44 * @endcode
45 */
46 protected $scripts = [];
47
48 /**
49 * @var array List of JavaScript files to include when using a specific language
50 * @par Usage:
51 * @code
52 * [ [language-code] => [ [file-path], [file-path], ... ], ... ]
53 * @endcode
54 */
55 protected $languageScripts = [];
56
57 /**
58 * @var array List of JavaScript files to include when using a specific skin
59 * @par Usage:
60 * @code
61 * [ [skin-name] => [ [file-path], [file-path], ... ], ... ]
62 * @endcode
63 */
64 protected $skinScripts = [];
65
66 /**
67 * @var array List of paths to JavaScript files to include in debug mode
68 * @par Usage:
69 * @code
70 * [ [skin-name] => [ [file-path], [file-path], ... ], ... ]
71 * @endcode
72 */
73 protected $debugScripts = [];
74
75 /**
76 * @var array List of paths to CSS files to always include
77 * @par Usage:
78 * @code
79 * [ [file-path], [file-path], ... ]
80 * @endcode
81 */
82 protected $styles = [];
83
84 /**
85 * @var array List of paths to CSS files to include when using specific skins
86 * @par Usage:
87 * @code
88 * [ [file-path], [file-path], ... ]
89 * @endcode
90 */
91 protected $skinStyles = [];
92
93 /**
94 * @var array List of packaged files to make available through require()
95 * @par Usage:
96 * @code
97 * [ [file-path], [file-path], ... ]
98 * @endcode
99 */
100 protected $packageFiles = null;
101
102 /**
103 * @var array Expanded versions of $packageFiles, lazy-computed by expandPackageFiles();
104 * keyed by context hash
105 */
106 private $expandedPackageFiles = [];
107
108 /**
109 * @var array List of modules this module depends on
110 * @par Usage:
111 * @code
112 * [ [file-path], [file-path], ... ]
113 * @endcode
114 */
115 protected $dependencies = [];
116
117 /**
118 * @var string File name containing the body of the skip function
119 */
120 protected $skipFunction = null;
121
122 /**
123 * @var array List of message keys used by this module
124 * @par Usage:
125 * @code
126 * [ [message-key], [message-key], ... ]
127 * @endcode
128 */
129 protected $messages = [];
130
131 /** @var string Name of group to load this module in */
132 protected $group;
133
134 /** @var bool Link to raw files in debug mode */
135 protected $debugRaw = true;
136
137 /** @var bool Whether mw.loader.state() call should be omitted */
138 protected $raw = false;
139
140 protected $targets = [ 'desktop' ];
141
142 /** @var bool Whether CSSJanus flipping should be skipped for this module */
143 protected $noflip = false;
144
145 /**
146 * @var bool Whether getStyleURLsForDebug should return raw file paths,
147 * or return load.php urls
148 */
149 protected $hasGeneratedStyles = false;
150
151 /**
152 * @var array Place where readStyleFile() tracks file dependencies
153 * @par Usage:
154 * @code
155 * [ [file-path], [file-path], ... ]
156 * @endcode
157 */
158 protected $localFileRefs = [];
159
160 /**
161 * @var array Place where readStyleFile() tracks file dependencies for non-existent files.
162 * Used in tests to detect missing dependencies.
163 */
164 protected $missingLocalFileRefs = [];
165
166 /**
167 * Constructs a new module from an options array.
168 *
169 * @param array $options List of options; if not given or empty, an empty module will be
170 * constructed
171 * @param string|null $localBasePath Base path to prepend to all local paths in $options.
172 * Defaults to $IP
173 * @param string|null $remoteBasePath Base path to prepend to all remote paths in $options.
174 * Defaults to $wgResourceBasePath
175 *
176 * Below is a description for the $options array:
177 * @throws InvalidArgumentException
178 * @par Construction options:
179 * @code
180 * [
181 * // Base path to prepend to all local paths in $options. Defaults to $IP
182 * 'localBasePath' => [base path],
183 * // Base path to prepend to all remote paths in $options. Defaults to $wgResourceBasePath
184 * 'remoteBasePath' => [base path],
185 * // Equivalent of remoteBasePath, but relative to $wgExtensionAssetsPath
186 * 'remoteExtPath' => [base path],
187 * // Equivalent of remoteBasePath, but relative to $wgStylePath
188 * 'remoteSkinPath' => [base path],
189 * // Scripts to always include (cannot be set if 'packageFiles' is also set, see below)
190 * 'scripts' => [file path string or array of file path strings],
191 * // Scripts to include in specific language contexts
192 * 'languageScripts' => [
193 * [language code] => [file path string or array of file path strings],
194 * ],
195 * // Scripts to include in specific skin contexts
196 * 'skinScripts' => [
197 * [skin name] => [file path string or array of file path strings],
198 * ],
199 * // Scripts to include in debug contexts
200 * 'debugScripts' => [file path string or array of file path strings],
201 * // For package modules: files to be made available for internal require() do not
202 * // need to have 'type' defined; it will be inferred from the file name extension
203 * // if omitted. 'config' can only be used when 'type' is 'data'; the variables are
204 * // resolved with Config::get(). The first entry in 'packageFiles' is always the
205 * // module entry point. If 'packageFiles' is set, 'scripts' cannot also be set.
206 * 'packageFiles' => [
207 * [file path string], // or:
208 * [ 'name' => [file name], 'file' => [file path], 'type' => 'script'|'data' ], // or:
209 * [ 'name' => [name], 'content' => [string], 'type' => 'script'|'data' ], // or:
210 * [ 'name' => [name], 'callback' => [callable], 'type' => 'script'|'data' ],
211 * [ 'name' => [name], 'config' => [ [config var name], ... ], 'type' => 'data' ],
212 * [ 'name' => [name], 'config' => [ [JS name] => [PHP name] ], 'type' => 'data' ],
213 * ],
214 * // Modules which must be loaded before this module
215 * 'dependencies' => [module name string or array of module name strings],
216 * 'templates' => [
217 * [template alias with file.ext] => [file path to a template file],
218 * ],
219 * // Styles to always load
220 * 'styles' => [file path string or array of file path strings],
221 * // Styles to include in specific skin contexts
222 * 'skinStyles' => [
223 * [skin name] => [file path string or array of file path strings],
224 * ],
225 * // Messages to always load
226 * 'messages' => [array of message key strings],
227 * // Group which this module should be loaded together with
228 * 'group' => [group name string],
229 * // Function that, if it returns true, makes the loader skip this module.
230 * // The file must contain valid JavaScript for execution in a private function.
231 * // The file must not contain the "function () {" and "}" wrapper though.
232 * 'skipFunction' => [file path]
233 * ]
234 * @endcode
235 */
236 public function __construct(
237 $options = [],
238 $localBasePath = null,
239 $remoteBasePath = null
240 ) {
241 // Flag to decide whether to automagically add the mediawiki.template module
242 $hasTemplates = false;
243 // localBasePath and remoteBasePath both have unbelievably long fallback chains
244 // and need to be handled separately.
245 list( $this->localBasePath, $this->remoteBasePath ) =
246 self::extractBasePaths( $options, $localBasePath, $remoteBasePath );
247
248 // Extract, validate and normalise remaining options
249 foreach ( $options as $member => $option ) {
250 switch ( $member ) {
251 // Lists of file paths
252 case 'scripts':
253 case 'debugScripts':
254 case 'styles':
255 case 'packageFiles':
256 $this->{$member} = (array)$option;
257 break;
258 case 'templates':
259 $hasTemplates = true;
260 $this->{$member} = (array)$option;
261 break;
262 // Collated lists of file paths
263 case 'languageScripts':
264 case 'skinScripts':
265 case 'skinStyles':
266 if ( !is_array( $option ) ) {
267 throw new InvalidArgumentException(
268 "Invalid collated file path list error. " .
269 "'$option' given, array expected."
270 );
271 }
272 foreach ( $option as $key => $value ) {
273 if ( !is_string( $key ) ) {
274 throw new InvalidArgumentException(
275 "Invalid collated file path list key error. " .
276 "'$key' given, string expected."
277 );
278 }
279 $this->{$member}[$key] = (array)$value;
280 }
281 break;
282 case 'deprecated':
283 $this->deprecated = $option;
284 break;
285 // Lists of strings
286 case 'dependencies':
287 case 'messages':
288 case 'targets':
289 // Normalise
290 $option = array_values( array_unique( (array)$option ) );
291 sort( $option );
292
293 $this->{$member} = $option;
294 break;
295 // Single strings
296 case 'group':
297 case 'skipFunction':
298 $this->{$member} = (string)$option;
299 break;
300 // Single booleans
301 case 'debugRaw':
302 case 'raw':
303 case 'noflip':
304 $this->{$member} = (bool)$option;
305 break;
306 }
307 }
308 if ( isset( $options['scripts'] ) && isset( $options['packageFiles'] ) ) {
309 throw new InvalidArgumentException( "A module may not set both 'scripts' and 'packageFiles'" );
310 }
311 if ( $hasTemplates ) {
312 $this->dependencies[] = 'mediawiki.template';
313 // Ensure relevant template compiler module gets loaded
314 foreach ( $this->templates as $alias => $templatePath ) {
315 if ( is_int( $alias ) ) {
316 $alias = $templatePath;
317 }
318 $suffix = explode( '.', $alias );
319 $suffix = end( $suffix );
320 $compilerModule = 'mediawiki.template.' . $suffix;
321 if ( $suffix !== 'html' && !in_array( $compilerModule, $this->dependencies ) ) {
322 $this->dependencies[] = $compilerModule;
323 }
324 }
325 }
326 }
327
328 /**
329 * Extract a pair of local and remote base paths from module definition information.
330 * Implementation note: the amount of global state used in this function is staggering.
331 *
332 * @param array $options Module definition
333 * @param string|null $localBasePath Path to use if not provided in module definition. Defaults
334 * to $IP
335 * @param string|null $remoteBasePath Path to use if not provided in module definition. Defaults
336 * to $wgResourceBasePath
337 * @return array [ localBasePath, remoteBasePath ]
338 */
339 public static function extractBasePaths(
340 $options = [],
341 $localBasePath = null,
342 $remoteBasePath = null
343 ) {
344 global $IP, $wgResourceBasePath;
345
346 // The different ways these checks are done, and their ordering, look very silly,
347 // but were preserved for backwards-compatibility just in case. Tread lightly.
348
349 if ( $localBasePath === null ) {
350 $localBasePath = $IP;
351 }
352 if ( $remoteBasePath === null ) {
353 $remoteBasePath = $wgResourceBasePath;
354 }
355
356 if ( isset( $options['remoteExtPath'] ) ) {
357 global $wgExtensionAssetsPath;
358 $remoteBasePath = $wgExtensionAssetsPath . '/' . $options['remoteExtPath'];
359 }
360
361 if ( isset( $options['remoteSkinPath'] ) ) {
362 global $wgStylePath;
363 $remoteBasePath = $wgStylePath . '/' . $options['remoteSkinPath'];
364 }
365
366 if ( array_key_exists( 'localBasePath', $options ) ) {
367 $localBasePath = (string)$options['localBasePath'];
368 }
369
370 if ( array_key_exists( 'remoteBasePath', $options ) ) {
371 $remoteBasePath = (string)$options['remoteBasePath'];
372 }
373
374 return [ $localBasePath, $remoteBasePath ];
375 }
376
377 /**
378 * Gets all scripts for a given context concatenated together.
379 *
380 * @param ResourceLoaderContext $context Context in which to generate script
381 * @return string|array JavaScript code for $context, or package files data structure
382 */
383 public function getScript( ResourceLoaderContext $context ) {
384 $deprecationScript = $this->getDeprecationInformation();
385 if ( $this->packageFiles !== null ) {
386 $packageFiles = $this->getPackageFiles( $context );
387 if ( $deprecationScript ) {
388 $mainFile =& $packageFiles['files'][$packageFiles['main']];
389 $mainFile['content'] = $deprecationScript . $mainFile['content'];
390 }
391 return $packageFiles;
392 }
393
394 $files = $this->getScriptFiles( $context );
395 return $deprecationScript . $this->readScriptFiles( $files );
396 }
397
398 /**
399 * @param ResourceLoaderContext $context
400 * @return array
401 */
402 public function getScriptURLsForDebug( ResourceLoaderContext $context ) {
403 $urls = [];
404 foreach ( $this->getScriptFiles( $context ) as $file ) {
405 $urls[] = OutputPage::transformResourcePath(
406 $this->getConfig(),
407 $this->getRemotePath( $file )
408 );
409 }
410 return $urls;
411 }
412
413 /**
414 * @return bool
415 */
416 public function supportsURLLoading() {
417 // If package files are involved, don't support URL loading, because that breaks
418 // scoped require() functions
419 return $this->debugRaw && !$this->packageFiles;
420 }
421
422 /**
423 * Get all styles for a given context.
424 *
425 * @param ResourceLoaderContext $context
426 * @return array CSS code for $context as an associative array mapping media type to CSS text.
427 */
428 public function getStyles( ResourceLoaderContext $context ) {
429 $styles = $this->readStyleFiles(
430 $this->getStyleFiles( $context ),
431 $this->getFlip( $context ),
432 $context
433 );
434 // Collect referenced files
435 $this->saveFileDependencies( $context, $this->localFileRefs );
436
437 return $styles;
438 }
439
440 /**
441 * @param ResourceLoaderContext $context
442 * @return array
443 */
444 public function getStyleURLsForDebug( ResourceLoaderContext $context ) {
445 if ( $this->hasGeneratedStyles ) {
446 // Do the default behaviour of returning a url back to load.php
447 // but with only=styles.
448 return parent::getStyleURLsForDebug( $context );
449 }
450 // Our module consists entirely of real css files,
451 // in debug mode we can load those directly.
452 $urls = [];
453 foreach ( $this->getStyleFiles( $context ) as $mediaType => $list ) {
454 $urls[$mediaType] = [];
455 foreach ( $list as $file ) {
456 $urls[$mediaType][] = OutputPage::transformResourcePath(
457 $this->getConfig(),
458 $this->getRemotePath( $file )
459 );
460 }
461 }
462 return $urls;
463 }
464
465 /**
466 * Gets list of message keys used by this module.
467 *
468 * @return array List of message keys
469 */
470 public function getMessages() {
471 return $this->messages;
472 }
473
474 /**
475 * Gets the name of the group this module should be loaded in.
476 *
477 * @return string Group name
478 */
479 public function getGroup() {
480 return $this->group;
481 }
482
483 /**
484 * Gets list of names of modules this module depends on.
485 * @param ResourceLoaderContext|null $context
486 * @return array List of module names
487 */
488 public function getDependencies( ResourceLoaderContext $context = null ) {
489 return $this->dependencies;
490 }
491
492 /**
493 * Get the skip function.
494 * @return null|string
495 * @throws MWException
496 */
497 public function getSkipFunction() {
498 if ( !$this->skipFunction ) {
499 return null;
500 }
501
502 $localPath = $this->getLocalPath( $this->skipFunction );
503 if ( !file_exists( $localPath ) ) {
504 throw new MWException( __METHOD__ . ": skip function file not found: \"$localPath\"" );
505 }
506 $contents = $this->stripBom( file_get_contents( $localPath ) );
507 return $contents;
508 }
509
510 /**
511 * @return bool
512 */
513 public function isRaw() {
514 return $this->raw;
515 }
516
517 /**
518 * Disable module content versioning.
519 *
520 * This class uses getDefinitionSummary() instead, to avoid filesystem overhead
521 * involved with building the full module content inside a startup request.
522 *
523 * @return bool
524 */
525 public function enableModuleContentVersion() {
526 return false;
527 }
528
529 /**
530 * Helper method for getDefinitionSummary.
531 *
532 * @see ResourceLoaderModule::getFileDependencies
533 * @param ResourceLoaderContext $context
534 * @return array
535 */
536 private function getFileHashes( ResourceLoaderContext $context ) {
537 $files = [];
538
539 // Flatten style files into $files
540 $styles = self::collateFilePathListByOption( $this->styles, 'media', 'all' );
541 foreach ( $styles as $styleFiles ) {
542 $files = array_merge( $files, $styleFiles );
543 }
544
545 $skinFiles = self::collateFilePathListByOption(
546 self::tryForKey( $this->skinStyles, $context->getSkin(), 'default' ),
547 'media',
548 'all'
549 );
550 foreach ( $skinFiles as $styleFiles ) {
551 $files = array_merge( $files, $styleFiles );
552 }
553
554 // Extract file names for package files
555 $expandedPackageFiles = $this->expandPackageFiles( $context );
556 $packageFiles = $expandedPackageFiles ?
557 array_filter( array_map( function ( $fileInfo ) {
558 return $fileInfo['filePath'] ?? null;
559 }, $expandedPackageFiles['files'] ) ) :
560 [];
561
562 // Final merge, this should result in a master list of dependent files
563 $files = array_merge(
564 $files,
565 $packageFiles,
566 $this->scripts,
567 $this->templates,
568 $context->getDebug() ? $this->debugScripts : [],
569 $this->getLanguageScripts( $context->getLanguage() ),
570 self::tryForKey( $this->skinScripts, $context->getSkin(), 'default' )
571 );
572 if ( $this->skipFunction ) {
573 $files[] = $this->skipFunction;
574 }
575 $files = array_map( [ $this, 'getLocalPath' ], $files );
576 // File deps need to be treated separately because they're already prefixed
577 $files = array_merge( $files, $this->getFileDependencies( $context ) );
578 // Filter out any duplicates from getFileDependencies() and others.
579 // Most commonly introduced by compileLessFile(), which always includes the
580 // entry point Less file we already know about.
581 $files = array_values( array_unique( $files ) );
582
583 // Don't include keys or file paths here, only the hashes. Including that would needlessly
584 // cause global cache invalidation when files move or if e.g. the MediaWiki path changes.
585 // Any significant ordering is already detected by the definition summary.
586 return array_map( [ __CLASS__, 'safeFileHash' ], $files );
587 }
588
589 /**
590 * Get the definition summary for this module.
591 *
592 * @param ResourceLoaderContext $context
593 * @return array
594 */
595 public function getDefinitionSummary( ResourceLoaderContext $context ) {
596 $summary = parent::getDefinitionSummary( $context );
597
598 $options = [];
599 foreach ( [
600 // The following properties are omitted because they don't affect the module reponse:
601 // - localBasePath (Per T104950; Changes when absolute directory name changes. If
602 // this affects 'scripts' and other file paths, getFileHashes accounts for that.)
603 // - remoteBasePath (Per T104950)
604 // - dependencies (provided via startup module)
605 // - targets
606 // - group (provided via startup module)
607 'scripts',
608 'debugScripts',
609 'styles',
610 'languageScripts',
611 'skinScripts',
612 'skinStyles',
613 'messages',
614 'templates',
615 'skipFunction',
616 'debugRaw',
617 'raw',
618 ] as $member ) {
619 $options[$member] = $this->{$member};
620 }
621
622 $packageFiles = $this->expandPackageFiles( $context );
623 if ( $packageFiles ) {
624 // Extract the minimum needed:
625 // - The 'main' pointer (included as-is).
626 // - The 'files' array, simplied to only which files exist (the keys of
627 // this array), and something that represents their non-file content.
628 // For packaged files that reflect files directly from disk, the
629 // 'getFileHashes' method tracks this already.
630 // It is important that the keys of the 'files' array are preserved,
631 // as they affect the module output.
632 $packageFiles['files'] = array_map( function ( $fileInfo ) {
633 return $fileInfo['definitionSummary'] ?? ( $fileInfo['content'] ?? null );
634 }, $packageFiles['files'] );
635 }
636
637 $summary[] = [
638 'options' => $options,
639 'packageFiles' => $packageFiles,
640 'fileHashes' => $this->getFileHashes( $context ),
641 'messageBlob' => $this->getMessageBlob( $context ),
642 ];
643
644 $lessVars = $this->getLessVars( $context );
645 if ( $lessVars ) {
646 $summary[] = [ 'lessVars' => $lessVars ];
647 }
648
649 return $summary;
650 }
651
652 /**
653 * @param string|ResourceLoaderFilePath $path
654 * @return string
655 */
656 protected function getLocalPath( $path ) {
657 if ( $path instanceof ResourceLoaderFilePath ) {
658 return $path->getLocalPath();
659 }
660
661 return "{$this->localBasePath}/$path";
662 }
663
664 /**
665 * @param string|ResourceLoaderFilePath $path
666 * @return string
667 */
668 protected function getRemotePath( $path ) {
669 if ( $path instanceof ResourceLoaderFilePath ) {
670 return $path->getRemotePath();
671 }
672
673 return "{$this->remoteBasePath}/$path";
674 }
675
676 /**
677 * Infer the stylesheet language from a stylesheet file path.
678 *
679 * @since 1.22
680 * @param string $path
681 * @return string The stylesheet language name
682 */
683 public function getStyleSheetLang( $path ) {
684 return preg_match( '/\.less$/i', $path ) ? 'less' : 'css';
685 }
686
687 /**
688 * Infer the file type from a package file path.
689 * @param string $path
690 * @return string 'script' or 'data'
691 */
692 public static function getPackageFileType( $path ) {
693 if ( preg_match( '/\.json$/i', $path ) ) {
694 return 'data';
695 }
696 return 'script';
697 }
698
699 /**
700 * Collates file paths by option (where provided).
701 *
702 * @param array $list List of file paths in any combination of index/path
703 * or path/options pairs
704 * @param string $option Option name
705 * @param mixed $default Default value if the option isn't set
706 * @return array List of file paths, collated by $option
707 */
708 protected static function collateFilePathListByOption( array $list, $option, $default ) {
709 $collatedFiles = [];
710 foreach ( (array)$list as $key => $value ) {
711 if ( is_int( $key ) ) {
712 // File name as the value
713 if ( !isset( $collatedFiles[$default] ) ) {
714 $collatedFiles[$default] = [];
715 }
716 $collatedFiles[$default][] = $value;
717 } elseif ( is_array( $value ) ) {
718 // File name as the key, options array as the value
719 $optionValue = $value[$option] ?? $default;
720 if ( !isset( $collatedFiles[$optionValue] ) ) {
721 $collatedFiles[$optionValue] = [];
722 }
723 $collatedFiles[$optionValue][] = $key;
724 }
725 }
726 return $collatedFiles;
727 }
728
729 /**
730 * Get a list of element that match a key, optionally using a fallback key.
731 *
732 * @param array $list List of lists to select from
733 * @param string $key Key to look for in $map
734 * @param string|null $fallback Key to look for in $list if $key doesn't exist
735 * @return array List of elements from $map which matched $key or $fallback,
736 * or an empty list in case of no match
737 */
738 protected static function tryForKey( array $list, $key, $fallback = null ) {
739 if ( isset( $list[$key] ) && is_array( $list[$key] ) ) {
740 return $list[$key];
741 } elseif ( is_string( $fallback )
742 && isset( $list[$fallback] )
743 && is_array( $list[$fallback] )
744 ) {
745 return $list[$fallback];
746 }
747 return [];
748 }
749
750 /**
751 * Get a list of script file paths for this module, in order of proper execution.
752 *
753 * @param ResourceLoaderContext $context
754 * @return array List of file paths
755 */
756 private function getScriptFiles( ResourceLoaderContext $context ) {
757 $files = array_merge(
758 $this->scripts,
759 $this->getLanguageScripts( $context->getLanguage() ),
760 self::tryForKey( $this->skinScripts, $context->getSkin(), 'default' )
761 );
762 if ( $context->getDebug() ) {
763 $files = array_merge( $files, $this->debugScripts );
764 }
765
766 return array_unique( $files, SORT_REGULAR );
767 }
768
769 /**
770 * Get the set of language scripts for the given language,
771 * possibly using a fallback language.
772 *
773 * @param string $lang
774 * @return array
775 */
776 private function getLanguageScripts( $lang ) {
777 $scripts = self::tryForKey( $this->languageScripts, $lang );
778 if ( $scripts ) {
779 return $scripts;
780 }
781 $fallbacks = Language::getFallbacksFor( $lang );
782 foreach ( $fallbacks as $lang ) {
783 $scripts = self::tryForKey( $this->languageScripts, $lang );
784 if ( $scripts ) {
785 return $scripts;
786 }
787 }
788
789 return [];
790 }
791
792 /**
793 * Get a list of file paths for all styles in this module, in order of proper inclusion.
794 *
795 * This is considered a private method. Exposed for internal use by WebInstallerOutput.
796 *
797 * @private
798 * @param ResourceLoaderContext $context
799 * @return array List of file paths
800 */
801 public function getStyleFiles( ResourceLoaderContext $context ) {
802 return array_merge_recursive(
803 self::collateFilePathListByOption( $this->styles, 'media', 'all' ),
804 self::collateFilePathListByOption(
805 self::tryForKey( $this->skinStyles, $context->getSkin(), 'default' ),
806 'media',
807 'all'
808 )
809 );
810 }
811
812 /**
813 * Gets a list of file paths for all skin styles in the module used by
814 * the skin.
815 *
816 * @param string $skinName The name of the skin
817 * @return array A list of file paths collated by media type
818 */
819 protected function getSkinStyleFiles( $skinName ) {
820 return self::collateFilePathListByOption(
821 self::tryForKey( $this->skinStyles, $skinName ),
822 'media',
823 'all'
824 );
825 }
826
827 /**
828 * Gets a list of file paths for all skin style files in the module,
829 * for all available skins.
830 *
831 * @return array A list of file paths collated by media type
832 */
833 protected function getAllSkinStyleFiles() {
834 $styleFiles = [];
835 $internalSkinNames = array_keys( Skin::getSkinNames() );
836 $internalSkinNames[] = 'default';
837
838 foreach ( $internalSkinNames as $internalSkinName ) {
839 $styleFiles = array_merge_recursive(
840 $styleFiles,
841 $this->getSkinStyleFiles( $internalSkinName )
842 );
843 }
844
845 return $styleFiles;
846 }
847
848 /**
849 * Returns all style files and all skin style files used by this module.
850 *
851 * @return array
852 */
853 public function getAllStyleFiles() {
854 $collatedStyleFiles = array_merge_recursive(
855 self::collateFilePathListByOption( $this->styles, 'media', 'all' ),
856 $this->getAllSkinStyleFiles()
857 );
858
859 $result = [];
860
861 foreach ( $collatedStyleFiles as $media => $styleFiles ) {
862 foreach ( $styleFiles as $styleFile ) {
863 $result[] = $this->getLocalPath( $styleFile );
864 }
865 }
866
867 return $result;
868 }
869
870 /**
871 * Get the contents of a list of JavaScript files. Helper for getScript().
872 *
873 * @param array $scripts List of file paths to scripts to read, remap and concetenate
874 * @return string Concatenated JavaScript data from $scripts
875 * @throws MWException
876 */
877 private function readScriptFiles( array $scripts ) {
878 if ( empty( $scripts ) ) {
879 return '';
880 }
881 $js = '';
882 foreach ( array_unique( $scripts, SORT_REGULAR ) as $fileName ) {
883 $localPath = $this->getLocalPath( $fileName );
884 if ( !file_exists( $localPath ) ) {
885 throw new MWException( __METHOD__ . ": script file not found: \"$localPath\"" );
886 }
887 $contents = $this->stripBom( file_get_contents( $localPath ) );
888 $js .= $contents . "\n";
889 }
890 return $js;
891 }
892
893 /**
894 * Get the contents of a list of CSS files.
895 *
896 * @internal This is considered a private method. Exposed for internal use by WebInstallerOutput.
897 * @param array $styles Map of media type to file paths to read, remap, and concatenate
898 * @param bool $flip
899 * @param ResourceLoaderContext $context
900 * @return array List of concatenated and remapped CSS data from $styles,
901 * keyed by media type
902 * @throws MWException
903 */
904 public function readStyleFiles( array $styles, $flip, $context ) {
905 if ( !$styles ) {
906 return [];
907 }
908 foreach ( $styles as $media => $files ) {
909 $uniqueFiles = array_unique( $files, SORT_REGULAR );
910 $styleFiles = [];
911 foreach ( $uniqueFiles as $file ) {
912 $styleFiles[] = $this->readStyleFile( $file, $flip, $context );
913 }
914 $styles[$media] = implode( "\n", $styleFiles );
915 }
916 return $styles;
917 }
918
919 /**
920 * Reads a style file.
921 *
922 * This method can be used as a callback for array_map()
923 *
924 * @param string $path File path of style file to read
925 * @param bool $flip
926 * @param ResourceLoaderContext $context
927 *
928 * @return string CSS data in script file
929 * @throws MWException If the file doesn't exist
930 */
931 protected function readStyleFile( $path, $flip, $context ) {
932 $localPath = $this->getLocalPath( $path );
933 $remotePath = $this->getRemotePath( $path );
934 if ( !file_exists( $localPath ) ) {
935 $msg = __METHOD__ . ": style file not found: \"$localPath\"";
936 wfDebugLog( 'resourceloader', $msg );
937 throw new MWException( $msg );
938 }
939
940 if ( $this->getStyleSheetLang( $localPath ) === 'less' ) {
941 $style = $this->compileLessFile( $localPath, $context );
942 $this->hasGeneratedStyles = true;
943 } else {
944 $style = $this->stripBom( file_get_contents( $localPath ) );
945 }
946
947 if ( $flip ) {
948 $style = CSSJanus::transform( $style, true, false );
949 }
950 $localDir = dirname( $localPath );
951 $remoteDir = dirname( $remotePath );
952 // Get and register local file references
953 $localFileRefs = CSSMin::getLocalFileReferences( $style, $localDir );
954 foreach ( $localFileRefs as $file ) {
955 if ( file_exists( $file ) ) {
956 $this->localFileRefs[] = $file;
957 } else {
958 $this->missingLocalFileRefs[] = $file;
959 }
960 }
961 // Don't cache this call. remap() ensures data URIs embeds are up to date,
962 // and urls contain correct content hashes in their query string. (T128668)
963 return CSSMin::remap( $style, $localDir, $remoteDir, true );
964 }
965
966 /**
967 * Get whether CSS for this module should be flipped
968 * @param ResourceLoaderContext $context
969 * @return bool
970 */
971 public function getFlip( $context ) {
972 return $context->getDirection() === 'rtl' && !$this->noflip;
973 }
974
975 /**
976 * Get target(s) for the module, eg ['desktop'] or ['desktop', 'mobile']
977 *
978 * @return array Array of strings
979 */
980 public function getTargets() {
981 return $this->targets;
982 }
983
984 /**
985 * Get the module's load type.
986 *
987 * @since 1.28
988 * @return string
989 */
990 public function getType() {
991 $canBeStylesOnly = !(
992 // All options except 'styles', 'skinStyles' and 'debugRaw'
993 $this->scripts
994 || $this->debugScripts
995 || $this->templates
996 || $this->languageScripts
997 || $this->skinScripts
998 || $this->dependencies
999 || $this->messages
1000 || $this->skipFunction
1001 || $this->raw
1002 );
1003 return $canBeStylesOnly ? self::LOAD_STYLES : self::LOAD_GENERAL;
1004 }
1005
1006 /**
1007 * Compile a LESS file into CSS.
1008 *
1009 * Keeps track of all used files and adds them to localFileRefs.
1010 *
1011 * @since 1.22
1012 * @since 1.27 Added $context paramter.
1013 * @throws Exception If less.php encounters a parse error
1014 * @param string $fileName File path of LESS source
1015 * @param ResourceLoaderContext $context Context in which to generate script
1016 * @return string CSS source
1017 */
1018 protected function compileLessFile( $fileName, ResourceLoaderContext $context ) {
1019 static $cache;
1020
1021 if ( !$cache ) {
1022 $cache = ObjectCache::getLocalServerInstance( CACHE_ANYTHING );
1023 }
1024
1025 $vars = $this->getLessVars( $context );
1026 // Construct a cache key from the LESS file name, and a hash digest
1027 // of the LESS variables used for compilation.
1028 ksort( $vars );
1029 $varsHash = hash( 'md4', serialize( $vars ) );
1030 $cacheKey = $cache->makeGlobalKey( 'LESS', $fileName, $varsHash );
1031 $cachedCompile = $cache->get( $cacheKey );
1032
1033 // If we got a cached value, we have to validate it by getting a
1034 // checksum of all the files that were loaded by the parser and
1035 // ensuring it matches the cached entry's.
1036 if ( isset( $cachedCompile['hash'] ) ) {
1037 $contentHash = FileContentsHasher::getFileContentsHash( $cachedCompile['files'] );
1038 if ( $contentHash === $cachedCompile['hash'] ) {
1039 $this->localFileRefs = array_merge( $this->localFileRefs, $cachedCompile['files'] );
1040 return $cachedCompile['css'];
1041 }
1042 }
1043
1044 $compiler = $context->getResourceLoader()->getLessCompiler( $vars );
1045 $css = $compiler->parseFile( $fileName )->getCss();
1046 $files = $compiler->AllParsedFiles();
1047 $this->localFileRefs = array_merge( $this->localFileRefs, $files );
1048
1049 // Cache for 24 hours (86400 seconds).
1050 $cache->set( $cacheKey, [
1051 'css' => $css,
1052 'files' => $files,
1053 'hash' => FileContentsHasher::getFileContentsHash( $files ),
1054 ], 3600 * 24 );
1055
1056 return $css;
1057 }
1058
1059 /**
1060 * Takes named templates by the module and returns an array mapping.
1061 * @return array Templates mapping template alias to content
1062 * @throws MWException
1063 */
1064 public function getTemplates() {
1065 $templates = [];
1066
1067 foreach ( $this->templates as $alias => $templatePath ) {
1068 // Alias is optional
1069 if ( is_int( $alias ) ) {
1070 $alias = $templatePath;
1071 }
1072 $localPath = $this->getLocalPath( $templatePath );
1073 if ( file_exists( $localPath ) ) {
1074 $content = file_get_contents( $localPath );
1075 $templates[$alias] = $this->stripBom( $content );
1076 } else {
1077 $msg = __METHOD__ . ": template file not found: \"$localPath\"";
1078 wfDebugLog( 'resourceloader', $msg );
1079 throw new MWException( $msg );
1080 }
1081 }
1082 return $templates;
1083 }
1084
1085 /**
1086 * Internal helper for use by getPackageFiles(), getFileHashes() and getDefinitionSummary().
1087 *
1088 * This expands the 'packageFiles' definition into something that's (almost) the right format
1089 * for getPackageFiles() to return. It expands shorthands, resolves config vars, and handles
1090 * summarising any non-file data for getVersionHash(). For file-based data, getFileHashes()
1091 * handles it instead, which also ends up in getDefinitionSummary().
1092 *
1093 * What it does not do is reading the actual contents of any specified files, nor invoking
1094 * the computation callbacks. Those things are done by getPackageFiles() instead to improve
1095 * backend performance by only doing this work when the module response is needed, and not
1096 * when merely computing the version hash for StartupModule, or when checking
1097 * If-None-Match headers for a HTTP 304 response.
1098 *
1099 * @param ResourceLoaderContext $context
1100 * @return array|null
1101 * @throws MWException If the 'packageFiles' definition is invalid.
1102 */
1103 private function expandPackageFiles( ResourceLoaderContext $context ) {
1104 $hash = $context->getHash();
1105 if ( isset( $this->expandedPackageFiles[$hash] ) ) {
1106 return $this->expandedPackageFiles[$hash];
1107 }
1108 if ( $this->packageFiles === null ) {
1109 return null;
1110 }
1111 $expandedFiles = [];
1112 $mainFile = null;
1113
1114 foreach ( $this->packageFiles as $alias => $fileInfo ) {
1115 if ( is_string( $fileInfo ) ) {
1116 $fileInfo = [ 'name' => $fileInfo, 'file' => $fileInfo ];
1117 } elseif ( !isset( $fileInfo['name'] ) ) {
1118 $msg = __METHOD__ . ": invalid package file definition for module " .
1119 "\"{$this->getName()}\": 'name' key is required when value is not a string";
1120 wfDebugLog( 'resourceloader', $msg );
1121 throw new MWException( $msg );
1122 }
1123
1124 // Infer type from alias if needed
1125 $type = $fileInfo['type'] ?? self::getPackageFileType( $fileInfo['name'] );
1126 $expanded = [ 'type' => $type ];
1127 if ( !empty( $fileInfo['main'] ) ) {
1128 $mainFile = $fileInfo['name'];
1129 if ( $type !== 'script' ) {
1130 $msg = __METHOD__ . ": invalid package file definition for module " .
1131 "\"{$this->getName()}\": main file \"$mainFile\" must be of type \"script\", not \"$type\"";
1132 wfDebugLog( 'resourceloader', $msg );
1133 throw new MWException( $msg );
1134 }
1135 }
1136
1137 // Perform expansions (except 'file' and 'callback'), creating one of these keys:
1138 // - 'content': literal value.
1139 // - 'filePath': content to be read from a file.
1140 // - 'callback': content computed by a callable.
1141 if ( isset( $fileInfo['content'] ) ) {
1142 $expanded['content'] = $fileInfo['content'];
1143 } elseif ( isset( $fileInfo['file'] ) ) {
1144 $expanded['filePath'] = $fileInfo['file'];
1145 } elseif ( isset( $fileInfo['callback'] ) ) {
1146 if ( !is_callable( $fileInfo['callback'] ) ) {
1147 $msg = __METHOD__ . ": invalid callback for package file \"{$fileInfo['name']}\"" .
1148 " in module \"{$this->getName()}\"";
1149 wfDebugLog( 'resourceloader', $msg );
1150 throw new MWException( $msg );
1151 }
1152 if ( isset( $fileInfo['versionCallback'] ) ) {
1153 if ( !is_callable( $fileInfo['versionCallback'] ) ) {
1154 throw new MWException( __METHOD__ . ": invalid versionCallback for file" .
1155 " \"{$fileInfo['name']}\" in module \"{$this->getName()}\"" );
1156 }
1157 $expanded['definitionSummary'] = ( $fileInfo['versionCallback'] )( $context );
1158 // Don't invoke 'callback' here as it may be expensive (T223260).
1159 $expanded['callback'] = $fileInfo['callback'];
1160 } else {
1161 $expanded['content'] = ( $fileInfo['callback'] )( $context );
1162 }
1163 } elseif ( isset( $fileInfo['config'] ) ) {
1164 if ( $type !== 'data' ) {
1165 $msg = __METHOD__ . ": invalid use of \"config\" for package file \"{$fileInfo['name']}\" " .
1166 "in module \"{$this->getName()}\": type must be \"data\" but is \"$type\"";
1167 wfDebugLog( 'resourceloader', $msg );
1168 throw new MWException( $msg );
1169 }
1170 $expandedConfig = [];
1171 foreach ( $fileInfo['config'] as $key => $var ) {
1172 $expandedConfig[ is_numeric( $key ) ? $var : $key ] = $this->getConfig()->get( $var );
1173 }
1174 $expanded['content'] = $expandedConfig;
1175 } elseif ( !empty( $fileInfo['main'] ) ) {
1176 // [ 'name' => 'foo.js', 'main' => true ] is shorthand
1177 $expanded['filePath'] = $fileInfo['name'];
1178 } else {
1179 $msg = __METHOD__ . ": invalid package file definition for \"{$fileInfo['name']}\" " .
1180 "in module \"{$this->getName()}\": one of \"file\", \"content\", \"callback\" or \"config\" " .
1181 "must be set";
1182 wfDebugLog( 'resourceloader', $msg );
1183 throw new MWException( $msg );
1184 }
1185
1186 $expandedFiles[$fileInfo['name']] = $expanded;
1187 }
1188
1189 if ( $expandedFiles && $mainFile === null ) {
1190 // The first package file that is a script is the main file
1191 foreach ( $expandedFiles as $path => &$file ) {
1192 if ( $file['type'] === 'script' ) {
1193 $mainFile = $path;
1194 break;
1195 }
1196 }
1197 }
1198
1199 $result = [
1200 'main' => $mainFile,
1201 'files' => $expandedFiles
1202 ];
1203
1204 $this->expandedPackageFiles[$hash] = $result;
1205 return $result;
1206 }
1207
1208 /**
1209 * Resolves the package files defintion and generates the content of each package file.
1210 * @param ResourceLoaderContext $context
1211 * @return array Package files data structure, see ResourceLoaderModule::getScript()
1212 */
1213 public function getPackageFiles( ResourceLoaderContext $context ) {
1214 if ( $this->packageFiles === null ) {
1215 return null;
1216 }
1217 $expandedPackageFiles = $this->expandPackageFiles( $context );
1218
1219 // Expand file contents
1220 foreach ( $expandedPackageFiles['files'] as &$fileInfo ) {
1221 // Turn any 'filePath' or 'callback' key into actual 'content',
1222 // and remove the key after that.
1223 if ( isset( $fileInfo['filePath'] ) ) {
1224 $localPath = $this->getLocalPath( $fileInfo['filePath'] );
1225 if ( !file_exists( $localPath ) ) {
1226 $msg = __METHOD__ . ": package file not found: \"$localPath\"" .
1227 " in module \"{$this->getName()}\"";
1228 wfDebugLog( 'resourceloader', $msg );
1229 throw new MWException( $msg );
1230 }
1231 $content = $this->stripBom( file_get_contents( $localPath ) );
1232 if ( $fileInfo['type'] === 'data' ) {
1233 $content = json_decode( $content );
1234 }
1235 $fileInfo['content'] = $content;
1236 unset( $fileInfo['filePath'] );
1237 } elseif ( isset( $fileInfo['callback'] ) ) {
1238 $fileInfo['content'] = ( $fileInfo['callback'] )( $context );
1239 unset( $fileInfo['callback'] );
1240 }
1241
1242 // Not needed for client response, exists for getDefinitionSummary().
1243 unset( $fileInfo['definitionSummary'] );
1244 }
1245
1246 return $expandedPackageFiles;
1247 }
1248
1249 /**
1250 * Takes an input string and removes the UTF-8 BOM character if present
1251 *
1252 * We need to remove these after reading a file, because we concatenate our files and
1253 * the BOM character is not valid in the middle of a string.
1254 * We already assume UTF-8 everywhere, so this should be safe.
1255 *
1256 * @param string $input
1257 * @return string Input minus the intial BOM char
1258 */
1259 protected function stripBom( $input ) {
1260 if ( substr_compare( "\xef\xbb\xbf", $input, 0, 3 ) === 0 ) {
1261 return substr( $input, 3 );
1262 }
1263 return $input;
1264 }
1265 }