3 class ExtensionProcessor
implements Processor
{
6 * Keys that should be set to $GLOBALS
10 protected static $globalSettings = [
11 'ResourceLoaderSources',
12 'ResourceLoaderLESSVars',
18 'GrantPermissionGroups',
21 'GroupsRemoveFromSelf',
28 'AuthManagerAutoConfig',
29 'CentralIdLookupProviders',
34 'ExtensionEntryPointListFiles',
56 * Mapping of global settings to their specific merge strategies.
58 * @see ExtensionRegistry::exportExtractedData
59 * @see getExtractedInfo
62 protected static $mergeStrategies = [
63 'wgGroupPermissions' => 'array_plus_2d',
64 'wgRevokePermissions' => 'array_plus_2d',
65 'wgGrantPermissions' => 'array_plus_2d',
66 'wgHooks' => 'array_merge_recursive',
67 'wgExtensionCredits' => 'array_merge_recursive',
68 'wgExtraGenderNamespaces' => 'array_plus',
69 'wgNamespacesWithSubpages' => 'array_plus',
70 'wgNamespaceContentModels' => 'array_plus',
71 'wgNamespaceProtection' => 'array_plus',
72 'wgCapitalLinkOverrides' => 'array_plus',
73 'wgRateLimits' => 'array_plus_2d',
74 'wgAuthManagerAutoConfig' => 'array_plus_2d',
78 * Keys that are part of the extension credits
82 protected static $creditsAttributes = [
94 * Things that are not 'attributes', but are not in
95 * $globalSettings or $creditsAttributes.
99 protected static $notAttributes = [
103 'ResourceFileModulePaths',
105 'ResourceModuleSkinStyles',
106 'ExtensionMessagesFiles',
114 'load_composer_autoloader',
118 * Stuff that is going to be set to $GLOBALS
120 * Some keys are pre-set to arrays so we can += to them
124 protected $globals = [
125 'wgExtensionMessagesFiles' => [],
126 'wgMessagesDirs' => [],
130 * Things that should be define()'d
134 protected $defines = [];
137 * Things to be called once registration of these extensions are done
141 protected $callbacks = [];
146 protected $credits = [];
149 * Any thing else in the $info that hasn't
150 * already been processed
154 protected $attributes = [];
157 * @param string $path
159 * @param int $version manifest_version for info
162 public function extractInfo( $path, array $info, $version ) {
163 if ( $version === 2 ) {
164 $this->extractConfig2( $info );
167 $this->extractConfig1( $info );
169 $this->extractHooks( $info );
170 $dir = dirname( $path );
171 $this->extractExtensionMessagesFiles( $dir, $info );
172 $this->extractMessagesDirs( $dir, $info );
173 $this->extractNamespaces( $info );
174 $this->extractResourceLoaderModules( $dir, $info );
175 $this->extractParserTestFiles( $dir, $info );
176 if ( isset( $info['callback'] ) ) {
177 $this->callbacks
[] = $info['callback'];
180 $this->extractCredits( $path, $info );
181 foreach ( $info as $key => $val ) {
182 if ( in_array( $key, self
::$globalSettings ) ) {
183 $this->storeToArray( $path, "wg$key", $val, $this->globals
);
184 // Ignore anything that starts with a @
185 } elseif ( $key[0] !== '@' && !in_array( $key, self
::$notAttributes )
186 && !in_array( $key, self
::$creditsAttributes )
188 $this->storeToArray( $path, $key, $val, $this->attributes
);
193 public function getExtractedInfo() {
194 // Make sure the merge strategies are set
195 foreach ( $this->globals
as $key => $val ) {
196 if ( isset( self
::$mergeStrategies[$key] ) ) {
197 $this->globals
[$key][ExtensionRegistry
::MERGE_STRATEGY
] = self
::$mergeStrategies[$key];
202 'globals' => $this->globals
,
203 'defines' => $this->defines
,
204 'callbacks' => $this->callbacks
,
205 'credits' => $this->credits
,
206 'attributes' => $this->attributes
,
210 public function getRequirements( array $info ) {
212 $key = ExtensionRegistry
::MEDIAWIKI_CORE
;
213 if ( isset( $info['requires'][$key] ) ) {
214 $requirements[$key] = $info['requires'][$key];
217 return $requirements;
220 protected function extractHooks( array $info ) {
221 if ( isset( $info['Hooks'] ) ) {
222 foreach ( $info['Hooks'] as $name => $value ) {
223 if ( is_array( $value ) ) {
224 foreach ( $value as $callback ) {
225 $this->globals
['wgHooks'][$name][] = $callback;
228 $this->globals
['wgHooks'][$name][] = $value;
235 * Register namespaces with the appropriate global settings
239 protected function extractNamespaces( array $info ) {
240 if ( isset( $info['namespaces'] ) ) {
241 foreach ( $info['namespaces'] as $ns ) {
243 $this->defines
[$ns['constant']] = $id;
244 $this->attributes
['ExtensionNamespaces'][$id] = $ns['name'];
245 if ( isset( $ns['gender'] ) ) {
246 $this->globals
['wgExtraGenderNamespaces'][$id] = $ns['gender'];
248 if ( isset( $ns['subpages'] ) && $ns['subpages'] ) {
249 $this->globals
['wgNamespacesWithSubpages'][$id] = true;
251 if ( isset( $ns['content'] ) && $ns['content'] ) {
252 $this->globals
['wgContentNamespaces'][] = $id;
254 if ( isset( $ns['defaultcontentmodel'] ) ) {
255 $this->globals
['wgNamespaceContentModels'][$id] = $ns['defaultcontentmodel'];
257 if ( isset( $ns['protection'] ) ) {
258 $this->globals
['wgNamespaceProtection'][$id] = $ns['protection'];
260 if ( isset( $ns['capitallinkoverride'] ) ) {
261 $this->globals
['wgCapitalLinkOverrides'][$id] = $ns['capitallinkoverride'];
267 protected function extractResourceLoaderModules( $dir, array $info ) {
268 $defaultPaths = isset( $info['ResourceFileModulePaths'] )
269 ?
$info['ResourceFileModulePaths']
271 if ( isset( $defaultPaths['localBasePath'] ) ) {
272 if ( $defaultPaths['localBasePath'] === '' ) {
273 // Avoid double slashes (e.g. /extensions/Example//path)
274 $defaultPaths['localBasePath'] = $dir;
276 $defaultPaths['localBasePath'] = "$dir/{$defaultPaths['localBasePath']}";
280 foreach ( [ 'ResourceModules', 'ResourceModuleSkinStyles' ] as $setting ) {
281 if ( isset( $info[$setting] ) ) {
282 foreach ( $info[$setting] as $name => $data ) {
283 if ( isset( $data['localBasePath'] ) ) {
284 if ( $data['localBasePath'] === '' ) {
285 // Avoid double slashes (e.g. /extensions/Example//path)
286 $data['localBasePath'] = $dir;
288 $data['localBasePath'] = "$dir/{$data['localBasePath']}";
291 if ( $defaultPaths ) {
292 $data +
= $defaultPaths;
294 $this->globals
["wg$setting"][$name] = $data;
300 protected function extractExtensionMessagesFiles( $dir, array $info ) {
301 if ( isset( $info['ExtensionMessagesFiles'] ) ) {
302 $this->globals
["wgExtensionMessagesFiles"] +
= array_map( function( $file ) use ( $dir ) {
304 }, $info['ExtensionMessagesFiles'] );
309 * Set message-related settings, which need to be expanded to use
315 protected function extractMessagesDirs( $dir, array $info ) {
316 if ( isset( $info['MessagesDirs'] ) ) {
317 foreach ( $info['MessagesDirs'] as $name => $files ) {
318 foreach ( (array)$files as $file ) {
319 $this->globals
["wgMessagesDirs"][$name][] = "$dir/$file";
326 * @param string $path
330 protected function extractCredits( $path, array $info ) {
333 'type' => isset( $info['type'] ) ?
$info['type'] : 'other',
335 foreach ( self
::$creditsAttributes as $attr ) {
336 if ( isset( $info[$attr] ) ) {
337 $credits[$attr] = $info[$attr];
341 $name = $credits['name'];
343 // If someone is loading the same thing twice, throw
344 // a nice error (T121493)
345 if ( isset( $this->credits
[$name] ) ) {
346 $firstPath = $this->credits
[$name]['path'];
347 $secondPath = $credits['path'];
348 throw new Exception( "It was attempted to load $name twice, from $firstPath and $secondPath." );
351 $this->credits
[$name] = $credits;
352 $this->globals
['wgExtensionCredits'][$credits['type']][] = $credits;
356 * Set configuration settings for manifest_version == 1
357 * @todo In the future, this should be done via Config interfaces
361 protected function extractConfig1( array $info ) {
362 if ( isset( $info['config'] ) ) {
363 if ( isset( $info['config']['_prefix'] ) ) {
364 $prefix = $info['config']['_prefix'];
365 unset( $info['config']['_prefix'] );
369 foreach ( $info['config'] as $key => $val ) {
370 if ( $key[0] !== '@' ) {
371 $this->globals
["$prefix$key"] = $val;
378 * Set configuration settings for manifest_version == 2
379 * @todo In the future, this should be done via Config interfaces
383 protected function extractConfig2( array $info ) {
384 if ( isset( $info['config_prefix'] ) ) {
385 $prefix = $info['config_prefix'];
389 if ( isset( $info['config'] ) ) {
390 foreach ( $info['config'] as $key => $data ) {
391 $value = $data['value'];
392 if ( isset( $value['merge_strategy'] ) ) {
393 $value[ExtensionRegistry
::MERGE_STRATEGY
] = $value['merge_strategy'];
395 $this->globals
["$prefix$key"] = $value;
400 protected function extractParserTestFiles( $dir, array $info ) {
401 if ( isset( $info['ParserTestFiles'] ) ) {
402 foreach ( $info['ParserTestFiles'] as $path ) {
403 $this->globals
['wgParserTestFiles'][] = "$dir/$path";
409 * @param string $path
410 * @param string $name
411 * @param array $value
412 * @param array &$array
413 * @throws InvalidArgumentException
415 protected function storeToArray( $path, $name, $value, &$array ) {
416 if ( !is_array( $value ) ) {
417 throw new InvalidArgumentException( "The value for '$name' should be an array (from $path)" );
419 if ( isset( $array[$name] ) ) {
420 $array[$name] = array_merge_recursive( $array[$name], $value );
422 $array[$name] = $value;
426 public function getExtraAutoloaderPaths( $dir, array $info ) {
428 if ( isset( $info['load_composer_autoloader'] ) && $info['load_composer_autoloader'] === true ) {
429 $path = "$dir/vendor/autoload.php";
430 if ( file_exists( $path ) ) {