19 protected static $globalSettings = [
20 MainConfigNames::ActionFilteredLogs,
21 MainConfigNames::Actions,
22 MainConfigNames::AddGroups,
23 MainConfigNames::APIFormatModules,
24 MainConfigNames::APIListModules,
25 MainConfigNames::APIMetaModules,
26 MainConfigNames::APIModules,
27 MainConfigNames::APIPropModules,
28 MainConfigNames::AuthManagerAutoConfig,
29 MainConfigNames::AvailableRights,
30 MainConfigNames::CentralIdLookupProviders,
31 MainConfigNames::ChangeCredentialsBlacklist,
32 MainConfigNames::ConditionalUserOptions,
33 MainConfigNames::ConfigRegistry,
34 MainConfigNames::ContentHandlers,
35 MainConfigNames::DefaultUserOptions,
36 MainConfigNames::ExtensionEntryPointListFiles,
37 MainConfigNames::ExtensionFunctions,
38 MainConfigNames::FeedClasses,
39 MainConfigNames::FileExtensions,
40 MainConfigNames::FilterLogTypes,
41 MainConfigNames::GrantPermissionGroups,
42 MainConfigNames::GrantPermissions,
43 MainConfigNames::GrantRiskGroups,
44 MainConfigNames::GroupPermissions,
45 MainConfigNames::GroupsAddToSelf,
46 MainConfigNames::GroupsRemoveFromSelf,
47 MainConfigNames::HiddenPrefs,
48 MainConfigNames::ImplicitGroups,
49 MainConfigNames::JobClasses,
50 MainConfigNames::LogActions,
51 MainConfigNames::LogActionsHandlers,
52 MainConfigNames::LogHeaders,
53 MainConfigNames::LogNames,
54 MainConfigNames::LogRestrictions,
55 MainConfigNames::LogTypes,
56 MainConfigNames::MediaHandlers,
57 MainConfigNames::OutputPipelineStages,
58 MainConfigNames::PasswordPolicy,
59 MainConfigNames::PrivilegedGroups,
60 MainConfigNames::RateLimits,
61 MainConfigNames::RawHtmlMessages,
62 MainConfigNames::ReauthenticateTime,
63 MainConfigNames::RecentChangesFlags,
64 MainConfigNames::RemoveCredentialsBlacklist,
65 MainConfigNames::RemoveGroups,
66 MainConfigNames::ResourceLoaderSources,
67 MainConfigNames::RevokePermissions,
68 MainConfigNames::SessionProviders,
69 MainConfigNames::SpecialPages,
70 MainConfigNames::UserRegistrationProviders,
78 protected const CORE_ATTRIBS = [
85 'LateJSConfigVarNames',
86 'TempUserSerialProviders',
87 'TempUserSerialMappings',
88 'DatabaseVirtualDomains',
89 'UserOptionsStoreProviders',
99 protected const MERGE_STRATEGIES = [
100 'wgAuthManagerAutoConfig' =>
'array_plus_2d',
101 'wgCapitalLinkOverrides' =>
'array_plus',
102 'wgExtraGenderNamespaces' =>
'array_plus',
103 'wgGrantPermissions' =>
'array_plus_2d',
104 'wgGroupPermissions' =>
'array_plus_2d',
105 'wgHooks' =>
'array_merge_recursive',
106 'wgNamespaceContentModels' =>
'array_plus',
107 'wgNamespaceProtection' =>
'array_plus',
108 'wgNamespacesWithSubpages' =>
'array_plus',
109 'wgPasswordPolicy' =>
'array_merge_recursive',
110 'wgRateLimits' =>
'array_plus_2d',
111 'wgRevokePermissions' =>
'array_plus_2d',
119 protected const CREDIT_ATTRIBS = [
137 protected const NOT_ATTRIBS = [
141 'load_composer_autoloader',
146 'AutoloadNamespaces',
147 'ExtensionMessagesFiles',
148 'TranslationAliasesDirs',
149 'ForeignResourcesDir',
151 'MessagePosterModule',
155 'ResourceFileModulePaths',
156 'ResourceModuleSkinStyles',
158 'ServiceWiringFiles',
168 protected $globals = [
169 'wgExtensionMessagesFiles' => [],
170 'wgRestAPIAdditionalRouteFiles' => [],
171 'wgMessagesDirs' => [],
172 'TranslationAliasesDirs' => [],
180 protected $defines = [];
189 protected $callbacks = [];
194 protected $credits = [];
203 protected $autoload = [
215 protected $autoloadDev = [
227 protected $attributes = [];
235 protected $extAttributes = [];
245 $json = file_get_contents(
$path );
246 $info = json_decode( $json,
true );
249 throw new RuntimeException(
"Failed to load JSON data from $path" );
261 $dir = dirname(
$path );
272 if ( isset( $info[
'ServiceWiringFiles'] ) ) {
274 'wgServiceWiringFiles',
276 $info[
'ServiceWiringFiles']
280 if ( isset( $info[
'callback'] ) ) {
281 $this->callbacks[$name] = $info[
'callback'];
284 $this->extractAutoload( $info, $dir );
288 if ( $version >= 2 ) {
296 if ( isset( $info[
'ParsoidModules'] ) ) {
297 foreach ( $info[
'ParsoidModules'] as &$module ) {
298 if ( is_string( $module ) ) {
299 $className = $module;
301 'class' => $className,
304 $module[
'name'] = $name;
310 if ( $version >= 2 ) {
314 foreach ( $info as $key => $val ) {
316 if ( in_array( $key, self::$globalSettings ) ) {
321 if ( $key[0] ===
'@' ) {
325 if ( $version >= 2 ) {
327 if ( in_array( $key, self::CORE_ATTRIBS ) ) {
332 if ( !in_array( $key, self::NOT_ATTRIBS )
333 && !in_array( $key, self::CREDIT_ATTRIBS )
347 if ( isset( $info[
'attributes'] ) ) {
348 foreach ( $info[
'attributes'] as $extName => $value ) {
356 foreach ( $this->globals as $key => $val ) {
357 if ( isset( self::MERGE_STRATEGIES[$key] ) ) {
358 $this->globals[$key][ExtensionRegistry::MERGE_STRATEGY] = self::MERGE_STRATEGIES[$key];
363 foreach ( $this->extAttributes as $extName => $value ) {
365 if ( isset( $this->credits[$extName] ) ) {
366 foreach ( $value as $attrName => $attrValue ) {
369 $extName . $attrName,
374 unset( $this->extAttributes[$extName] );
380 'globals' => $this->globals,
381 'defines' => $this->defines,
382 'callbacks' => $this->callbacks,
383 'credits' => $this->credits,
384 'attributes' => $this->attributes,
385 'autoloaderPaths' => $autoload[
'files'],
386 'autoloaderClasses' => $autoload[
'classes'],
387 'autoloaderNS' => $autoload[
'namespaces'],
393 if ( !$includeDev || !isset( $info[
'dev-requires'] ) ) {
394 return $info[
'requires'] ?? [];
397 if ( !isset( $info[
'requires'] ) ) {
398 return $info[
'dev-requires'] ?? [];
408 $pick =
static function ( $a, $b ) {
411 } elseif ( $b ===
null ) {
418 $req = $info[
'requires'];
419 $dev = $info[
'dev-requires'];
420 if ( isset( $req[
'MediaWiki'] ) || isset( $dev[
'MediaWiki'] ) ) {
421 $merged[
'MediaWiki'] = $pick(
422 $req[
'MediaWiki'] ??
null,
423 $dev[
'MediaWiki'] ??
null
427 $platform = array_merge(
428 array_keys( $req[
'platform'] ?? [] ),
429 array_keys( $dev[
'platform'] ?? [] )
432 foreach ( $platform as $pkey ) {
433 if ( $pkey ===
'php' ) {
435 $req[
'platform'][
'php'] ??
null,
436 $dev[
'platform'][
'php'] ??
null
441 $value = $dev[
'platform'][$pkey] ?? $req[
'platform'][$pkey];
443 $merged[
'platform'][$pkey] = $value;
447 foreach ( [
'extensions',
'skins' ] as $thing ) {
448 $things = array_merge(
449 array_keys( $req[$thing] ?? [] ),
450 array_keys( $dev[$thing] ?? [] )
452 foreach ( $things as $name ) {
453 $merged[$thing][$name] = $pick(
454 $req[$thing][$name] ??
null,
455 $dev[$thing][$name] ??
null
473 private function setArrayHookHandler(
475 array $hookHandlersAttr,
479 if ( isset( $callback[
'handler'] ) ) {
480 $handlerName = $callback[
'handler'];
481 $handlerDefinition = $hookHandlersAttr[$handlerName] ??
false;
482 if ( !$handlerDefinition ) {
483 throw new UnexpectedValueException(
484 "Missing handler definition for $name in HookHandlers attribute in $path"
487 $callback[
'handler'] = $handlerDefinition;
488 $callback[
'extensionPath'] =
$path;
489 $this->attributes[
'Hooks'][$name][] = $callback;
491 foreach ( $callback as $callable ) {
492 if ( is_array( $callable ) ) {
493 if ( isset( $callable[
'handler'] ) ) {
494 $this->setArrayHookHandler( $callable, $hookHandlersAttr, $name,
$path );
496 $this->globals[
'wgHooks'][$name][] = $callable;
498 } elseif ( is_string( $callable ) ) {
499 $this->setStringHookHandler( $callable, $hookHandlersAttr, $name,
$path );
515 private function setStringHookHandler(
517 array $hookHandlersAttr,
521 if ( isset( $hookHandlersAttr[$callback] ) ) {
523 'handler' => $hookHandlersAttr[$callback],
524 'extensionPath' =>
$path
526 $this->attributes[
'Hooks'][$name][] = $handler;
528 $this->globals[
'wgHooks'][$name][] = $callback;
541 $extName = $info[
'name'];
542 if ( isset( $info[
'Hooks'] ) ) {
543 $hookHandlersAttr = [];
544 foreach ( $info[
'HookHandlers'] ?? [] as $name => $def ) {
545 $hookHandlersAttr[$name] = [
'name' =>
"$extName-$name" ] + $def;
547 foreach ( $info[
'Hooks'] as $name => $callback ) {
548 if ( is_string( $callback ) ) {
549 $this->setStringHookHandler( $callback, $hookHandlersAttr, $name,
$path );
550 } elseif ( is_array( $callback ) ) {
551 $this->setArrayHookHandler( $callback, $hookHandlersAttr, $name,
$path );
555 if ( isset( $info[
'DeprecatedHooks'] ) ) {
556 $deprecatedHooks = [];
557 foreach ( $info[
'DeprecatedHooks'] as $name => $deprecatedHookInfo ) {
558 $deprecatedHookInfo += [
'component' => $extName ];
559 $deprecatedHooks[$name] = $deprecatedHookInfo;
561 if ( isset( $this->attributes[
'DeprecatedHooks'] ) ) {
562 $this->attributes[
'DeprecatedHooks'] += $deprecatedHooks;
564 $this->attributes[
'DeprecatedHooks'] = $deprecatedHooks;
575 if ( isset( $info[
'namespaces'] ) ) {
576 foreach ( $info[
'namespaces'] as $ns ) {
577 if ( defined( $ns[
'constant'] ) ) {
580 $id = constant( $ns[
'constant'] );
584 $this->defines[ $ns[
'constant'] ] = $id;
586 if ( !( isset( $ns[
'conditional'] ) && $ns[
'conditional'] ) ) {
588 $this->attributes[
'ExtensionNamespaces'][$id] = $ns[
'name'];
590 if ( isset( $ns[
'movable'] ) && !$ns[
'movable'] ) {
591 $this->attributes[
'ImmovableNamespaces'][] = $id;
593 if ( isset( $ns[
'gender'] ) ) {
594 $this->globals[
'wgExtraGenderNamespaces'][$id] = $ns[
'gender'];
596 if ( isset( $ns[
'subpages'] ) && $ns[
'subpages'] ) {
597 $this->globals[
'wgNamespacesWithSubpages'][$id] =
true;
599 if ( isset( $ns[
'content'] ) && $ns[
'content'] ) {
600 $this->globals[
'wgContentNamespaces'][] = $id;
602 if ( isset( $ns[
'defaultcontentmodel'] ) ) {
603 $this->globals[
'wgNamespaceContentModels'][$id] = $ns[
'defaultcontentmodel'];
605 if ( isset( $ns[
'protection'] ) ) {
606 $this->globals[
'wgNamespaceProtection'][$id] = $ns[
'protection'];
608 if ( isset( $ns[
'capitallinkoverride'] ) ) {
609 $this->globals[
'wgCapitalLinkOverrides'][$id] = $ns[
'capitallinkoverride'];
611 if ( isset( $ns[
'includable'] ) && !$ns[
'includable'] ) {
612 $this->globals[
'wgNonincludableNamespaces'][] = $id;
619 $defaultPaths = $info[
'ResourceFileModulePaths'] ??
false;
620 if ( isset( $defaultPaths[
'localBasePath'] ) ) {
621 if ( $defaultPaths[
'localBasePath'] ===
'' ) {
623 $defaultPaths[
'localBasePath'] = $dir;
625 $defaultPaths[
'localBasePath'] =
"$dir/{$defaultPaths['localBasePath']}";
629 foreach ( [
'ResourceModules',
'ResourceModuleSkinStyles',
'OOUIThemePaths' ] as $setting ) {
630 if ( isset( $info[$setting] ) ) {
631 foreach ( $info[$setting] as $name => $data ) {
632 if ( isset( $data[
'localBasePath'] ) ) {
633 if ( $data[
'localBasePath'] ===
'' ) {
635 $data[
'localBasePath'] = $dir;
637 $data[
'localBasePath'] =
"$dir/{$data['localBasePath']}";
640 if ( $defaultPaths ) {
641 $data += $defaultPaths;
643 $this->attributes[$setting][$name] = $data;
648 if ( isset( $info[
'QUnitTestModule'] ) ) {
649 $data = $info[
'QUnitTestModule'];
650 if ( isset( $data[
'localBasePath'] ) ) {
651 if ( $data[
'localBasePath'] ===
'' ) {
653 $data[
'localBasePath'] = $dir;
655 $data[
'localBasePath'] =
"$dir/{$data['localBasePath']}";
658 $this->attributes[
'QUnitTestModules'][
"test.{$info['name']}"] = $data;
661 if ( isset( $info[
'MessagePosterModule'] ) ) {
662 $data = $info[
'MessagePosterModule'];
663 $basePath = $data[
'localBasePath'] ??
'';
664 $baseDir = $basePath ===
'' ? $dir :
"$dir/$basePath";
665 foreach ( $data[
'scripts'] ?? [] as $scripts ) {
666 $this->attributes[
'MessagePosterModule'][
'scripts'][] =
669 foreach ( $data[
'dependencies'] ?? [] as $dependency ) {
670 $this->attributes[
'MessagePosterModule'][
'dependencies'][] = $dependency;
676 if ( isset( $info[
'ExtensionMessagesFiles'] ) ) {
677 foreach ( $info[
'ExtensionMessagesFiles'] as &$file ) {
678 $file =
"$dir/$file";
680 $this->globals[
"wgExtensionMessagesFiles"] += $info[
'ExtensionMessagesFiles'];
685 $var = MainConfigNames::RestAPIAdditionalRouteFiles;
686 if ( isset( $info[
'RestModuleFiles'] ) ) {
687 foreach ( $info[
'RestModuleFiles'] as &$file ) {
688 $this->globals[
"wg$var"][] =
"$dir/$file";
701 if ( isset( $info[
'MessagesDirs'] ) ) {
702 foreach ( $info[
'MessagesDirs'] as $name => $files ) {
703 foreach ( (array)$files as $file ) {
704 $this->globals[
"wgMessagesDirs"][$name][] =
"$dir/$file";
718 foreach ( $info[
'TranslationAliasesDirs'] ?? [] as $name => $files ) {
719 foreach ( (array)$files as $file ) {
720 $this->globals[
'wgTranslationAliasesDirs'][$name][] =
"$dir/$file";
732 if ( isset( $info[
'ValidSkinNames'] ) ) {
733 foreach ( $info[
'ValidSkinNames'] as $skinKey => $data ) {
734 if ( isset( $data[
'args'][0] ) ) {
735 $templateDirectory = $data[
'args'][0][
'templateDirectory'] ??
'templates';
736 $data[
'args'][0][
'templateDirectory'] = $dir .
'/' . $templateDirectory;
738 $this->globals[
'wgValidSkinNames'][$skinKey] = $data;
753 if ( isset( $info[
'RateLimits'] ) ) {
754 $rights = array_keys( $info[
'RateLimits'] );
756 if ( isset( $info[
'AvailableRights'] ) ) {
757 $rights = array_diff( $rights, $info[
'AvailableRights'] );
760 $this->globals[
'wgImplicitRights'] = array_merge(
761 $this->globals[
'wgImplicitRights'] ?? [],
772 if ( isset( $info[
'SkinLessImportPaths'] ) ) {
773 foreach ( $info[
'SkinLessImportPaths'] as $skin => $subpath ) {
774 $this->attributes[
'SkinLessImportPaths'][$skin] =
"$dir/$subpath";
790 foreach ( self::CREDIT_ATTRIBS as $attr ) {
791 if ( isset( $info[$attr] ) ) {
792 $credits[$attr] = $info[$attr];
796 $name = $credits[
'name'];
800 if ( isset( $this->credits[$name] ) ) {
801 $firstPath = $this->credits[$name][
'path'];
802 $secondPath = $credits[
'path'];
803 throw new InvalidArgumentException(
804 "It was attempted to load $name twice, from $firstPath and $secondPath."
808 $this->credits[$name] = $credits;
814 if ( array_key_exists(
'ForeignResourcesDir', $info ) ) {
815 if ( !is_string( $info[
'ForeignResourcesDir'] ) ) {
816 throw new InvalidArgumentException(
"Incorrect ForeignResourcesDir type, must be a string (in $name)" );
818 $this->attributes[
'ForeignResourcesDir'][$name] =
"{$dir}/{$info['ForeignResourcesDir']}";
829 if ( isset( $info[
'config'] ) ) {
830 if ( isset( $info[
'config'][
'_prefix'] ) ) {
831 $prefix = $info[
'config'][
'_prefix'];
832 unset( $info[
'config'][
'_prefix'] );
836 foreach ( $info[
'config'] as $key => $val ) {
837 if ( $key[0] !==
'@' ) {
838 $this->addConfigGlobal(
"$prefix$key", $val, $info[
'name'] );
852 private function applyPath( array $value,
string $dir ): array {
855 foreach ( $value as $k => $v ) {
856 $result[$k] = $dir .
'/' . $v;
869 $prefix = $info[
'config_prefix'] ??
'wg';
870 if ( isset( $info[
'config'] ) ) {
871 foreach ( $info[
'config'] as $key => $data ) {
872 if ( !array_key_exists(
'value', $data ) ) {
873 throw new UnexpectedValueException(
"Missing value for config $key" );
876 $value = $data[
'value'];
877 if ( isset( $data[
'path'] ) && $data[
'path'] ) {
878 if ( is_array( $value ) ) {
879 $value = $this->applyPath( $value, $dir );
881 $value =
"$dir/$value";
884 if ( isset( $data[
'merge_strategy'] ) ) {
885 $value[ExtensionRegistry::MERGE_STRATEGY] = $data[
'merge_strategy'];
887 $this->addConfigGlobal(
"$prefix$key", $value, $info[
'name'] );
888 $data[
'providedby'] = $info[
'name'];
889 if ( isset( $info[
'ConfigRegistry'][0] ) ) {
890 $data[
'configregistry'] = array_keys( $info[
'ConfigRegistry'] )[0];
903 private function addConfigGlobal( $key, $value, $extName ) {
904 if ( array_key_exists( $key, $this->globals ) ) {
905 throw new RuntimeException(
906 "The configuration setting '$key' was already set by MediaWiki core or"
907 .
" another extension, and cannot be set again by $extName." );
909 if ( isset( $value[ExtensionRegistry::MERGE_STRATEGY] ) &&
910 $value[ExtensionRegistry::MERGE_STRATEGY] ===
'array_merge_recursive' ) {
912 "Using the array_merge_recursive merge strategy in extension.json and skin.json" .
913 " was deprecated in MediaWiki 1.42",
917 $this->globals[$key] = $value;
921 foreach ( $paths as
$path ) {
922 $this->globals[$global][] =
"$dir/$path";
936 if ( !is_array( $value ) ) {
937 throw new InvalidArgumentException(
"The value for '$name' should be an array (from $path)" );
939 if ( isset( $array[$name] ) ) {
940 $array[$name] = array_merge_recursive( $array[$name], $value );
942 $array[$name] = $value;
956 if ( !is_array( $value ) ) {
957 throw new InvalidArgumentException(
"The value for '$name' should be an array (from $path)" );
959 if ( isset( $array[$name] ) ) {
960 $array[$name] = array_merge( $array[$name], $value );
962 $array[$name] = $value;
981 $autoload = $this->autoload;
984 $autoload[
'classes'] += $this->autoloadDev[
'classes'];
985 $autoload[
'namespaces'] += $this->autoloadDev[
'namespaces'];
990 if ( !empty( $this->autoloadDev[
'files'] ) ) {
993 $autoload[
'files'] = array_merge(
995 $this->autoloadDev[
'files']
1007 private function extractAutoload( array $info,
string $dir ) {
1008 if ( isset( $info[
'load_composer_autoloader'] ) && $info[
'load_composer_autoloader'] ===
true ) {
1009 $file =
"$dir/vendor/autoload.php";
1010 if ( file_exists( $file ) ) {
1011 $this->autoload[
'files'][] = $file;
1015 if ( isset( $info[
'AutoloadClasses'] ) ) {
1016 $paths = $this->applyPath( $info[
'AutoloadClasses'], $dir );
1017 $this->autoload[
'classes'] += $paths;
1020 if ( isset( $info[
'AutoloadNamespaces'] ) ) {
1021 $paths = $this->applyPath( $info[
'AutoloadNamespaces'], $dir );
1022 $this->autoload[
'namespaces'] += $paths;
1025 if ( isset( $info[
'TestAutoloadClasses'] ) ) {
1026 $paths = $this->applyPath( $info[
'TestAutoloadClasses'], $dir );
1027 $this->autoloadDev[
'classes'] += $paths;
1030 if ( isset( $info[
'TestAutoloadNamespaces'] ) ) {
1031 $paths = $this->applyPath( $info[
'TestAutoloadNamespaces'], $dir );
1032 $this->autoloadDev[
'namespaces'] += $paths;