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 'ResourceLoaderLESSVars',
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 = [];
166 * Any thing else in the $info that hasn't
167 * already been processed
171 protected $attributes = [];
174 * Extension attributes, keyed by name =>
179 protected $extAttributes = [];
182 * @param string $path
184 * @param int $version manifest_version for info
187 public function extractInfo( $path, array $info, $version ) {
188 $dir = dirname( $path );
189 if ( $version === 2 ) {
190 $this->extractConfig2( $info, $dir );
193 $this->extractConfig1( $info );
195 $this->extractHooks( $info );
196 $this->extractExtensionMessagesFiles( $dir, $info );
197 $this->extractMessagesDirs( $dir, $info );
198 $this->extractNamespaces( $info );
199 $this->extractResourceLoaderModules( $dir, $info );
200 $this->extractServiceWiringFiles( $dir, $info );
201 $this->extractParserTestFiles( $dir, $info );
202 $name = $this->extractCredits( $path, $info );
203 if ( isset( $info['callback'] ) ) {
204 $this->callbacks
[$name] = $info['callback'];
207 if ( $version === 2 ) {
208 $this->extractAttributes( $path, $info );
211 foreach ( $info as $key => $val ) {
212 // If it's a global setting,
213 if ( in_array( $key, self
::$globalSettings ) ) {
214 $this->storeToArray( $path, "wg$key", $val, $this->globals
);
217 // Ignore anything that starts with a @
218 if ( $key[0] === '@' ) {
222 if ( $version === 2 ) {
223 // Only whitelisted attributes are set
224 if ( in_array( $key, self
::$coreAttributes ) ) {
225 $this->storeToArray( $path, $key, $val, $this->attributes
);
229 if ( !in_array( $key, self
::$notAttributes )
230 && !in_array( $key, self
::$creditsAttributes )
232 // If it's not blacklisted, it's an attribute
233 $this->storeToArray( $path, $key, $val, $this->attributes
);
241 * @param string $path
244 protected function extractAttributes( $path, array $info ) {
245 if ( isset( $info['attributes'] ) ) {
246 foreach ( $info['attributes'] as $extName => $value ) {
247 $this->storeToArray( $path, $extName, $value, $this->extAttributes
);
252 public function getExtractedInfo() {
253 // Make sure the merge strategies are set
254 foreach ( $this->globals
as $key => $val ) {
255 if ( isset( self
::$mergeStrategies[$key] ) ) {
256 $this->globals
[$key][ExtensionRegistry
::MERGE_STRATEGY
] = self
::$mergeStrategies[$key];
260 // Merge $this->extAttributes into $this->attributes depending on what is loaded
261 foreach ( $this->extAttributes
as $extName => $value ) {
262 // Only set the attribute if $extName is loaded (and hence present in credits)
263 if ( isset( $this->credits
[$extName] ) ) {
264 foreach ( $value as $attrName => $attrValue ) {
266 '', // Don't provide a path since it's impossible to generate an error here
267 $extName . $attrName,
272 unset( $this->extAttributes
[$extName] );
277 'globals' => $this->globals
,
278 'defines' => $this->defines
,
279 'callbacks' => $this->callbacks
,
280 'credits' => $this->credits
,
281 'attributes' => $this->attributes
,
285 public function getRequirements( array $info ) {
286 return isset( $info['requires'] ) ?
$info['requires'] : [];
289 protected function extractHooks( array $info ) {
290 if ( isset( $info['Hooks'] ) ) {
291 foreach ( $info['Hooks'] as $name => $value ) {
292 if ( is_array( $value ) ) {
293 foreach ( $value as $callback ) {
294 $this->globals
['wgHooks'][$name][] = $callback;
297 $this->globals
['wgHooks'][$name][] = $value;
304 * Register namespaces with the appropriate global settings
308 protected function extractNamespaces( array $info ) {
309 if ( isset( $info['namespaces'] ) ) {
310 foreach ( $info['namespaces'] as $ns ) {
311 if ( defined( $ns['constant'] ) ) {
312 // If the namespace constant is already defined, use it.
313 // This allows namespace IDs to be overwritten locally.
314 $id = constant( $ns['constant'] );
317 $this->defines
[ $ns['constant'] ] = $id;
320 if ( !( isset( $ns['conditional'] ) && $ns['conditional'] ) ) {
321 // If it is not conditional, register it
322 $this->attributes
['ExtensionNamespaces'][$id] = $ns['name'];
324 if ( isset( $ns['gender'] ) ) {
325 $this->globals
['wgExtraGenderNamespaces'][$id] = $ns['gender'];
327 if ( isset( $ns['subpages'] ) && $ns['subpages'] ) {
328 $this->globals
['wgNamespacesWithSubpages'][$id] = true;
330 if ( isset( $ns['content'] ) && $ns['content'] ) {
331 $this->globals
['wgContentNamespaces'][] = $id;
333 if ( isset( $ns['defaultcontentmodel'] ) ) {
334 $this->globals
['wgNamespaceContentModels'][$id] = $ns['defaultcontentmodel'];
336 if ( isset( $ns['protection'] ) ) {
337 $this->globals
['wgNamespaceProtection'][$id] = $ns['protection'];
339 if ( isset( $ns['capitallinkoverride'] ) ) {
340 $this->globals
['wgCapitalLinkOverrides'][$id] = $ns['capitallinkoverride'];
346 protected function extractResourceLoaderModules( $dir, array $info ) {
347 $defaultPaths = isset( $info['ResourceFileModulePaths'] )
348 ?
$info['ResourceFileModulePaths']
350 if ( isset( $defaultPaths['localBasePath'] ) ) {
351 if ( $defaultPaths['localBasePath'] === '' ) {
352 // Avoid double slashes (e.g. /extensions/Example//path)
353 $defaultPaths['localBasePath'] = $dir;
355 $defaultPaths['localBasePath'] = "$dir/{$defaultPaths['localBasePath']}";
359 foreach ( [ 'ResourceModules', 'ResourceModuleSkinStyles' ] as $setting ) {
360 if ( isset( $info[$setting] ) ) {
361 foreach ( $info[$setting] as $name => $data ) {
362 if ( isset( $data['localBasePath'] ) ) {
363 if ( $data['localBasePath'] === '' ) {
364 // Avoid double slashes (e.g. /extensions/Example//path)
365 $data['localBasePath'] = $dir;
367 $data['localBasePath'] = "$dir/{$data['localBasePath']}";
370 if ( $defaultPaths ) {
371 $data +
= $defaultPaths;
373 $this->globals
["wg$setting"][$name] = $data;
379 protected function extractExtensionMessagesFiles( $dir, array $info ) {
380 if ( isset( $info['ExtensionMessagesFiles'] ) ) {
381 $this->globals
["wgExtensionMessagesFiles"] +
= array_map( function ( $file ) use ( $dir ) {
383 }, $info['ExtensionMessagesFiles'] );
388 * Set message-related settings, which need to be expanded to use
394 protected function extractMessagesDirs( $dir, array $info ) {
395 if ( isset( $info['MessagesDirs'] ) ) {
396 foreach ( $info['MessagesDirs'] as $name => $files ) {
397 foreach ( (array)$files as $file ) {
398 $this->globals
["wgMessagesDirs"][$name][] = "$dir/$file";
405 * @param string $path
407 * @return string Name of thing
410 protected function extractCredits( $path, array $info ) {
413 'type' => isset( $info['type'] ) ?
$info['type'] : 'other',
415 foreach ( self
::$creditsAttributes as $attr ) {
416 if ( isset( $info[$attr] ) ) {
417 $credits[$attr] = $info[$attr];
421 $name = $credits['name'];
423 // If someone is loading the same thing twice, throw
424 // a nice error (T121493)
425 if ( isset( $this->credits
[$name] ) ) {
426 $firstPath = $this->credits
[$name]['path'];
427 $secondPath = $credits['path'];
428 throw new Exception( "It was attempted to load $name twice, from $firstPath and $secondPath." );
431 $this->credits
[$name] = $credits;
432 $this->globals
['wgExtensionCredits'][$credits['type']][] = $credits;
438 * Set configuration settings for manifest_version == 1
439 * @todo In the future, this should be done via Config interfaces
443 protected function extractConfig1( array $info ) {
444 if ( isset( $info['config'] ) ) {
445 if ( isset( $info['config']['_prefix'] ) ) {
446 $prefix = $info['config']['_prefix'];
447 unset( $info['config']['_prefix'] );
451 foreach ( $info['config'] as $key => $val ) {
452 if ( $key[0] !== '@' ) {
453 $this->addConfigGlobal( "$prefix$key", $val );
460 * Set configuration settings for manifest_version == 2
461 * @todo In the future, this should be done via Config interfaces
466 protected function extractConfig2( array $info, $dir ) {
467 if ( isset( $info['config_prefix'] ) ) {
468 $prefix = $info['config_prefix'];
472 if ( isset( $info['config'] ) ) {
473 foreach ( $info['config'] as $key => $data ) {
474 $value = $data['value'];
475 if ( isset( $data['merge_strategy'] ) ) {
476 $value[ExtensionRegistry
::MERGE_STRATEGY
] = $data['merge_strategy'];
478 if ( isset( $data['path'] ) && $data['path'] ) {
479 $value = "$dir/$value";
481 $this->addConfigGlobal( "$prefix$key", $value );
487 * Helper function to set a value to a specific global, if it isn't set already.
489 * @param string $key The config key with the prefix and anything
490 * @param mixed $value The value of the config
492 private function addConfigGlobal( $key, $value ) {
493 if ( array_key_exists( $key, $this->globals
) ) {
494 throw new RuntimeException(
495 "The configuration setting '$key' was already set by another extension,"
496 . " and cannot be set again." );
498 $this->globals
[$key] = $value;
501 protected function extractServiceWiringFiles( $dir, array $info ) {
502 if ( isset( $info['ServiceWiringFiles'] ) ) {
503 foreach ( $info['ServiceWiringFiles'] as $path ) {
504 $this->globals
['wgServiceWiringFiles'][] = "$dir/$path";
509 protected function extractParserTestFiles( $dir, array $info ) {
510 if ( isset( $info['ParserTestFiles'] ) ) {
511 foreach ( $info['ParserTestFiles'] as $path ) {
512 $this->globals
['wgParserTestFiles'][] = "$dir/$path";
518 * @param string $path
519 * @param string $name
520 * @param array $value
521 * @param array &$array
522 * @throws InvalidArgumentException
524 protected function storeToArray( $path, $name, $value, &$array ) {
525 if ( !is_array( $value ) ) {
526 throw new InvalidArgumentException( "The value for '$name' should be an array (from $path)" );
528 if ( isset( $array[$name] ) ) {
529 $array[$name] = array_merge_recursive( $array[$name], $value );
531 $array[$name] = $value;
535 public function getExtraAutoloaderPaths( $dir, array $info ) {
537 if ( isset( $info['load_composer_autoloader'] ) && $info['load_composer_autoloader'] === true ) {
538 $paths[] = "$dir/vendor/autoload.php";