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',
115 'load_composer_autoloader',
119 * Stuff that is going to be set to $GLOBALS
121 * Some keys are pre-set to arrays so we can += to them
125 protected $globals = [
126 'wgExtensionMessagesFiles' => [],
127 'wgMessagesDirs' => [],
131 * Things that should be define()'d
135 protected $defines = [];
138 * Things to be called once registration of these extensions are done
142 protected $callbacks = [];
147 protected $credits = [];
150 * Any thing else in the $info that hasn't
151 * already been processed
155 protected $attributes = [];
158 * @param string $path
160 * @param int $version manifest_version for info
163 public function extractInfo( $path, array $info, $version ) {
164 $this->extractConfig( $info );
165 $this->extractHooks( $info );
166 $dir = dirname( $path );
167 $this->extractExtensionMessagesFiles( $dir, $info );
168 $this->extractMessagesDirs( $dir, $info );
169 $this->extractNamespaces( $info );
170 $this->extractResourceLoaderModules( $dir, $info );
171 $this->extractParserTestFiles( $dir, $info );
172 if ( isset( $info['callback'] ) ) {
173 $this->callbacks
[] = $info['callback'];
176 $this->extractCredits( $path, $info );
177 foreach ( $info as $key => $val ) {
178 if ( in_array( $key, self
::$globalSettings ) ) {
179 $this->storeToArray( $path, "wg$key", $val, $this->globals
);
180 // Ignore anything that starts with a @
181 } elseif ( $key[0] !== '@' && !in_array( $key, self
::$notAttributes )
182 && !in_array( $key, self
::$creditsAttributes )
184 $this->storeToArray( $path, $key, $val, $this->attributes
);
189 public function getExtractedInfo() {
190 // Make sure the merge strategies are set
191 foreach ( $this->globals
as $key => $val ) {
192 if ( isset( self
::$mergeStrategies[$key] ) ) {
193 $this->globals
[$key][ExtensionRegistry
::MERGE_STRATEGY
] = self
::$mergeStrategies[$key];
198 'globals' => $this->globals
,
199 'defines' => $this->defines
,
200 'callbacks' => $this->callbacks
,
201 'credits' => $this->credits
,
202 'attributes' => $this->attributes
,
206 public function getRequirements( array $info ) {
208 $key = ExtensionRegistry
::MEDIAWIKI_CORE
;
209 if ( isset( $info['requires'][$key] ) ) {
210 $requirements[$key] = $info['requires'][$key];
213 return $requirements;
216 protected function extractHooks( array $info ) {
217 if ( isset( $info['Hooks'] ) ) {
218 foreach ( $info['Hooks'] as $name => $value ) {
219 if ( is_array( $value ) ) {
220 foreach ( $value as $callback ) {
221 $this->globals
['wgHooks'][$name][] = $callback;
224 $this->globals
['wgHooks'][$name][] = $value;
231 * Register namespaces with the appropriate global settings
235 protected function extractNamespaces( array $info ) {
236 if ( isset( $info['namespaces'] ) ) {
237 foreach ( $info['namespaces'] as $ns ) {
239 $this->defines
[$ns['constant']] = $id;
240 $this->attributes
['ExtensionNamespaces'][$id] = $ns['name'];
241 if ( isset( $ns['gender'] ) ) {
242 $this->globals
['wgExtraGenderNamespaces'][$id] = $ns['gender'];
244 if ( isset( $ns['subpages'] ) && $ns['subpages'] ) {
245 $this->globals
['wgNamespacesWithSubpages'][$id] = true;
247 if ( isset( $ns['content'] ) && $ns['content'] ) {
248 $this->globals
['wgContentNamespaces'][] = $id;
250 if ( isset( $ns['defaultcontentmodel'] ) ) {
251 $this->globals
['wgNamespaceContentModels'][$id] = $ns['defaultcontentmodel'];
253 if ( isset( $ns['protection'] ) ) {
254 $this->globals
['wgNamespaceProtection'][$id] = $ns['protection'];
256 if ( isset( $ns['capitallinkoverride'] ) ) {
257 $this->globals
['wgCapitalLinkOverrides'][$id] = $ns['capitallinkoverride'];
263 protected function extractResourceLoaderModules( $dir, array $info ) {
264 $defaultPaths = isset( $info['ResourceFileModulePaths'] )
265 ?
$info['ResourceFileModulePaths']
267 if ( isset( $defaultPaths['localBasePath'] ) ) {
268 if ( $defaultPaths['localBasePath'] === '' ) {
269 // Avoid double slashes (e.g. /extensions/Example//path)
270 $defaultPaths['localBasePath'] = $dir;
272 $defaultPaths['localBasePath'] = "$dir/{$defaultPaths['localBasePath']}";
276 foreach ( [ 'ResourceModules', 'ResourceModuleSkinStyles' ] as $setting ) {
277 if ( isset( $info[$setting] ) ) {
278 foreach ( $info[$setting] as $name => $data ) {
279 if ( isset( $data['localBasePath'] ) ) {
280 if ( $data['localBasePath'] === '' ) {
281 // Avoid double slashes (e.g. /extensions/Example//path)
282 $data['localBasePath'] = $dir;
284 $data['localBasePath'] = "$dir/{$data['localBasePath']}";
287 if ( $defaultPaths ) {
288 $data +
= $defaultPaths;
290 $this->globals
["wg$setting"][$name] = $data;
296 protected function extractExtensionMessagesFiles( $dir, array $info ) {
297 if ( isset( $info['ExtensionMessagesFiles'] ) ) {
298 $this->globals
["wgExtensionMessagesFiles"] +
= array_map( function( $file ) use ( $dir ) {
300 }, $info['ExtensionMessagesFiles'] );
305 * Set message-related settings, which need to be expanded to use
311 protected function extractMessagesDirs( $dir, array $info ) {
312 if ( isset( $info['MessagesDirs'] ) ) {
313 foreach ( $info['MessagesDirs'] as $name => $files ) {
314 foreach ( (array)$files as $file ) {
315 $this->globals
["wgMessagesDirs"][$name][] = "$dir/$file";
322 * @param string $path
326 protected function extractCredits( $path, array $info ) {
329 'type' => isset( $info['type'] ) ?
$info['type'] : 'other',
331 foreach ( self
::$creditsAttributes as $attr ) {
332 if ( isset( $info[$attr] ) ) {
333 $credits[$attr] = $info[$attr];
337 $name = $credits['name'];
339 // If someone is loading the same thing twice, throw
340 // a nice error (T121493)
341 if ( isset( $this->credits
[$name] ) ) {
342 $firstPath = $this->credits
[$name]['path'];
343 $secondPath = $credits['path'];
344 throw new Exception( "It was attempted to load $name twice, from $firstPath and $secondPath." );
347 $this->credits
[$name] = $credits;
348 $this->globals
['wgExtensionCredits'][$credits['type']][] = $credits;
352 * Set configuration settings
353 * @todo In the future, this should be done via Config interfaces
357 protected function extractConfig( array $info ) {
358 if ( isset( $info['config'] ) ) {
359 if ( isset( $info['config']['_prefix'] ) ) {
360 $prefix = $info['config']['_prefix'];
361 unset( $info['config']['_prefix'] );
365 foreach ( $info['config'] as $key => $val ) {
366 if ( $key[0] !== '@' ) {
367 $this->globals
["$prefix$key"] = $val;
373 protected function extractParserTestFiles( $dir, array $info ) {
374 if ( isset( $info['ParserTestFiles'] ) ) {
375 foreach ( $info['ParserTestFiles'] as $path ) {
376 $this->globals
['wgParserTestFiles'][] = "$dir/$path";
382 * @param string $path
383 * @param string $name
384 * @param array $value
385 * @param array &$array
386 * @throws InvalidArgumentException
388 protected function storeToArray( $path, $name, $value, &$array ) {
389 if ( !is_array( $value ) ) {
390 throw new InvalidArgumentException( "The value for '$name' should be an array (from $path)" );
392 if ( isset( $array[$name] ) ) {
393 $array[$name] = array_merge_recursive( $array[$name], $value );
395 $array[$name] = $value;
399 public function getExtraAutoloaderPaths( $dir, array $info ) {
401 if ( isset( $info['load_composer_autoloader'] ) && $info['load_composer_autoloader'] === true ) {
402 $path = "$dir/vendor/autoload.php";
403 if ( file_exists( $path ) ) {