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',
49 'RemoveCredentialsBlacklist',
51 'ResourceLoaderSources',
59 * Top-level attributes that come from MW core
63 protected static $coreAttributes = [
69 * Mapping of global settings to their specific merge strategies.
71 * @see ExtensionRegistry::exportExtractedData
72 * @see getExtractedInfo
75 protected static $mergeStrategies = [
76 'wgAuthManagerAutoConfig' => 'array_plus_2d',
77 'wgCapitalLinkOverrides' => 'array_plus',
78 'wgExtensionCredits' => 'array_merge_recursive',
79 'wgExtraGenderNamespaces' => 'array_plus',
80 'wgGrantPermissions' => 'array_plus_2d',
81 'wgGroupPermissions' => 'array_plus_2d',
82 'wgHooks' => 'array_merge_recursive',
83 'wgNamespaceContentModels' => 'array_plus',
84 'wgNamespaceProtection' => 'array_plus',
85 'wgNamespacesWithSubpages' => 'array_plus',
86 'wgPasswordPolicy' => 'array_merge_recursive',
87 'wgRateLimits' => 'array_plus_2d',
88 'wgRevokePermissions' => 'array_plus_2d',
92 * Keys that are part of the extension credits
96 protected static $creditsAttributes = [
108 * Things that are not 'attributes', but are not in
109 * $globalSettings or $creditsAttributes.
113 protected static $notAttributes = [
117 'ResourceFileModulePaths',
119 'ResourceModuleSkinStyles',
120 'ExtensionMessagesFiles',
125 'ServiceWiringFiles',
129 'load_composer_autoloader',
133 * Stuff that is going to be set to $GLOBALS
135 * Some keys are pre-set to arrays so we can += to them
139 protected $globals = [
140 'wgExtensionMessagesFiles' => [],
141 'wgMessagesDirs' => [],
145 * Things that should be define()'d
149 protected $defines = [];
152 * Things to be called once registration of these extensions are done
153 * keyed by the name of the extension that it belongs to
157 protected $callbacks = [];
162 protected $credits = [];
167 protected $config = [];
170 * Any thing else in the $info that hasn't
171 * already been processed
175 protected $attributes = [];
178 * Extension attributes, keyed by name =>
183 protected $extAttributes = [];
186 * @param string $path
188 * @param int $version manifest_version for info
191 public function extractInfo( $path, array $info, $version ) {
192 $dir = dirname( $path );
193 $this->extractHooks( $info );
194 $this->extractExtensionMessagesFiles( $dir, $info );
195 $this->extractMessagesDirs( $dir, $info );
196 $this->extractNamespaces( $info );
197 $this->extractResourceLoaderModules( $dir, $info );
198 if ( isset( $info['ServiceWiringFiles'] ) ) {
199 $this->extractPathBasedGlobal(
200 'wgServiceWiringFiles',
202 $info['ServiceWiringFiles']
205 if ( isset( $info['ParserTestFiles'] ) ) {
206 $this->extractPathBasedGlobal(
209 $info['ParserTestFiles']
212 $name = $this->extractCredits( $path, $info );
213 if ( isset( $info['callback'] ) ) {
214 $this->callbacks
[$name] = $info['callback'];
217 // config should be after all core globals are extracted,
218 // so duplicate setting detection will work fully
219 if ( $version === 2 ) {
220 $this->extractConfig2( $info, $dir );
223 $this->extractConfig1( $info );
226 if ( $version === 2 ) {
227 $this->extractAttributes( $path, $info );
230 foreach ( $info as $key => $val ) {
231 // If it's a global setting,
232 if ( in_array( $key, self
::$globalSettings ) ) {
233 $this->storeToArray( $path, "wg$key", $val, $this->globals
);
236 // Ignore anything that starts with a @
237 if ( $key[0] === '@' ) {
241 if ( $version === 2 ) {
242 // Only whitelisted attributes are set
243 if ( in_array( $key, self
::$coreAttributes ) ) {
244 $this->storeToArray( $path, $key, $val, $this->attributes
);
248 if ( !in_array( $key, self
::$notAttributes )
249 && !in_array( $key, self
::$creditsAttributes )
251 // If it's not blacklisted, it's an attribute
252 $this->storeToArray( $path, $key, $val, $this->attributes
);
260 * @param string $path
263 protected function extractAttributes( $path, array $info ) {
264 if ( isset( $info['attributes'] ) ) {
265 foreach ( $info['attributes'] as $extName => $value ) {
266 $this->storeToArray( $path, $extName, $value, $this->extAttributes
);
271 public function getExtractedInfo() {
272 // Make sure the merge strategies are set
273 foreach ( $this->globals
as $key => $val ) {
274 if ( isset( self
::$mergeStrategies[$key] ) ) {
275 $this->globals
[$key][ExtensionRegistry
::MERGE_STRATEGY
] = self
::$mergeStrategies[$key];
279 // Merge $this->extAttributes into $this->attributes depending on what is loaded
280 foreach ( $this->extAttributes
as $extName => $value ) {
281 // Only set the attribute if $extName is loaded (and hence present in credits)
282 if ( isset( $this->credits
[$extName] ) ) {
283 foreach ( $value as $attrName => $attrValue ) {
285 '', // Don't provide a path since it's impossible to generate an error here
286 $extName . $attrName,
291 unset( $this->extAttributes
[$extName] );
296 'globals' => $this->globals
,
297 'config' => $this->config
,
298 'defines' => $this->defines
,
299 'callbacks' => $this->callbacks
,
300 'credits' => $this->credits
,
301 'attributes' => $this->attributes
,
305 public function getRequirements( array $info ) {
306 return $info['requires'] ??
[];
309 protected function extractHooks( array $info ) {
310 if ( isset( $info['Hooks'] ) ) {
311 foreach ( $info['Hooks'] as $name => $value ) {
312 if ( is_array( $value ) ) {
313 foreach ( $value as $callback ) {
314 $this->globals
['wgHooks'][$name][] = $callback;
317 $this->globals
['wgHooks'][$name][] = $value;
324 * Register namespaces with the appropriate global settings
328 protected function extractNamespaces( array $info ) {
329 if ( isset( $info['namespaces'] ) ) {
330 foreach ( $info['namespaces'] as $ns ) {
331 if ( defined( $ns['constant'] ) ) {
332 // If the namespace constant is already defined, use it.
333 // This allows namespace IDs to be overwritten locally.
334 $id = constant( $ns['constant'] );
337 $this->defines
[ $ns['constant'] ] = $id;
340 if ( !( isset( $ns['conditional'] ) && $ns['conditional'] ) ) {
341 // If it is not conditional, register it
342 $this->attributes
['ExtensionNamespaces'][$id] = $ns['name'];
344 if ( isset( $ns['gender'] ) ) {
345 $this->globals
['wgExtraGenderNamespaces'][$id] = $ns['gender'];
347 if ( isset( $ns['subpages'] ) && $ns['subpages'] ) {
348 $this->globals
['wgNamespacesWithSubpages'][$id] = true;
350 if ( isset( $ns['content'] ) && $ns['content'] ) {
351 $this->globals
['wgContentNamespaces'][] = $id;
353 if ( isset( $ns['defaultcontentmodel'] ) ) {
354 $this->globals
['wgNamespaceContentModels'][$id] = $ns['defaultcontentmodel'];
356 if ( isset( $ns['protection'] ) ) {
357 $this->globals
['wgNamespaceProtection'][$id] = $ns['protection'];
359 if ( isset( $ns['capitallinkoverride'] ) ) {
360 $this->globals
['wgCapitalLinkOverrides'][$id] = $ns['capitallinkoverride'];
366 protected function extractResourceLoaderModules( $dir, array $info ) {
367 $defaultPaths = $info['ResourceFileModulePaths'] ??
false;
368 if ( isset( $defaultPaths['localBasePath'] ) ) {
369 if ( $defaultPaths['localBasePath'] === '' ) {
370 // Avoid double slashes (e.g. /extensions/Example//path)
371 $defaultPaths['localBasePath'] = $dir;
373 $defaultPaths['localBasePath'] = "$dir/{$defaultPaths['localBasePath']}";
377 foreach ( [ 'ResourceModules', 'ResourceModuleSkinStyles' ] as $setting ) {
378 if ( isset( $info[$setting] ) ) {
379 foreach ( $info[$setting] as $name => $data ) {
380 if ( isset( $data['localBasePath'] ) ) {
381 if ( $data['localBasePath'] === '' ) {
382 // Avoid double slashes (e.g. /extensions/Example//path)
383 $data['localBasePath'] = $dir;
385 $data['localBasePath'] = "$dir/{$data['localBasePath']}";
388 if ( $defaultPaths ) {
389 $data +
= $defaultPaths;
391 $this->globals
["wg$setting"][$name] = $data;
397 protected function extractExtensionMessagesFiles( $dir, array $info ) {
398 if ( isset( $info['ExtensionMessagesFiles'] ) ) {
399 foreach ( $info['ExtensionMessagesFiles'] as &$file ) {
400 $file = "$dir/$file";
402 $this->globals
["wgExtensionMessagesFiles"] +
= $info['ExtensionMessagesFiles'];
407 * Set message-related settings, which need to be expanded to use
413 protected function extractMessagesDirs( $dir, array $info ) {
414 if ( isset( $info['MessagesDirs'] ) ) {
415 foreach ( $info['MessagesDirs'] as $name => $files ) {
416 foreach ( (array)$files as $file ) {
417 $this->globals
["wgMessagesDirs"][$name][] = "$dir/$file";
424 * @param string $path
426 * @return string Name of thing
429 protected function extractCredits( $path, array $info ) {
432 'type' => $info['type'] ??
'other',
434 foreach ( self
::$creditsAttributes as $attr ) {
435 if ( isset( $info[$attr] ) ) {
436 $credits[$attr] = $info[$attr];
440 $name = $credits['name'];
442 // If someone is loading the same thing twice, throw
443 // a nice error (T121493)
444 if ( isset( $this->credits
[$name] ) ) {
445 $firstPath = $this->credits
[$name]['path'];
446 $secondPath = $credits['path'];
447 throw new Exception( "It was attempted to load $name twice, from $firstPath and $secondPath." );
450 $this->credits
[$name] = $credits;
451 $this->globals
['wgExtensionCredits'][$credits['type']][] = $credits;
457 * Set configuration settings for manifest_version == 1
458 * @todo In the future, this should be done via Config interfaces
462 protected function extractConfig1( array $info ) {
463 if ( isset( $info['config'] ) ) {
464 if ( isset( $info['config']['_prefix'] ) ) {
465 $prefix = $info['config']['_prefix'];
466 unset( $info['config']['_prefix'] );
470 foreach ( $info['config'] as $key => $val ) {
471 if ( $key[0] !== '@' ) {
472 $this->addConfigGlobal( "$prefix$key", $val, $info['name'] );
479 * Set configuration settings for manifest_version == 2
480 * @todo In the future, this should be done via Config interfaces
485 protected function extractConfig2( array $info, $dir ) {
486 if ( isset( $info['config_prefix'] ) ) {
487 $prefix = $info['config_prefix'];
491 if ( isset( $info['config'] ) ) {
492 foreach ( $info['config'] as $key => $data ) {
493 $value = $data['value'];
494 if ( isset( $data['merge_strategy'] ) ) {
495 $value[ExtensionRegistry
::MERGE_STRATEGY
] = $data['merge_strategy'];
497 if ( isset( $data['path'] ) && $data['path'] ) {
498 $value = "$dir/$value";
500 $this->addConfigGlobal( "$prefix$key", $value, $info['name'] );
501 $data['providedby'] = $info['name'];
502 if ( isset( $info['ConfigRegistry'][0] ) ) {
503 $data['configregistry'] = array_keys( $info['ConfigRegistry'] )[0];
505 $this->config
[$key] = $data;
511 * Helper function to set a value to a specific global, if it isn't set already.
513 * @param string $key The config key with the prefix and anything
514 * @param mixed $value The value of the config
515 * @param string $extName Name of the extension
517 private function addConfigGlobal( $key, $value, $extName ) {
518 if ( array_key_exists( $key, $this->globals
) ) {
519 throw new RuntimeException(
520 "The configuration setting '$key' was already set by MediaWiki core or"
521 . " another extension, and cannot be set again by $extName." );
523 $this->globals
[$key] = $value;
526 protected function extractPathBasedGlobal( $global, $dir, $paths ) {
527 foreach ( $paths as $path ) {
528 $this->globals
[$global][] = "$dir/$path";
533 * @param string $path
534 * @param string $name
535 * @param array $value
536 * @param array &$array
537 * @throws InvalidArgumentException
539 protected function storeToArray( $path, $name, $value, &$array ) {
540 if ( !is_array( $value ) ) {
541 throw new InvalidArgumentException( "The value for '$name' should be an array (from $path)" );
543 if ( isset( $array[$name] ) ) {
544 $array[$name] = array_merge_recursive( $array[$name], $value );
546 $array[$name] = $value;
550 public function getExtraAutoloaderPaths( $dir, array $info ) {
552 if ( isset( $info['load_composer_autoloader'] ) && $info['load_composer_autoloader'] === true ) {
553 $paths[] = "$dir/vendor/autoload.php";