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 * Mapping of global settings to their specific merge strategies.
62 * @see ExtensionRegistry::exportExtractedData
63 * @see getExtractedInfo
66 protected static $mergeStrategies = [
67 'wgAuthManagerAutoConfig' => 'array_plus_2d',
68 'wgCapitalLinkOverrides' => 'array_plus',
69 'wgExtensionCredits' => 'array_merge_recursive',
70 'wgExtraGenderNamespaces' => 'array_plus',
71 'wgGrantPermissions' => 'array_plus_2d',
72 'wgGroupPermissions' => 'array_plus_2d',
73 'wgHooks' => 'array_merge_recursive',
74 'wgNamespaceContentModels' => 'array_plus',
75 'wgNamespaceProtection' => 'array_plus',
76 'wgNamespacesWithSubpages' => 'array_plus',
77 'wgPasswordPolicy' => 'array_merge_recursive',
78 'wgRateLimits' => 'array_plus_2d',
79 'wgRevokePermissions' => 'array_plus_2d',
83 * Keys that are part of the extension credits
87 protected static $creditsAttributes = [
99 * Things that are not 'attributes', but are not in
100 * $globalSettings or $creditsAttributes.
104 protected static $notAttributes = [
108 'ResourceFileModulePaths',
110 'ResourceModuleSkinStyles',
111 'ExtensionMessagesFiles',
116 'ServiceWiringFiles',
120 'load_composer_autoloader',
124 * Stuff that is going to be set to $GLOBALS
126 * Some keys are pre-set to arrays so we can += to them
130 protected $globals = [
131 'wgExtensionMessagesFiles' => [],
132 'wgMessagesDirs' => [],
136 * Things that should be define()'d
140 protected $defines = [];
143 * Things to be called once registration of these extensions are done
144 * keyed by the name of the extension that it belongs to
148 protected $callbacks = [];
153 protected $credits = [];
156 * Any thing else in the $info that hasn't
157 * already been processed
161 protected $attributes = [];
164 * @param string $path
166 * @param int $version manifest_version for info
169 public function extractInfo( $path, array $info, $version ) {
170 $dir = dirname( $path );
171 if ( $version === 2 ) {
172 $this->extractConfig2( $info, $dir );
175 $this->extractConfig1( $info );
177 $this->extractHooks( $info );
178 $this->extractExtensionMessagesFiles( $dir, $info );
179 $this->extractMessagesDirs( $dir, $info );
180 $this->extractNamespaces( $info );
181 $this->extractResourceLoaderModules( $dir, $info );
182 $this->extractServiceWiringFiles( $dir, $info );
183 $this->extractParserTestFiles( $dir, $info );
184 $name = $this->extractCredits( $path, $info );
185 if ( isset( $info['callback'] ) ) {
186 $this->callbacks
[$name] = $info['callback'];
189 foreach ( $info as $key => $val ) {
190 if ( in_array( $key, self
::$globalSettings ) ) {
191 $this->storeToArray( $path, "wg$key", $val, $this->globals
);
192 // Ignore anything that starts with a @
193 } elseif ( $key[0] !== '@' && !in_array( $key, self
::$notAttributes )
194 && !in_array( $key, self
::$creditsAttributes )
196 $this->storeToArray( $path, $key, $val, $this->attributes
);
201 public function getExtractedInfo() {
202 // Make sure the merge strategies are set
203 foreach ( $this->globals
as $key => $val ) {
204 if ( isset( self
::$mergeStrategies[$key] ) ) {
205 $this->globals
[$key][ExtensionRegistry
::MERGE_STRATEGY
] = self
::$mergeStrategies[$key];
210 'globals' => $this->globals
,
211 'defines' => $this->defines
,
212 'callbacks' => $this->callbacks
,
213 'credits' => $this->credits
,
214 'attributes' => $this->attributes
,
218 public function getRequirements( array $info ) {
219 return isset( $info['requires'] ) ?
$info['requires'] : [];
222 protected function extractHooks( array $info ) {
223 if ( isset( $info['Hooks'] ) ) {
224 foreach ( $info['Hooks'] as $name => $value ) {
225 if ( is_array( $value ) ) {
226 foreach ( $value as $callback ) {
227 $this->globals
['wgHooks'][$name][] = $callback;
230 $this->globals
['wgHooks'][$name][] = $value;
237 * Register namespaces with the appropriate global settings
241 protected function extractNamespaces( array $info ) {
242 if ( isset( $info['namespaces'] ) ) {
243 foreach ( $info['namespaces'] as $ns ) {
244 if ( defined( $ns['constant'] ) ) {
245 // If the namespace constant is already defined, use it.
246 // This allows namespace IDs to be overwritten locally.
247 $id = constant( $ns['constant'] );
250 $this->defines
[ $ns['constant'] ] = $id;
253 if ( !( isset( $ns['conditional'] ) && $ns['conditional'] ) ) {
254 // If it is not conditional, register it
255 $this->attributes
['ExtensionNamespaces'][$id] = $ns['name'];
257 if ( isset( $ns['gender'] ) ) {
258 $this->globals
['wgExtraGenderNamespaces'][$id] = $ns['gender'];
260 if ( isset( $ns['subpages'] ) && $ns['subpages'] ) {
261 $this->globals
['wgNamespacesWithSubpages'][$id] = true;
263 if ( isset( $ns['content'] ) && $ns['content'] ) {
264 $this->globals
['wgContentNamespaces'][] = $id;
266 if ( isset( $ns['defaultcontentmodel'] ) ) {
267 $this->globals
['wgNamespaceContentModels'][$id] = $ns['defaultcontentmodel'];
269 if ( isset( $ns['protection'] ) ) {
270 $this->globals
['wgNamespaceProtection'][$id] = $ns['protection'];
272 if ( isset( $ns['capitallinkoverride'] ) ) {
273 $this->globals
['wgCapitalLinkOverrides'][$id] = $ns['capitallinkoverride'];
279 protected function extractResourceLoaderModules( $dir, array $info ) {
280 $defaultPaths = isset( $info['ResourceFileModulePaths'] )
281 ?
$info['ResourceFileModulePaths']
283 if ( isset( $defaultPaths['localBasePath'] ) ) {
284 if ( $defaultPaths['localBasePath'] === '' ) {
285 // Avoid double slashes (e.g. /extensions/Example//path)
286 $defaultPaths['localBasePath'] = $dir;
288 $defaultPaths['localBasePath'] = "$dir/{$defaultPaths['localBasePath']}";
292 foreach ( [ 'ResourceModules', 'ResourceModuleSkinStyles' ] as $setting ) {
293 if ( isset( $info[$setting] ) ) {
294 foreach ( $info[$setting] as $name => $data ) {
295 if ( isset( $data['localBasePath'] ) ) {
296 if ( $data['localBasePath'] === '' ) {
297 // Avoid double slashes (e.g. /extensions/Example//path)
298 $data['localBasePath'] = $dir;
300 $data['localBasePath'] = "$dir/{$data['localBasePath']}";
303 if ( $defaultPaths ) {
304 $data +
= $defaultPaths;
306 $this->globals
["wg$setting"][$name] = $data;
312 protected function extractExtensionMessagesFiles( $dir, array $info ) {
313 if ( isset( $info['ExtensionMessagesFiles'] ) ) {
314 $this->globals
["wgExtensionMessagesFiles"] +
= array_map( function( $file ) use ( $dir ) {
316 }, $info['ExtensionMessagesFiles'] );
321 * Set message-related settings, which need to be expanded to use
327 protected function extractMessagesDirs( $dir, array $info ) {
328 if ( isset( $info['MessagesDirs'] ) ) {
329 foreach ( $info['MessagesDirs'] as $name => $files ) {
330 foreach ( (array)$files as $file ) {
331 $this->globals
["wgMessagesDirs"][$name][] = "$dir/$file";
338 * @param string $path
340 * @return string Name of thing
343 protected function extractCredits( $path, array $info ) {
346 'type' => isset( $info['type'] ) ?
$info['type'] : 'other',
348 foreach ( self
::$creditsAttributes as $attr ) {
349 if ( isset( $info[$attr] ) ) {
350 $credits[$attr] = $info[$attr];
354 $name = $credits['name'];
356 // If someone is loading the same thing twice, throw
357 // a nice error (T121493)
358 if ( isset( $this->credits
[$name] ) ) {
359 $firstPath = $this->credits
[$name]['path'];
360 $secondPath = $credits['path'];
361 throw new Exception( "It was attempted to load $name twice, from $firstPath and $secondPath." );
364 $this->credits
[$name] = $credits;
365 $this->globals
['wgExtensionCredits'][$credits['type']][] = $credits;
371 * Set configuration settings for manifest_version == 1
372 * @todo In the future, this should be done via Config interfaces
376 protected function extractConfig1( array $info ) {
377 if ( isset( $info['config'] ) ) {
378 if ( isset( $info['config']['_prefix'] ) ) {
379 $prefix = $info['config']['_prefix'];
380 unset( $info['config']['_prefix'] );
384 foreach ( $info['config'] as $key => $val ) {
385 if ( $key[0] !== '@' ) {
386 $this->globals
["$prefix$key"] = $val;
393 * Set configuration settings for manifest_version == 2
394 * @todo In the future, this should be done via Config interfaces
399 protected function extractConfig2( array $info, $dir ) {
400 if ( isset( $info['config_prefix'] ) ) {
401 $prefix = $info['config_prefix'];
405 if ( isset( $info['config'] ) ) {
406 foreach ( $info['config'] as $key => $data ) {
407 $value = $data['value'];
408 if ( isset( $data['merge_strategy'] ) ) {
409 $value[ExtensionRegistry
::MERGE_STRATEGY
] = $data['merge_strategy'];
411 if ( isset( $data['path'] ) && $data['path'] ) {
412 $value = "$dir/$value";
414 $this->globals
["$prefix$key"] = $value;
419 protected function extractServiceWiringFiles( $dir, array $info ) {
420 if ( isset( $info['ServiceWiringFiles'] ) ) {
421 foreach ( $info['ServiceWiringFiles'] as $path ) {
422 $this->globals
['wgServiceWiringFiles'][] = "$dir/$path";
427 protected function extractParserTestFiles( $dir, array $info ) {
428 if ( isset( $info['ParserTestFiles'] ) ) {
429 foreach ( $info['ParserTestFiles'] as $path ) {
430 $this->globals
['wgParserTestFiles'][] = "$dir/$path";
436 * @param string $path
437 * @param string $name
438 * @param array $value
439 * @param array &$array
440 * @throws InvalidArgumentException
442 protected function storeToArray( $path, $name, $value, &$array ) {
443 if ( !is_array( $value ) ) {
444 throw new InvalidArgumentException( "The value for '$name' should be an array (from $path)" );
446 if ( isset( $array[$name] ) ) {
447 $array[$name] = array_merge_recursive( $array[$name], $value );
449 $array[$name] = $value;
453 public function getExtraAutoloaderPaths( $dir, array $info ) {
455 if ( isset( $info['load_composer_autoloader'] ) && $info['load_composer_autoloader'] === true ) {
456 $path = "$dir/vendor/autoload.php";
457 if ( file_exists( $path ) ) {