Merge "Chinese Conversion Table Update 2017-6"
[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 modules this module depends on
95 * @par Usage:
96 * @code
97 * [ [file-path], [file-path], ... ]
98 * @endcode
99 */
100 protected $dependencies = [];
101
102 /**
103 * @var string File name containing the body of the skip function
104 */
105 protected $skipFunction = null;
106
107 /**
108 * @var array List of message keys used by this module
109 * @par Usage:
110 * @code
111 * [ [message-key], [message-key], ... ]
112 * @endcode
113 */
114 protected $messages = [];
115
116 /** @var string Name of group to load this module in */
117 protected $group;
118
119 /** @var bool Link to raw files in debug mode */
120 protected $debugRaw = true;
121
122 /** @var bool Whether mw.loader.state() call should be omitted */
123 protected $raw = false;
124
125 protected $targets = [ 'desktop' ];
126
127 /** @var bool Whether CSSJanus flipping should be skipped for this module */
128 protected $noflip = false;
129
130 /**
131 * @var bool Whether getStyleURLsForDebug should return raw file paths,
132 * or return load.php urls
133 */
134 protected $hasGeneratedStyles = false;
135
136 /**
137 * @var array Place where readStyleFile() tracks file dependencies
138 * @par Usage:
139 * @code
140 * [ [file-path], [file-path], ... ]
141 * @endcode
142 */
143 protected $localFileRefs = [];
144
145 /**
146 * @var array Place where readStyleFile() tracks file dependencies for non-existent files.
147 * Used in tests to detect missing dependencies.
148 */
149 protected $missingLocalFileRefs = [];
150
151 /**
152 * Constructs a new module from an options array.
153 *
154 * @param array $options List of options; if not given or empty, an empty module will be
155 * constructed
156 * @param string $localBasePath Base path to prepend to all local paths in $options. Defaults
157 * to $IP
158 * @param string $remoteBasePath Base path to prepend to all remote paths in $options. Defaults
159 * to $wgResourceBasePath
160 *
161 * Below is a description for the $options array:
162 * @throws InvalidArgumentException
163 * @par Construction options:
164 * @code
165 * [
166 * // Base path to prepend to all local paths in $options. Defaults to $IP
167 * 'localBasePath' => [base path],
168 * // Base path to prepend to all remote paths in $options. Defaults to $wgResourceBasePath
169 * 'remoteBasePath' => [base path],
170 * // Equivalent of remoteBasePath, but relative to $wgExtensionAssetsPath
171 * 'remoteExtPath' => [base path],
172 * // Equivalent of remoteBasePath, but relative to $wgStylePath
173 * 'remoteSkinPath' => [base path],
174 * // Scripts to always include
175 * 'scripts' => [file path string or array of file path strings],
176 * // Scripts to include in specific language contexts
177 * 'languageScripts' => [
178 * [language code] => [file path string or array of file path strings],
179 * ],
180 * // Scripts to include in specific skin contexts
181 * 'skinScripts' => [
182 * [skin name] => [file path string or array of file path strings],
183 * ],
184 * // Scripts to include in debug contexts
185 * 'debugScripts' => [file path string or array of file path strings],
186 * // Modules which must be loaded before this module
187 * 'dependencies' => [module name string or array of module name strings],
188 * 'templates' => [
189 * [template alias with file.ext] => [file path to a template file],
190 * ],
191 * // Styles to always load
192 * 'styles' => [file path string or array of file path strings],
193 * // Styles to include in specific skin contexts
194 * 'skinStyles' => [
195 * [skin name] => [file path string or array of file path strings],
196 * ],
197 * // Messages to always load
198 * 'messages' => [array of message key strings],
199 * // Group which this module should be loaded together with
200 * 'group' => [group name string],
201 * // Function that, if it returns true, makes the loader skip this module.
202 * // The file must contain valid JavaScript for execution in a private function.
203 * // The file must not contain the "function () {" and "}" wrapper though.
204 * 'skipFunction' => [file path]
205 * ]
206 * @endcode
207 */
208 public function __construct(
209 $options = [],
210 $localBasePath = null,
211 $remoteBasePath = null
212 ) {
213 // Flag to decide whether to automagically add the mediawiki.template module
214 $hasTemplates = false;
215 // localBasePath and remoteBasePath both have unbelievably long fallback chains
216 // and need to be handled separately.
217 list( $this->localBasePath, $this->remoteBasePath ) =
218 self::extractBasePaths( $options, $localBasePath, $remoteBasePath );
219
220 // Extract, validate and normalise remaining options
221 foreach ( $options as $member => $option ) {
222 switch ( $member ) {
223 // Lists of file paths
224 case 'scripts':
225 case 'debugScripts':
226 case 'styles':
227 $this->{$member} = (array)$option;
228 break;
229 case 'templates':
230 $hasTemplates = true;
231 $this->{$member} = (array)$option;
232 break;
233 // Collated lists of file paths
234 case 'languageScripts':
235 case 'skinScripts':
236 case 'skinStyles':
237 if ( !is_array( $option ) ) {
238 throw new InvalidArgumentException(
239 "Invalid collated file path list error. " .
240 "'$option' given, array expected."
241 );
242 }
243 foreach ( $option as $key => $value ) {
244 if ( !is_string( $key ) ) {
245 throw new InvalidArgumentException(
246 "Invalid collated file path list key error. " .
247 "'$key' given, string expected."
248 );
249 }
250 $this->{$member}[$key] = (array)$value;
251 }
252 break;
253 case 'deprecated':
254 $this->deprecated = $option;
255 break;
256 // Lists of strings
257 case 'dependencies':
258 case 'messages':
259 case 'targets':
260 // Normalise
261 $option = array_values( array_unique( (array)$option ) );
262 sort( $option );
263
264 $this->{$member} = $option;
265 break;
266 // Single strings
267 case 'group':
268 case 'skipFunction':
269 $this->{$member} = (string)$option;
270 break;
271 // Single booleans
272 case 'debugRaw':
273 case 'raw':
274 case 'noflip':
275 $this->{$member} = (bool)$option;
276 break;
277 }
278 }
279 if ( $hasTemplates ) {
280 $this->dependencies[] = 'mediawiki.template';
281 // Ensure relevant template compiler module gets loaded
282 foreach ( $this->templates as $alias => $templatePath ) {
283 if ( is_int( $alias ) ) {
284 $alias = $templatePath;
285 }
286 $suffix = explode( '.', $alias );
287 $suffix = end( $suffix );
288 $compilerModule = 'mediawiki.template.' . $suffix;
289 if ( $suffix !== 'html' && !in_array( $compilerModule, $this->dependencies ) ) {
290 $this->dependencies[] = $compilerModule;
291 }
292 }
293 }
294 }
295
296 /**
297 * Extract a pair of local and remote base paths from module definition information.
298 * Implementation note: the amount of global state used in this function is staggering.
299 *
300 * @param array $options Module definition
301 * @param string $localBasePath Path to use if not provided in module definition. Defaults
302 * to $IP
303 * @param string $remoteBasePath Path to use if not provided in module definition. Defaults
304 * to $wgResourceBasePath
305 * @return array Array( localBasePath, remoteBasePath )
306 */
307 public static function extractBasePaths(
308 $options = [],
309 $localBasePath = null,
310 $remoteBasePath = null
311 ) {
312 global $IP, $wgResourceBasePath;
313
314 // The different ways these checks are done, and their ordering, look very silly,
315 // but were preserved for backwards-compatibility just in case. Tread lightly.
316
317 if ( $localBasePath === null ) {
318 $localBasePath = $IP;
319 }
320 if ( $remoteBasePath === null ) {
321 $remoteBasePath = $wgResourceBasePath;
322 }
323
324 if ( isset( $options['remoteExtPath'] ) ) {
325 global $wgExtensionAssetsPath;
326 $remoteBasePath = $wgExtensionAssetsPath . '/' . $options['remoteExtPath'];
327 }
328
329 if ( isset( $options['remoteSkinPath'] ) ) {
330 global $wgStylePath;
331 $remoteBasePath = $wgStylePath . '/' . $options['remoteSkinPath'];
332 }
333
334 if ( array_key_exists( 'localBasePath', $options ) ) {
335 $localBasePath = (string)$options['localBasePath'];
336 }
337
338 if ( array_key_exists( 'remoteBasePath', $options ) ) {
339 $remoteBasePath = (string)$options['remoteBasePath'];
340 }
341
342 return [ $localBasePath, $remoteBasePath ];
343 }
344
345 /**
346 * Gets all scripts for a given context concatenated together.
347 *
348 * @param ResourceLoaderContext $context Context in which to generate script
349 * @return string JavaScript code for $context
350 */
351 public function getScript( ResourceLoaderContext $context ) {
352 $files = $this->getScriptFiles( $context );
353 return $this->getDeprecationInformation() . $this->readScriptFiles( $files );
354 }
355
356 /**
357 * @param ResourceLoaderContext $context
358 * @return array
359 */
360 public function getScriptURLsForDebug( ResourceLoaderContext $context ) {
361 $urls = [];
362 foreach ( $this->getScriptFiles( $context ) as $file ) {
363 $urls[] = OutputPage::transformResourcePath(
364 $this->getConfig(),
365 $this->getRemotePath( $file )
366 );
367 }
368 return $urls;
369 }
370
371 /**
372 * @return bool
373 */
374 public function supportsURLLoading() {
375 return $this->debugRaw;
376 }
377
378 /**
379 * Get all styles for a given context.
380 *
381 * @param ResourceLoaderContext $context
382 * @return array CSS code for $context as an associative array mapping media type to CSS text.
383 */
384 public function getStyles( ResourceLoaderContext $context ) {
385 $styles = $this->readStyleFiles(
386 $this->getStyleFiles( $context ),
387 $this->getFlip( $context ),
388 $context
389 );
390 // Collect referenced files
391 $this->saveFileDependencies( $context, $this->localFileRefs );
392
393 return $styles;
394 }
395
396 /**
397 * @param ResourceLoaderContext $context
398 * @return array
399 */
400 public function getStyleURLsForDebug( ResourceLoaderContext $context ) {
401 if ( $this->hasGeneratedStyles ) {
402 // Do the default behaviour of returning a url back to load.php
403 // but with only=styles.
404 return parent::getStyleURLsForDebug( $context );
405 }
406 // Our module consists entirely of real css files,
407 // in debug mode we can load those directly.
408 $urls = [];
409 foreach ( $this->getStyleFiles( $context ) as $mediaType => $list ) {
410 $urls[$mediaType] = [];
411 foreach ( $list as $file ) {
412 $urls[$mediaType][] = OutputPage::transformResourcePath(
413 $this->getConfig(),
414 $this->getRemotePath( $file )
415 );
416 }
417 }
418 return $urls;
419 }
420
421 /**
422 * Gets list of message keys used by this module.
423 *
424 * @return array List of message keys
425 */
426 public function getMessages() {
427 return $this->messages;
428 }
429
430 /**
431 * Gets the name of the group this module should be loaded in.
432 *
433 * @return string Group name
434 */
435 public function getGroup() {
436 return $this->group;
437 }
438
439 /**
440 * Gets list of names of modules this module depends on.
441 * @param ResourceLoaderContext|null $context
442 * @return array List of module names
443 */
444 public function getDependencies( ResourceLoaderContext $context = null ) {
445 return $this->dependencies;
446 }
447
448 /**
449 * Get the skip function.
450 * @return null|string
451 * @throws MWException
452 */
453 public function getSkipFunction() {
454 if ( !$this->skipFunction ) {
455 return null;
456 }
457
458 $localPath = $this->getLocalPath( $this->skipFunction );
459 if ( !file_exists( $localPath ) ) {
460 throw new MWException( __METHOD__ . ": skip function file not found: \"$localPath\"" );
461 }
462 $contents = $this->stripBom( file_get_contents( $localPath ) );
463 if ( $this->getConfig()->get( 'ResourceLoaderValidateStaticJS' ) ) {
464 $contents = $this->validateScriptFile( $localPath, $contents );
465 }
466 return $contents;
467 }
468
469 /**
470 * @return bool
471 */
472 public function isRaw() {
473 return $this->raw;
474 }
475
476 /**
477 * Disable module content versioning.
478 *
479 * This class uses getDefinitionSummary() instead, to avoid filesystem overhead
480 * involved with building the full module content inside a startup request.
481 *
482 * @return bool
483 */
484 public function enableModuleContentVersion() {
485 return false;
486 }
487
488 /**
489 * Helper method to gather file hashes for getDefinitionSummary.
490 *
491 * This function is context-sensitive, only computing hashes of files relevant to the
492 * given language, skin, etc.
493 *
494 * @see ResourceLoaderModule::getFileDependencies
495 * @param ResourceLoaderContext $context
496 * @return array
497 */
498 protected function getFileHashes( ResourceLoaderContext $context ) {
499 $files = [];
500
501 // Flatten style files into $files
502 $styles = self::collateFilePathListByOption( $this->styles, 'media', 'all' );
503 foreach ( $styles as $styleFiles ) {
504 $files = array_merge( $files, $styleFiles );
505 }
506
507 $skinFiles = self::collateFilePathListByOption(
508 self::tryForKey( $this->skinStyles, $context->getSkin(), 'default' ),
509 'media',
510 'all'
511 );
512 foreach ( $skinFiles as $styleFiles ) {
513 $files = array_merge( $files, $styleFiles );
514 }
515
516 // Final merge, this should result in a master list of dependent files
517 $files = array_merge(
518 $files,
519 $this->scripts,
520 $this->templates,
521 $context->getDebug() ? $this->debugScripts : [],
522 $this->getLanguageScripts( $context->getLanguage() ),
523 self::tryForKey( $this->skinScripts, $context->getSkin(), 'default' )
524 );
525 if ( $this->skipFunction ) {
526 $files[] = $this->skipFunction;
527 }
528 $files = array_map( [ $this, 'getLocalPath' ], $files );
529 // File deps need to be treated separately because they're already prefixed
530 $files = array_merge( $files, $this->getFileDependencies( $context ) );
531 // Filter out any duplicates from getFileDependencies() and others.
532 // Most commonly introduced by compileLessFile(), which always includes the
533 // entry point Less file we already know about.
534 $files = array_values( array_unique( $files ) );
535
536 // Don't include keys or file paths here, only the hashes. Including that would needlessly
537 // cause global cache invalidation when files move or if e.g. the MediaWiki path changes.
538 // Any significant ordering is already detected by the definition summary.
539 return array_map( [ __CLASS__, 'safeFileHash' ], $files );
540 }
541
542 /**
543 * Get the definition summary for this module.
544 *
545 * @param ResourceLoaderContext $context
546 * @return array
547 */
548 public function getDefinitionSummary( ResourceLoaderContext $context ) {
549 $summary = parent::getDefinitionSummary( $context );
550
551 $options = [];
552 foreach ( [
553 // The following properties are omitted because they don't affect the module reponse:
554 // - localBasePath (Per T104950; Changes when absolute directory name changes. If
555 // this affects 'scripts' and other file paths, getFileHashes accounts for that.)
556 // - remoteBasePath (Per T104950)
557 // - dependencies (provided via startup module)
558 // - targets
559 // - group (provided via startup module)
560 'scripts',
561 'debugScripts',
562 'styles',
563 'languageScripts',
564 'skinScripts',
565 'skinStyles',
566 'messages',
567 'templates',
568 'skipFunction',
569 'debugRaw',
570 'raw',
571 ] as $member ) {
572 $options[$member] = $this->{$member};
573 };
574
575 $summary[] = [
576 'options' => $options,
577 'fileHashes' => $this->getFileHashes( $context ),
578 'messageBlob' => $this->getMessageBlob( $context ),
579 ];
580
581 $lessVars = $this->getLessVars( $context );
582 if ( $lessVars ) {
583 $summary[] = [ 'lessVars' => $lessVars ];
584 }
585
586 return $summary;
587 }
588
589 /**
590 * @param string|ResourceLoaderFilePath $path
591 * @return string
592 */
593 protected function getLocalPath( $path ) {
594 if ( $path instanceof ResourceLoaderFilePath ) {
595 return $path->getLocalPath();
596 }
597
598 return "{$this->localBasePath}/$path";
599 }
600
601 /**
602 * @param string|ResourceLoaderFilePath $path
603 * @return string
604 */
605 protected function getRemotePath( $path ) {
606 if ( $path instanceof ResourceLoaderFilePath ) {
607 return $path->getRemotePath();
608 }
609
610 return "{$this->remoteBasePath}/$path";
611 }
612
613 /**
614 * Infer the stylesheet language from a stylesheet file path.
615 *
616 * @since 1.22
617 * @param string $path
618 * @return string The stylesheet language name
619 */
620 public function getStyleSheetLang( $path ) {
621 return preg_match( '/\.less$/i', $path ) ? 'less' : 'css';
622 }
623
624 /**
625 * Collates file paths by option (where provided).
626 *
627 * @param array $list List of file paths in any combination of index/path
628 * or path/options pairs
629 * @param string $option Option name
630 * @param mixed $default Default value if the option isn't set
631 * @return array List of file paths, collated by $option
632 */
633 protected static function collateFilePathListByOption( array $list, $option, $default ) {
634 $collatedFiles = [];
635 foreach ( (array)$list as $key => $value ) {
636 if ( is_int( $key ) ) {
637 // File name as the value
638 if ( !isset( $collatedFiles[$default] ) ) {
639 $collatedFiles[$default] = [];
640 }
641 $collatedFiles[$default][] = $value;
642 } elseif ( is_array( $value ) ) {
643 // File name as the key, options array as the value
644 $optionValue = isset( $value[$option] ) ? $value[$option] : $default;
645 if ( !isset( $collatedFiles[$optionValue] ) ) {
646 $collatedFiles[$optionValue] = [];
647 }
648 $collatedFiles[$optionValue][] = $key;
649 }
650 }
651 return $collatedFiles;
652 }
653
654 /**
655 * Get a list of element that match a key, optionally using a fallback key.
656 *
657 * @param array $list List of lists to select from
658 * @param string $key Key to look for in $map
659 * @param string $fallback Key to look for in $list if $key doesn't exist
660 * @return array List of elements from $map which matched $key or $fallback,
661 * or an empty list in case of no match
662 */
663 protected static function tryForKey( array $list, $key, $fallback = null ) {
664 if ( isset( $list[$key] ) && is_array( $list[$key] ) ) {
665 return $list[$key];
666 } elseif ( is_string( $fallback )
667 && isset( $list[$fallback] )
668 && is_array( $list[$fallback] )
669 ) {
670 return $list[$fallback];
671 }
672 return [];
673 }
674
675 /**
676 * Get a list of file paths for all scripts in this module, in order of proper execution.
677 *
678 * @param ResourceLoaderContext $context
679 * @return array List of file paths
680 */
681 protected function getScriptFiles( ResourceLoaderContext $context ) {
682 $files = array_merge(
683 $this->scripts,
684 $this->getLanguageScripts( $context->getLanguage() ),
685 self::tryForKey( $this->skinScripts, $context->getSkin(), 'default' )
686 );
687 if ( $context->getDebug() ) {
688 $files = array_merge( $files, $this->debugScripts );
689 }
690
691 return array_unique( $files, SORT_REGULAR );
692 }
693
694 /**
695 * Get the set of language scripts for the given language,
696 * possibly using a fallback language.
697 *
698 * @param string $lang
699 * @return array
700 */
701 private function getLanguageScripts( $lang ) {
702 $scripts = self::tryForKey( $this->languageScripts, $lang );
703 if ( $scripts ) {
704 return $scripts;
705 }
706 $fallbacks = Language::getFallbacksFor( $lang );
707 foreach ( $fallbacks as $lang ) {
708 $scripts = self::tryForKey( $this->languageScripts, $lang );
709 if ( $scripts ) {
710 return $scripts;
711 }
712 }
713
714 return [];
715 }
716
717 /**
718 * Get a list of file paths for all styles in this module, in order of proper inclusion.
719 *
720 * @param ResourceLoaderContext $context
721 * @return array List of file paths
722 */
723 public function getStyleFiles( ResourceLoaderContext $context ) {
724 return array_merge_recursive(
725 self::collateFilePathListByOption( $this->styles, 'media', 'all' ),
726 self::collateFilePathListByOption(
727 self::tryForKey( $this->skinStyles, $context->getSkin(), 'default' ),
728 'media',
729 'all'
730 )
731 );
732 }
733
734 /**
735 * Gets a list of file paths for all skin styles in the module used by
736 * the skin.
737 *
738 * @param string $skinName The name of the skin
739 * @return array A list of file paths collated by media type
740 */
741 protected function getSkinStyleFiles( $skinName ) {
742 return self::collateFilePathListByOption(
743 self::tryForKey( $this->skinStyles, $skinName ),
744 'media',
745 'all'
746 );
747 }
748
749 /**
750 * Gets a list of file paths for all skin style files in the module,
751 * for all available skins.
752 *
753 * @return array A list of file paths collated by media type
754 */
755 protected function getAllSkinStyleFiles() {
756 $styleFiles = [];
757 $internalSkinNames = array_keys( Skin::getSkinNames() );
758 $internalSkinNames[] = 'default';
759
760 foreach ( $internalSkinNames as $internalSkinName ) {
761 $styleFiles = array_merge_recursive(
762 $styleFiles,
763 $this->getSkinStyleFiles( $internalSkinName )
764 );
765 }
766
767 return $styleFiles;
768 }
769
770 /**
771 * Returns all style files and all skin style files used by this module.
772 *
773 * @return array
774 */
775 public function getAllStyleFiles() {
776 $collatedStyleFiles = array_merge_recursive(
777 self::collateFilePathListByOption( $this->styles, 'media', 'all' ),
778 $this->getAllSkinStyleFiles()
779 );
780
781 $result = [];
782
783 foreach ( $collatedStyleFiles as $media => $styleFiles ) {
784 foreach ( $styleFiles as $styleFile ) {
785 $result[] = $this->getLocalPath( $styleFile );
786 }
787 }
788
789 return $result;
790 }
791
792 /**
793 * Gets the contents of a list of JavaScript files.
794 *
795 * @param array $scripts List of file paths to scripts to read, remap and concetenate
796 * @throws MWException
797 * @return string Concatenated and remapped JavaScript data from $scripts
798 */
799 protected function readScriptFiles( array $scripts ) {
800 if ( empty( $scripts ) ) {
801 return '';
802 }
803 $js = '';
804 foreach ( array_unique( $scripts, SORT_REGULAR ) as $fileName ) {
805 $localPath = $this->getLocalPath( $fileName );
806 if ( !file_exists( $localPath ) ) {
807 throw new MWException( __METHOD__ . ": script file not found: \"$localPath\"" );
808 }
809 $contents = $this->stripBom( file_get_contents( $localPath ) );
810 if ( $this->getConfig()->get( 'ResourceLoaderValidateStaticJS' ) ) {
811 // Static files don't really need to be checked as often; unlike
812 // on-wiki module they shouldn't change unexpectedly without
813 // admin interference.
814 $contents = $this->validateScriptFile( $fileName, $contents );
815 }
816 $js .= $contents . "\n";
817 }
818 return $js;
819 }
820
821 /**
822 * Gets the contents of a list of CSS files.
823 *
824 * @param array $styles List of media type/list of file paths pairs, to read, remap and
825 * concetenate
826 * @param bool $flip
827 * @param ResourceLoaderContext $context
828 *
829 * @throws MWException
830 * @return array List of concatenated and remapped CSS data from $styles,
831 * keyed by media type
832 *
833 * @since 1.27 Calling this method without a ResourceLoaderContext instance
834 * is deprecated.
835 */
836 public function readStyleFiles( array $styles, $flip, $context = null ) {
837 if ( $context === null ) {
838 wfDeprecated( __METHOD__ . ' without a ResourceLoader context', '1.27' );
839 $context = ResourceLoaderContext::newDummyContext();
840 }
841
842 if ( empty( $styles ) ) {
843 return [];
844 }
845 foreach ( $styles as $media => $files ) {
846 $uniqueFiles = array_unique( $files, SORT_REGULAR );
847 $styleFiles = [];
848 foreach ( $uniqueFiles as $file ) {
849 $styleFiles[] = $this->readStyleFile( $file, $flip, $context );
850 }
851 $styles[$media] = implode( "\n", $styleFiles );
852 }
853 return $styles;
854 }
855
856 /**
857 * Reads a style file.
858 *
859 * This method can be used as a callback for array_map()
860 *
861 * @param string $path File path of style file to read
862 * @param bool $flip
863 * @param ResourceLoaderContext $context
864 *
865 * @return string CSS data in script file
866 * @throws MWException If the file doesn't exist
867 */
868 protected function readStyleFile( $path, $flip, $context ) {
869 $localPath = $this->getLocalPath( $path );
870 $remotePath = $this->getRemotePath( $path );
871 if ( !file_exists( $localPath ) ) {
872 $msg = __METHOD__ . ": style file not found: \"$localPath\"";
873 wfDebugLog( 'resourceloader', $msg );
874 throw new MWException( $msg );
875 }
876
877 if ( $this->getStyleSheetLang( $localPath ) === 'less' ) {
878 $style = $this->compileLessFile( $localPath, $context );
879 $this->hasGeneratedStyles = true;
880 } else {
881 $style = $this->stripBom( file_get_contents( $localPath ) );
882 }
883
884 if ( $flip ) {
885 $style = CSSJanus::transform( $style, true, false );
886 }
887 $localDir = dirname( $localPath );
888 $remoteDir = dirname( $remotePath );
889 // Get and register local file references
890 $localFileRefs = CSSMin::getLocalFileReferences( $style, $localDir );
891 foreach ( $localFileRefs as $file ) {
892 if ( file_exists( $file ) ) {
893 $this->localFileRefs[] = $file;
894 } else {
895 $this->missingLocalFileRefs[] = $file;
896 }
897 }
898 // Don't cache this call. remap() ensures data URIs embeds are up to date,
899 // and urls contain correct content hashes in their query string. (T128668)
900 return CSSMin::remap( $style, $localDir, $remoteDir, true );
901 }
902
903 /**
904 * Get whether CSS for this module should be flipped
905 * @param ResourceLoaderContext $context
906 * @return bool
907 */
908 public function getFlip( $context ) {
909 return $context->getDirection() === 'rtl' && !$this->noflip;
910 }
911
912 /**
913 * Get target(s) for the module, eg ['desktop'] or ['desktop', 'mobile']
914 *
915 * @return array Array of strings
916 */
917 public function getTargets() {
918 return $this->targets;
919 }
920
921 /**
922 * Get the module's load type.
923 *
924 * @since 1.28
925 * @return string
926 */
927 public function getType() {
928 $canBeStylesOnly = !(
929 // All options except 'styles', 'skinStyles' and 'debugRaw'
930 $this->scripts
931 || $this->debugScripts
932 || $this->templates
933 || $this->languageScripts
934 || $this->skinScripts
935 || $this->dependencies
936 || $this->messages
937 || $this->skipFunction
938 || $this->raw
939 );
940 return $canBeStylesOnly ? self::LOAD_STYLES : self::LOAD_GENERAL;
941 }
942
943 /**
944 * Compile a LESS file into CSS.
945 *
946 * Keeps track of all used files and adds them to localFileRefs.
947 *
948 * @since 1.22
949 * @since 1.27 Added $context paramter.
950 * @throws Exception If less.php encounters a parse error
951 * @param string $fileName File path of LESS source
952 * @param ResourceLoaderContext $context Context in which to generate script
953 * @return string CSS source
954 */
955 protected function compileLessFile( $fileName, ResourceLoaderContext $context ) {
956 static $cache;
957
958 if ( !$cache ) {
959 $cache = ObjectCache::getLocalServerInstance( CACHE_ANYTHING );
960 }
961
962 // Construct a cache key from the LESS file name and a hash digest
963 // of the LESS variables used for compilation.
964 $vars = $this->getLessVars( $context );
965 ksort( $vars );
966 $varsHash = hash( 'md4', serialize( $vars ) );
967 $cacheKey = $cache->makeGlobalKey( 'LESS', $fileName, $varsHash );
968 $cachedCompile = $cache->get( $cacheKey );
969
970 // If we got a cached value, we have to validate it by getting a
971 // checksum of all the files that were loaded by the parser and
972 // ensuring it matches the cached entry's.
973 if ( isset( $cachedCompile['hash'] ) ) {
974 $contentHash = FileContentsHasher::getFileContentsHash( $cachedCompile['files'] );
975 if ( $contentHash === $cachedCompile['hash'] ) {
976 $this->localFileRefs = array_merge( $this->localFileRefs, $cachedCompile['files'] );
977 return $cachedCompile['css'];
978 }
979 }
980
981 $compiler = $context->getResourceLoader()->getLessCompiler( $vars );
982 $css = $compiler->parseFile( $fileName )->getCss();
983 $files = $compiler->AllParsedFiles();
984 $this->localFileRefs = array_merge( $this->localFileRefs, $files );
985
986 // Cache for 24 hours (86400 seconds).
987 $cache->set( $cacheKey, [
988 'css' => $css,
989 'files' => $files,
990 'hash' => FileContentsHasher::getFileContentsHash( $files ),
991 ], 3600 * 24 );
992
993 return $css;
994 }
995
996 /**
997 * Takes named templates by the module and returns an array mapping.
998 * @return array Templates mapping template alias to content
999 * @throws MWException
1000 */
1001 public function getTemplates() {
1002 $templates = [];
1003
1004 foreach ( $this->templates as $alias => $templatePath ) {
1005 // Alias is optional
1006 if ( is_int( $alias ) ) {
1007 $alias = $templatePath;
1008 }
1009 $localPath = $this->getLocalPath( $templatePath );
1010 if ( file_exists( $localPath ) ) {
1011 $content = file_get_contents( $localPath );
1012 $templates[$alias] = $this->stripBom( $content );
1013 } else {
1014 $msg = __METHOD__ . ": template file not found: \"$localPath\"";
1015 wfDebugLog( 'resourceloader', $msg );
1016 throw new MWException( $msg );
1017 }
1018 }
1019 return $templates;
1020 }
1021
1022 /**
1023 * Takes an input string and removes the UTF-8 BOM character if present
1024 *
1025 * We need to remove these after reading a file, because we concatenate our files and
1026 * the BOM character is not valid in the middle of a string.
1027 * We already assume UTF-8 everywhere, so this should be safe.
1028 *
1029 * @param string $input
1030 * @return string Input minus the intial BOM char
1031 */
1032 protected function stripBom( $input ) {
1033 if ( substr_compare( "\xef\xbb\xbf", $input, 0, 3 ) === 0 ) {
1034 return substr( $input, 3 );
1035 }
1036 return $input;
1037 }
1038 }