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',
51 'RemoveCredentialsBlacklist',
53 'ResourceLoaderSources',
61 * Top-level attributes that come from MW core
65 protected static $coreAttributes = [
72 * Mapping of global settings to their specific merge strategies.
74 * @see ExtensionRegistry::exportExtractedData
75 * @see getExtractedInfo
78 protected static $mergeStrategies = [
79 'wgAuthManagerAutoConfig' => 'array_plus_2d',
80 'wgCapitalLinkOverrides' => 'array_plus',
81 'wgExtensionCredits' => 'array_merge_recursive',
82 'wgExtraGenderNamespaces' => 'array_plus',
83 'wgGrantPermissions' => 'array_plus_2d',
84 'wgGroupPermissions' => 'array_plus_2d',
85 'wgHooks' => 'array_merge_recursive',
86 'wgNamespaceContentModels' => 'array_plus',
87 'wgNamespaceProtection' => 'array_plus',
88 'wgNamespacesWithSubpages' => 'array_plus',
89 'wgPasswordPolicy' => 'array_merge_recursive',
90 'wgRateLimits' => 'array_plus_2d',
91 'wgRevokePermissions' => 'array_plus_2d',
95 * Keys that are part of the extension credits
99 protected static $creditsAttributes = [
111 * Things that are not 'attributes', and are not in
112 * $globalSettings or $creditsAttributes.
116 protected static $notAttributes = [
120 'ResourceFileModulePaths',
122 'ResourceModuleSkinStyles',
125 'ExtensionMessagesFiles',
130 'ServiceWiringFiles',
134 'load_composer_autoloader',
138 * Stuff that is going to be set to $GLOBALS
140 * Some keys are pre-set to arrays so we can += to them
144 protected $globals = [
145 'wgExtensionMessagesFiles' => [],
146 'wgMessagesDirs' => [],
150 * Things that should be define()'d
154 protected $defines = [];
157 * Things to be called once registration of these extensions are done
158 * keyed by the name of the extension that it belongs to
162 protected $callbacks = [];
167 protected $credits = [];
172 protected $config = [];
175 * Any thing else in the $info that hasn't
176 * already been processed
180 protected $attributes = [];
183 * Extension attributes, keyed by name =>
188 protected $extAttributes = [];
191 * @param string $path
193 * @param int $version manifest_version for info
195 public function extractInfo( $path, array $info, $version ) {
196 $dir = dirname( $path );
197 $this->extractHooks( $info );
198 $this->extractExtensionMessagesFiles( $dir, $info );
199 $this->extractMessagesDirs( $dir, $info );
200 $this->extractNamespaces( $info );
201 $this->extractResourceLoaderModules( $dir, $info );
202 if ( isset( $info['ServiceWiringFiles'] ) ) {
203 $this->extractPathBasedGlobal(
204 'wgServiceWiringFiles',
206 $info['ServiceWiringFiles']
209 if ( isset( $info['ParserTestFiles'] ) ) {
210 $this->extractPathBasedGlobal(
213 $info['ParserTestFiles']
216 $name = $this->extractCredits( $path, $info );
217 if ( isset( $info['callback'] ) ) {
218 $this->callbacks
[$name] = $info['callback'];
221 // config should be after all core globals are extracted,
222 // so duplicate setting detection will work fully
223 if ( $version === 2 ) {
224 $this->extractConfig2( $info, $dir );
227 $this->extractConfig1( $info );
230 if ( $version === 2 ) {
231 $this->extractAttributes( $path, $info );
234 foreach ( $info as $key => $val ) {
235 // If it's a global setting,
236 if ( in_array( $key, self
::$globalSettings ) ) {
237 $this->storeToArray( $path, "wg$key", $val, $this->globals
);
240 // Ignore anything that starts with a @
241 if ( $key[0] === '@' ) {
245 if ( $version === 2 ) {
246 // Only whitelisted attributes are set
247 if ( in_array( $key, self
::$coreAttributes ) ) {
248 $this->storeToArray( $path, $key, $val, $this->attributes
);
252 if ( !in_array( $key, self
::$notAttributes )
253 && !in_array( $key, self
::$creditsAttributes )
255 // If it's not blacklisted, it's an attribute
256 $this->storeToArray( $path, $key, $val, $this->attributes
);
264 * @param string $path
267 protected function extractAttributes( $path, array $info ) {
268 if ( isset( $info['attributes'] ) ) {
269 foreach ( $info['attributes'] as $extName => $value ) {
270 $this->storeToArray( $path, $extName, $value, $this->extAttributes
);
275 public function getExtractedInfo() {
276 // Make sure the merge strategies are set
277 foreach ( $this->globals
as $key => $val ) {
278 if ( isset( self
::$mergeStrategies[$key] ) ) {
279 $this->globals
[$key][ExtensionRegistry
::MERGE_STRATEGY
] = self
::$mergeStrategies[$key];
283 // Merge $this->extAttributes into $this->attributes depending on what is loaded
284 foreach ( $this->extAttributes
as $extName => $value ) {
285 // Only set the attribute if $extName is loaded (and hence present in credits)
286 if ( isset( $this->credits
[$extName] ) ) {
287 foreach ( $value as $attrName => $attrValue ) {
289 '', // Don't provide a path since it's impossible to generate an error here
290 $extName . $attrName,
295 unset( $this->extAttributes
[$extName] );
300 'globals' => $this->globals
,
301 'config' => $this->config
,
302 'defines' => $this->defines
,
303 'callbacks' => $this->callbacks
,
304 'credits' => $this->credits
,
305 'attributes' => $this->attributes
,
309 public function getRequirements( array $info, $includeDev ) {
311 if ( !$includeDev ||
!isset( $info['dev-requires'] ) ) {
312 return $info['requires'] ??
[];
315 if ( !isset( $info['requires'] ) ) {
316 return $info['dev-requires'] ??
[];
319 // OK, we actually have to merge everything
322 // Helper that combines version requirements by
323 // picking the non-null if one is, or combines
324 // the two. Note that it is not possible for
325 // both inputs to be null.
326 $pick = function ( $a, $b ) {
329 } elseif ( $b === null ) {
336 $req = $info['requires'];
337 $dev = $info['dev-requires'];
338 if ( isset( $req['MediaWiki'] ) ||
isset( $dev['MediaWiki'] ) ) {
339 $merged['MediaWiki'] = $pick(
340 $req['MediaWiki'] ??
null,
341 $dev['MediaWiki'] ??
null
345 $platform = array_merge(
346 array_keys( $req['platform'] ??
[] ),
347 array_keys( $dev['platform'] ??
[] )
350 foreach ( $platform as $pkey ) {
351 if ( $pkey === 'php' ) {
353 $req['platform']['php'] ??
null,
354 $dev['platform']['php'] ??
null
357 // Prefer dev value, but these should be constant
358 // anyways (ext-* and ability-*)
359 $value = $dev['platform'][$pkey] ??
$req['platform'][$pkey];
361 $merged['platform'][$pkey] = $value;
365 foreach ( [ 'extensions', 'skins' ] as $thing ) {
366 $things = array_merge(
367 array_keys( $req[$thing] ??
[] ),
368 array_keys( $dev[$thing] ??
[] )
370 foreach ( $things as $name ) {
371 $merged[$thing][$name] = $pick(
372 $req[$thing][$name] ??
null,
373 $dev[$thing][$name] ??
null
381 protected function extractHooks( array $info ) {
382 if ( isset( $info['Hooks'] ) ) {
383 foreach ( $info['Hooks'] as $name => $value ) {
384 if ( is_array( $value ) ) {
385 foreach ( $value as $callback ) {
386 $this->globals
['wgHooks'][$name][] = $callback;
389 $this->globals
['wgHooks'][$name][] = $value;
396 * Register namespaces with the appropriate global settings
400 protected function extractNamespaces( array $info ) {
401 if ( isset( $info['namespaces'] ) ) {
402 foreach ( $info['namespaces'] as $ns ) {
403 if ( defined( $ns['constant'] ) ) {
404 // If the namespace constant is already defined, use it.
405 // This allows namespace IDs to be overwritten locally.
406 $id = constant( $ns['constant'] );
409 $this->defines
[ $ns['constant'] ] = $id;
412 if ( !( isset( $ns['conditional'] ) && $ns['conditional'] ) ) {
413 // If it is not conditional, register it
414 $this->attributes
['ExtensionNamespaces'][$id] = $ns['name'];
416 if ( isset( $ns['gender'] ) ) {
417 $this->globals
['wgExtraGenderNamespaces'][$id] = $ns['gender'];
419 if ( isset( $ns['subpages'] ) && $ns['subpages'] ) {
420 $this->globals
['wgNamespacesWithSubpages'][$id] = true;
422 if ( isset( $ns['content'] ) && $ns['content'] ) {
423 $this->globals
['wgContentNamespaces'][] = $id;
425 if ( isset( $ns['defaultcontentmodel'] ) ) {
426 $this->globals
['wgNamespaceContentModels'][$id] = $ns['defaultcontentmodel'];
428 if ( isset( $ns['protection'] ) ) {
429 $this->globals
['wgNamespaceProtection'][$id] = $ns['protection'];
431 if ( isset( $ns['capitallinkoverride'] ) ) {
432 $this->globals
['wgCapitalLinkOverrides'][$id] = $ns['capitallinkoverride'];
438 protected function extractResourceLoaderModules( $dir, array $info ) {
439 $defaultPaths = $info['ResourceFileModulePaths'] ??
false;
440 if ( isset( $defaultPaths['localBasePath'] ) ) {
441 if ( $defaultPaths['localBasePath'] === '' ) {
442 // Avoid double slashes (e.g. /extensions/Example//path)
443 $defaultPaths['localBasePath'] = $dir;
445 $defaultPaths['localBasePath'] = "$dir/{$defaultPaths['localBasePath']}";
449 foreach ( [ 'ResourceModules', 'ResourceModuleSkinStyles', 'OOUIThemePaths' ] as $setting ) {
450 if ( isset( $info[$setting] ) ) {
451 foreach ( $info[$setting] as $name => $data ) {
452 if ( isset( $data['localBasePath'] ) ) {
453 if ( $data['localBasePath'] === '' ) {
454 // Avoid double slashes (e.g. /extensions/Example//path)
455 $data['localBasePath'] = $dir;
457 $data['localBasePath'] = "$dir/{$data['localBasePath']}";
460 if ( $defaultPaths ) {
461 $data +
= $defaultPaths;
463 if ( $setting === 'OOUIThemePaths' ) {
464 $this->attributes
[$setting][$name] = $data;
466 $this->globals
["wg$setting"][$name] = $data;
472 if ( isset( $info['QUnitTestModule'] ) ) {
473 $data = $info['QUnitTestModule'];
474 if ( isset( $data['localBasePath'] ) ) {
475 if ( $data['localBasePath'] === '' ) {
476 // Avoid double slashes (e.g. /extensions/Example//path)
477 $data['localBasePath'] = $dir;
479 $data['localBasePath'] = "$dir/{$data['localBasePath']}";
482 $this->attributes
['QUnitTestModules']["test.{$info['name']}"] = $data;
486 protected function extractExtensionMessagesFiles( $dir, array $info ) {
487 if ( isset( $info['ExtensionMessagesFiles'] ) ) {
488 foreach ( $info['ExtensionMessagesFiles'] as &$file ) {
489 $file = "$dir/$file";
491 $this->globals
["wgExtensionMessagesFiles"] +
= $info['ExtensionMessagesFiles'];
496 * Set message-related settings, which need to be expanded to use
502 protected function extractMessagesDirs( $dir, array $info ) {
503 if ( isset( $info['MessagesDirs'] ) ) {
504 foreach ( $info['MessagesDirs'] as $name => $files ) {
505 foreach ( (array)$files as $file ) {
506 $this->globals
["wgMessagesDirs"][$name][] = "$dir/$file";
513 * @param string $path
515 * @return string Name of thing
518 protected function extractCredits( $path, array $info ) {
521 'type' => $info['type'] ??
'other',
523 foreach ( self
::$creditsAttributes as $attr ) {
524 if ( isset( $info[$attr] ) ) {
525 $credits[$attr] = $info[$attr];
529 $name = $credits['name'];
531 // If someone is loading the same thing twice, throw
532 // a nice error (T121493)
533 if ( isset( $this->credits
[$name] ) ) {
534 $firstPath = $this->credits
[$name]['path'];
535 $secondPath = $credits['path'];
536 throw new Exception( "It was attempted to load $name twice, from $firstPath and $secondPath." );
539 $this->credits
[$name] = $credits;
540 $this->globals
['wgExtensionCredits'][$credits['type']][] = $credits;
546 * Set configuration settings for manifest_version == 1
547 * @todo In the future, this should be done via Config interfaces
551 protected function extractConfig1( array $info ) {
552 if ( isset( $info['config'] ) ) {
553 if ( isset( $info['config']['_prefix'] ) ) {
554 $prefix = $info['config']['_prefix'];
555 unset( $info['config']['_prefix'] );
559 foreach ( $info['config'] as $key => $val ) {
560 if ( $key[0] !== '@' ) {
561 $this->addConfigGlobal( "$prefix$key", $val, $info['name'] );
568 * Set configuration settings for manifest_version == 2
569 * @todo In the future, this should be done via Config interfaces
574 protected function extractConfig2( array $info, $dir ) {
575 $prefix = $info['config_prefix'] ??
'wg';
576 if ( isset( $info['config'] ) ) {
577 foreach ( $info['config'] as $key => $data ) {
578 $value = $data['value'];
579 if ( isset( $data['merge_strategy'] ) ) {
580 $value[ExtensionRegistry
::MERGE_STRATEGY
] = $data['merge_strategy'];
582 if ( isset( $data['path'] ) && $data['path'] ) {
583 $value = "$dir/$value";
585 $this->addConfigGlobal( "$prefix$key", $value, $info['name'] );
586 $data['providedby'] = $info['name'];
587 if ( isset( $info['ConfigRegistry'][0] ) ) {
588 $data['configregistry'] = array_keys( $info['ConfigRegistry'] )[0];
590 $this->config
[$key] = $data;
596 * Helper function to set a value to a specific global, if it isn't set already.
598 * @param string $key The config key with the prefix and anything
599 * @param mixed $value The value of the config
600 * @param string $extName Name of the extension
602 private function addConfigGlobal( $key, $value, $extName ) {
603 if ( array_key_exists( $key, $this->globals
) ) {
604 throw new RuntimeException(
605 "The configuration setting '$key' was already set by MediaWiki core or"
606 . " another extension, and cannot be set again by $extName." );
608 $this->globals
[$key] = $value;
611 protected function extractPathBasedGlobal( $global, $dir, $paths ) {
612 foreach ( $paths as $path ) {
613 $this->globals
[$global][] = "$dir/$path";
618 * @param string $path
619 * @param string $name
620 * @param array $value
621 * @param array &$array
622 * @throws InvalidArgumentException
624 protected function storeToArray( $path, $name, $value, &$array ) {
625 if ( !is_array( $value ) ) {
626 throw new InvalidArgumentException( "The value for '$name' should be an array (from $path)" );
628 if ( isset( $array[$name] ) ) {
629 $array[$name] = array_merge_recursive( $array[$name], $value );
631 $array[$name] = $value;
635 public function getExtraAutoloaderPaths( $dir, array $info ) {
637 if ( isset( $info['load_composer_autoloader'] ) && $info['load_composer_autoloader'] === true ) {
638 $paths[] = "$dir/vendor/autoload.php";