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 if ( isset( $info['ServiceWiringFiles'] ) ) {
201 $this->extractPathBasedGlobal(
202 'wgServiceWiringFiles',
204 $info['ServiceWiringFiles']
207 if ( isset( $info['ParserTestFiles'] ) ) {
208 $this->extractPathBasedGlobal(
211 $info['ParserTestFiles']
214 $name = $this->extractCredits( $path, $info );
215 if ( isset( $info['callback'] ) ) {
216 $this->callbacks
[$name] = $info['callback'];
219 if ( $version === 2 ) {
220 $this->extractAttributes( $path, $info );
223 foreach ( $info as $key => $val ) {
224 // If it's a global setting,
225 if ( in_array( $key, self
::$globalSettings ) ) {
226 $this->storeToArray( $path, "wg$key", $val, $this->globals
);
229 // Ignore anything that starts with a @
230 if ( $key[0] === '@' ) {
234 if ( $version === 2 ) {
235 // Only whitelisted attributes are set
236 if ( in_array( $key, self
::$coreAttributes ) ) {
237 $this->storeToArray( $path, $key, $val, $this->attributes
);
241 if ( !in_array( $key, self
::$notAttributes )
242 && !in_array( $key, self
::$creditsAttributes )
244 // If it's not blacklisted, it's an attribute
245 $this->storeToArray( $path, $key, $val, $this->attributes
);
253 * @param string $path
256 protected function extractAttributes( $path, array $info ) {
257 if ( isset( $info['attributes'] ) ) {
258 foreach ( $info['attributes'] as $extName => $value ) {
259 $this->storeToArray( $path, $extName, $value, $this->extAttributes
);
264 public function getExtractedInfo() {
265 // Make sure the merge strategies are set
266 foreach ( $this->globals
as $key => $val ) {
267 if ( isset( self
::$mergeStrategies[$key] ) ) {
268 $this->globals
[$key][ExtensionRegistry
::MERGE_STRATEGY
] = self
::$mergeStrategies[$key];
272 // Merge $this->extAttributes into $this->attributes depending on what is loaded
273 foreach ( $this->extAttributes
as $extName => $value ) {
274 // Only set the attribute if $extName is loaded (and hence present in credits)
275 if ( isset( $this->credits
[$extName] ) ) {
276 foreach ( $value as $attrName => $attrValue ) {
278 '', // Don't provide a path since it's impossible to generate an error here
279 $extName . $attrName,
284 unset( $this->extAttributes
[$extName] );
289 'globals' => $this->globals
,
290 'defines' => $this->defines
,
291 'callbacks' => $this->callbacks
,
292 'credits' => $this->credits
,
293 'attributes' => $this->attributes
,
297 public function getRequirements( array $info ) {
298 return isset( $info['requires'] ) ?
$info['requires'] : [];
301 protected function extractHooks( array $info ) {
302 if ( isset( $info['Hooks'] ) ) {
303 foreach ( $info['Hooks'] as $name => $value ) {
304 if ( is_array( $value ) ) {
305 foreach ( $value as $callback ) {
306 $this->globals
['wgHooks'][$name][] = $callback;
309 $this->globals
['wgHooks'][$name][] = $value;
316 * Register namespaces with the appropriate global settings
320 protected function extractNamespaces( array $info ) {
321 if ( isset( $info['namespaces'] ) ) {
322 foreach ( $info['namespaces'] as $ns ) {
323 if ( defined( $ns['constant'] ) ) {
324 // If the namespace constant is already defined, use it.
325 // This allows namespace IDs to be overwritten locally.
326 $id = constant( $ns['constant'] );
329 $this->defines
[ $ns['constant'] ] = $id;
332 if ( !( isset( $ns['conditional'] ) && $ns['conditional'] ) ) {
333 // If it is not conditional, register it
334 $this->attributes
['ExtensionNamespaces'][$id] = $ns['name'];
336 if ( isset( $ns['gender'] ) ) {
337 $this->globals
['wgExtraGenderNamespaces'][$id] = $ns['gender'];
339 if ( isset( $ns['subpages'] ) && $ns['subpages'] ) {
340 $this->globals
['wgNamespacesWithSubpages'][$id] = true;
342 if ( isset( $ns['content'] ) && $ns['content'] ) {
343 $this->globals
['wgContentNamespaces'][] = $id;
345 if ( isset( $ns['defaultcontentmodel'] ) ) {
346 $this->globals
['wgNamespaceContentModels'][$id] = $ns['defaultcontentmodel'];
348 if ( isset( $ns['protection'] ) ) {
349 $this->globals
['wgNamespaceProtection'][$id] = $ns['protection'];
351 if ( isset( $ns['capitallinkoverride'] ) ) {
352 $this->globals
['wgCapitalLinkOverrides'][$id] = $ns['capitallinkoverride'];
358 protected function extractResourceLoaderModules( $dir, array $info ) {
359 $defaultPaths = isset( $info['ResourceFileModulePaths'] )
360 ?
$info['ResourceFileModulePaths']
362 if ( isset( $defaultPaths['localBasePath'] ) ) {
363 if ( $defaultPaths['localBasePath'] === '' ) {
364 // Avoid double slashes (e.g. /extensions/Example//path)
365 $defaultPaths['localBasePath'] = $dir;
367 $defaultPaths['localBasePath'] = "$dir/{$defaultPaths['localBasePath']}";
371 foreach ( [ 'ResourceModules', 'ResourceModuleSkinStyles' ] as $setting ) {
372 if ( isset( $info[$setting] ) ) {
373 foreach ( $info[$setting] as $name => $data ) {
374 if ( isset( $data['localBasePath'] ) ) {
375 if ( $data['localBasePath'] === '' ) {
376 // Avoid double slashes (e.g. /extensions/Example//path)
377 $data['localBasePath'] = $dir;
379 $data['localBasePath'] = "$dir/{$data['localBasePath']}";
382 if ( $defaultPaths ) {
383 $data +
= $defaultPaths;
385 $this->globals
["wg$setting"][$name] = $data;
391 protected function extractExtensionMessagesFiles( $dir, array $info ) {
392 if ( isset( $info['ExtensionMessagesFiles'] ) ) {
393 foreach ( $info['ExtensionMessagesFiles'] as &$file ) {
394 $file = "$dir/$file";
396 $this->globals
["wgExtensionMessagesFiles"] +
= $info['ExtensionMessagesFiles'];
401 * Set message-related settings, which need to be expanded to use
407 protected function extractMessagesDirs( $dir, array $info ) {
408 if ( isset( $info['MessagesDirs'] ) ) {
409 foreach ( $info['MessagesDirs'] as $name => $files ) {
410 foreach ( (array)$files as $file ) {
411 $this->globals
["wgMessagesDirs"][$name][] = "$dir/$file";
418 * @param string $path
420 * @return string Name of thing
423 protected function extractCredits( $path, array $info ) {
426 'type' => isset( $info['type'] ) ?
$info['type'] : 'other',
428 foreach ( self
::$creditsAttributes as $attr ) {
429 if ( isset( $info[$attr] ) ) {
430 $credits[$attr] = $info[$attr];
434 $name = $credits['name'];
436 // If someone is loading the same thing twice, throw
437 // a nice error (T121493)
438 if ( isset( $this->credits
[$name] ) ) {
439 $firstPath = $this->credits
[$name]['path'];
440 $secondPath = $credits['path'];
441 throw new Exception( "It was attempted to load $name twice, from $firstPath and $secondPath." );
444 $this->credits
[$name] = $credits;
445 $this->globals
['wgExtensionCredits'][$credits['type']][] = $credits;
451 * Set configuration settings for manifest_version == 1
452 * @todo In the future, this should be done via Config interfaces
456 protected function extractConfig1( array $info ) {
457 if ( isset( $info['config'] ) ) {
458 if ( isset( $info['config']['_prefix'] ) ) {
459 $prefix = $info['config']['_prefix'];
460 unset( $info['config']['_prefix'] );
464 foreach ( $info['config'] as $key => $val ) {
465 if ( $key[0] !== '@' ) {
466 $this->addConfigGlobal( "$prefix$key", $val );
473 * Set configuration settings for manifest_version == 2
474 * @todo In the future, this should be done via Config interfaces
479 protected function extractConfig2( array $info, $dir ) {
480 if ( isset( $info['config_prefix'] ) ) {
481 $prefix = $info['config_prefix'];
485 if ( isset( $info['config'] ) ) {
486 foreach ( $info['config'] as $key => $data ) {
487 $value = $data['value'];
488 if ( isset( $data['merge_strategy'] ) ) {
489 $value[ExtensionRegistry
::MERGE_STRATEGY
] = $data['merge_strategy'];
491 if ( isset( $data['path'] ) && $data['path'] ) {
492 $value = "$dir/$value";
494 $this->addConfigGlobal( "$prefix$key", $value );
500 * Helper function to set a value to a specific global, if it isn't set already.
502 * @param string $key The config key with the prefix and anything
503 * @param mixed $value The value of the config
505 private function addConfigGlobal( $key, $value ) {
506 if ( array_key_exists( $key, $this->globals
) ) {
507 throw new RuntimeException(
508 "The configuration setting '$key' was already set by another extension,"
509 . " and cannot be set again." );
511 $this->globals
[$key] = $value;
514 protected function extractPathBasedGlobal( $global, $dir, $paths ) {
515 foreach ( $paths as $path ) {
516 $this->globals
[$global][] = "$dir/$path";
521 * @param string $path
522 * @param string $name
523 * @param array $value
524 * @param array &$array
525 * @throws InvalidArgumentException
527 protected function storeToArray( $path, $name, $value, &$array ) {
528 if ( !is_array( $value ) ) {
529 throw new InvalidArgumentException( "The value for '$name' should be an array (from $path)" );
531 if ( isset( $array[$name] ) ) {
532 $array[$name] = array_merge_recursive( $array[$name], $value );
534 $array[$name] = $value;
538 public function getExtraAutoloaderPaths( $dir, array $info ) {
540 if ( isset( $info['load_composer_autoloader'] ) && $info['load_composer_autoloader'] === true ) {
541 $paths[] = "$dir/vendor/autoload.php";