MediaWiki master
FileModule.php
Go to the documentation of this file.
1<?php
24
25use CSSJanus;
26use Exception;
28use InvalidArgumentException;
29use LogicException;
35use RuntimeException;
36use Wikimedia\Minify\CSSMin;
37use Wikimedia\RequestTimeout\TimeoutException;
38
52class FileModule extends Module {
54 protected $localBasePath = '';
55
57 protected $remoteBasePath = '';
58
62 protected $scripts = [];
63
67 protected $languageScripts = [];
68
72 protected $skinScripts = [];
73
77 protected $debugScripts = [];
78
82 protected $styles = [];
83
87 protected $skinStyles = [];
88
96 protected $packageFiles = null;
97
102 private $expandedPackageFiles = [];
103
108 private $fullyExpandedPackageFiles = [];
109
113 protected $dependencies = [];
114
118 protected $skipFunction = null;
119
123 protected $messages = [];
124
126 protected $templates = [];
127
129 protected $group = null;
130
132 protected $debugRaw = true;
133
135 protected $noflip = false;
136
138 protected $skipStructureTest = false;
139
144 protected $hasGeneratedStyles = false;
145
149 protected $localFileRefs = [];
150
155 protected $missingLocalFileRefs = [];
156
160 protected $vueComponentParser = null;
161
171 public function __construct(
172 array $options = [],
173 string $localBasePath = null,
174 string $remoteBasePath = null
175 ) {
176 // Flag to decide whether to automagically add the mediawiki.template module
177 $hasTemplates = false;
178 // localBasePath and remoteBasePath both have unbelievably long fallback chains
179 // and need to be handled separately.
182
183 // Extract, validate and normalise remaining options
184 foreach ( $options as $member => $option ) {
185 switch ( $member ) {
186 // Lists of file paths
187 case 'scripts':
188 case 'debugScripts':
189 case 'styles':
190 case 'packageFiles':
191 $this->{$member} = is_array( $option ) ? $option : [ $option ];
192 break;
193 case 'templates':
194 $hasTemplates = true;
195 $this->{$member} = is_array( $option ) ? $option : [ $option ];
196 break;
197 // Collated lists of file paths
198 case 'languageScripts':
199 case 'skinScripts':
200 case 'skinStyles':
201 if ( !is_array( $option ) ) {
202 throw new InvalidArgumentException(
203 "Invalid collated file path list error. " .
204 "'$option' given, array expected."
205 );
206 }
207 foreach ( $option as $key => $value ) {
208 if ( !is_string( $key ) ) {
209 throw new InvalidArgumentException(
210 "Invalid collated file path list key error. " .
211 "'$key' given, string expected."
212 );
213 }
214 $this->{$member}[$key] = is_array( $value ) ? $value : [ $value ];
215 }
216 break;
217 case 'deprecated':
218 $this->deprecated = $option;
219 break;
220 // Lists of strings
221 case 'dependencies':
222 case 'messages':
223 // Normalise
224 $option = array_values( array_unique( (array)$option ) );
225 sort( $option );
226
227 $this->{$member} = $option;
228 break;
229 // Single strings
230 case 'group':
231 case 'skipFunction':
232 $this->{$member} = (string)$option;
233 break;
234 // Single booleans
235 case 'debugRaw':
236 case 'noflip':
237 case 'skipStructureTest':
238 $this->{$member} = (bool)$option;
239 break;
240 }
241 }
242 if ( isset( $options['scripts'] ) && isset( $options['packageFiles'] ) ) {
243 throw new InvalidArgumentException( "A module may not set both 'scripts' and 'packageFiles'" );
244 }
245 if ( isset( $options['packageFiles'] ) && isset( $options['skinScripts'] ) ) {
246 throw new InvalidArgumentException( "Options 'skinScripts' and 'packageFiles' cannot be used together." );
247 }
248 if ( $hasTemplates ) {
249 $this->dependencies[] = 'mediawiki.template';
250 // Ensure relevant template compiler module gets loaded
251 foreach ( $this->templates as $alias => $templatePath ) {
252 if ( is_int( $alias ) ) {
253 $alias = $this->getPath( $templatePath );
254 }
255 $suffix = explode( '.', $alias );
256 $suffix = end( $suffix );
257 $compilerModule = 'mediawiki.template.' . $suffix;
258 if ( $suffix !== 'html' && !in_array( $compilerModule, $this->dependencies ) ) {
259 $this->dependencies[] = $compilerModule;
260 }
261 }
262 }
263 }
264
276 public static function extractBasePaths(
277 array $options = [],
278 $localBasePath = null,
279 $remoteBasePath = null
280 ) {
281 // The different ways these checks are done, and their ordering, look very silly,
282 // but were preserved for backwards-compatibility just in case. Tread lightly.
283
286
287 if ( isset( $options['remoteExtPath'] ) ) {
288 $extensionAssetsPath = MediaWikiServices::getInstance()->getMainConfig()
290 $remoteBasePath = $extensionAssetsPath . '/' . $options['remoteExtPath'];
291 }
292
293 if ( isset( $options['remoteSkinPath'] ) ) {
294 $stylePath = MediaWikiServices::getInstance()->getMainConfig()
296 $remoteBasePath = $stylePath . '/' . $options['remoteSkinPath'];
297 }
298
299 if ( array_key_exists( 'localBasePath', $options ) ) {
300 $localBasePath = (string)$options['localBasePath'];
301 }
302
303 if ( array_key_exists( 'remoteBasePath', $options ) ) {
304 $remoteBasePath = (string)$options['remoteBasePath'];
305 }
306
307 if ( $remoteBasePath === '' ) {
308 // If MediaWiki is installed at the document root (not recommended),
309 // then wgScriptPath is set to the empty string by the installer to
310 // ensure safe concatenating of file paths (avoid "/" + "/foo" being "//foo").
311 // However, this also means the path itself can be an invalid URI path,
312 // as those must start with a slash. Within ResourceLoader, we will not
313 // do such primitive/unsafe slash concatenation and use URI resolution
314 // instead, so beyond this point, to avoid fatal errors in CSSMin::resolveUrl(),
315 // do a best-effort support for docroot installs by casting this to a slash.
316 $remoteBasePath = '/';
317 }
318
319 return [ $localBasePath ?? MW_INSTALL_PATH, $remoteBasePath ];
320 }
321
322 public function getScript( Context $context ) {
323 $packageFiles = $this->getPackageFiles( $context );
324 if ( $packageFiles !== null ) {
325 foreach ( $packageFiles['files'] as &$file ) {
326 if ( $file['type'] === 'script+style' ) {
327 $file['content'] = $file['content']['script'];
328 $file['type'] = 'script';
329 }
330 }
331 return $packageFiles;
332 }
333
334 $files = $this->getScriptFiles( $context );
335 foreach ( $files as &$file ) {
336 $this->readFileInfo( $context, $file );
337 }
338 return [ 'plainScripts' => $files ];
339 }
340
345 public function getScriptURLsForDebug( Context $context ) {
346 $rl = $context->getResourceLoader();
347 $config = $this->getConfig();
348 $server = $config->get( MainConfigNames::Server );
349
350 $urls = [];
351 foreach ( $this->getScriptFiles( $context ) as $file ) {
352 if ( isset( $file['filePath'] ) ) {
353 $url = OutputPage::transformResourcePath( $config, $this->getRemotePath( $file['filePath'] ) );
354 // Expand debug URL in case we are another wiki's module source (T255367)
355 $url = $rl->expandUrl( $server, $url );
356 $urls[] = $url;
357 }
358 }
359 return $urls;
360 }
361
365 public function supportsURLLoading() {
366 // phpcs:ignore Generic.WhiteSpace.LanguageConstructSpacing.IncorrectSingle
367 return
368 // Denied by options?
369 $this->debugRaw
370 // If package files are involved, don't support URL loading, because that breaks
371 // scoped require() functions
372 && !$this->packageFiles
373 // Can't link to scripts generated by callbacks
374 && !$this->hasGeneratedScripts();
375 }
376
377 public function shouldSkipStructureTest() {
378 return $this->skipStructureTest || parent::shouldSkipStructureTest();
379 }
380
386 private function hasGeneratedScripts() {
387 foreach (
388 [ $this->scripts, $this->languageScripts, $this->skinScripts, $this->debugScripts ]
389 as $scripts
390 ) {
391 foreach ( $scripts as $script ) {
392 if ( is_array( $script ) ) {
393 if ( isset( $script['callback'] ) || isset( $script['versionCallback'] ) ) {
394 return true;
395 }
396 }
397 }
398 }
399 return false;
400 }
401
408 public function getStyles( Context $context ) {
409 $styles = $this->readStyleFiles(
410 $this->getStyleFiles( $context ),
411 $context
412 );
413
414 $packageFiles = $this->getPackageFiles( $context );
415 if ( $packageFiles !== null ) {
416 foreach ( $packageFiles['files'] as $fileName => $file ) {
417 if ( $file['type'] === 'script+style' ) {
418 $style = $this->processStyle(
419 $file['content']['style'],
420 $file['content']['styleLang'],
421 $fileName,
422 $context
423 );
424 $styles['all'] = ( $styles['all'] ?? '' ) . "\n" . $style;
425 }
426 }
427 }
428
429 // Track indirect file dependencies so that StartUpModule can check for
430 // on-disk file changes to any of this files without having to recompute the file list
431 $this->saveFileDependencies( $context, $this->localFileRefs );
432
433 return $styles;
434 }
435
440 public function getStyleURLsForDebug( Context $context ) {
441 if ( $this->hasGeneratedStyles ) {
442 // Do the default behaviour of returning a url back to load.php
443 // but with only=styles.
444 return parent::getStyleURLsForDebug( $context );
445 }
446 // Our module consists entirely of real css files,
447 // in debug mode we can load those directly.
448 $urls = [];
449 foreach ( $this->getStyleFiles( $context ) as $mediaType => $list ) {
450 $urls[$mediaType] = [];
451 foreach ( $list as $file ) {
452 $urls[$mediaType][] = OutputPage::transformResourcePath(
453 $this->getConfig(),
454 $this->getRemotePath( $file )
455 );
456 }
457 }
458 return $urls;
459 }
460
466 public function getMessages() {
467 return $this->messages;
468 }
469
475 public function getGroup() {
476 return $this->group;
477 }
478
485 public function getDependencies( Context $context = null ) {
486 return $this->dependencies;
487 }
488
496 private function getFileContents( $localPath, $type ) {
497 if ( !is_file( $localPath ) ) {
498 throw new RuntimeException( "$type file not found or not a file: \"$localPath\"" );
499 }
500 return $this->stripBom( file_get_contents( $localPath ) );
501 }
502
506 public function getSkipFunction() {
507 if ( !$this->skipFunction ) {
508 return null;
509 }
510 $localPath = $this->getLocalPath( $this->skipFunction );
511 return $this->getFileContents( $localPath, 'skip function' );
512 }
513
514 public function requiresES6() {
515 return true;
516 }
517
526 public function enableModuleContentVersion() {
527 return false;
528 }
529
536 private function getFileHashes( Context $context ) {
537 $files = [];
538
539 foreach ( $this->getStyleFiles( $context ) as $filePaths ) {
540 foreach ( $filePaths as $filePath ) {
541 $files[] = $this->getLocalPath( $filePath );
542 }
543 }
544
545 // Extract file paths for package files
546 // Optimisation: Use foreach() and isset() instead of array_map/array_filter.
547 // This is a hot code path, called by StartupModule for thousands of modules.
548 $expandedPackageFiles = $this->expandPackageFiles( $context );
549 if ( $expandedPackageFiles ) {
550 foreach ( $expandedPackageFiles['files'] as $fileInfo ) {
551 if ( isset( $fileInfo['filePath'] ) ) {
553 $filePath = $fileInfo['filePath'];
554 $files[] = $filePath->getLocalPath();
555 }
556 }
557 }
558
559 // Add other configured paths
560 $scriptFileInfos = $this->getScriptFiles( $context );
561 foreach ( $scriptFileInfos as $fileInfo ) {
562 $filePath = $fileInfo['filePath'] ?? $fileInfo['versionFilePath'] ?? null;
563 if ( $filePath instanceof FilePath ) {
564 $files[] = $filePath->getLocalPath();
565 }
566 }
567
568 foreach ( $this->templates as $filePath ) {
569 $files[] = $this->getLocalPath( $filePath );
570 }
571
572 if ( $this->skipFunction ) {
573 $files[] = $this->getLocalPath( $this->skipFunction );
574 }
575
576 // Add any lazily discovered file dependencies from previous module builds.
577 // These are already absolute paths.
578 foreach ( $this->getFileDependencies( $context ) as $file ) {
579 $files[] = $file;
580 }
581
582 // Filter out any duplicates. Typically introduced by getFileDependencies() which
583 // may lazily re-discover a primary file.
584 $files = array_unique( $files );
585
586 // Don't return array keys or any other form of file path here, only the hashes.
587 // Including file paths would needlessly cause global cache invalidation when files
588 // move on disk or if e.g. the MediaWiki directory name changes.
589 // Anything where order is significant is already detected by the definition summary.
591 }
592
599 public function getDefinitionSummary( Context $context ) {
600 $summary = parent::getDefinitionSummary( $context );
601
602 $options = [];
603 foreach ( [
604 // The following properties are omitted because they don't affect the module response:
605 // - localBasePath (Per T104950; Changes when absolute directory name changes. If
606 // this affects 'scripts' and other file paths, getFileHashes accounts for that.)
607 // - remoteBasePath (Per T104950)
608 // - dependencies (provided via startup module)
609 // - group (provided via startup module)
610 'styles',
611 'skinStyles',
612 'messages',
613 'templates',
614 'skipFunction',
615 'debugRaw',
616 ] as $member ) {
617 $options[$member] = $this->{$member};
618 }
619
620 $packageFiles = $this->expandPackageFiles( $context );
621 $packageSummaries = [];
622 if ( $packageFiles ) {
623 // Extract the minimum needed:
624 // - The 'main' pointer (included as-is).
625 // - The 'files' array, simplified to only which files exist (the keys of
626 // this array), and something that represents their non-file content.
627 // For packaged files that reflect files directly from disk, the
628 // 'getFileHashes' method tracks their content already.
629 // It is important that the keys of the $packageFiles['files'] array
630 // are preserved, as they do affect the module output.
631 foreach ( $packageFiles['files'] as $fileName => $fileInfo ) {
632 $packageSummaries[$fileName] =
633 $fileInfo['definitionSummary'] ?? $fileInfo['content'] ?? null;
634 }
635 }
636
637 $scriptFiles = $this->getScriptFiles( $context );
638 $scriptSummaries = [];
639 foreach ( $scriptFiles as $fileName => $fileInfo ) {
640 $scriptSummaries[$fileName] =
641 $fileInfo['definitionSummary'] ?? $fileInfo['content'] ?? null;
642 }
643
644 $summary[] = [
645 'options' => $options,
646 'packageFiles' => $packageSummaries,
647 'scripts' => $scriptSummaries,
648 'fileHashes' => $this->getFileHashes( $context ),
649 'messageBlob' => $this->getMessageBlob( $context ),
650 ];
651
652 $lessVars = $this->getLessVars( $context );
653 if ( $lessVars ) {
654 $summary[] = [ 'lessVars' => $lessVars ];
655 }
656
657 return $summary;
658 }
659
663 protected function getVueComponentParser() {
664 if ( $this->vueComponentParser === null ) {
665 $this->vueComponentParser = new VueComponentParser;
666 }
668 }
669
674 protected function getPath( $path ) {
675 if ( $path instanceof FilePath ) {
676 return $path->getPath();
677 }
678
679 return $path;
680 }
681
686 protected function getLocalPath( $path ) {
687 if ( $path instanceof FilePath ) {
688 if ( $path->getLocalBasePath() !== null ) {
689 return $path->getLocalPath();
690 }
691 $path = $path->getPath();
692 }
693
694 return "{$this->localBasePath}/$path";
695 }
696
701 protected function getRemotePath( $path ) {
702 if ( $path instanceof FilePath ) {
703 if ( $path->getRemoteBasePath() !== null ) {
704 return $path->getRemotePath();
705 }
706 $path = $path->getPath();
707 }
708
709 if ( $this->remoteBasePath === '/' ) {
710 return "/$path";
711 } else {
712 return "{$this->remoteBasePath}/$path";
713 }
714 }
715
723 public function getStyleSheetLang( $path ) {
724 return preg_match( '/\.less$/i', $path ) ? 'less' : 'css';
725 }
726
733 public static function getPackageFileType( $path ) {
734 if ( preg_match( '/\.json$/i', $path ) ) {
735 return 'data';
736 }
737 if ( preg_match( '/\.vue$/i', $path ) ) {
738 return 'script-vue';
739 }
740 return 'script';
741 }
742
750 private static function collateStyleFilesByMedia( array $list ) {
751 $collatedFiles = [];
752 foreach ( $list as $key => $value ) {
753 if ( is_int( $key ) ) {
754 // File name as the value
755 $collatedFiles['all'][] = $value;
756 } elseif ( is_array( $value ) ) {
757 // File name as the key, options array as the value
758 $optionValue = $value['media'] ?? 'all';
759 $collatedFiles[$optionValue][] = $key;
760 }
761 }
762 return $collatedFiles;
763 }
764
774 protected static function tryForKey( array $list, $key, $fallback = null ) {
775 if ( isset( $list[$key] ) && is_array( $list[$key] ) ) {
776 return $list[$key];
777 } elseif ( is_string( $fallback )
778 && isset( $list[$fallback] )
779 && is_array( $list[$fallback] )
780 ) {
781 return $list[$fallback];
782 }
783 return [];
784 }
785
792 private function getScriptFiles( Context $context ): array {
793 // List in execution order: scripts, languageScripts, skinScripts, debugScripts.
794 // Documented at MediaWiki\MainConfigSchema::ResourceModules.
795 $filesByCategory = [
796 'scripts' => $this->scripts,
797 'languageScripts' => $this->getLanguageScripts( $context->getLanguage() ),
798 'skinScripts' => self::tryForKey( $this->skinScripts, $context->getSkin(), 'default' ),
799 ];
800 if ( $context->getDebug() ) {
801 $filesByCategory['debugScripts'] = $this->debugScripts;
802 }
803
804 $expandedFiles = [];
805 foreach ( $filesByCategory as $category => $files ) {
806 foreach ( $files as $key => $fileInfo ) {
807 $expandedFileInfo = $this->expandFileInfo( $context, $fileInfo, "$category\[$key]" );
808 $expandedFiles[$expandedFileInfo['name']] = $expandedFileInfo;
809 }
810 }
811
812 return $expandedFiles;
813 }
814
822 private function getLanguageScripts( string $lang ): array {
823 $scripts = self::tryForKey( $this->languageScripts, $lang );
824 if ( $scripts ) {
825 return $scripts;
826 }
827
828 // Optimization: Avoid initialising and calling into language services
829 // for the majority of modules that don't use this option.
830 if ( $this->languageScripts ) {
831 $fallbacks = MediaWikiServices::getInstance()
832 ->getLanguageFallback()
833 ->getAll( $lang, LanguageFallback::MESSAGES );
834 foreach ( $fallbacks as $lang ) {
835 $scripts = self::tryForKey( $this->languageScripts, $lang );
836 if ( $scripts ) {
837 return $scripts;
838 }
839 }
840 }
841
842 return [];
843 }
844
845 public function setSkinStylesOverride( array $moduleSkinStyles ): void {
846 $moduleName = $this->getName();
847 foreach ( $moduleSkinStyles as $skinName => $overrides ) {
848 // If a module provides overrides for a skin, and that skin also provides overrides
849 // for the same module, then the module has precedence.
850 if ( isset( $this->skinStyles[$skinName] ) ) {
851 continue;
852 }
853
854 // If $moduleName in ResourceModuleSkinStyles is preceded with a '+', the defined style
855 // files will be added to 'default' skinStyles, otherwise 'default' will be ignored.
856 if ( isset( $overrides[$moduleName] ) ) {
857 $paths = (array)$overrides[$moduleName];
858 $styleFiles = [];
859 } elseif ( isset( $overrides['+' . $moduleName] ) ) {
860 $paths = (array)$overrides['+' . $moduleName];
861 $styleFiles = isset( $this->skinStyles['default'] ) ?
862 (array)$this->skinStyles['default'] :
863 [];
864 } else {
865 continue;
866 }
867
868 // Add new file paths, remapping them to refer to our directories and not use settings
869 // from the module we're modifying, which come from the base definition.
870 [ $localBasePath, $remoteBasePath ] = self::extractBasePaths( $overrides );
871
872 foreach ( $paths as $path ) {
873 $styleFiles[] = new FilePath( $path, $localBasePath, $remoteBasePath );
874 }
875
876 $this->skinStyles[$skinName] = $styleFiles;
877 }
878 }
879
887 public function getStyleFiles( Context $context ) {
888 return array_merge_recursive(
889 self::collateStyleFilesByMedia( $this->styles ),
890 self::collateStyleFilesByMedia(
891 self::tryForKey( $this->skinStyles, $context->getSkin(), 'default' )
892 )
893 );
894 }
895
903 protected function getSkinStyleFiles( $skinName ) {
904 return self::collateStyleFilesByMedia(
905 self::tryForKey( $this->skinStyles, $skinName )
906 );
907 }
908
915 protected function getAllSkinStyleFiles() {
916 $skinFactory = MediaWikiServices::getInstance()->getSkinFactory();
917 $styleFiles = [];
918
919 $internalSkinNames = array_keys( $skinFactory->getInstalledSkins() );
920 $internalSkinNames[] = 'default';
921
922 foreach ( $internalSkinNames as $internalSkinName ) {
923 $styleFiles = array_merge_recursive(
924 $styleFiles,
925 $this->getSkinStyleFiles( $internalSkinName )
926 );
927 }
928
929 return $styleFiles;
930 }
931
937 public function getAllStyleFiles() {
938 $collatedStyleFiles = array_merge_recursive(
939 self::collateStyleFilesByMedia( $this->styles ),
940 $this->getAllSkinStyleFiles()
941 );
942
943 $result = [];
944
945 foreach ( $collatedStyleFiles as $styleFiles ) {
946 foreach ( $styleFiles as $styleFile ) {
947 $result[] = $this->getLocalPath( $styleFile );
948 }
949 }
950
951 return $result;
952 }
953
962 public function readStyleFiles( array $styles, Context $context ) {
963 if ( !$styles ) {
964 return [];
965 }
966 foreach ( $styles as $media => $files ) {
967 $uniqueFiles = array_unique( $files, SORT_REGULAR );
968 $styleFiles = [];
969 foreach ( $uniqueFiles as $file ) {
970 $styleFiles[] = $this->readStyleFile( $file, $context );
971 }
972 $styles[$media] = implode( "\n", $styleFiles );
973 }
974 return $styles;
975 }
976
987 protected function readStyleFile( $path, Context $context ) {
988 $localPath = $this->getLocalPath( $path );
989 $style = $this->getFileContents( $localPath, 'style' );
990 $styleLang = $this->getStyleSheetLang( $localPath );
991
992 return $this->processStyle( $style, $styleLang, $path, $context );
993 }
994
1011 protected function processStyle( $style, $styleLang, $path, Context $context ) {
1012 $localPath = $this->getLocalPath( $path );
1013 $remotePath = $this->getRemotePath( $path );
1014
1015 if ( $styleLang === 'less' ) {
1016 $style = $this->compileLessString( $style, $localPath, $context );
1017 $this->hasGeneratedStyles = true;
1018 }
1019
1020 if ( $this->getFlip( $context ) ) {
1021 $style = CSSJanus::transform(
1022 $style,
1023 /* $swapLtrRtlInURL = */ true,
1024 /* $swapLeftRightInURL = */ false
1025 );
1026 $this->hasGeneratedStyles = true;
1027 }
1028
1029 $localDir = dirname( $localPath );
1030 $remoteDir = dirname( $remotePath );
1031 // Get and register local file references
1032 $localFileRefs = CSSMin::getLocalFileReferences( $style, $localDir );
1033 foreach ( $localFileRefs as $file ) {
1034 if ( is_file( $file ) ) {
1035 $this->localFileRefs[] = $file;
1036 } else {
1037 $this->missingLocalFileRefs[] = $file;
1038 }
1039 }
1040 // Don't cache this call. remap() ensures data URIs embeds are up to date,
1041 // and urls contain correct content hashes in their query string. (T128668)
1042 return CSSMin::remap( $style, $localDir, $remoteDir, true );
1043 }
1044
1050 public function getFlip( Context $context ) {
1051 return $context->getDirection() === 'rtl' && !$this->noflip;
1052 }
1053
1060 public function getType() {
1061 $canBeStylesOnly = !(
1062 // All options except 'styles', 'skinStyles' and 'debugRaw'
1063 $this->scripts
1064 || $this->debugScripts
1065 || $this->templates
1066 || $this->languageScripts
1067 || $this->skinScripts
1068 || $this->dependencies
1069 || $this->messages
1070 || $this->skipFunction
1071 || $this->packageFiles
1072 );
1073 return $canBeStylesOnly ? self::LOAD_STYLES : self::LOAD_GENERAL;
1074 }
1075
1087 protected function compileLessString( $style, $stylePath, Context $context ) {
1088 static $cache;
1089 // @TODO: dependency injection
1090 if ( !$cache ) {
1091 $cache = MediaWikiServices::getInstance()->getObjectCacheFactory()
1092 ->getLocalServerInstance( CACHE_ANYTHING );
1093 }
1094
1095 $skinName = $context->getSkin();
1096 $skinImportPaths = ExtensionRegistry::getInstance()->getAttribute( 'SkinLessImportPaths' );
1097 $importDirs = [];
1098 if ( isset( $skinImportPaths[ $skinName ] ) ) {
1099 $importDirs[] = $skinImportPaths[ $skinName ];
1100 }
1101
1102 $vars = $this->getLessVars( $context );
1103 // Construct a cache key from a hash of the LESS source, and a hash digest
1104 // of the LESS variables and import dirs used for compilation.
1105 ksort( $vars );
1106 $compilerParams = [
1107 'vars' => $vars,
1108 'importDirs' => $importDirs,
1109 // CodexDevelopmentDir affects import path mapping in ResourceLoader::getLessCompiler(),
1110 // so take that into account too
1111 'codexDevDir' => $this->getConfig()->get( MainConfigNames::CodexDevelopmentDir )
1112 ];
1113 $key = $cache->makeGlobalKey(
1114 'resourceloader-less',
1115 'v1',
1116 hash( 'md4', $style ),
1117 hash( 'md4', serialize( $compilerParams ) )
1118 );
1119
1120 // If we got a cached value, we have to validate it by getting a checksum of all the
1121 // files that were loaded by the parser and ensuring it matches the cached entry's.
1122 $data = $cache->get( $key );
1123 if (
1124 !$data ||
1125 $data['hash'] !== FileContentsHasher::getFileContentsHash( $data['files'] )
1126 ) {
1127 $compiler = $context->getResourceLoader()->getLessCompiler( $vars, $importDirs );
1128
1129 $css = $compiler->parse( $style, $stylePath )->getCss();
1130 // T253055: store the implicit dependency paths in a form relative to any install
1131 // path so that multiple version of the application can share the cache for identical
1132 // less stylesheets. This also avoids churn during application updates.
1133 $files = $compiler->getParsedFiles();
1134 $data = [
1135 'css' => $css,
1136 'files' => Module::getRelativePaths( $files ),
1137 'hash' => FileContentsHasher::getFileContentsHash( $files )
1138 ];
1139 $cache->set( $key, $data, $cache::TTL_DAY );
1140 }
1141
1142 foreach ( Module::expandRelativePaths( $data['files'] ) as $path ) {
1143 $this->localFileRefs[] = $path;
1144 }
1145
1146 return $data['css'];
1147 }
1148
1154 public function getTemplates() {
1155 $templates = [];
1156
1157 foreach ( $this->templates as $alias => $templatePath ) {
1158 // Alias is optional
1159 if ( is_int( $alias ) ) {
1160 $alias = $this->getPath( $templatePath );
1161 }
1162 $localPath = $this->getLocalPath( $templatePath );
1163 $content = $this->getFileContents( $localPath, 'template' );
1164
1165 $templates[$alias] = $this->stripBom( $content );
1166 }
1167 return $templates;
1168 }
1169
1189 private function expandPackageFiles( Context $context ) {
1190 $hash = $context->getHash();
1191 if ( isset( $this->expandedPackageFiles[$hash] ) ) {
1192 return $this->expandedPackageFiles[$hash];
1193 }
1194 if ( $this->packageFiles === null ) {
1195 return null;
1196 }
1197 $expandedFiles = [];
1198 $mainFile = null;
1199
1200 foreach ( $this->packageFiles as $key => $fileInfo ) {
1201 $expanded = $this->expandFileInfo( $context, $fileInfo, "packageFiles[$key]" );
1202 $fileName = $expanded['name'];
1203 if ( !empty( $expanded['main'] ) ) {
1204 unset( $expanded['main'] );
1205 $type = $expanded['type'];
1206 $mainFile = $fileName;
1207 if ( $type !== 'script' && $type !== 'script-vue' ) {
1208 $msg = "Main file in package must be of type 'script', module " .
1209 "'{$this->getName()}', main file '{$mainFile}' is '{$type}'.";
1210 $this->getLogger()->error( $msg );
1211 throw new LogicException( $msg );
1212 }
1213 }
1214 $expandedFiles[$fileName] = $expanded;
1215 }
1216
1217 if ( $expandedFiles && $mainFile === null ) {
1218 // The first package file that is a script is the main file
1219 foreach ( $expandedFiles as $path => $file ) {
1220 if ( $file['type'] === 'script' || $file['type'] === 'script-vue' ) {
1221 $mainFile = $path;
1222 break;
1223 }
1224 }
1225 }
1226
1227 $result = [
1228 'main' => $mainFile,
1229 'files' => $expandedFiles
1230 ];
1231
1232 $this->expandedPackageFiles[$hash] = $result;
1233 return $result;
1234 }
1235
1265 private function expandFileInfo( Context $context, $fileInfo, $debugKey ) {
1266 if ( is_string( $fileInfo ) ) {
1267 // Inline common case
1268 return [
1269 'name' => $fileInfo,
1270 'type' => self::getPackageFileType( $fileInfo ),
1271 'filePath' => new FilePath( $fileInfo, $this->localBasePath, $this->remoteBasePath )
1272 ];
1273 } elseif ( $fileInfo instanceof FilePath ) {
1274 $fileInfo = [
1275 'name' => $fileInfo->getPath(),
1276 'file' => $fileInfo
1277 ];
1278 } elseif ( !is_array( $fileInfo ) ) {
1279 $msg = "Invalid type in $debugKey for module '{$this->getName()}', " .
1280 "must be array, string or FilePath";
1281 $this->getLogger()->error( $msg );
1282 throw new LogicException( $msg );
1283 }
1284 if ( !isset( $fileInfo['name'] ) ) {
1285 $msg = "Missing 'name' key in $debugKey for module '{$this->getName()}'";
1286 $this->getLogger()->error( $msg );
1287 throw new LogicException( $msg );
1288 }
1289 $fileName = $this->getPath( $fileInfo['name'] );
1290
1291 // Infer type from alias if needed
1292 $type = $fileInfo['type'] ?? self::getPackageFileType( $fileName );
1293 $expanded = [
1294 'name' => $fileName,
1295 'type' => $type
1296 ];
1297 if ( !empty( $fileInfo['main'] ) ) {
1298 $expanded['main'] = true;
1299 }
1300
1301 // Perform expansions (except 'file' and 'callback'), creating one of these keys:
1302 // - 'content': literal value.
1303 // - 'filePath': content to be read from a file.
1304 // - 'callback': content computed by a callable.
1305 if ( isset( $fileInfo['content'] ) ) {
1306 $expanded['content'] = $fileInfo['content'];
1307 } elseif ( isset( $fileInfo['file'] ) ) {
1308 $expanded['filePath'] = $this->makeFilePath( $fileInfo['file'] );
1309 } elseif ( isset( $fileInfo['callback'] ) ) {
1310 // If no extra parameter for the callback is given, use null.
1311 $expanded['callbackParam'] = $fileInfo['callbackParam'] ?? null;
1312
1313 if ( !is_callable( $fileInfo['callback'] ) ) {
1314 $msg = "Invalid 'callback' for module '{$this->getName()}', file '{$fileName}'.";
1315 $this->getLogger()->error( $msg );
1316 throw new LogicException( $msg );
1317 }
1318 if ( isset( $fileInfo['versionCallback'] ) ) {
1319 if ( !is_callable( $fileInfo['versionCallback'] ) ) {
1320 throw new LogicException( "Invalid 'versionCallback' for "
1321 . "module '{$this->getName()}', file '{$fileName}'."
1322 );
1323 }
1324
1325 // Execute the versionCallback with the same arguments that
1326 // would be given to the callback
1327 $callbackResult = ( $fileInfo['versionCallback'] )(
1328 $context,
1329 $this->getConfig(),
1330 $expanded['callbackParam']
1331 );
1332 if ( $callbackResult instanceof FilePath ) {
1333 $callbackResult->initBasePaths( $this->localBasePath, $this->remoteBasePath );
1334 $expanded['versionFilePath'] = $callbackResult;
1335 } else {
1336 $expanded['definitionSummary'] = $callbackResult;
1337 }
1338 // Don't invoke 'callback' here as it may be expensive (T223260).
1339 $expanded['callback'] = $fileInfo['callback'];
1340 } else {
1341 // Else go ahead invoke callback with its arguments.
1342 $callbackResult = ( $fileInfo['callback'] )(
1343 $context,
1344 $this->getConfig(),
1345 $expanded['callbackParam']
1346 );
1347 if ( $callbackResult instanceof FilePath ) {
1348 $callbackResult->initBasePaths( $this->localBasePath, $this->remoteBasePath );
1349 $expanded['filePath'] = $callbackResult;
1350 } else {
1351 $expanded['content'] = $callbackResult;
1352 }
1353 }
1354 } elseif ( isset( $fileInfo['config'] ) ) {
1355 if ( $type !== 'data' ) {
1356 $msg = "Key 'config' only valid for data files. "
1357 . " Module '{$this->getName()}', file '{$fileName}' is '{$type}'.";
1358 $this->getLogger()->error( $msg );
1359 throw new LogicException( $msg );
1360 }
1361 $expandedConfig = [];
1362 foreach ( $fileInfo['config'] as $configKey => $var ) {
1363 $expandedConfig[ is_numeric( $configKey ) ? $var : $configKey ] = $this->getConfig()->get( $var );
1364 }
1365 $expanded['content'] = $expandedConfig;
1366 } elseif ( !empty( $fileInfo['main'] ) ) {
1367 // [ 'name' => 'foo.js', 'main' => true ] is shorthand
1368 $expanded['filePath'] = $this->makeFilePath( $fileName );
1369 } else {
1370 $msg = "Incomplete definition for module '{$this->getName()}', file '{$fileName}'. "
1371 . "One of 'file', 'content', 'callback', or 'config' must be set.";
1372 $this->getLogger()->error( $msg );
1373 throw new LogicException( $msg );
1374 }
1375 if ( !isset( $expanded['filePath'] ) ) {
1376 $expanded['virtualFilePath'] = $this->makeFilePath( $fileName );
1377 }
1378 return $expanded;
1379 }
1380
1387 private function makeFilePath( $path ): FilePath {
1388 if ( $path instanceof FilePath ) {
1389 return $path;
1390 } elseif ( is_string( $path ) ) {
1391 return new FilePath( $path, $this->localBasePath, $this->remoteBasePath );
1392 } else {
1393 throw new InvalidArgumentException( '$path must be either FilePath or string' );
1394 }
1395 }
1396
1403 public function getPackageFiles( Context $context ) {
1404 if ( $this->packageFiles === null ) {
1405 return null;
1406 }
1407 $hash = $context->getHash();
1408 if ( isset( $this->fullyExpandedPackageFiles[ $hash ] ) ) {
1409 return $this->fullyExpandedPackageFiles[ $hash ];
1410 }
1411 $expandedPackageFiles = $this->expandPackageFiles( $context ) ?? [];
1412
1413 foreach ( $expandedPackageFiles['files'] as &$fileInfo ) {
1414 $this->readFileInfo( $context, $fileInfo );
1415 }
1416
1417 $this->fullyExpandedPackageFiles[ $hash ] = $expandedPackageFiles;
1418 return $expandedPackageFiles;
1419 }
1420
1429 private function readFileInfo( Context $context, array &$fileInfo ) {
1430 // Turn any 'filePath' or 'callback' key into actual 'content',
1431 // and remove the key after that. The callback could return a
1432 // FilePath object; if that happens, fall through to the 'filePath'
1433 // handling.
1434 if ( !isset( $fileInfo['content'] ) && isset( $fileInfo['callback'] ) ) {
1435 $callbackResult = ( $fileInfo['callback'] )(
1436 $context,
1437 $this->getConfig(),
1438 $fileInfo['callbackParam']
1439 );
1440 if ( $callbackResult instanceof FilePath ) {
1441 // Fall through to the filePath handling code below
1442 $fileInfo['filePath'] = $callbackResult;
1443 } else {
1444 $fileInfo['content'] = $callbackResult;
1445 }
1446 unset( $fileInfo['callback'] );
1447 }
1448 // Only interpret 'filePath' if 'content' hasn't been set already.
1449 // This can happen if 'versionCallback' provided 'filePath',
1450 // while 'callback' provides 'content'. In that case both are set
1451 // at this point. The 'filePath' from 'versionCallback' in that case is
1452 // only to inform getDefinitionSummary().
1453 if ( !isset( $fileInfo['content'] ) && isset( $fileInfo['filePath'] ) ) {
1454 $localPath = $this->getLocalPath( $fileInfo['filePath'] );
1455 $content = $this->getFileContents( $localPath, 'package' );
1456 if ( $fileInfo['type'] === 'data' ) {
1457 $content = json_decode( $content, false, 512, JSON_THROW_ON_ERROR );
1458 }
1459 $fileInfo['content'] = $content;
1460 }
1461 if ( $fileInfo['type'] === 'script-vue' ) {
1462 try {
1463 $parsedComponent = $this->getVueComponentParser()->parse(
1464 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
1465 $fileInfo['content'],
1466 [ 'minifyTemplate' => !$context->getDebug() ]
1467 );
1468 } catch ( TimeoutException $e ) {
1469 throw $e;
1470 } catch ( Exception $e ) {
1471 $msg = "Error parsing file '{$fileInfo['name']}' in module '{$this->getName()}': " .
1472 $e->getMessage();
1473 $this->getLogger()->error( $msg );
1474 throw new RuntimeException( $msg );
1475 }
1476 $encodedTemplate = json_encode( $parsedComponent['template'] );
1477 if ( $context->getDebug() ) {
1478 // Replace \n (backslash-n) with space + backslash-n + backslash-newline in debug mode
1479 // The \n has to be preserved to prevent Vue parser issues (T351771)
1480 // We only replace \n if not preceded by a backslash, to avoid breaking '\\n'
1481 $encodedTemplate = preg_replace( '/(?<!\\\\)\\\\n/', " \\n\\\n", $encodedTemplate );
1482 // Expand \t to real tabs in debug mode
1483 $encodedTemplate = strtr( $encodedTemplate, [ "\\t" => "\t" ] );
1484 }
1485 $fileInfo['content'] = [
1486 'script' => $parsedComponent['script'] .
1487 ";\nmodule.exports.template = $encodedTemplate;",
1488 'style' => $parsedComponent['style'] ?? '',
1489 'styleLang' => $parsedComponent['styleLang'] ?? 'css'
1490 ];
1491 $fileInfo['type'] = 'script+style';
1492 }
1493 if ( !isset( $fileInfo['content'] ) ) {
1494 // This should not be possible due to validation in expandFileInfo()
1495 $msg = "Unable to resolve contents for file {$fileInfo['name']}";
1496 $this->getLogger()->error( $msg );
1497 throw new RuntimeException( $msg );
1498 }
1499
1500 // Not needed for client response, exists for use by getDefinitionSummary().
1501 unset( $fileInfo['definitionSummary'] );
1502 // Not needed for client response, used by callbacks only.
1503 unset( $fileInfo['callbackParam'] );
1504 }
1505
1516 protected function stripBom( $input ) {
1517 if ( str_starts_with( $input, "\xef\xbb\xbf" ) ) {
1518 return substr( $input, 3 );
1519 }
1520 return $input;
1521 }
1522}
const CACHE_ANYTHING
Definition Defines.php:86
$fallback
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:81
static getFileContentsHash( $filePaths)
Get a hash of the combined contents of one or more files, either by retrieving a previously-computed ...
A class containing constants representing the names of configuration variables.
const StylePath
Name constant for the StylePath setting, for use with Config::get()
const ExtensionAssetsPath
Name constant for the ExtensionAssetsPath setting, for use with Config::get()
const Server
Name constant for the Server setting, for use with Config::get()
const ResourceBasePath
Name constant for the ResourceBasePath setting, for use with Config::get()
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
This is one of the Core classes and should be read at least once by any new developers.
Load JSON files, and uses a Processor to extract information.
Context object that contains information about the state of a specific ResourceLoader web request.
Definition Context.php:45
getHash()
All factors that uniquely identify this request, except 'modules'.
Definition Context.php:446
Module based on local JavaScript/CSS files.
getDependencies(Context $context=null)
Get names of modules this module depends on.
array< string, array< int, string|FilePath > > $skinScripts
Lists of JavaScript files by skin name.
getScriptURLsForDebug(Context $context)
static tryForKey(array $list, $key, $fallback=null)
Get a list of element that match a key, optionally using a fallback key.
getAllSkinStyleFiles()
Get a list of file paths for all skin style files in the module, for all available skins.
readStyleFile( $path, Context $context)
Read and process a style file.
getTemplates()
Get content of named templates for this module.
getStyleSheetLang( $path)
Infer the stylesheet language from a stylesheet file path.
processStyle( $style, $styleLang, $path, Context $context)
Process a CSS/LESS string.
getDefinitionSummary(Context $context)
Get the definition summary for this module.
requiresES6()
Whether the module requires ES6 support in the client.
readStyleFiles(array $styles, Context $context)
Read the contents of a list of CSS files and remap and concatenate these.
array< string, array< int, string|FilePath > > $languageScripts
Lists of JavaScript files by language code.
array< string, array< int, string|FilePath > > $skinStyles
Lists of CSS files by skin name.
array< int|string, string|FilePath > $templates
List of the named templates used by this module.
bool $noflip
Whether CSSJanus flipping should be skipped for this module.
enableModuleContentVersion()
Disable module content versioning.
string[] $dependencies
List of modules this module depends on.
static extractBasePaths(array $options=[], $localBasePath=null, $remoteBasePath=null)
Extract a pair of local and remote base paths from module definition information.
string $remoteBasePath
Remote base path, see __construct()
array< int, string|FilePath > $scripts
List of JavaScript file paths to always include.
stripBom( $input)
Take an input string and remove the UTF-8 BOM character if present.
getFlip(Context $context)
Get whether CSS for this module should be flipped.
getSkinStyleFiles( $skinName)
Get a list of file paths for all skin styles in the module used by the skin.
null string $skipFunction
File name containing the body of the skip function.
VueComponentParser null $vueComponentParser
Lazy-created by getVueComponentParser()
getPackageFiles(Context $context)
Resolve the package files definition and generate the content of each package file.
array< int, string|FilePath > $debugScripts
List of paths to JavaScript files to include in debug mode.
__construct(array $options=[], string $localBasePath=null, string $remoteBasePath=null)
Construct a new module from an options array.
string $localBasePath
Local base path, see __construct()
string[] $localFileRefs
Place where readStyleFile() tracks file dependencies.
compileLessString( $style, $stylePath, Context $context)
Compile a LESS string into CSS.
bool $hasGeneratedStyles
Whether getStyleURLsForDebug should return raw file paths, or return load.php urls.
getStyleFiles(Context $context)
Get a list of file paths for all styles in this module, in order of proper inclusion.
getScript(Context $context)
Get all JS for this module for a given language and skin.
getMessages()
Get message keys used by this module.
getAllStyleFiles()
Get all style files and all skin style files used by this module.
getGroup()
Get the name of the group this module should be loaded in.
string[] $missingLocalFileRefs
Place where readStyleFile() tracks file dependencies for non-existent files.
getStyles(Context $context)
Get all styles for a given context.
shouldSkipStructureTest()
Whether to skip the structure test ResourcesTest::testRespond() for this module.
static getPackageFileType( $path)
Infer the file type from a package file path.
bool $debugRaw
Link to raw files in debug mode.
string[] $messages
List of message keys used by this module.
null string $group
Name of group to load this module in.
setSkinStylesOverride(array $moduleSkinStyles)
Provide overrides for skinStyles to modules that support that.
getType()
Get the module's load type.
null array $packageFiles
Packaged files definition, to bundle and make available client-side via require().
bool $skipStructureTest
Whether to skip the structure test ResourcesTest::testRespond()
array< int, string|FilePath > $styles
List of CSS file files to always include.
A path to a bundled file (such as JavaScript or CSS), along with a remote and local base path.
Definition FilePath.php:34
Abstraction for ResourceLoader modules, with name registration and maxage functionality.
Definition Module.php:49
saveFileDependencies(Context $context, array $curFileRefs)
Save the indirect dependencies for this module pursuant to the skin/language context.
Definition Module.php:608
getLessVars(Context $context)
Get module-specific LESS variables, if any.
Definition Module.php:796
getFileDependencies(Context $context)
Get the indirect dependencies for this module pursuant to the skin/language context.
Definition Module.php:570
getMessageBlob(Context $context)
Get the hash of the message blob.
Definition Module.php:683
Parser for Vue single file components (.vue files).
get( $name)
Get a configuration variable such as "Sitename" or "UploadMaintenance.".