3 class ExtensionProcessor
implements Processor
{
6 * Keys that should be set to $GLOBALS
10 protected static $globalSettings = [
19 'AuthManagerAutoConfig',
21 'CentralIdLookupProviders',
22 'ChangeCredentialsBlacklist',
26 'ExtensionEntryPointListFiles',
31 'GrantPermissionGroups',
35 'GroupsRemoveFromSelf',
50 'RemoveCredentialsBlacklist',
52 'ResourceLoaderSources',
60 * Top-level attributes that come from MW core
64 protected static $coreAttributes = [
70 * Mapping of global settings to their specific merge strategies.
72 * @see ExtensionRegistry::exportExtractedData
73 * @see getExtractedInfo
76 protected static $mergeStrategies = [
77 'wgAuthManagerAutoConfig' => 'array_plus_2d',
78 'wgCapitalLinkOverrides' => 'array_plus',
79 'wgExtensionCredits' => 'array_merge_recursive',
80 'wgExtraGenderNamespaces' => 'array_plus',
81 'wgGrantPermissions' => 'array_plus_2d',
82 'wgGroupPermissions' => 'array_plus_2d',
83 'wgHooks' => 'array_merge_recursive',
84 'wgNamespaceContentModels' => 'array_plus',
85 'wgNamespaceProtection' => 'array_plus',
86 'wgNamespacesWithSubpages' => 'array_plus',
87 'wgPasswordPolicy' => 'array_merge_recursive',
88 'wgRateLimits' => 'array_plus_2d',
89 'wgRevokePermissions' => 'array_plus_2d',
93 * Keys that are part of the extension credits
97 protected static $creditsAttributes = [
109 * Things that are not 'attributes', but are not in
110 * $globalSettings or $creditsAttributes.
114 protected static $notAttributes = [
118 'ResourceFileModulePaths',
120 'ResourceModuleSkinStyles',
121 'ExtensionMessagesFiles',
126 'ServiceWiringFiles',
130 'load_composer_autoloader',
134 * Stuff that is going to be set to $GLOBALS
136 * Some keys are pre-set to arrays so we can += to them
140 protected $globals = [
141 'wgExtensionMessagesFiles' => [],
142 'wgMessagesDirs' => [],
146 * Things that should be define()'d
150 protected $defines = [];
153 * Things to be called once registration of these extensions are done
154 * keyed by the name of the extension that it belongs to
158 protected $callbacks = [];
163 protected $credits = [];
168 protected $config = [];
171 * Any thing else in the $info that hasn't
172 * already been processed
176 protected $attributes = [];
179 * Extension attributes, keyed by name =>
184 protected $extAttributes = [];
187 * @param string $path
189 * @param int $version manifest_version for info
192 public function extractInfo( $path, array $info, $version ) {
193 $dir = dirname( $path );
194 $this->extractHooks( $info );
195 $this->extractExtensionMessagesFiles( $dir, $info );
196 $this->extractMessagesDirs( $dir, $info );
197 $this->extractNamespaces( $info );
198 $this->extractResourceLoaderModules( $dir, $info );
199 if ( isset( $info['ServiceWiringFiles'] ) ) {
200 $this->extractPathBasedGlobal(
201 'wgServiceWiringFiles',
203 $info['ServiceWiringFiles']
206 if ( isset( $info['ParserTestFiles'] ) ) {
207 $this->extractPathBasedGlobal(
210 $info['ParserTestFiles']
213 $name = $this->extractCredits( $path, $info );
214 if ( isset( $info['callback'] ) ) {
215 $this->callbacks
[$name] = $info['callback'];
218 // config should be after all core globals are extracted,
219 // so duplicate setting detection will work fully
220 if ( $version === 2 ) {
221 $this->extractConfig2( $info, $dir );
224 $this->extractConfig1( $info );
227 if ( $version === 2 ) {
228 $this->extractAttributes( $path, $info );
231 foreach ( $info as $key => $val ) {
232 // If it's a global setting,
233 if ( in_array( $key, self
::$globalSettings ) ) {
234 $this->storeToArray( $path, "wg$key", $val, $this->globals
);
237 // Ignore anything that starts with a @
238 if ( $key[0] === '@' ) {
242 if ( $version === 2 ) {
243 // Only whitelisted attributes are set
244 if ( in_array( $key, self
::$coreAttributes ) ) {
245 $this->storeToArray( $path, $key, $val, $this->attributes
);
249 if ( !in_array( $key, self
::$notAttributes )
250 && !in_array( $key, self
::$creditsAttributes )
252 // If it's not blacklisted, it's an attribute
253 $this->storeToArray( $path, $key, $val, $this->attributes
);
261 * @param string $path
264 protected function extractAttributes( $path, array $info ) {
265 if ( isset( $info['attributes'] ) ) {
266 foreach ( $info['attributes'] as $extName => $value ) {
267 $this->storeToArray( $path, $extName, $value, $this->extAttributes
);
272 public function getExtractedInfo() {
273 // Make sure the merge strategies are set
274 foreach ( $this->globals
as $key => $val ) {
275 if ( isset( self
::$mergeStrategies[$key] ) ) {
276 $this->globals
[$key][ExtensionRegistry
::MERGE_STRATEGY
] = self
::$mergeStrategies[$key];
280 // Merge $this->extAttributes into $this->attributes depending on what is loaded
281 foreach ( $this->extAttributes
as $extName => $value ) {
282 // Only set the attribute if $extName is loaded (and hence present in credits)
283 if ( isset( $this->credits
[$extName] ) ) {
284 foreach ( $value as $attrName => $attrValue ) {
286 '', // Don't provide a path since it's impossible to generate an error here
287 $extName . $attrName,
292 unset( $this->extAttributes
[$extName] );
297 'globals' => $this->globals
,
298 'config' => $this->config
,
299 'defines' => $this->defines
,
300 'callbacks' => $this->callbacks
,
301 'credits' => $this->credits
,
302 'attributes' => $this->attributes
,
306 public function getRequirements( array $info ) {
307 return $info['requires'] ??
[];
310 protected function extractHooks( array $info ) {
311 if ( isset( $info['Hooks'] ) ) {
312 foreach ( $info['Hooks'] as $name => $value ) {
313 if ( is_array( $value ) ) {
314 foreach ( $value as $callback ) {
315 $this->globals
['wgHooks'][$name][] = $callback;
318 $this->globals
['wgHooks'][$name][] = $value;
325 * Register namespaces with the appropriate global settings
329 protected function extractNamespaces( array $info ) {
330 if ( isset( $info['namespaces'] ) ) {
331 foreach ( $info['namespaces'] as $ns ) {
332 if ( defined( $ns['constant'] ) ) {
333 // If the namespace constant is already defined, use it.
334 // This allows namespace IDs to be overwritten locally.
335 $id = constant( $ns['constant'] );
338 $this->defines
[ $ns['constant'] ] = $id;
341 if ( !( isset( $ns['conditional'] ) && $ns['conditional'] ) ) {
342 // If it is not conditional, register it
343 $this->attributes
['ExtensionNamespaces'][$id] = $ns['name'];
345 if ( isset( $ns['gender'] ) ) {
346 $this->globals
['wgExtraGenderNamespaces'][$id] = $ns['gender'];
348 if ( isset( $ns['subpages'] ) && $ns['subpages'] ) {
349 $this->globals
['wgNamespacesWithSubpages'][$id] = true;
351 if ( isset( $ns['content'] ) && $ns['content'] ) {
352 $this->globals
['wgContentNamespaces'][] = $id;
354 if ( isset( $ns['defaultcontentmodel'] ) ) {
355 $this->globals
['wgNamespaceContentModels'][$id] = $ns['defaultcontentmodel'];
357 if ( isset( $ns['protection'] ) ) {
358 $this->globals
['wgNamespaceProtection'][$id] = $ns['protection'];
360 if ( isset( $ns['capitallinkoverride'] ) ) {
361 $this->globals
['wgCapitalLinkOverrides'][$id] = $ns['capitallinkoverride'];
367 protected function extractResourceLoaderModules( $dir, array $info ) {
368 $defaultPaths = $info['ResourceFileModulePaths'] ??
false;
369 if ( isset( $defaultPaths['localBasePath'] ) ) {
370 if ( $defaultPaths['localBasePath'] === '' ) {
371 // Avoid double slashes (e.g. /extensions/Example//path)
372 $defaultPaths['localBasePath'] = $dir;
374 $defaultPaths['localBasePath'] = "$dir/{$defaultPaths['localBasePath']}";
378 foreach ( [ 'ResourceModules', 'ResourceModuleSkinStyles' ] as $setting ) {
379 if ( isset( $info[$setting] ) ) {
380 foreach ( $info[$setting] as $name => $data ) {
381 if ( isset( $data['localBasePath'] ) ) {
382 if ( $data['localBasePath'] === '' ) {
383 // Avoid double slashes (e.g. /extensions/Example//path)
384 $data['localBasePath'] = $dir;
386 $data['localBasePath'] = "$dir/{$data['localBasePath']}";
389 if ( $defaultPaths ) {
390 $data +
= $defaultPaths;
392 $this->globals
["wg$setting"][$name] = $data;
398 protected function extractExtensionMessagesFiles( $dir, array $info ) {
399 if ( isset( $info['ExtensionMessagesFiles'] ) ) {
400 foreach ( $info['ExtensionMessagesFiles'] as &$file ) {
401 $file = "$dir/$file";
403 $this->globals
["wgExtensionMessagesFiles"] +
= $info['ExtensionMessagesFiles'];
408 * Set message-related settings, which need to be expanded to use
414 protected function extractMessagesDirs( $dir, array $info ) {
415 if ( isset( $info['MessagesDirs'] ) ) {
416 foreach ( $info['MessagesDirs'] as $name => $files ) {
417 foreach ( (array)$files as $file ) {
418 $this->globals
["wgMessagesDirs"][$name][] = "$dir/$file";
425 * @param string $path
427 * @return string Name of thing
430 protected function extractCredits( $path, array $info ) {
433 'type' => $info['type'] ??
'other',
435 foreach ( self
::$creditsAttributes as $attr ) {
436 if ( isset( $info[$attr] ) ) {
437 $credits[$attr] = $info[$attr];
441 $name = $credits['name'];
443 // If someone is loading the same thing twice, throw
444 // a nice error (T121493)
445 if ( isset( $this->credits
[$name] ) ) {
446 $firstPath = $this->credits
[$name]['path'];
447 $secondPath = $credits['path'];
448 throw new Exception( "It was attempted to load $name twice, from $firstPath and $secondPath." );
451 $this->credits
[$name] = $credits;
452 $this->globals
['wgExtensionCredits'][$credits['type']][] = $credits;
458 * Set configuration settings for manifest_version == 1
459 * @todo In the future, this should be done via Config interfaces
463 protected function extractConfig1( array $info ) {
464 if ( isset( $info['config'] ) ) {
465 if ( isset( $info['config']['_prefix'] ) ) {
466 $prefix = $info['config']['_prefix'];
467 unset( $info['config']['_prefix'] );
471 foreach ( $info['config'] as $key => $val ) {
472 if ( $key[0] !== '@' ) {
473 $this->addConfigGlobal( "$prefix$key", $val, $info['name'] );
480 * Set configuration settings for manifest_version == 2
481 * @todo In the future, this should be done via Config interfaces
486 protected function extractConfig2( array $info, $dir ) {
487 if ( isset( $info['config_prefix'] ) ) {
488 $prefix = $info['config_prefix'];
492 if ( isset( $info['config'] ) ) {
493 foreach ( $info['config'] as $key => $data ) {
494 $value = $data['value'];
495 if ( isset( $data['merge_strategy'] ) ) {
496 $value[ExtensionRegistry
::MERGE_STRATEGY
] = $data['merge_strategy'];
498 if ( isset( $data['path'] ) && $data['path'] ) {
499 $value = "$dir/$value";
501 $this->addConfigGlobal( "$prefix$key", $value, $info['name'] );
502 $data['providedby'] = $info['name'];
503 if ( isset( $info['ConfigRegistry'][0] ) ) {
504 $data['configregistry'] = array_keys( $info['ConfigRegistry'] )[0];
506 $this->config
[$key] = $data;
512 * Helper function to set a value to a specific global, if it isn't set already.
514 * @param string $key The config key with the prefix and anything
515 * @param mixed $value The value of the config
516 * @param string $extName Name of the extension
518 private function addConfigGlobal( $key, $value, $extName ) {
519 if ( array_key_exists( $key, $this->globals
) ) {
520 throw new RuntimeException(
521 "The configuration setting '$key' was already set by MediaWiki core or"
522 . " another extension, and cannot be set again by $extName." );
524 $this->globals
[$key] = $value;
527 protected function extractPathBasedGlobal( $global, $dir, $paths ) {
528 foreach ( $paths as $path ) {
529 $this->globals
[$global][] = "$dir/$path";
534 * @param string $path
535 * @param string $name
536 * @param array $value
537 * @param array &$array
538 * @throws InvalidArgumentException
540 protected function storeToArray( $path, $name, $value, &$array ) {
541 if ( !is_array( $value ) ) {
542 throw new InvalidArgumentException( "The value for '$name' should be an array (from $path)" );
544 if ( isset( $array[$name] ) ) {
545 $array[$name] = array_merge_recursive( $array[$name], $value );
547 $array[$name] = $value;
551 public function getExtraAutoloaderPaths( $dir, array $info ) {
553 if ( isset( $info['load_composer_autoloader'] ) && $info['load_composer_autoloader'] === true ) {
554 $paths[] = "$dir/vendor/autoload.php";