3 class ExtensionProcessor
implements Processor
{
6 * Keys that should be set to $GLOBALS
10 protected static $globalSettings = [
11 'ResourceLoaderSources',
12 'ResourceLoaderLESSVars',
13 'ResourceLoaderLESSImportPaths',
19 'GrantPermissionGroups',
22 'GroupsRemoveFromSelf',
29 'AuthManagerAutoConfig',
30 'CentralIdLookupProviders',
31 'ChangeCredentialsBlacklist',
32 'RemoveCredentialsBlacklist',
37 'ExtensionEntryPointListFiles',
59 * Mapping of global settings to their specific merge strategies.
61 * @see ExtensionRegistry::exportExtractedData
62 * @see getExtractedInfo
65 protected static $mergeStrategies = [
66 'wgGroupPermissions' => 'array_plus_2d',
67 'wgRevokePermissions' => 'array_plus_2d',
68 'wgGrantPermissions' => 'array_plus_2d',
69 'wgHooks' => 'array_merge_recursive',
70 'wgExtensionCredits' => 'array_merge_recursive',
71 'wgExtraGenderNamespaces' => 'array_plus',
72 'wgNamespacesWithSubpages' => 'array_plus',
73 'wgNamespaceContentModels' => 'array_plus',
74 'wgNamespaceProtection' => 'array_plus',
75 'wgCapitalLinkOverrides' => 'array_plus',
76 'wgRateLimits' => 'array_plus_2d',
77 'wgAuthManagerAutoConfig' => 'array_plus_2d',
81 * Keys that are part of the extension credits
85 protected static $creditsAttributes = [
97 * Things that are not 'attributes', but are not in
98 * $globalSettings or $creditsAttributes.
102 protected static $notAttributes = [
106 'ResourceFileModulePaths',
108 'ResourceModuleSkinStyles',
109 'ExtensionMessagesFiles',
116 'load_composer_autoloader',
120 * Stuff that is going to be set to $GLOBALS
122 * Some keys are pre-set to arrays so we can += to them
126 protected $globals = [
127 'wgExtensionMessagesFiles' => [],
128 'wgMessagesDirs' => [],
132 * Things that should be define()'d
136 protected $defines = [];
139 * Things to be called once registration of these extensions are done
143 protected $callbacks = [];
148 protected $credits = [];
151 * Any thing else in the $info that hasn't
152 * already been processed
156 protected $attributes = [];
159 * @param string $path
161 * @param int $version manifest_version for info
164 public function extractInfo( $path, array $info, $version ) {
165 $this->extractConfig( $info );
166 $this->extractHooks( $info );
167 $dir = dirname( $path );
168 $this->extractExtensionMessagesFiles( $dir, $info );
169 $this->extractMessagesDirs( $dir, $info );
170 $this->extractNamespaces( $info );
171 $this->extractResourceLoaderModules( $dir, $info );
172 $this->extractParserTestFiles( $dir, $info );
173 if ( isset( $info['callback'] ) ) {
174 $this->callbacks
[] = $info['callback'];
177 $this->extractCredits( $path, $info );
178 foreach ( $info as $key => $val ) {
179 if ( in_array( $key, self
::$globalSettings ) ) {
180 $this->storeToArray( $path, "wg$key", $val, $this->globals
);
181 // Ignore anything that starts with a @
182 } elseif ( $key[0] !== '@' && !in_array( $key, self
::$notAttributes )
183 && !in_array( $key, self
::$creditsAttributes )
185 $this->storeToArray( $path, $key, $val, $this->attributes
);
190 public function getExtractedInfo() {
191 // Make sure the merge strategies are set
192 foreach ( $this->globals
as $key => $val ) {
193 if ( isset( self
::$mergeStrategies[$key] ) ) {
194 $this->globals
[$key][ExtensionRegistry
::MERGE_STRATEGY
] = self
::$mergeStrategies[$key];
199 'globals' => $this->globals
,
200 'defines' => $this->defines
,
201 'callbacks' => $this->callbacks
,
202 'credits' => $this->credits
,
203 'attributes' => $this->attributes
,
207 public function getRequirements( array $info ) {
209 $key = ExtensionRegistry
::MEDIAWIKI_CORE
;
210 if ( isset( $info['requires'][$key] ) ) {
211 $requirements[$key] = $info['requires'][$key];
214 return $requirements;
217 protected function extractHooks( array $info ) {
218 if ( isset( $info['Hooks'] ) ) {
219 foreach ( $info['Hooks'] as $name => $value ) {
220 if ( is_array( $value ) ) {
221 foreach ( $value as $callback ) {
222 $this->globals
['wgHooks'][$name][] = $callback;
225 $this->globals
['wgHooks'][$name][] = $value;
232 * Register namespaces with the appropriate global settings
236 protected function extractNamespaces( array $info ) {
237 if ( isset( $info['namespaces'] ) ) {
238 foreach ( $info['namespaces'] as $ns ) {
240 $this->defines
[$ns['constant']] = $id;
241 $this->attributes
['ExtensionNamespaces'][$id] = $ns['name'];
242 if ( isset( $ns['gender'] ) ) {
243 $this->globals
['wgExtraGenderNamespaces'][$id] = $ns['gender'];
245 if ( isset( $ns['subpages'] ) && $ns['subpages'] ) {
246 $this->globals
['wgNamespacesWithSubpages'][$id] = true;
248 if ( isset( $ns['content'] ) && $ns['content'] ) {
249 $this->globals
['wgContentNamespaces'][] = $id;
251 if ( isset( $ns['defaultcontentmodel'] ) ) {
252 $this->globals
['wgNamespaceContentModels'][$id] = $ns['defaultcontentmodel'];
254 if ( isset( $ns['protection'] ) ) {
255 $this->globals
['wgNamespaceProtection'][$id] = $ns['protection'];
257 if ( isset( $ns['capitallinkoverride'] ) ) {
258 $this->globals
['wgCapitalLinkOverrides'][$id] = $ns['capitallinkoverride'];
264 protected function extractResourceLoaderModules( $dir, array $info ) {
265 $defaultPaths = isset( $info['ResourceFileModulePaths'] )
266 ?
$info['ResourceFileModulePaths']
268 if ( isset( $defaultPaths['localBasePath'] ) ) {
269 if ( $defaultPaths['localBasePath'] === '' ) {
270 // Avoid double slashes (e.g. /extensions/Example//path)
271 $defaultPaths['localBasePath'] = $dir;
273 $defaultPaths['localBasePath'] = "$dir/{$defaultPaths['localBasePath']}";
277 foreach ( [ 'ResourceModules', 'ResourceModuleSkinStyles' ] as $setting ) {
278 if ( isset( $info[$setting] ) ) {
279 foreach ( $info[$setting] as $name => $data ) {
280 if ( isset( $data['localBasePath'] ) ) {
281 if ( $data['localBasePath'] === '' ) {
282 // Avoid double slashes (e.g. /extensions/Example//path)
283 $data['localBasePath'] = $dir;
285 $data['localBasePath'] = "$dir/{$data['localBasePath']}";
288 if ( $defaultPaths ) {
289 $data +
= $defaultPaths;
291 $this->globals
["wg$setting"][$name] = $data;
297 protected function extractExtensionMessagesFiles( $dir, array $info ) {
298 if ( isset( $info['ExtensionMessagesFiles'] ) ) {
299 $this->globals
["wgExtensionMessagesFiles"] +
= array_map( function( $file ) use ( $dir ) {
301 }, $info['ExtensionMessagesFiles'] );
306 * Set message-related settings, which need to be expanded to use
312 protected function extractMessagesDirs( $dir, array $info ) {
313 if ( isset( $info['MessagesDirs'] ) ) {
314 foreach ( $info['MessagesDirs'] as $name => $files ) {
315 foreach ( (array)$files as $file ) {
316 $this->globals
["wgMessagesDirs"][$name][] = "$dir/$file";
323 * @param string $path
327 protected function extractCredits( $path, array $info ) {
330 'type' => isset( $info['type'] ) ?
$info['type'] : 'other',
332 foreach ( self
::$creditsAttributes as $attr ) {
333 if ( isset( $info[$attr] ) ) {
334 $credits[$attr] = $info[$attr];
338 $name = $credits['name'];
340 // If someone is loading the same thing twice, throw
341 // a nice error (T121493)
342 if ( isset( $this->credits
[$name] ) ) {
343 $firstPath = $this->credits
[$name]['path'];
344 $secondPath = $credits['path'];
345 throw new Exception( "It was attempted to load $name twice, from $firstPath and $secondPath." );
348 $this->credits
[$name] = $credits;
349 $this->globals
['wgExtensionCredits'][$credits['type']][] = $credits;
353 * Set configuration settings
354 * @todo In the future, this should be done via Config interfaces
358 protected function extractConfig( array $info ) {
359 if ( isset( $info['config'] ) ) {
360 if ( isset( $info['config']['_prefix'] ) ) {
361 $prefix = $info['config']['_prefix'];
362 unset( $info['config']['_prefix'] );
366 foreach ( $info['config'] as $key => $val ) {
367 if ( $key[0] !== '@' ) {
368 $this->globals
["$prefix$key"] = $val;
374 protected function extractParserTestFiles( $dir, array $info ) {
375 if ( isset( $info['ParserTestFiles'] ) ) {
376 foreach ( $info['ParserTestFiles'] as $path ) {
377 $this->globals
['wgParserTestFiles'][] = "$dir/$path";
383 * @param string $path
384 * @param string $name
385 * @param array $value
386 * @param array &$array
387 * @throws InvalidArgumentException
389 protected function storeToArray( $path, $name, $value, &$array ) {
390 if ( !is_array( $value ) ) {
391 throw new InvalidArgumentException( "The value for '$name' should be an array (from $path)" );
393 if ( isset( $array[$name] ) ) {
394 $array[$name] = array_merge_recursive( $array[$name], $value );
396 $array[$name] = $value;
400 public function getExtraAutoloaderPaths( $dir, array $info ) {
402 if ( isset( $info['load_composer_autoloader'] ) && $info['load_composer_autoloader'] === true ) {
403 $path = "$dir/vendor/autoload.php";
404 if ( file_exists( $path ) ) {