28use 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;
287 if ( isset( $options[
'remoteExtPath'] ) ) {
290 $remoteBasePath = $extensionAssetsPath .
'/' . $options[
'remoteExtPath'];
293 if ( isset( $options[
'remoteSkinPath'] ) ) {
299 if ( array_key_exists(
'localBasePath', $options ) ) {
303 if ( array_key_exists(
'remoteBasePath', $options ) ) {
326 if ( $file[
'type'] ===
'script+style' ) {
327 $file[
'content'] = $file[
'content'][
'script'];
328 $file[
'type'] =
'script';
334 $files = $this->getScriptFiles( $context );
335 foreach ( $files as &$file ) {
336 $this->readFileInfo( $context, $file );
338 return [
'plainScripts' => $files ];
351 foreach ( $this->getScriptFiles( $context ) as $file ) {
352 if ( isset( $file[
'filePath'] ) ) {
355 $url = $rl->expandUrl( $server,
$url );
372 && !$this->packageFiles
374 && !$this->hasGeneratedScripts();
378 return $this->skipStructureTest || parent::shouldSkipStructureTest();
386 private function hasGeneratedScripts() {
388 [ $this->scripts, $this->languageScripts, $this->skinScripts, $this->debugScripts ]
392 if ( is_array( $script ) ) {
393 if ( isset( $script[
'callback'] ) || isset( $script[
'versionCallback'] ) ) {
417 if ( $file[
'type'] ===
'script+style' ) {
419 $file[
'content'][
'style'],
420 $file[
'content'][
'styleLang'],
441 if ( $this->hasGeneratedStyles ) {
444 return parent::getStyleURLsForDebug( $context );
449 foreach ( $this->
getStyleFiles( $context ) as $mediaType => $list ) {
450 $urls[$mediaType] = [];
451 foreach ( $list as $file ) {
452 $urls[$mediaType][] = OutputPage::transformResourcePath(
496 private function getFileContents( $localPath, $type ) {
497 if ( !is_file( $localPath ) ) {
498 throw new RuntimeException(
"$type file not found or not a file: \"$localPath\"" );
500 return $this->
stripBom( file_get_contents( $localPath ) );
507 if ( !$this->skipFunction ) {
510 $localPath = $this->
getLocalPath( $this->skipFunction );
511 return $this->getFileContents( $localPath,
'skip function' );
536 private function getFileHashes(
Context $context ) {
540 foreach ( $filePaths as $filePath ) {
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();
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();
568 foreach ( $this->templates as $filePath ) {
572 if ( $this->skipFunction ) {
584 $files = array_unique( $files );
600 $summary = parent::getDefinitionSummary( $context );
617 $options[$member] = $this->{$member};
621 $packageSummaries = [];
631 foreach (
$packageFiles[
'files'] as $fileName => $fileInfo ) {
632 $packageSummaries[$fileName] =
633 $fileInfo[
'definitionSummary'] ?? $fileInfo[
'content'] ??
null;
637 $scriptFiles = $this->getScriptFiles( $context );
638 $scriptSummaries = [];
639 foreach ( $scriptFiles as $fileName => $fileInfo ) {
640 $scriptSummaries[$fileName] =
641 $fileInfo[
'definitionSummary'] ?? $fileInfo[
'content'] ??
null;
645 'options' => $options,
646 'packageFiles' => $packageSummaries,
647 'scripts' => $scriptSummaries,
648 'fileHashes' => $this->getFileHashes( $context ),
654 $summary[] = [
'lessVars' => $lessVars ];
664 if ( $this->vueComponentParser ===
null ) {
676 return $path->getPath();
688 if (
$path->getLocalBasePath() !==
null ) {
689 return $path->getLocalPath();
694 return "{$this->localBasePath}/$path";
703 if (
$path->getRemoteBasePath() !==
null ) {
704 return $path->getRemotePath();
709 if ( $this->remoteBasePath ===
'/' ) {
712 return "{$this->remoteBasePath}/$path";
724 return preg_match(
'/\.less$/i',
$path ) ?
'less' :
'css';
734 if ( preg_match(
'/\.json$/i',
$path ) ) {
737 if ( preg_match(
'/\.vue$/i',
$path ) ) {
750 private static function collateStyleFilesByMedia( array $list ) {
752 foreach ( $list as $key => $value ) {
753 if ( is_int( $key ) ) {
755 $collatedFiles[
'all'][] = $value;
756 } elseif ( is_array( $value ) ) {
758 $optionValue = $value[
'media'] ??
'all';
759 $collatedFiles[$optionValue][] = $key;
762 return $collatedFiles;
775 if ( isset( $list[$key] ) && is_array( $list[$key] ) ) {
792 private function getScriptFiles(
Context $context ): array {
796 'scripts' => $this->scripts,
797 'languageScripts' => $this->getLanguageScripts( $context->getLanguage() ),
798 'skinScripts' => self::
tryForKey( $this->skinScripts, $context->getSkin(),
'default' ),
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;
812 return $expandedFiles;
822 private function getLanguageScripts(
string $lang ): array {
823 $scripts = self::tryForKey( $this->languageScripts, $lang );
830 if ( $this->languageScripts ) {
832 ->getLanguageFallback()
833 ->getAll( $lang, LanguageFallback::MESSAGES );
834 foreach ( $fallbacks as $lang ) {
835 $scripts = self::tryForKey( $this->languageScripts, $lang );
846 $moduleName = $this->getName();
847 foreach ( $moduleSkinStyles as $skinName => $overrides ) {
850 if ( isset( $this->skinStyles[$skinName] ) ) {
856 if ( isset( $overrides[$moduleName] ) ) {
857 $paths = (array)$overrides[$moduleName];
859 } elseif ( isset( $overrides[
'+' . $moduleName] ) ) {
860 $paths = (array)$overrides[
'+' . $moduleName];
861 $styleFiles = isset( $this->skinStyles[
'default'] ) ?
862 (array)$this->skinStyles[
'default'] :
870 [ $localBasePath, $remoteBasePath ] = self::extractBasePaths( $overrides );
872 foreach ( $paths as
$path ) {
873 $styleFiles[] =
new FilePath(
$path, $localBasePath, $remoteBasePath );
876 $this->skinStyles[$skinName] = $styleFiles;
888 return array_merge_recursive(
889 self::collateStyleFilesByMedia( $this->styles ),
890 self::collateStyleFilesByMedia(
891 self::tryForKey( $this->skinStyles, $context->
getSkin(),
'default' )
904 return self::collateStyleFilesByMedia(
905 self::tryForKey( $this->skinStyles, $skinName )
916 $skinFactory = MediaWikiServices::getInstance()->getSkinFactory();
919 $internalSkinNames = array_keys( $skinFactory->getInstalledSkins() );
920 $internalSkinNames[] =
'default';
922 foreach ( $internalSkinNames as $internalSkinName ) {
923 $styleFiles = array_merge_recursive(
925 $this->getSkinStyleFiles( $internalSkinName )
938 $collatedStyleFiles = array_merge_recursive(
939 self::collateStyleFilesByMedia( $this->styles ),
940 $this->getAllSkinStyleFiles()
945 foreach ( $collatedStyleFiles as $styleFiles ) {
946 foreach ( $styleFiles as $styleFile ) {
947 $result[] = $this->getLocalPath( $styleFile );
966 foreach ( $styles as $media => $files ) {
967 $uniqueFiles = array_unique( $files, SORT_REGULAR );
969 foreach ( $uniqueFiles as $file ) {
970 $styleFiles[] = $this->readStyleFile( $file, $context );
972 $styles[$media] = implode(
"\n", $styleFiles );
988 $localPath = $this->getLocalPath(
$path );
989 $style = $this->getFileContents( $localPath,
'style' );
990 $styleLang = $this->getStyleSheetLang( $localPath );
992 return $this->processStyle( $style, $styleLang,
$path, $context );
1012 $localPath = $this->getLocalPath(
$path );
1013 $remotePath = $this->getRemotePath(
$path );
1015 if ( $styleLang ===
'less' ) {
1016 $style = $this->compileLessString( $style, $localPath, $context );
1017 $this->hasGeneratedStyles =
true;
1020 if ( $this->getFlip( $context ) ) {
1021 $style = CSSJanus::transform(
1026 $this->hasGeneratedStyles =
true;
1029 $localDir = dirname( $localPath );
1030 $remoteDir = dirname( $remotePath );
1032 $localFileRefs = CSSMin::getLocalFileReferences( $style, $localDir );
1033 foreach ( $localFileRefs as $file ) {
1034 if ( is_file( $file ) ) {
1035 $this->localFileRefs[] = $file;
1037 $this->missingLocalFileRefs[] = $file;
1042 return CSSMin::remap( $style, $localDir, $remoteDir,
true );
1051 return $context->
getDirection() ===
'rtl' && !$this->noflip;
1061 $canBeStylesOnly = !(
1064 || $this->debugScripts
1066 || $this->languageScripts
1067 || $this->skinScripts
1068 || $this->dependencies
1070 || $this->skipFunction
1071 || $this->packageFiles
1073 return $canBeStylesOnly ? self::LOAD_STYLES : self::LOAD_GENERAL;
1091 $cache = MediaWikiServices::getInstance()->getObjectCacheFactory()
1095 $skinName = $context->
getSkin();
1096 $skinImportPaths = ExtensionRegistry::getInstance()->getAttribute(
'SkinLessImportPaths' );
1098 if ( isset( $skinImportPaths[ $skinName ] ) ) {
1099 $importDirs[] = $skinImportPaths[ $skinName ];
1102 $vars = $this->getLessVars( $context );
1108 'importDirs' => $importDirs,
1111 'codexDevDir' => $this->getConfig()->get( MainConfigNames::CodexDevelopmentDir )
1113 $key = $cache->makeGlobalKey(
1114 'resourceloader-less',
1116 hash(
'md4', $style ),
1117 hash(
'md4', serialize( $compilerParams ) )
1122 $data = $cache->get( $key );
1125 $data[
'hash'] !== FileContentsHasher::getFileContentsHash( $data[
'files'] )
1127 $compiler = $context->
getResourceLoader()->getLessCompiler( $vars, $importDirs );
1129 $css = $compiler->parse( $style, $stylePath )->getCss();
1133 $files = $compiler->getParsedFiles();
1136 'files' => Module::getRelativePaths( $files ),
1137 'hash' => FileContentsHasher::getFileContentsHash( $files )
1139 $cache->set( $key, $data, $cache::TTL_DAY );
1142 foreach ( Module::expandRelativePaths( $data[
'files'] ) as
$path ) {
1143 $this->localFileRefs[] =
$path;
1146 return $data[
'css'];
1157 foreach ( $this->templates as $alias => $templatePath ) {
1159 if ( is_int( $alias ) ) {
1160 $alias = $this->getPath( $templatePath );
1162 $localPath = $this->getLocalPath( $templatePath );
1163 $content = $this->getFileContents( $localPath,
'template' );
1165 $templates[$alias] = $this->stripBom( $content );
1189 private function expandPackageFiles(
Context $context ) {
1191 if ( isset( $this->expandedPackageFiles[$hash] ) ) {
1192 return $this->expandedPackageFiles[$hash];
1194 if ( $this->packageFiles ===
null ) {
1197 $expandedFiles = [];
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 );
1214 $expandedFiles[$fileName] = $expanded;
1217 if ( $expandedFiles && $mainFile ===
null ) {
1219 foreach ( $expandedFiles as
$path => $file ) {
1220 if ( $file[
'type'] ===
'script' || $file[
'type'] ===
'script-vue' ) {
1228 'main' => $mainFile,
1229 'files' => $expandedFiles
1232 $this->expandedPackageFiles[$hash] = $result;
1265 private function expandFileInfo( Context $context, $fileInfo, $debugKey ) {
1266 if ( is_string( $fileInfo ) ) {
1269 'name' => $fileInfo,
1270 'type' => self::getPackageFileType( $fileInfo ),
1271 'filePath' =>
new FilePath( $fileInfo, $this->localBasePath, $this->remoteBasePath )
1273 } elseif ( $fileInfo instanceof FilePath ) {
1275 'name' => $fileInfo->getPath(),
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 );
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 );
1289 $fileName = $this->getPath( $fileInfo[
'name'] );
1292 $type = $fileInfo[
'type'] ?? self::getPackageFileType( $fileName );
1294 'name' => $fileName,
1297 if ( !empty( $fileInfo[
'main'] ) ) {
1298 $expanded[
'main'] =
true;
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'] ) ) {
1311 $expanded[
'callbackParam'] = $fileInfo[
'callbackParam'] ??
null;
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 );
1318 if ( isset( $fileInfo[
'versionCallback'] ) ) {
1319 if ( !is_callable( $fileInfo[
'versionCallback'] ) ) {
1320 throw new LogicException(
"Invalid 'versionCallback' for "
1321 .
"module '{$this->getName()}', file '{$fileName}'."
1327 $callbackResult = ( $fileInfo[
'versionCallback'] )(
1330 $expanded[
'callbackParam']
1332 if ( $callbackResult instanceof FilePath ) {
1333 $callbackResult->initBasePaths( $this->localBasePath, $this->remoteBasePath );
1334 $expanded[
'versionFilePath'] = $callbackResult;
1336 $expanded[
'definitionSummary'] = $callbackResult;
1339 $expanded[
'callback'] = $fileInfo[
'callback'];
1342 $callbackResult = ( $fileInfo[
'callback'] )(
1345 $expanded[
'callbackParam']
1347 if ( $callbackResult instanceof FilePath ) {
1348 $callbackResult->initBasePaths( $this->localBasePath, $this->remoteBasePath );
1349 $expanded[
'filePath'] = $callbackResult;
1351 $expanded[
'content'] = $callbackResult;
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 );
1361 $expandedConfig = [];
1362 foreach ( $fileInfo[
'config'] as $configKey => $var ) {
1363 $expandedConfig[ is_numeric( $configKey ) ? $var : $configKey ] = $this->getConfig()->get( $var );
1365 $expanded[
'content'] = $expandedConfig;
1366 } elseif ( !empty( $fileInfo[
'main'] ) ) {
1368 $expanded[
'filePath'] = $this->makeFilePath( $fileName );
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 );
1375 if ( !isset( $expanded[
'filePath'] ) ) {
1376 $expanded[
'virtualFilePath'] = $this->makeFilePath( $fileName );
1387 private function makeFilePath(
$path ): FilePath {
1388 if (
$path instanceof FilePath ) {
1390 } elseif ( is_string(
$path ) ) {
1391 return new FilePath(
$path, $this->localBasePath, $this->remoteBasePath );
1393 throw new InvalidArgumentException(
'$path must be either FilePath or string' );
1404 if ( $this->packageFiles ===
null ) {
1408 if ( isset( $this->fullyExpandedPackageFiles[ $hash ] ) ) {
1409 return $this->fullyExpandedPackageFiles[ $hash ];
1411 $expandedPackageFiles = $this->expandPackageFiles( $context ) ?? [];
1413 foreach ( $expandedPackageFiles[
'files'] as &$fileInfo ) {
1414 $this->readFileInfo( $context, $fileInfo );
1417 $this->fullyExpandedPackageFiles[ $hash ] = $expandedPackageFiles;
1418 return $expandedPackageFiles;
1429 private function readFileInfo(
Context $context, array &$fileInfo ) {
1434 if ( !isset( $fileInfo[
'content'] ) && isset( $fileInfo[
'callback'] ) ) {
1435 $callbackResult = ( $fileInfo[
'callback'] )(
1438 $fileInfo[
'callbackParam']
1440 if ( $callbackResult instanceof
FilePath ) {
1442 $fileInfo[
'filePath'] = $callbackResult;
1444 $fileInfo[
'content'] = $callbackResult;
1446 unset( $fileInfo[
'callback'] );
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 );
1459 $fileInfo[
'content'] = $content;
1461 if ( $fileInfo[
'type'] ===
'script-vue' ) {
1463 $parsedComponent = $this->getVueComponentParser()->parse(
1465 $fileInfo[
'content'],
1466 [
'minifyTemplate' => !$context->
getDebug() ]
1468 }
catch ( TimeoutException $e ) {
1470 }
catch ( Exception $e ) {
1471 $msg =
"Error parsing file '{$fileInfo['name']}' in module '{$this->getName()}': " .
1473 $this->getLogger()->error( $msg );
1474 throw new RuntimeException( $msg );
1476 $encodedTemplate = json_encode( $parsedComponent[
'template'] );
1481 $encodedTemplate = preg_replace(
'/(?<!\\\\)\\\\n/',
" \\n\\\n", $encodedTemplate );
1483 $encodedTemplate = strtr( $encodedTemplate, [
"\\t" =>
"\t" ] );
1485 $fileInfo[
'content'] = [
1486 'script' => $parsedComponent[
'script'] .
1487 ";\nmodule.exports.template = $encodedTemplate;",
1488 'style' => $parsedComponent[
'style'] ??
'',
1489 'styleLang' => $parsedComponent[
'styleLang'] ??
'css'
1491 $fileInfo[
'type'] =
'script+style';
1493 if ( !isset( $fileInfo[
'content'] ) ) {
1495 $msg =
"Unable to resolve contents for file {$fileInfo['name']}";
1496 $this->getLogger()->error( $msg );
1497 throw new RuntimeException( $msg );
1501 unset( $fileInfo[
'definitionSummary'] );
1503 unset( $fileInfo[
'callbackParam'] );
1517 if ( str_starts_with( $input,
"\xef\xbb\xbf" ) ) {
1518 return substr( $input, 3 );
if(!defined('MW_SETUP_CALLBACK'))
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'.