8fa71be8559a6f5ea6f86b7bc765706a68b3eb39
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',
30 'GrantPermissionGroups',
34 'GroupsRemoveFromSelf',
48 'RemoveCredentialsBlacklist',
50 'ResourceLoaderLESSVars',
51 'ResourceLoaderSources',
59 * Mapping of global settings to their specific merge strategies.
61 * @see ExtensionRegistry::exportExtractedData
62 * @see getExtractedInfo
65 protected static $mergeStrategies = [
66 'wgAuthManagerAutoConfig' => 'array_plus_2d',
67 'wgCapitalLinkOverrides' => 'array_plus',
68 'wgExtensionCredits' => 'array_merge_recursive',
69 'wgExtraGenderNamespaces' => 'array_plus',
70 'wgGrantPermissions' => 'array_plus_2d',
71 'wgGroupPermissions' => 'array_plus_2d',
72 'wgHooks' => 'array_merge_recursive',
73 'wgNamespaceContentModels' => 'array_plus',
74 'wgNamespaceProtection' => 'array_plus',
75 'wgNamespacesWithSubpages' => 'array_plus',
76 'wgPasswordPolicy' => 'array_merge_recursive',
77 'wgRateLimits' => 'array_plus_2d',
78 'wgRevokePermissions' => 'array_plus_2d',
82 * Keys that are part of the extension credits
86 protected static $creditsAttributes = [
98 * Things that are not 'attributes', but are not in
99 * $globalSettings or $creditsAttributes.
103 protected static $notAttributes = [
107 'ResourceFileModulePaths',
109 'ResourceModuleSkinStyles',
110 'ExtensionMessagesFiles',
115 'ServiceWiringFiles',
119 'load_composer_autoloader',
123 * Stuff that is going to be set to $GLOBALS
125 * Some keys are pre-set to arrays so we can += to them
129 protected $globals = [
130 'wgExtensionMessagesFiles' => [],
131 'wgMessagesDirs' => [],
135 * Things that should be define()'d
139 protected $defines = [];
142 * Things to be called once registration of these extensions are done
146 protected $callbacks = [];
151 protected $credits = [];
154 * Any thing else in the $info that hasn't
155 * already been processed
159 protected $attributes = [];
162 * @param string $path
164 * @param int $version manifest_version for info
167 public function extractInfo( $path, array $info, $version ) {
168 $dir = dirname( $path );
169 if ( $version === 2 ) {
170 $this->extractConfig2( $info, $dir );
173 $this->extractConfig1( $info );
175 $this->extractHooks( $info );
176 $this->extractExtensionMessagesFiles( $dir, $info );
177 $this->extractMessagesDirs( $dir, $info );
178 $this->extractNamespaces( $info );
179 $this->extractResourceLoaderModules( $dir, $info );
180 $this->extractServiceWiringFiles( $dir, $info );
181 $this->extractParserTestFiles( $dir, $info );
182 if ( isset( $info['callback'] ) ) {
183 $this->callbacks
[] = $info['callback'];
186 $this->extractCredits( $path, $info );
187 foreach ( $info as $key => $val ) {
188 if ( in_array( $key, self
::$globalSettings ) ) {
189 $this->storeToArray( $path, "wg$key", $val, $this->globals
);
190 // Ignore anything that starts with a @
191 } elseif ( $key[0] !== '@' && !in_array( $key, self
::$notAttributes )
192 && !in_array( $key, self
::$creditsAttributes )
194 $this->storeToArray( $path, $key, $val, $this->attributes
);
199 public function getExtractedInfo() {
200 // Make sure the merge strategies are set
201 foreach ( $this->globals
as $key => $val ) {
202 if ( isset( self
::$mergeStrategies[$key] ) ) {
203 $this->globals
[$key][ExtensionRegistry
::MERGE_STRATEGY
] = self
::$mergeStrategies[$key];
208 'globals' => $this->globals
,
209 'defines' => $this->defines
,
210 'callbacks' => $this->callbacks
,
211 'credits' => $this->credits
,
212 'attributes' => $this->attributes
,
216 public function getRequirements( array $info ) {
218 $key = ExtensionRegistry
::MEDIAWIKI_CORE
;
219 if ( isset( $info['requires'][$key] ) ) {
220 $requirements[$key] = $info['requires'][$key];
223 return $requirements;
226 protected function extractHooks( array $info ) {
227 if ( isset( $info['Hooks'] ) ) {
228 foreach ( $info['Hooks'] as $name => $value ) {
229 if ( is_array( $value ) ) {
230 foreach ( $value as $callback ) {
231 $this->globals
['wgHooks'][$name][] = $callback;
234 $this->globals
['wgHooks'][$name][] = $value;
241 * Register namespaces with the appropriate global settings
245 protected function extractNamespaces( array $info ) {
246 if ( isset( $info['namespaces'] ) ) {
247 foreach ( $info['namespaces'] as $ns ) {
249 $this->defines
[$ns['constant']] = $id;
250 if ( !( isset( $ns['conditional'] ) && $ns['conditional'] ) ) {
251 // If it is not conditional, register it
252 $this->attributes
['ExtensionNamespaces'][$id] = $ns['name'];
254 if ( isset( $ns['gender'] ) ) {
255 $this->globals
['wgExtraGenderNamespaces'][$id] = $ns['gender'];
257 if ( isset( $ns['subpages'] ) && $ns['subpages'] ) {
258 $this->globals
['wgNamespacesWithSubpages'][$id] = true;
260 if ( isset( $ns['content'] ) && $ns['content'] ) {
261 $this->globals
['wgContentNamespaces'][] = $id;
263 if ( isset( $ns['defaultcontentmodel'] ) ) {
264 $this->globals
['wgNamespaceContentModels'][$id] = $ns['defaultcontentmodel'];
266 if ( isset( $ns['protection'] ) ) {
267 $this->globals
['wgNamespaceProtection'][$id] = $ns['protection'];
269 if ( isset( $ns['capitallinkoverride'] ) ) {
270 $this->globals
['wgCapitalLinkOverrides'][$id] = $ns['capitallinkoverride'];
276 protected function extractResourceLoaderModules( $dir, array $info ) {
277 $defaultPaths = isset( $info['ResourceFileModulePaths'] )
278 ?
$info['ResourceFileModulePaths']
280 if ( isset( $defaultPaths['localBasePath'] ) ) {
281 if ( $defaultPaths['localBasePath'] === '' ) {
282 // Avoid double slashes (e.g. /extensions/Example//path)
283 $defaultPaths['localBasePath'] = $dir;
285 $defaultPaths['localBasePath'] = "$dir/{$defaultPaths['localBasePath']}";
289 foreach ( [ 'ResourceModules', 'ResourceModuleSkinStyles' ] as $setting ) {
290 if ( isset( $info[$setting] ) ) {
291 foreach ( $info[$setting] as $name => $data ) {
292 if ( isset( $data['localBasePath'] ) ) {
293 if ( $data['localBasePath'] === '' ) {
294 // Avoid double slashes (e.g. /extensions/Example//path)
295 $data['localBasePath'] = $dir;
297 $data['localBasePath'] = "$dir/{$data['localBasePath']}";
300 if ( $defaultPaths ) {
301 $data +
= $defaultPaths;
303 $this->globals
["wg$setting"][$name] = $data;
309 protected function extractExtensionMessagesFiles( $dir, array $info ) {
310 if ( isset( $info['ExtensionMessagesFiles'] ) ) {
311 $this->globals
["wgExtensionMessagesFiles"] +
= array_map( function( $file ) use ( $dir ) {
313 }, $info['ExtensionMessagesFiles'] );
318 * Set message-related settings, which need to be expanded to use
324 protected function extractMessagesDirs( $dir, array $info ) {
325 if ( isset( $info['MessagesDirs'] ) ) {
326 foreach ( $info['MessagesDirs'] as $name => $files ) {
327 foreach ( (array)$files as $file ) {
328 $this->globals
["wgMessagesDirs"][$name][] = "$dir/$file";
335 * @param string $path
339 protected function extractCredits( $path, array $info ) {
342 'type' => isset( $info['type'] ) ?
$info['type'] : 'other',
344 foreach ( self
::$creditsAttributes as $attr ) {
345 if ( isset( $info[$attr] ) ) {
346 $credits[$attr] = $info[$attr];
350 $name = $credits['name'];
352 // If someone is loading the same thing twice, throw
353 // a nice error (T121493)
354 if ( isset( $this->credits
[$name] ) ) {
355 $firstPath = $this->credits
[$name]['path'];
356 $secondPath = $credits['path'];
357 throw new Exception( "It was attempted to load $name twice, from $firstPath and $secondPath." );
360 $this->credits
[$name] = $credits;
361 $this->globals
['wgExtensionCredits'][$credits['type']][] = $credits;
365 * Set configuration settings for manifest_version == 1
366 * @todo In the future, this should be done via Config interfaces
370 protected function extractConfig1( array $info ) {
371 if ( isset( $info['config'] ) ) {
372 if ( isset( $info['config']['_prefix'] ) ) {
373 $prefix = $info['config']['_prefix'];
374 unset( $info['config']['_prefix'] );
378 foreach ( $info['config'] as $key => $val ) {
379 if ( $key[0] !== '@' ) {
380 $this->globals
["$prefix$key"] = $val;
387 * Set configuration settings for manifest_version == 2
388 * @todo In the future, this should be done via Config interfaces
393 protected function extractConfig2( array $info, $dir ) {
394 if ( isset( $info['config_prefix'] ) ) {
395 $prefix = $info['config_prefix'];
399 if ( isset( $info['config'] ) ) {
400 foreach ( $info['config'] as $key => $data ) {
401 $value = $data['value'];
402 if ( isset( $data['merge_strategy'] ) ) {
403 $value[ExtensionRegistry
::MERGE_STRATEGY
] = $data['merge_strategy'];
405 if ( isset( $data['path'] ) && $data['path'] ) {
406 $value = "$dir/$value";
408 $this->globals
["$prefix$key"] = $value;
413 protected function extractServiceWiringFiles( $dir, array $info ) {
414 if ( isset( $info['ServiceWiringFiles'] ) ) {
415 foreach ( $info['ServiceWiringFiles'] as $path ) {
416 $this->globals
['wgServiceWiringFiles'][] = "$dir/$path";
421 protected function extractParserTestFiles( $dir, array $info ) {
422 if ( isset( $info['ParserTestFiles'] ) ) {
423 foreach ( $info['ParserTestFiles'] as $path ) {
424 $this->globals
['wgParserTestFiles'][] = "$dir/$path";
430 * @param string $path
431 * @param string $name
432 * @param array $value
433 * @param array &$array
434 * @throws InvalidArgumentException
436 protected function storeToArray( $path, $name, $value, &$array ) {
437 if ( !is_array( $value ) ) {
438 throw new InvalidArgumentException( "The value for '$name' should be an array (from $path)" );
440 if ( isset( $array[$name] ) ) {
441 $array[$name] = array_merge_recursive( $array[$name], $value );
443 $array[$name] = $value;
447 public function getExtraAutoloaderPaths( $dir, array $info ) {
449 if ( isset( $info['load_composer_autoloader'] ) && $info['load_composer_autoloader'] === true ) {
450 $path = "$dir/vendor/autoload.php";
451 if ( file_exists( $path ) ) {