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',
35 'ExtensionEntryPointListFiles',
57 * Mapping of global settings to their specific merge strategies.
59 * @see ExtensionRegistry::exportExtractedData
60 * @see getExtractedInfo
63 protected static $mergeStrategies = [
64 'wgGroupPermissions' => 'array_plus_2d',
65 'wgRevokePermissions' => 'array_plus_2d',
66 'wgGrantPermissions' => 'array_plus_2d',
67 'wgHooks' => 'array_merge_recursive',
68 'wgExtensionCredits' => 'array_merge_recursive',
69 'wgExtraGenderNamespaces' => 'array_plus',
70 'wgNamespacesWithSubpages' => 'array_plus',
71 'wgNamespaceContentModels' => 'array_plus',
72 'wgNamespaceProtection' => 'array_plus',
73 'wgCapitalLinkOverrides' => 'array_plus',
74 'wgRateLimits' => 'array_plus_2d',
75 'wgAuthManagerAutoConfig' => 'array_plus_2d',
79 * Keys that are part of the extension credits
83 protected static $creditsAttributes = [
95 * Things that are not 'attributes', but are not in
96 * $globalSettings or $creditsAttributes.
100 protected static $notAttributes = [
104 'ResourceFileModulePaths',
106 'ResourceModuleSkinStyles',
107 '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 $this->extractConfig( $info );
164 $this->extractHooks( $info );
165 $dir = dirname( $path );
166 $this->extractExtensionMessagesFiles( $dir, $info );
167 $this->extractMessagesDirs( $dir, $info );
168 $this->extractNamespaces( $info );
169 $this->extractResourceLoaderModules( $dir, $info );
170 $this->extractParserTestFiles( $dir, $info );
171 if ( isset( $info['callback'] ) ) {
172 $this->callbacks
[] = $info['callback'];
175 $this->extractCredits( $path, $info );
176 foreach ( $info as $key => $val ) {
177 if ( in_array( $key, self
::$globalSettings ) ) {
178 $this->storeToArray( $path, "wg$key", $val, $this->globals
);
179 // Ignore anything that starts with a @
180 } elseif ( $key[0] !== '@' && !in_array( $key, self
::$notAttributes )
181 && !in_array( $key, self
::$creditsAttributes )
183 $this->storeToArray( $path, $key, $val, $this->attributes
);
188 public function getExtractedInfo() {
189 // Make sure the merge strategies are set
190 foreach ( $this->globals
as $key => $val ) {
191 if ( isset( self
::$mergeStrategies[$key] ) ) {
192 $this->globals
[$key][ExtensionRegistry
::MERGE_STRATEGY
] = self
::$mergeStrategies[$key];
197 'globals' => $this->globals
,
198 'defines' => $this->defines
,
199 'callbacks' => $this->callbacks
,
200 'credits' => $this->credits
,
201 'attributes' => $this->attributes
,
205 public function getRequirements( array $info ) {
207 $key = ExtensionRegistry
::MEDIAWIKI_CORE
;
208 if ( isset( $info['requires'][$key] ) ) {
209 $requirements[$key] = $info['requires'][$key];
212 return $requirements;
215 protected function extractHooks( array $info ) {
216 if ( isset( $info['Hooks'] ) ) {
217 foreach ( $info['Hooks'] as $name => $value ) {
218 if ( is_array( $value ) ) {
219 foreach ( $value as $callback ) {
220 $this->globals
['wgHooks'][$name][] = $callback;
223 $this->globals
['wgHooks'][$name][] = $value;
230 * Register namespaces with the appropriate global settings
234 protected function extractNamespaces( array $info ) {
235 if ( isset( $info['namespaces'] ) ) {
236 foreach ( $info['namespaces'] as $ns ) {
238 $this->defines
[$ns['constant']] = $id;
239 $this->attributes
['ExtensionNamespaces'][$id] = $ns['name'];
240 if ( isset( $ns['gender'] ) ) {
241 $this->globals
['wgExtraGenderNamespaces'][$id] = $ns['gender'];
243 if ( isset( $ns['subpages'] ) && $ns['subpages'] ) {
244 $this->globals
['wgNamespacesWithSubpages'][$id] = true;
246 if ( isset( $ns['content'] ) && $ns['content'] ) {
247 $this->globals
['wgContentNamespaces'][] = $id;
249 if ( isset( $ns['defaultcontentmodel'] ) ) {
250 $this->globals
['wgNamespaceContentModels'][$id] = $ns['defaultcontentmodel'];
252 if ( isset( $ns['protection'] ) ) {
253 $this->globals
['wgNamespaceProtection'][$id] = $ns['protection'];
255 if ( isset( $ns['capitallinkoverride'] ) ) {
256 $this->globals
['wgCapitalLinkOverrides'][$id] = $ns['capitallinkoverride'];
262 protected function extractResourceLoaderModules( $dir, array $info ) {
263 $defaultPaths = isset( $info['ResourceFileModulePaths'] )
264 ?
$info['ResourceFileModulePaths']
266 if ( isset( $defaultPaths['localBasePath'] ) ) {
267 if ( $defaultPaths['localBasePath'] === '' ) {
268 // Avoid double slashes (e.g. /extensions/Example//path)
269 $defaultPaths['localBasePath'] = $dir;
271 $defaultPaths['localBasePath'] = "$dir/{$defaultPaths['localBasePath']}";
275 foreach ( [ 'ResourceModules', 'ResourceModuleSkinStyles' ] as $setting ) {
276 if ( isset( $info[$setting] ) ) {
277 foreach ( $info[$setting] as $name => $data ) {
278 if ( isset( $data['localBasePath'] ) ) {
279 if ( $data['localBasePath'] === '' ) {
280 // Avoid double slashes (e.g. /extensions/Example//path)
281 $data['localBasePath'] = $dir;
283 $data['localBasePath'] = "$dir/{$data['localBasePath']}";
286 if ( $defaultPaths ) {
287 $data +
= $defaultPaths;
289 $this->globals
["wg$setting"][$name] = $data;
295 protected function extractExtensionMessagesFiles( $dir, array $info ) {
296 if ( isset( $info['ExtensionMessagesFiles'] ) ) {
297 $this->globals
["wgExtensionMessagesFiles"] +
= array_map( function( $file ) use ( $dir ) {
299 }, $info['ExtensionMessagesFiles'] );
304 * Set message-related settings, which need to be expanded to use
310 protected function extractMessagesDirs( $dir, array $info ) {
311 if ( isset( $info['MessagesDirs'] ) ) {
312 foreach ( $info['MessagesDirs'] as $name => $files ) {
313 foreach ( (array)$files as $file ) {
314 $this->globals
["wgMessagesDirs"][$name][] = "$dir/$file";
321 * @param string $path
325 protected function extractCredits( $path, array $info ) {
328 'type' => isset( $info['type'] ) ?
$info['type'] : 'other',
330 foreach ( self
::$creditsAttributes as $attr ) {
331 if ( isset( $info[$attr] ) ) {
332 $credits[$attr] = $info[$attr];
336 $name = $credits['name'];
338 // If someone is loading the same thing twice, throw
339 // a nice error (T121493)
340 if ( isset( $this->credits
[$name] ) ) {
341 $firstPath = $this->credits
[$name]['path'];
342 $secondPath = $credits['path'];
343 throw new Exception( "It was attempted to load $name twice, from $firstPath and $secondPath." );
346 $this->credits
[$name] = $credits;
347 $this->globals
['wgExtensionCredits'][$credits['type']][] = $credits;
351 * Set configuration settings
352 * @todo In the future, this should be done via Config interfaces
356 protected function extractConfig( array $info ) {
357 if ( isset( $info['config'] ) ) {
358 if ( isset( $info['config']['_prefix'] ) ) {
359 $prefix = $info['config']['_prefix'];
360 unset( $info['config']['_prefix'] );
364 foreach ( $info['config'] as $key => $val ) {
365 if ( $key[0] !== '@' ) {
366 $this->globals
["$prefix$key"] = $val;
372 protected function extractParserTestFiles( $dir, array $info ) {
373 if ( isset( $info['ParserTestFiles'] ) ) {
374 foreach ( $info['ParserTestFiles'] as $path ) {
375 $this->globals
['wgParserTestFiles'][] = "$dir/$path";
381 * @param string $path
382 * @param string $name
383 * @param array $value
384 * @param array &$array
385 * @throws InvalidArgumentException
387 protected function storeToArray( $path, $name, $value, &$array ) {
388 if ( !is_array( $value ) ) {
389 throw new InvalidArgumentException( "The value for '$name' should be an array (from $path)" );
391 if ( isset( $array[$name] ) ) {
392 $array[$name] = array_merge_recursive( $array[$name], $value );
394 $array[$name] = $value;
398 public function getExtraAutoloaderPaths( $dir, array $info ) {
400 if ( isset( $info['load_composer_autoloader'] ) && $info['load_composer_autoloader'] === true ) {
401 $path = "$dir/vendor/autoload.php";
402 if ( file_exists( $path ) ) {