29use InvalidArgumentException;
36use Wikimedia\Minify\CSSMin;
37use Wikimedia\RequestTimeout\TimeoutException;
102 private $expandedPackageFiles = [];
108 private $fullyExpandedPackageFiles = [];
177 $hasTemplates =
false;
184 foreach ( $options as $member => $option ) {
191 $this->{$member} = is_array( $option ) ? $option : [ $option ];
194 $hasTemplates =
true;
195 $this->{$member} = is_array( $option ) ? $option : [ $option ];
198 case 'languageScripts':
201 if ( !is_array( $option ) ) {
202 throw new InvalidArgumentException(
203 "Invalid collated file path list error. " .
204 "'$option' given, array expected."
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."
214 $this->{$member}[$key] = is_array( $value ) ? $value : [ $value ];
218 $this->deprecated = $option;
224 $option = array_values( array_unique( (array)$option ) );
227 $this->{$member} = $option;
232 $this->{$member} = (string)$option;
237 case 'skipStructureTest':
238 $this->{$member} = (bool)$option;
242 if ( isset( $options[
'scripts'] ) && isset( $options[
'packageFiles'] ) ) {
243 throw new InvalidArgumentException(
"A module may not set both 'scripts' and 'packageFiles'" );
245 if ( isset( $options[
'packageFiles'] ) && isset( $options[
'skinScripts'] ) ) {
246 throw new InvalidArgumentException(
"Options 'skinScripts' and 'packageFiles' cannot be used together." );
248 if ( $hasTemplates ) {
249 $this->dependencies[] =
'mediawiki.template';
251 foreach ( $this->templates as $alias => $templatePath ) {
252 if ( is_int( $alias ) ) {
253 $alias = $this->
getPath( $templatePath );
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;
289 if ( isset( $options[
'remoteExtPath'] ) ) {
292 $remoteBasePath = $extensionAssetsPath .
'/' . $options[
'remoteExtPath'];
295 if ( isset( $options[
'remoteSkinPath'] ) ) {
301 if ( array_key_exists(
'localBasePath', $options ) ) {
305 if ( array_key_exists(
'remoteBasePath', $options ) ) {
328 if ( $file[
'type'] ===
'script+style' ) {
329 $file[
'content'] = $file[
'content'][
'script'];
330 $file[
'type'] =
'script';
336 $files = $this->getScriptFiles( $context );
337 foreach ( $files as &$file ) {
338 $this->readFileInfo( $context, $file );
340 return [
'plainScripts' => $files ];
353 foreach ( $this->getScriptFiles( $context ) as $file ) {
354 if ( isset( $file[
'filePath'] ) ) {
357 $url = $rl->expandUrl( $server, $url );
374 && !$this->packageFiles
376 && !$this->hasGeneratedScripts();
380 return $this->skipStructureTest || parent::shouldSkipStructureTest();
388 private function hasGeneratedScripts() {
390 [ $this->scripts, $this->languageScripts, $this->skinScripts, $this->debugScripts ]
394 if ( is_array( $script ) ) {
395 if ( isset( $script[
'callback'] ) || isset( $script[
'versionCallback'] ) ) {
419 if ( $file[
'type'] ===
'script+style' ) {
421 $file[
'content'][
'style'],
422 $file[
'content'][
'styleLang'],
443 if ( $this->hasGeneratedStyles ) {
446 return parent::getStyleURLsForDebug( $context );
451 foreach ( $this->
getStyleFiles( $context ) as $mediaType => $list ) {
452 $urls[$mediaType] = [];
453 foreach ( $list as $file ) {
454 $urls[$mediaType][] = OutputPage::transformResourcePath(
498 private function getFileContents( $localPath, $type ) {
499 if ( !is_file( $localPath ) ) {
500 throw new RuntimeException(
"$type file not found or not a file: \"$localPath\"" );
502 return $this->
stripBom( file_get_contents( $localPath ) );
509 if ( !$this->skipFunction ) {
512 $localPath = $this->
getLocalPath( $this->skipFunction );
513 return $this->getFileContents( $localPath,
'skip function' );
538 private function getFileHashes(
Context $context ) {
542 foreach ( $filePaths as $filePath ) {
550 $expandedPackageFiles = $this->expandPackageFiles( $context );
551 if ( $expandedPackageFiles ) {
552 foreach ( $expandedPackageFiles[
'files'] as $fileInfo ) {
553 if ( isset( $fileInfo[
'filePath'] ) ) {
555 $filePath = $fileInfo[
'filePath'];
556 $files[] = $filePath->getLocalPath();
562 $scriptFileInfos = $this->getScriptFiles( $context );
563 foreach ( $scriptFileInfos as $fileInfo ) {
564 $filePath = $fileInfo[
'filePath'] ?? $fileInfo[
'versionFilePath'] ??
null;
565 if ( $filePath instanceof FilePath ) {
566 $files[] = $filePath->getLocalPath();
570 foreach ( $this->templates as $filePath ) {
574 if ( $this->skipFunction ) {
586 $files = array_unique( $files );
602 $summary = parent::getDefinitionSummary( $context );
619 $options[$member] = $this->{$member};
623 $packageSummaries = [];
633 foreach (
$packageFiles[
'files'] as $fileName => $fileInfo ) {
634 $packageSummaries[$fileName] =
635 $fileInfo[
'definitionSummary'] ?? $fileInfo[
'content'] ??
null;
639 $scriptFiles = $this->getScriptFiles( $context );
640 $scriptSummaries = [];
641 foreach ( $scriptFiles as $fileName => $fileInfo ) {
642 $scriptSummaries[$fileName] =
643 $fileInfo[
'definitionSummary'] ?? $fileInfo[
'content'] ??
null;
647 'options' => $options,
648 'packageFiles' => $packageSummaries,
649 'scripts' => $scriptSummaries,
650 'fileHashes' => $this->getFileHashes( $context ),
656 $summary[] = [
'lessVars' => $lessVars ];
666 if ( $this->vueComponentParser ===
null ) {
678 return $path->getPath();
690 if (
$path->getLocalBasePath() !==
null ) {
691 return $path->getLocalPath();
696 return "{$this->localBasePath}/$path";
705 if (
$path->getRemoteBasePath() !==
null ) {
706 return $path->getRemotePath();
711 if ( $this->remoteBasePath ===
'/' ) {
714 return "{$this->remoteBasePath}/$path";
726 return preg_match(
'/\.less$/i',
$path ) ?
'less' :
'css';
736 if ( preg_match(
'/\.json$/i',
$path ) ) {
739 if ( preg_match(
'/\.vue$/i',
$path ) ) {
752 private static function collateStyleFilesByMedia( array $list ) {
754 foreach ( $list as $key => $value ) {
755 if ( is_int( $key ) ) {
757 $collatedFiles[
'all'][] = $value;
758 } elseif ( is_array( $value ) ) {
760 $optionValue = $value[
'media'] ??
'all';
761 $collatedFiles[$optionValue][] = $key;
764 return $collatedFiles;
777 if ( isset( $list[$key] ) && is_array( $list[$key] ) ) {
794 private function getScriptFiles(
Context $context ): array {
798 'scripts' => $this->scripts,
799 'languageScripts' => $this->getLanguageScripts( $context->getLanguage() ),
800 'skinScripts' => self::
tryForKey( $this->skinScripts, $context->getSkin(),
'default' ),
807 foreach ( $filesByCategory as $category => $files ) {
808 foreach ( $files as $key => $fileInfo ) {
809 $expandedFileInfo = $this->expandFileInfo( $context, $fileInfo,
"$category\[$key]" );
810 $expandedFiles[$expandedFileInfo[
'name']] = $expandedFileInfo;
814 return $expandedFiles;
824 private function getLanguageScripts(
string $lang ): array {
825 $scripts = self::tryForKey( $this->languageScripts, $lang );
832 if ( $this->languageScripts ) {
834 ->getLanguageFallback()
835 ->getAll( $lang, LanguageFallback::MESSAGES );
836 foreach ( $fallbacks as $lang ) {
837 $scripts = self::tryForKey( $this->languageScripts, $lang );
848 $moduleName = $this->getName();
849 foreach ( $moduleSkinStyles as $skinName => $overrides ) {
852 if ( isset( $this->skinStyles[$skinName] ) ) {
858 if ( isset( $overrides[$moduleName] ) ) {
859 $paths = (array)$overrides[$moduleName];
861 } elseif ( isset( $overrides[
'+' . $moduleName] ) ) {
862 $paths = (array)$overrides[
'+' . $moduleName];
863 $styleFiles = isset( $this->skinStyles[
'default'] ) ?
864 (array)$this->skinStyles[
'default'] :
872 [ $localBasePath, $remoteBasePath ] = self::extractBasePaths( $overrides );
874 foreach ( $paths as
$path ) {
875 $styleFiles[] =
new FilePath(
$path, $localBasePath, $remoteBasePath );
878 $this->skinStyles[$skinName] = $styleFiles;
890 return array_merge_recursive(
891 self::collateStyleFilesByMedia( $this->styles ),
892 self::collateStyleFilesByMedia(
893 self::tryForKey( $this->skinStyles, $context->
getSkin(),
'default' )
906 return self::collateStyleFilesByMedia(
907 self::tryForKey( $this->skinStyles, $skinName )
918 $skinFactory = MediaWikiServices::getInstance()->getSkinFactory();
921 $internalSkinNames = array_keys( $skinFactory->getInstalledSkins() );
922 $internalSkinNames[] =
'default';
924 foreach ( $internalSkinNames as $internalSkinName ) {
925 $styleFiles = array_merge_recursive(
927 $this->getSkinStyleFiles( $internalSkinName )
940 $collatedStyleFiles = array_merge_recursive(
941 self::collateStyleFilesByMedia( $this->styles ),
942 $this->getAllSkinStyleFiles()
947 foreach ( $collatedStyleFiles as $styleFiles ) {
948 foreach ( $styleFiles as $styleFile ) {
949 $result[] = $this->getLocalPath( $styleFile );
968 foreach ( $styles as $media => $files ) {
969 $uniqueFiles = array_unique( $files, SORT_REGULAR );
971 foreach ( $uniqueFiles as $file ) {
972 $styleFiles[] = $this->readStyleFile( $file, $context );
974 $styles[$media] = implode(
"\n", $styleFiles );
990 $localPath = $this->getLocalPath(
$path );
991 $style = $this->getFileContents( $localPath,
'style' );
992 $styleLang = $this->getStyleSheetLang( $localPath );
994 return $this->processStyle( $style, $styleLang,
$path, $context );
1014 $localPath = $this->getLocalPath(
$path );
1015 $remotePath = $this->getRemotePath(
$path );
1017 if ( $styleLang ===
'less' ) {
1018 $style = $this->compileLessString( $style, $localPath, $context );
1019 $this->hasGeneratedStyles =
true;
1022 if ( $this->getFlip( $context ) ) {
1023 $style = CSSJanus::transform(
1030 $localDir = dirname( $localPath );
1031 $remoteDir = dirname( $remotePath );
1033 $localFileRefs = CSSMin::getLocalFileReferences( $style, $localDir );
1034 foreach ( $localFileRefs as $file ) {
1035 if ( is_file( $file ) ) {
1036 $this->localFileRefs[] = $file;
1038 $this->missingLocalFileRefs[] = $file;
1043 return CSSMin::remap( $style, $localDir, $remoteDir,
true );
1052 return $context->
getDirection() ===
'rtl' && !$this->noflip;
1062 $canBeStylesOnly = !(
1065 || $this->debugScripts
1067 || $this->languageScripts
1068 || $this->skinScripts
1069 || $this->dependencies
1071 || $this->skipFunction
1072 || $this->packageFiles
1074 return $canBeStylesOnly ? self::LOAD_STYLES : self::LOAD_GENERAL;
1092 $cache = MediaWikiServices::getInstance()->getObjectCacheFactory()
1096 $skinName = $context->
getSkin();
1097 $skinImportPaths = ExtensionRegistry::getInstance()->getAttribute(
'SkinLessImportPaths' );
1099 if ( isset( $skinImportPaths[ $skinName ] ) ) {
1100 $importDirs[] = $skinImportPaths[ $skinName ];
1103 $vars = $this->getLessVars( $context );
1109 'importDirs' => $importDirs,
1111 $key = $cache->makeGlobalKey(
1112 'resourceloader-less',
1114 hash(
'md4', $style ),
1115 hash(
'md4', serialize( $compilerParams ) )
1120 $data = $cache->get( $key );
1123 $data[
'hash'] !== FileContentsHasher::getFileContentsHash( $data[
'files'] )
1125 $compiler = $context->
getResourceLoader()->getLessCompiler( $vars, $importDirs );
1127 $css = $compiler->parse( $style, $stylePath )->getCss();
1131 $files = $compiler->AllParsedFiles();
1134 'files' => Module::getRelativePaths( $files ),
1135 'hash' => FileContentsHasher::getFileContentsHash( $files )
1137 $cache->set( $key, $data, $cache::TTL_DAY );
1140 foreach ( Module::expandRelativePaths( $data[
'files'] ) as
$path ) {
1141 $this->localFileRefs[] =
$path;
1144 return $data[
'css'];
1155 foreach ( $this->templates as $alias => $templatePath ) {
1157 if ( is_int( $alias ) ) {
1158 $alias = $this->getPath( $templatePath );
1160 $localPath = $this->getLocalPath( $templatePath );
1161 $content = $this->getFileContents( $localPath,
'template' );
1163 $templates[$alias] = $this->stripBom( $content );
1187 private function expandPackageFiles(
Context $context ) {
1189 if ( isset( $this->expandedPackageFiles[$hash] ) ) {
1190 return $this->expandedPackageFiles[$hash];
1192 if ( $this->packageFiles ===
null ) {
1195 $expandedFiles = [];
1198 foreach ( $this->packageFiles as $key => $fileInfo ) {
1199 $expanded = $this->expandFileInfo( $context, $fileInfo,
"packageFiles[$key]" );
1200 $fileName = $expanded[
'name'];
1201 if ( !empty( $expanded[
'main'] ) ) {
1202 unset( $expanded[
'main'] );
1203 $type = $expanded[
'type'];
1204 $mainFile = $fileName;
1205 if ( $type !==
'script' && $type !==
'script-vue' ) {
1206 $msg =
"Main file in package must be of type 'script', module " .
1207 "'{$this->getName()}', main file '{$mainFile}' is '{$type}'.";
1208 $this->getLogger()->error( $msg );
1209 throw new LogicException( $msg );
1212 $expandedFiles[$fileName] = $expanded;
1215 if ( $expandedFiles && $mainFile ===
null ) {
1217 foreach ( $expandedFiles as
$path => $file ) {
1218 if ( $file[
'type'] ===
'script' || $file[
'type'] ===
'script-vue' ) {
1226 'main' => $mainFile,
1227 'files' => $expandedFiles
1230 $this->expandedPackageFiles[$hash] = $result;
1263 private function expandFileInfo( Context $context, $fileInfo, $debugKey ) {
1264 if ( is_string( $fileInfo ) ) {
1267 'name' => $fileInfo,
1268 'type' => self::getPackageFileType( $fileInfo ),
1269 'filePath' =>
new FilePath( $fileInfo, $this->localBasePath, $this->remoteBasePath )
1271 } elseif ( $fileInfo instanceof FilePath ) {
1273 'name' => $fileInfo->getPath(),
1276 } elseif ( !is_array( $fileInfo ) ) {
1277 $msg =
"Invalid type in $debugKey for module '{$this->getName()}', " .
1278 "must be array, string or FilePath";
1279 $this->getLogger()->error( $msg );
1280 throw new LogicException( $msg );
1282 if ( !isset( $fileInfo[
'name'] ) ) {
1283 $msg =
"Missing 'name' key in $debugKey for module '{$this->getName()}'";
1284 $this->getLogger()->error( $msg );
1285 throw new LogicException( $msg );
1287 $fileName = $this->getPath( $fileInfo[
'name'] );
1290 $type = $fileInfo[
'type'] ?? self::getPackageFileType( $fileName );
1292 'name' => $fileName,
1295 if ( !empty( $fileInfo[
'main'] ) ) {
1296 $expanded[
'main'] =
true;
1303 if ( isset( $fileInfo[
'content'] ) ) {
1304 $expanded[
'content'] = $fileInfo[
'content'];
1305 } elseif ( isset( $fileInfo[
'file'] ) ) {
1306 $expanded[
'filePath'] = $this->makeFilePath( $fileInfo[
'file'] );
1307 } elseif ( isset( $fileInfo[
'callback'] ) ) {
1309 $expanded[
'callbackParam'] = $fileInfo[
'callbackParam'] ??
null;
1311 if ( !is_callable( $fileInfo[
'callback'] ) ) {
1312 $msg =
"Invalid 'callback' for module '{$this->getName()}', file '{$fileName}'.";
1313 $this->getLogger()->error( $msg );
1314 throw new LogicException( $msg );
1316 if ( isset( $fileInfo[
'versionCallback'] ) ) {
1317 if ( !is_callable( $fileInfo[
'versionCallback'] ) ) {
1318 throw new LogicException(
"Invalid 'versionCallback' for "
1319 .
"module '{$this->getName()}', file '{$fileName}'."
1325 $callbackResult = ( $fileInfo[
'versionCallback'] )(
1328 $expanded[
'callbackParam']
1330 if ( $callbackResult instanceof FilePath ) {
1331 $callbackResult->initBasePaths( $this->localBasePath, $this->remoteBasePath );
1332 $expanded[
'versionFilePath'] = $callbackResult;
1334 $expanded[
'definitionSummary'] = $callbackResult;
1337 $expanded[
'callback'] = $fileInfo[
'callback'];
1340 $callbackResult = ( $fileInfo[
'callback'] )(
1343 $expanded[
'callbackParam']
1345 if ( $callbackResult instanceof FilePath ) {
1346 $callbackResult->initBasePaths( $this->localBasePath, $this->remoteBasePath );
1347 $expanded[
'filePath'] = $callbackResult;
1349 $expanded[
'content'] = $callbackResult;
1352 } elseif ( isset( $fileInfo[
'config'] ) ) {
1353 if ( $type !==
'data' ) {
1354 $msg =
"Key 'config' only valid for data files. "
1355 .
" Module '{$this->getName()}', file '{$fileName}' is '{$type}'.";
1356 $this->getLogger()->error( $msg );
1357 throw new LogicException( $msg );
1359 $expandedConfig = [];
1360 foreach ( $fileInfo[
'config'] as $configKey => $var ) {
1361 $expandedConfig[ is_numeric( $configKey ) ? $var : $configKey ] = $this->getConfig()->get( $var );
1363 $expanded[
'content'] = $expandedConfig;
1364 } elseif ( !empty( $fileInfo[
'main'] ) ) {
1366 $expanded[
'filePath'] = $this->makeFilePath( $fileName );
1368 $msg =
"Incomplete definition for module '{$this->getName()}', file '{$fileName}'. "
1369 .
"One of 'file', 'content', 'callback', or 'config' must be set.";
1370 $this->getLogger()->error( $msg );
1371 throw new LogicException( $msg );
1373 if ( !isset( $expanded[
'filePath'] ) ) {
1374 $expanded[
'virtualFilePath'] = $this->makeFilePath( $fileName );
1385 private function makeFilePath(
$path ): FilePath {
1386 if (
$path instanceof FilePath ) {
1388 } elseif ( is_string(
$path ) ) {
1389 return new FilePath(
$path, $this->localBasePath, $this->remoteBasePath );
1391 throw new InvalidArgumentException(
'$path must be either FilePath or string' );
1402 if ( $this->packageFiles ===
null ) {
1406 if ( isset( $this->fullyExpandedPackageFiles[ $hash ] ) ) {
1407 return $this->fullyExpandedPackageFiles[ $hash ];
1409 $expandedPackageFiles = $this->expandPackageFiles( $context ) ?? [];
1411 foreach ( $expandedPackageFiles[
'files'] as &$fileInfo ) {
1412 $this->readFileInfo( $context, $fileInfo );
1415 $this->fullyExpandedPackageFiles[ $hash ] = $expandedPackageFiles;
1416 return $expandedPackageFiles;
1427 private function readFileInfo(
Context $context, array &$fileInfo ) {
1432 if ( !isset( $fileInfo[
'content'] ) && isset( $fileInfo[
'callback'] ) ) {
1433 $callbackResult = ( $fileInfo[
'callback'] )(
1436 $fileInfo[
'callbackParam']
1438 if ( $callbackResult instanceof
FilePath ) {
1440 $fileInfo[
'filePath'] = $callbackResult;
1442 $fileInfo[
'content'] = $callbackResult;
1444 unset( $fileInfo[
'callback'] );
1451 if ( !isset( $fileInfo[
'content'] ) && isset( $fileInfo[
'filePath'] ) ) {
1452 $localPath = $this->getLocalPath( $fileInfo[
'filePath'] );
1453 $content = $this->getFileContents( $localPath,
'package' );
1454 if ( $fileInfo[
'type'] ===
'data' ) {
1455 $content = json_decode( $content,
false, 512, JSON_THROW_ON_ERROR );
1457 $fileInfo[
'content'] = $content;
1459 if ( $fileInfo[
'type'] ===
'script-vue' ) {
1461 $parsedComponent = $this->getVueComponentParser()->parse(
1463 $fileInfo[
'content'],
1464 [
'minifyTemplate' => !$context->
getDebug() ]
1466 }
catch ( TimeoutException $e ) {
1468 }
catch ( Exception $e ) {
1469 $msg =
"Error parsing file '{$fileInfo['name']}' in module '{$this->getName()}': " .
1471 $this->getLogger()->error( $msg );
1472 throw new RuntimeException( $msg );
1474 $encodedTemplate = json_encode( $parsedComponent[
'template'] );
1479 $encodedTemplate = preg_replace(
'/(?<!\\\\)\\\\n/',
" \\n\\\n", $encodedTemplate );
1481 $encodedTemplate = strtr( $encodedTemplate, [
"\\t" =>
"\t" ] );
1483 $fileInfo[
'content'] = [
1484 'script' => $parsedComponent[
'script'] .
1485 ";\nmodule.exports.template = $encodedTemplate;",
1486 'style' => $parsedComponent[
'style'] ??
'',
1487 'styleLang' => $parsedComponent[
'styleLang'] ??
'css'
1489 $fileInfo[
'type'] =
'script+style';
1491 if ( !isset( $fileInfo[
'content'] ) ) {
1493 $msg =
"Unable to resolve contents for file {$fileInfo['name']}";
1494 $this->getLogger()->error( $msg );
1495 throw new RuntimeException( $msg );
1499 unset( $fileInfo[
'definitionSummary'] );
1501 unset( $fileInfo[
'callbackParam'] );
1515 if ( str_starts_with( $input,
"\xef\xbb\xbf" ) ) {
1516 return substr( $input, 3 );
if(!defined('MW_SETUP_CALLBACK'))
Load JSON files, and uses a Processor to extract information.
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()
This is one of the Core classes and should be read at least once by any new developers.
Context object that contains information about the state of a specific ResourceLoader web request.
getHash()
All factors that uniquely identify this request, except 'modules'.