89 private $expandedPackageFiles = [];
95 private $fullyExpandedPackageFiles = [];
162 $hasTemplates =
false;
169 foreach ( $options as $member => $option ) {
176 $this->{$member} = is_array( $option ) ? $option : [ $option ];
179 $hasTemplates =
true;
180 $this->{$member} = is_array( $option ) ? $option : [ $option ];
183 case 'languageScripts':
186 if ( !is_array( $option ) ) {
187 throw new InvalidArgumentException(
188 "Invalid collated file path list error. " .
189 "'$option' given, array expected."
192 foreach ( $option as $key => $value ) {
193 if ( !is_string( $key ) ) {
194 throw new InvalidArgumentException(
195 "Invalid collated file path list key error. " .
196 "'$key' given, string expected."
199 $this->{$member}[$key] = is_array( $value ) ? $value : [ $value ];
203 $this->deprecated = $option;
210 $option = array_values( array_unique( (array)$option ) );
213 $this->{$member} = $option;
218 $this->{$member} = (string)$option;
223 case 'skipStructureTest':
224 $this->{$member} = (bool)$option;
228 if ( isset( $options[
'scripts'] ) && isset( $options[
'packageFiles'] ) ) {
229 throw new InvalidArgumentException(
"A module may not set both 'scripts' and 'packageFiles'" );
231 if ( isset( $options[
'packageFiles'] ) && isset( $options[
'skinScripts'] ) ) {
232 throw new InvalidArgumentException(
"Options 'skinScripts' and 'packageFiles' cannot be used together." );
234 if ( $hasTemplates ) {
235 $this->dependencies[] =
'mediawiki.template';
237 foreach ( $this->templates as $alias => $templatePath ) {
238 if ( is_int( $alias ) ) {
239 $alias = $this->
getPath( $templatePath );
241 $suffix = explode(
'.', $alias );
242 $suffix = end( $suffix );
243 $compilerModule =
'mediawiki.template.' . $suffix;
244 if ( $suffix !==
'html' && !in_array( $compilerModule, $this->dependencies ) ) {
245 $this->dependencies[] = $compilerModule;
273 if ( isset( $options[
'remoteExtPath'] ) ) {
276 $remoteBasePath = $extensionAssetsPath .
'/' . $options[
'remoteExtPath'];
279 if ( isset( $options[
'remoteSkinPath'] ) ) {
285 if ( array_key_exists(
'localBasePath', $options ) ) {
289 if ( array_key_exists(
'remoteBasePath', $options ) ) {
318 static function ( array $file ): array {
319 if ( $file[
'type'] ===
'script+style' ) {
320 $file[
'content'] = $file[
'content'][
'script'];
321 $file[
'type'] =
'script';
330 $files = $this->getScriptFiles( $context );
333 fn ( $file ) => $this->readFileInfo( $context, $file ),
336 return [
'plainScripts' => $files ];
349 && !$this->packageFiles
351 && !$this->hasGeneratedScripts();
356 return $this->skipStructureTest || parent::shouldSkipStructureTest();
364 private function hasGeneratedScripts() {
366 [ $this->scripts, $this->languageScripts, $this->skinScripts, $this->debugScripts ]
370 if ( is_array( $script ) ) {
371 if ( isset( $script[
'callback'] ) || isset( $script[
'versionCallback'] ) ) {
395 if ( $file[
'type'] ===
'script+style' ) {
397 $file[
'content'][
'style'],
398 $file[
'content'][
'styleLang'],
419 if ( $this->hasGeneratedStyles ) {
422 return parent::getStyleURLsForDebug( $context );
427 foreach ( $this->
getStyleFiles( $context ) as $mediaType => $list ) {
428 $urls[$mediaType] = [];
429 foreach ( $list as $file ) {
430 $urls[$mediaType][] = OutputPage::transformResourcePath(
445 return array_merge( $this->messages, $this->lessMessages );
455 private function pluckFromMessageBlob( $blob, array $allowed ): array {
456 $data = $blob ? json_decode( $blob, true ) : [];
459 return array_intersect_key( $data, array_fill_keys( $allowed,
true ) );
466 $blob = parent::getMessageBlob( $context );
477 $reducedMessages = $this->getMessages();
478 foreach ( $this->lessMessages as $messageKey ) {
479 $i = array_search( $messageKey, $reducedMessages );
480 if ( $i !==
false ) {
481 unset( $reducedMessages[$i] );
484 return json_encode( (
object)$this->pluckFromMessageBlob( $blob, $reducedMessages ) );
508 private static function wrapAndEscapeMessage( $msg ) {
509 return str_replace(
"'",
"\'", CSSMin::serializeStringValue( $msg ) );
521 $vars = parent::getLessVars( $context );
523 if ( $this->lessMessages ) {
524 $blob = parent::getMessageBlob( $context );
525 $messages = $this->pluckFromMessageBlob( $blob, $this->lessMessages );
532 foreach ( $this->lessMessages as $msgKey ) {
533 $vars[
'msg-' . $msgKey] = self::wrapAndEscapeMessage( $messages[$msgKey] ??
"â§¼{$msgKey}â§½" );
556 return $this->dependencies;
566 private function getFileContents( $localPath, $type ) {
567 if ( !is_file( $localPath ) ) {
568 throw new RuntimeException(
"$type file not found or not a file: \"$localPath\"" );
570 return $this->stripBom( file_get_contents( $localPath ) );
577 if ( !$this->skipFunction ) {
580 $localPath = $this->getLocalPath( $this->skipFunction );
581 return $this->getFileContents( $localPath,
'skip function' );
607 private function getFileHashes(
Context $context ) {
610 foreach ( $this->getStyleFiles( $context ) as $filePaths ) {
611 foreach ( $filePaths as $filePath ) {
612 $files[] = $this->getLocalPath( $filePath );
619 $expandedPackageFiles = $this->expandPackageFiles( $context );
620 if ( $expandedPackageFiles ) {
621 foreach ( $expandedPackageFiles[
'files'] as $fileInfo ) {
622 $filePath = $fileInfo[
'filePath'] ?? $fileInfo[
'versionFilePath'] ??
null;
623 if ( $filePath instanceof FilePath ) {
624 $files[] = $filePath->getLocalPath();
630 $scriptFileInfos = $this->getScriptFiles( $context );
631 foreach ( $scriptFileInfos as $fileInfo ) {
632 $filePath = $fileInfo[
'filePath'] ?? $fileInfo[
'versionFilePath'] ??
null;
633 if ( $filePath instanceof FilePath ) {
634 $files[] = $filePath->getLocalPath();
638 foreach ( $this->templates as $filePath ) {
639 $files[] = $this->getLocalPath( $filePath );
642 if ( $this->skipFunction ) {
643 $files[] = $this->getLocalPath( $this->skipFunction );
654 $files = array_unique( $files );
660 return FileContentsHasher::getFileContentsHash( $files );
670 $summary = parent::getDefinitionSummary( $context );
687 $options[$member] = $this->{$member};
690 $packageFiles = $this->expandPackageFiles( $context );
691 $packageSummaries = [];
692 if ( $packageFiles ) {
701 foreach ( $packageFiles[
'files'] as $fileName => $fileInfo ) {
702 $packageSummaries[$fileName] =
703 $fileInfo[
'definitionSummary'] ?? $fileInfo[
'content'] ??
null;
707 $scriptFiles = $this->getScriptFiles( $context );
708 $scriptSummaries = [];
709 foreach ( $scriptFiles as $fileName => $fileInfo ) {
710 $scriptSummaries[$fileName] =
711 $fileInfo[
'definitionSummary'] ?? $fileInfo[
'content'] ??
null;
715 'options' => $options,
716 'packageFiles' => $packageSummaries,
717 'scripts' => $scriptSummaries,
718 'fileHashes' => $this->getFileHashes( $context ),
719 'messageBlob' => $this->getMessageBlob( $context ),
722 $lessVars = $this->getLessVars( $context );
724 $summary[] = [
'lessVars' => $lessVars ];
736 return $path->getPath();
748 if (
$path->getLocalBasePath() !==
null ) {
749 return $path->getLocalPath();
754 return "{$this->localBasePath}/$path";
763 if (
$path->getRemoteBasePath() !==
null ) {
764 return $path->getRemotePath();
769 if ( $this->remoteBasePath ===
'/' ) {
772 return "{$this->remoteBasePath}/$path";
784 return preg_match(
'/\.less$/i',
$path ) ?
'less' :
'css';
794 if ( preg_match(
'/\.json$/i',
$path ) ) {
797 if ( preg_match(
'/\.vue$/i',
$path ) ) {
810 private static function collateStyleFilesByMedia( array $list ) {
812 foreach ( $list as $key => $value ) {
813 if ( is_int( $key ) ) {
815 $collatedFiles[
'all'][] = $value;
816 } elseif ( is_array( $value ) ) {
818 $optionValue = $value[
'media'] ??
'all';
819 $collatedFiles[$optionValue][] = $key;
822 return $collatedFiles;
835 if ( isset( $list[$key] ) && is_array( $list[$key] ) ) {
852 private function getScriptFiles(
Context $context ): array {
856 'scripts' => $this->scripts,
857 'languageScripts' => $this->getLanguageScripts( $context->getLanguage() ),
858 'skinScripts' => self::tryForKey( $this->skinScripts, $context->getSkin(),
'default' ),
861 $filesByCategory[
'debugScripts'] = $this->debugScripts;
865 foreach ( $filesByCategory as $category => $files ) {
866 foreach ( $files as $key => $fileInfo ) {
867 $expandedFileInfo = $this->expandFileInfo( $context, $fileInfo,
"$category\[$key]" );
868 $expandedFiles[$expandedFileInfo[
'name']] = $expandedFileInfo;
872 return $expandedFiles;
882 private function getLanguageScripts(
string $lang ): array {
883 $scripts = self::tryForKey( $this->languageScripts, $lang );
890 if ( $this->languageScripts ) {
891 $fallbacks = MediaWikiServices::getInstance()
892 ->getLanguageFallback()
893 ->getAll( $lang, LanguageFallbackMode::MESSAGES );
894 foreach ( $fallbacks as $lang ) {
895 $scripts = self::tryForKey( $this->languageScripts, $lang );
906 $moduleName = $this->getName();
907 foreach ( $moduleSkinStyles as $skinName => $overrides ) {
910 if ( isset( $this->skinStyles[$skinName] ) ) {
916 if ( isset( $overrides[$moduleName] ) ) {
917 $paths = (array)$overrides[$moduleName];
919 } elseif ( isset( $overrides[
'+' . $moduleName] ) ) {
920 $paths = (array)$overrides[
'+' . $moduleName];
921 $styleFiles = isset( $this->skinStyles[
'default'] ) ?
922 (array)$this->skinStyles[
'default'] :
930 [ $localBasePath, $remoteBasePath ] = self::extractBasePaths( $overrides );
932 foreach ( $paths as
$path ) {
933 $styleFiles[] =
new FilePath(
$path, $localBasePath, $remoteBasePath );
936 $this->skinStyles[$skinName] = $styleFiles;
948 return array_merge_recursive(
949 self::collateStyleFilesByMedia( $this->styles ),
950 self::collateStyleFilesByMedia(
951 self::tryForKey( $this->skinStyles, $context->
getSkin(),
'default' )
964 return self::collateStyleFilesByMedia(
965 self::tryForKey( $this->skinStyles, $skinName )
976 $skinFactory = MediaWikiServices::getInstance()->getSkinFactory();
979 $internalSkinNames = array_keys( $skinFactory->getInstalledSkins() );
980 $internalSkinNames[] =
'default';
982 foreach ( $internalSkinNames as $internalSkinName ) {
983 $styleFiles = array_merge_recursive(
985 $this->getSkinStyleFiles( $internalSkinName )
998 $collatedStyleFiles = array_merge_recursive(
999 self::collateStyleFilesByMedia( $this->styles ),
1000 $this->getAllSkinStyleFiles()
1005 foreach ( $collatedStyleFiles as $styleFiles ) {
1006 foreach ( $styleFiles as $styleFile ) {
1007 $result[] = $this->getLocalPath( $styleFile );
1026 foreach ( $styles as $media => $files ) {
1027 $uniqueFiles = array_unique( $files, SORT_REGULAR );
1029 foreach ( $uniqueFiles as $file ) {
1030 $styleFiles[] = $this->readStyleFile( $file, $context );
1032 $styles[$media] = implode(
"\n", $styleFiles );
1048 $localPath = $this->getLocalPath(
$path );
1049 $style = $this->getFileContents( $localPath,
'style' );
1050 $styleLang = $this->getStyleSheetLang( $localPath );
1052 return $this->processStyle( $style, $styleLang,
$path, $context );
1072 $localPath = $this->getLocalPath(
$path );
1073 $remotePath = $this->getRemotePath(
$path );
1075 if ( $styleLang ===
'less' ) {
1076 $style = $this->compileLessString( $style, $localPath, $context );
1077 $this->hasGeneratedStyles =
true;
1080 if ( $this->getFlip( $context ) ) {
1081 $style = CSSJanus::transform(
1086 $this->hasGeneratedStyles =
true;
1089 $localDir = dirname( $localPath );
1090 $remoteDir = dirname( $remotePath );
1092 $localFileRefs = CSSMin::getLocalFileReferences( $style, $localDir );
1093 foreach ( $localFileRefs as $file ) {
1094 if ( is_file( $file ) ) {
1095 $this->localFileRefs[] = $file;
1097 $this->missingLocalFileRefs[] = $file;
1102 return CSSMin::remap( $style, $localDir, $remoteDir,
true );
1111 return $context->
getDirection() ===
'rtl' && !$this->noflip;
1121 $canBeStylesOnly = !(
1124 || $this->debugScripts
1126 || $this->languageScripts
1127 || $this->skinScripts
1128 || $this->dependencies
1130 || $this->skipFunction
1131 || $this->packageFiles
1133 return $canBeStylesOnly ? self::LOAD_STYLES : self::LOAD_GENERAL;
1151 $cache = MediaWikiServices::getInstance()->getObjectCacheFactory()
1155 $skinName = $context->
getSkin();
1156 $skinImportPaths = ExtensionRegistry::getInstance()->getAttribute(
'SkinLessImportPaths' );
1158 if ( isset( $skinImportPaths[ $skinName ] ) ) {
1159 $importDirs[] = $skinImportPaths[ $skinName ];
1162 $vars = $this->getLessVars( $context );
1168 'importDirs' => $importDirs,
1171 'codexDevDir' => $this->getConfig()->get( MainConfigNames::CodexDevelopmentDir )
1173 $key = $cache->makeGlobalKey(
1174 'resourceloader-less',
1176 hash(
'md4', $style ),
1177 hash(
'md4', serialize( $compilerParams ) )
1182 $data = $cache->get( $key );
1185 $data[
'hash'] !== FileContentsHasher::getFileContentsHash( $data[
'files'] )
1187 $compiler = $context->
getResourceLoader()->getLessCompiler( $vars, $importDirs );
1189 $css = $compiler->parse( $style, $stylePath )->getCss();
1193 $files = $compiler->getParsedFiles();
1196 'files' => Module::getRelativePaths( $files ),
1197 'hash' => FileContentsHasher::getFileContentsHash( $files )
1199 $cache->set( $key, $data, $cache::TTL_DAY );
1202 foreach ( Module::expandRelativePaths( $data[
'files'] ) as
$path ) {
1203 $this->localFileRefs[] =
$path;
1206 return $data[
'css'];
1217 foreach ( $this->templates as $alias => $templatePath ) {
1219 if ( is_int( $alias ) ) {
1220 $alias = $this->getPath( $templatePath );
1222 $localPath = $this->getLocalPath( $templatePath );
1223 $content = $this->getFileContents( $localPath,
'template' );
1225 $templates[$alias] = $this->stripBom( $content );
1249 private function expandPackageFiles(
Context $context ) {
1251 if ( isset( $this->expandedPackageFiles[$hash] ) ) {
1252 return $this->expandedPackageFiles[$hash];
1254 if ( $this->packageFiles ===
null ) {
1257 $expandedFiles = [];
1260 foreach ( $this->packageFiles as $key => $fileInfo ) {
1261 $expanded = $this->expandFileInfo( $context, $fileInfo,
"packageFiles[$key]" );
1262 $fileName = $expanded[
'name'];
1263 if ( !empty( $expanded[
'main'] ) ) {
1264 unset( $expanded[
'main'] );
1265 $type = $expanded[
'type'];
1266 $mainFile = $fileName;
1267 if ( $type !==
'script' && $type !==
'script-vue' ) {
1268 $msg =
"Main file in package must be of type 'script', module " .
1269 "'{$this->getName()}', main file '{$mainFile}' is '{$type}'.";
1270 $this->getLogger()->error( $msg );
1271 throw new LogicException( $msg );
1274 $expandedFiles[$fileName] = $expanded;
1277 if ( $expandedFiles && $mainFile ===
null ) {
1279 foreach ( $expandedFiles as
$path => $file ) {
1280 if ( $file[
'type'] ===
'script' || $file[
'type'] ===
'script-vue' ) {
1288 'main' => $mainFile,
1289 'files' => $expandedFiles
1292 $this->expandedPackageFiles[$hash] = $result;
1325 private function expandFileInfo( Context $context, $fileInfo, $debugKey ) {
1326 if ( is_string( $fileInfo ) ) {
1329 'name' => $fileInfo,
1330 'type' => self::getPackageFileType( $fileInfo ),
1331 'filePath' =>
new FilePath( $fileInfo, $this->localBasePath, $this->remoteBasePath )
1333 } elseif ( $fileInfo instanceof FilePath ) {
1335 'name' => $fileInfo->getPath(),
1338 } elseif ( !is_array( $fileInfo ) ) {
1339 $msg =
"Invalid type in $debugKey for module '{$this->getName()}', " .
1340 "must be array, string or FilePath";
1341 $this->getLogger()->error( $msg );
1342 throw new LogicException( $msg );
1344 if ( !isset( $fileInfo[
'name'] ) ) {
1345 $msg =
"Missing 'name' key in $debugKey for module '{$this->getName()}'";
1346 $this->getLogger()->error( $msg );
1347 throw new LogicException( $msg );
1349 $fileName = $this->getPath( $fileInfo[
'name'] );
1352 $type = $fileInfo[
'type'] ?? self::getPackageFileType( $fileName );
1354 'name' => $fileName,
1357 if ( !empty( $fileInfo[
'main'] ) ) {
1358 $expanded[
'main'] =
true;
1365 if ( isset( $fileInfo[
'content'] ) ) {
1366 $expanded[
'content'] = $fileInfo[
'content'];
1367 } elseif ( isset( $fileInfo[
'file'] ) ) {
1368 $expanded[
'filePath'] = $this->makeFilePath( $fileInfo[
'file'] );
1369 } elseif ( isset( $fileInfo[
'callback'] ) ) {
1371 $expanded[
'callbackParam'] = $fileInfo[
'callbackParam'] ??
null;
1373 if ( !is_callable( $fileInfo[
'callback'] ) ) {
1374 $msg =
"Invalid 'callback' for module '{$this->getName()}', file '{$fileName}'.";
1375 $this->getLogger()->error( $msg );
1376 throw new LogicException( $msg );
1378 if ( isset( $fileInfo[
'versionCallback'] ) ) {
1379 if ( !is_callable( $fileInfo[
'versionCallback'] ) ) {
1380 throw new LogicException(
"Invalid 'versionCallback' for "
1381 .
"module '{$this->getName()}', file '{$fileName}'."
1387 $callbackResult = ( $fileInfo[
'versionCallback'] )(
1390 $expanded[
'callbackParam']
1392 if ( $callbackResult instanceof FilePath ) {
1393 $callbackResult->initBasePaths( $this->localBasePath, $this->remoteBasePath );
1394 $expanded[
'versionFilePath'] = $callbackResult;
1396 $expanded[
'definitionSummary'] = $callbackResult;
1399 $expanded[
'callback'] = $fileInfo[
'callback'];
1402 $callbackResult = ( $fileInfo[
'callback'] )(
1405 $expanded[
'callbackParam']
1407 if ( $callbackResult instanceof FilePath ) {
1408 $callbackResult->initBasePaths( $this->localBasePath, $this->remoteBasePath );
1409 $expanded[
'filePath'] = $callbackResult;
1411 $expanded[
'content'] = $callbackResult;
1414 } elseif ( isset( $fileInfo[
'config'] ) ) {
1415 if ( $type !==
'data' ) {
1416 $msg =
"Key 'config' only valid for data files. "
1417 .
" Module '{$this->getName()}', file '{$fileName}' is '{$type}'.";
1418 $this->getLogger()->error( $msg );
1419 throw new LogicException( $msg );
1421 $expandedConfig = [];
1422 foreach ( $fileInfo[
'config'] as $configKey => $var ) {
1423 $expandedConfig[ is_numeric( $configKey ) ? $var : $configKey ] = $this->
getConfig()->get( $var );
1425 $expanded[
'content'] = $expandedConfig;
1426 } elseif ( !empty( $fileInfo[
'main'] ) ) {
1428 $expanded[
'filePath'] = $this->makeFilePath( $fileName );
1430 $msg =
"Incomplete definition for module '{$this->getName()}', file '{$fileName}'. "
1431 .
"One of 'file', 'content', 'callback', or 'config' must be set.";
1432 $this->getLogger()->error( $msg );
1433 throw new LogicException( $msg );
1435 if ( !isset( $expanded[
'filePath'] ) ) {
1436 $expanded[
'virtualFilePath'] = $this->makeFilePath( $fileName );
1447 private function makeFilePath(
$path ): FilePath {
1448 if (
$path instanceof FilePath ) {
1450 } elseif ( is_string(
$path ) ) {
1451 return new FilePath(
$path, $this->localBasePath, $this->remoteBasePath );
1453 throw new InvalidArgumentException(
'$path must be either FilePath or string' );
1464 if ( $this->packageFiles ===
null ) {
1468 if ( isset( $this->fullyExpandedPackageFiles[ $hash ] ) ) {
1469 return $this->fullyExpandedPackageFiles[ $hash ];
1471 $expandedPackageFiles = $this->expandPackageFiles( $context ) ?? [];
1474 $expandedPackageFiles[
'files'] = array_map(
function ( array $fileInfo ) use ( $context ): array {
1475 return $this->readFileInfo( $context, $fileInfo );
1476 }, $expandedPackageFiles[
'files'] );
1478 $this->fullyExpandedPackageFiles[ $hash ] = $expandedPackageFiles;
1479 return $expandedPackageFiles;
1491 private function readFileInfo(
Context $context, array $fileInfo ): array {
1496 if ( !isset( $fileInfo[
'content'] ) && isset( $fileInfo[
'callback'] ) ) {
1497 $callbackResult = ( $fileInfo[
'callback'] )(
1500 $fileInfo[
'callbackParam']
1502 if ( $callbackResult instanceof
FilePath ) {
1504 $fileInfo[
'filePath'] = $callbackResult;
1506 $fileInfo[
'content'] = $callbackResult;
1508 unset( $fileInfo[
'callback'] );
1515 if ( !isset( $fileInfo[
'content'] ) && isset( $fileInfo[
'filePath'] ) ) {
1516 $localPath = $this->getLocalPath( $fileInfo[
'filePath'] );
1517 $content = $this->getFileContents( $localPath,
'package' );
1518 if ( $fileInfo[
'type'] ===
'data' ) {
1519 $content = json_decode( $content,
false, 512, JSON_THROW_ON_ERROR );
1521 $fileInfo[
'content'] = $content;
1523 if ( $fileInfo[
'type'] ===
'script-vue' ) {
1525 $fileInfo[
'content' ] = $this->parseVueContent( $context, $fileInfo[
'content' ] );
1526 }
catch ( InvalidArgumentException $e ) {
1527 $msg =
"Error parsing file '{$fileInfo['name']}' in module '{$this->getName()}': " .
1528 "{$e->getMessage()}";
1529 $this->getLogger()->error( $msg );
1530 throw new RuntimeException( $msg );
1532 $fileInfo[
'type'] =
'script+style';
1534 if ( !isset( $fileInfo[
'content'] ) ) {
1536 $msg =
"Unable to resolve contents for file {$fileInfo['name']}";
1537 $this->getLogger()->error( $msg );
1538 throw new RuntimeException( $msg );
1542 unset( $fileInfo[
'definitionSummary'] );
1544 unset( $fileInfo[
'callbackParam'] );
1560 if ( str_starts_with( $input,
"\xef\xbb\xbf" ) ) {
1561 return substr( $input, 3 );