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',
30 'ChangeCredentialsBlacklist',
31 'RemoveCredentialsBlacklist',
36 'ExtensionEntryPointListFiles',
58 * Mapping of global settings to their specific merge strategies.
60 * @see ExtensionRegistry::exportExtractedData
61 * @see getExtractedInfo
64 protected static $mergeStrategies = [
65 'wgGroupPermissions' => 'array_plus_2d',
66 'wgRevokePermissions' => 'array_plus_2d',
67 'wgGrantPermissions' => 'array_plus_2d',
68 'wgHooks' => 'array_merge_recursive',
69 'wgExtensionCredits' => 'array_merge_recursive',
70 'wgExtraGenderNamespaces' => 'array_plus',
71 'wgNamespacesWithSubpages' => 'array_plus',
72 'wgNamespaceContentModels' => 'array_plus',
73 'wgNamespaceProtection' => 'array_plus',
74 'wgCapitalLinkOverrides' => 'array_plus',
75 'wgRateLimits' => 'array_plus_2d',
76 'wgAuthManagerAutoConfig' => 'array_plus_2d',
80 * Keys that are part of the extension credits
84 protected static $creditsAttributes = [
96 * Things that are not 'attributes', but are not in
97 * $globalSettings or $creditsAttributes.
101 protected static $notAttributes = [
105 'ResourceFileModulePaths',
107 'ResourceModuleSkinStyles',
108 'ExtensionMessagesFiles',
113 'ServiceWiringFiles',
117 'load_composer_autoloader',
121 * Stuff that is going to be set to $GLOBALS
123 * Some keys are pre-set to arrays so we can += to them
127 protected $globals = [
128 'wgExtensionMessagesFiles' => [],
129 'wgMessagesDirs' => [],
133 * Things that should be define()'d
137 protected $defines = [];
140 * Things to be called once registration of these extensions are done
144 protected $callbacks = [];
149 protected $credits = [];
152 * Any thing else in the $info that hasn't
153 * already been processed
157 protected $attributes = [];
160 * @param string $path
162 * @param int $version manifest_version for info
165 public function extractInfo( $path, array $info, $version ) {
166 $dir = dirname( $path );
167 if ( $version === 2 ) {
168 $this->extractConfig2( $info, $dir );
171 $this->extractConfig1( $info );
173 $this->extractHooks( $info );
174 $this->extractExtensionMessagesFiles( $dir, $info );
175 $this->extractMessagesDirs( $dir, $info );
176 $this->extractNamespaces( $info );
177 $this->extractResourceLoaderModules( $dir, $info );
178 $this->extractServiceWiringFiles( $dir, $info );
179 $this->extractParserTestFiles( $dir, $info );
180 if ( isset( $info['callback'] ) ) {
181 $this->callbacks
[] = $info['callback'];
184 $this->extractCredits( $path, $info );
185 foreach ( $info as $key => $val ) {
186 if ( in_array( $key, self
::$globalSettings ) ) {
187 $this->storeToArray( $path, "wg$key", $val, $this->globals
);
188 // Ignore anything that starts with a @
189 } elseif ( $key[0] !== '@' && !in_array( $key, self
::$notAttributes )
190 && !in_array( $key, self
::$creditsAttributes )
192 $this->storeToArray( $path, $key, $val, $this->attributes
);
197 public function getExtractedInfo() {
198 // Make sure the merge strategies are set
199 foreach ( $this->globals
as $key => $val ) {
200 if ( isset( self
::$mergeStrategies[$key] ) ) {
201 $this->globals
[$key][ExtensionRegistry
::MERGE_STRATEGY
] = self
::$mergeStrategies[$key];
206 'globals' => $this->globals
,
207 'defines' => $this->defines
,
208 'callbacks' => $this->callbacks
,
209 'credits' => $this->credits
,
210 'attributes' => $this->attributes
,
214 public function getRequirements( array $info ) {
216 $key = ExtensionRegistry
::MEDIAWIKI_CORE
;
217 if ( isset( $info['requires'][$key] ) ) {
218 $requirements[$key] = $info['requires'][$key];
221 return $requirements;
224 protected function extractHooks( array $info ) {
225 if ( isset( $info['Hooks'] ) ) {
226 foreach ( $info['Hooks'] as $name => $value ) {
227 if ( is_array( $value ) ) {
228 foreach ( $value as $callback ) {
229 $this->globals
['wgHooks'][$name][] = $callback;
232 $this->globals
['wgHooks'][$name][] = $value;
239 * Register namespaces with the appropriate global settings
243 protected function extractNamespaces( array $info ) {
244 if ( isset( $info['namespaces'] ) ) {
245 foreach ( $info['namespaces'] as $ns ) {
247 $this->defines
[$ns['constant']] = $id;
248 if ( !( isset( $ns['conditional'] ) && $ns['conditional'] ) ) {
249 // If it is not conditional, register it
250 $this->attributes
['ExtensionNamespaces'][$id] = $ns['name'];
252 if ( isset( $ns['gender'] ) ) {
253 $this->globals
['wgExtraGenderNamespaces'][$id] = $ns['gender'];
255 if ( isset( $ns['subpages'] ) && $ns['subpages'] ) {
256 $this->globals
['wgNamespacesWithSubpages'][$id] = true;
258 if ( isset( $ns['content'] ) && $ns['content'] ) {
259 $this->globals
['wgContentNamespaces'][] = $id;
261 if ( isset( $ns['defaultcontentmodel'] ) ) {
262 $this->globals
['wgNamespaceContentModels'][$id] = $ns['defaultcontentmodel'];
264 if ( isset( $ns['protection'] ) ) {
265 $this->globals
['wgNamespaceProtection'][$id] = $ns['protection'];
267 if ( isset( $ns['capitallinkoverride'] ) ) {
268 $this->globals
['wgCapitalLinkOverrides'][$id] = $ns['capitallinkoverride'];
274 protected function extractResourceLoaderModules( $dir, array $info ) {
275 $defaultPaths = isset( $info['ResourceFileModulePaths'] )
276 ?
$info['ResourceFileModulePaths']
278 if ( isset( $defaultPaths['localBasePath'] ) ) {
279 if ( $defaultPaths['localBasePath'] === '' ) {
280 // Avoid double slashes (e.g. /extensions/Example//path)
281 $defaultPaths['localBasePath'] = $dir;
283 $defaultPaths['localBasePath'] = "$dir/{$defaultPaths['localBasePath']}";
287 foreach ( [ 'ResourceModules', 'ResourceModuleSkinStyles' ] as $setting ) {
288 if ( isset( $info[$setting] ) ) {
289 foreach ( $info[$setting] as $name => $data ) {
290 if ( isset( $data['localBasePath'] ) ) {
291 if ( $data['localBasePath'] === '' ) {
292 // Avoid double slashes (e.g. /extensions/Example//path)
293 $data['localBasePath'] = $dir;
295 $data['localBasePath'] = "$dir/{$data['localBasePath']}";
298 if ( $defaultPaths ) {
299 $data +
= $defaultPaths;
301 $this->globals
["wg$setting"][$name] = $data;
307 protected function extractExtensionMessagesFiles( $dir, array $info ) {
308 if ( isset( $info['ExtensionMessagesFiles'] ) ) {
309 $this->globals
["wgExtensionMessagesFiles"] +
= array_map( function( $file ) use ( $dir ) {
311 }, $info['ExtensionMessagesFiles'] );
316 * Set message-related settings, which need to be expanded to use
322 protected function extractMessagesDirs( $dir, array $info ) {
323 if ( isset( $info['MessagesDirs'] ) ) {
324 foreach ( $info['MessagesDirs'] as $name => $files ) {
325 foreach ( (array)$files as $file ) {
326 $this->globals
["wgMessagesDirs"][$name][] = "$dir/$file";
333 * @param string $path
337 protected function extractCredits( $path, array $info ) {
340 'type' => isset( $info['type'] ) ?
$info['type'] : 'other',
342 foreach ( self
::$creditsAttributes as $attr ) {
343 if ( isset( $info[$attr] ) ) {
344 $credits[$attr] = $info[$attr];
348 $name = $credits['name'];
350 // If someone is loading the same thing twice, throw
351 // a nice error (T121493)
352 if ( isset( $this->credits
[$name] ) ) {
353 $firstPath = $this->credits
[$name]['path'];
354 $secondPath = $credits['path'];
355 throw new Exception( "It was attempted to load $name twice, from $firstPath and $secondPath." );
358 $this->credits
[$name] = $credits;
359 $this->globals
['wgExtensionCredits'][$credits['type']][] = $credits;
363 * Set configuration settings for manifest_version == 1
364 * @todo In the future, this should be done via Config interfaces
368 protected function extractConfig1( array $info ) {
369 if ( isset( $info['config'] ) ) {
370 if ( isset( $info['config']['_prefix'] ) ) {
371 $prefix = $info['config']['_prefix'];
372 unset( $info['config']['_prefix'] );
376 foreach ( $info['config'] as $key => $val ) {
377 if ( $key[0] !== '@' ) {
378 $this->globals
["$prefix$key"] = $val;
385 * Set configuration settings for manifest_version == 2
386 * @todo In the future, this should be done via Config interfaces
391 protected function extractConfig2( array $info, $dir ) {
392 if ( isset( $info['config_prefix'] ) ) {
393 $prefix = $info['config_prefix'];
397 if ( isset( $info['config'] ) ) {
398 foreach ( $info['config'] as $key => $data ) {
399 $value = $data['value'];
400 if ( isset( $data['merge_strategy'] ) ) {
401 $value[ExtensionRegistry
::MERGE_STRATEGY
] = $data['merge_strategy'];
403 if ( isset( $data['path'] ) && $data['path'] ) {
404 $value = "$dir/$value";
406 $this->globals
["$prefix$key"] = $value;
411 protected function extractServiceWiringFiles( $dir, array $info ) {
412 if ( isset( $info['ServiceWiringFiles'] ) ) {
413 foreach ( $info['ServiceWiringFiles'] as $path ) {
414 $this->globals
['wgServiceWiringFiles'][] = "$dir/$path";
419 protected function extractParserTestFiles( $dir, array $info ) {
420 if ( isset( $info['ParserTestFiles'] ) ) {
421 foreach ( $info['ParserTestFiles'] as $path ) {
422 $this->globals
['wgParserTestFiles'][] = "$dir/$path";
428 * @param string $path
429 * @param string $name
430 * @param array $value
431 * @param array &$array
432 * @throws InvalidArgumentException
434 protected function storeToArray( $path, $name, $value, &$array ) {
435 if ( !is_array( $value ) ) {
436 throw new InvalidArgumentException( "The value for '$name' should be an array (from $path)" );
438 if ( isset( $array[$name] ) ) {
439 $array[$name] = array_merge_recursive( $array[$name], $value );
441 $array[$name] = $value;
445 public function getExtraAutoloaderPaths( $dir, array $info ) {
447 if ( isset( $info['load_composer_autoloader'] ) && $info['load_composer_autoloader'] === true ) {
448 $path = "$dir/vendor/autoload.php";
449 if ( file_exists( $path ) ) {