3 class ExtensionProcessor
implements Processor
{
6 * Keys that should be set to $GLOBALS
10 protected static $globalSettings = [
11 'ResourceLoaderSources',
12 'ResourceLoaderLESSVars',
13 'ResourceLoaderLESSImportPaths',
20 'GroupsRemoveFromSelf',
26 'CentralIdLookupProviders',
31 'ExtensionEntryPointListFiles',
53 * Mapping of global settings to their specific merge strategies.
55 * @see ExtensionRegistry::exportExtractedData
56 * @see getExtractedInfo
59 protected static $mergeStrategies = [
60 'wgGroupPermissions' => 'array_plus_2d',
61 'wgRevokePermissions' => 'array_plus_2d',
62 'wgHooks' => 'array_merge_recursive',
63 'wgExtensionCredits' => 'array_merge_recursive',
64 'wgExtraGenderNamespaces' => 'array_plus',
65 'wgNamespacesWithSubpages' => 'array_plus',
66 'wgNamespaceContentModels' => 'array_plus',
67 'wgNamespaceProtection' => 'array_plus',
68 'wgCapitalLinkOverrides' => 'array_plus',
69 'wgRateLimits' => 'array_plus_2d',
73 * Keys that are part of the extension credits
77 protected static $creditsAttributes = [
89 * Things that are not 'attributes', but are not in
90 * $globalSettings or $creditsAttributes.
94 protected static $notAttributes = [
98 'ResourceFileModulePaths',
100 'ResourceModuleSkinStyles',
101 'ExtensionMessagesFiles',
108 'load_composer_autoloader',
112 * Stuff that is going to be set to $GLOBALS
114 * Some keys are pre-set to arrays so we can += to them
118 protected $globals = [
119 'wgExtensionMessagesFiles' => [],
120 'wgMessagesDirs' => [],
124 * Things that should be define()'d
128 protected $defines = [];
131 * Things to be called once registration of these extensions are done
135 protected $callbacks = [];
140 protected $credits = [];
143 * Any thing else in the $info that hasn't
144 * already been processed
148 protected $attributes = [];
151 * @param string $path
153 * @param int $version manifest_version for info
156 public function extractInfo( $path, array $info, $version ) {
157 $this->extractConfig( $info );
158 $this->extractHooks( $info );
159 $dir = dirname( $path );
160 $this->extractExtensionMessagesFiles( $dir, $info );
161 $this->extractMessagesDirs( $dir, $info );
162 $this->extractNamespaces( $info );
163 $this->extractResourceLoaderModules( $dir, $info );
164 $this->extractParserTestFiles( $dir, $info );
165 if ( isset( $info['callback'] ) ) {
166 $this->callbacks
[] = $info['callback'];
169 $this->extractCredits( $path, $info );
170 foreach ( $info as $key => $val ) {
171 if ( in_array( $key, self
::$globalSettings ) ) {
172 $this->storeToArray( $path, "wg$key", $val, $this->globals
);
173 // Ignore anything that starts with a @
174 } elseif ( $key[0] !== '@' && !in_array( $key, self
::$notAttributes )
175 && !in_array( $key, self
::$creditsAttributes )
177 $this->storeToArray( $path, $key, $val, $this->attributes
);
182 public function getExtractedInfo() {
183 // Make sure the merge strategies are set
184 foreach ( $this->globals
as $key => $val ) {
185 if ( isset( self
::$mergeStrategies[$key] ) ) {
186 $this->globals
[$key][ExtensionRegistry
::MERGE_STRATEGY
] = self
::$mergeStrategies[$key];
191 'globals' => $this->globals
,
192 'defines' => $this->defines
,
193 'callbacks' => $this->callbacks
,
194 'credits' => $this->credits
,
195 'attributes' => $this->attributes
,
199 public function getRequirements( array $info ) {
201 $key = ExtensionRegistry
::MEDIAWIKI_CORE
;
202 if ( isset( $info['requires'][$key] ) ) {
203 $requirements[$key] = $info['requires'][$key];
206 return $requirements;
209 protected function extractHooks( array $info ) {
210 if ( isset( $info['Hooks'] ) ) {
211 foreach ( $info['Hooks'] as $name => $value ) {
212 if ( is_array( $value ) ) {
213 foreach ( $value as $callback ) {
214 $this->globals
['wgHooks'][$name][] = $callback;
217 $this->globals
['wgHooks'][$name][] = $value;
224 * Register namespaces with the appropriate global settings
228 protected function extractNamespaces( array $info ) {
229 if ( isset( $info['namespaces'] ) ) {
230 foreach ( $info['namespaces'] as $ns ) {
232 $this->defines
[$ns['constant']] = $id;
233 $this->attributes
['ExtensionNamespaces'][$id] = $ns['name'];
234 if ( isset( $ns['gender'] ) ) {
235 $this->globals
['wgExtraGenderNamespaces'][$id] = $ns['gender'];
237 if ( isset( $ns['subpages'] ) && $ns['subpages'] ) {
238 $this->globals
['wgNamespacesWithSubpages'][$id] = true;
240 if ( isset( $ns['content'] ) && $ns['content'] ) {
241 $this->globals
['wgContentNamespaces'][] = $id;
243 if ( isset( $ns['defaultcontentmodel'] ) ) {
244 $this->globals
['wgNamespaceContentModels'][$id] = $ns['defaultcontentmodel'];
246 if ( isset( $ns['protection'] ) ) {
247 $this->globals
['wgNamespaceProtection'][$id] = $ns['protection'];
249 if ( isset( $ns['capitallinkoverride'] ) ) {
250 $this->globals
['wgCapitalLinkOverrides'][$id] = $ns['capitallinkoverride'];
256 protected function extractResourceLoaderModules( $dir, array $info ) {
257 $defaultPaths = isset( $info['ResourceFileModulePaths'] )
258 ?
$info['ResourceFileModulePaths']
260 if ( isset( $defaultPaths['localBasePath'] ) ) {
261 if ( $defaultPaths['localBasePath'] === '' ) {
262 // Avoid double slashes (e.g. /extensions/Example//path)
263 $defaultPaths['localBasePath'] = $dir;
265 $defaultPaths['localBasePath'] = "$dir/{$defaultPaths['localBasePath']}";
269 foreach ( [ 'ResourceModules', 'ResourceModuleSkinStyles' ] as $setting ) {
270 if ( isset( $info[$setting] ) ) {
271 foreach ( $info[$setting] as $name => $data ) {
272 if ( isset( $data['localBasePath'] ) ) {
273 if ( $data['localBasePath'] === '' ) {
274 // Avoid double slashes (e.g. /extensions/Example//path)
275 $data['localBasePath'] = $dir;
277 $data['localBasePath'] = "$dir/{$data['localBasePath']}";
280 if ( $defaultPaths ) {
281 $data +
= $defaultPaths;
283 $this->globals
["wg$setting"][$name] = $data;
289 protected function extractExtensionMessagesFiles( $dir, array $info ) {
290 if ( isset( $info['ExtensionMessagesFiles'] ) ) {
291 $this->globals
["wgExtensionMessagesFiles"] +
= array_map( function( $file ) use ( $dir ) {
293 }, $info['ExtensionMessagesFiles'] );
298 * Set message-related settings, which need to be expanded to use
304 protected function extractMessagesDirs( $dir, array $info ) {
305 if ( isset( $info['MessagesDirs'] ) ) {
306 foreach ( $info['MessagesDirs'] as $name => $files ) {
307 foreach ( (array)$files as $file ) {
308 $this->globals
["wgMessagesDirs"][$name][] = "$dir/$file";
315 * @param string $path
319 protected function extractCredits( $path, array $info ) {
322 'type' => isset( $info['type'] ) ?
$info['type'] : 'other',
324 foreach ( self
::$creditsAttributes as $attr ) {
325 if ( isset( $info[$attr] ) ) {
326 $credits[$attr] = $info[$attr];
330 $name = $credits['name'];
332 // If someone is loading the same thing twice, throw
333 // a nice error (T121493)
334 if ( isset( $this->credits
[$name] ) ) {
335 $firstPath = $this->credits
[$name]['path'];
336 $secondPath = $credits['path'];
337 throw new Exception( "It was attempted to load $name twice, from $firstPath and $secondPath." );
340 $this->credits
[$name] = $credits;
341 $this->globals
['wgExtensionCredits'][$credits['type']][] = $credits;
345 * Set configuration settings
346 * @todo In the future, this should be done via Config interfaces
350 protected function extractConfig( array $info ) {
351 if ( isset( $info['config'] ) ) {
352 if ( isset( $info['config']['_prefix'] ) ) {
353 $prefix = $info['config']['_prefix'];
354 unset( $info['config']['_prefix'] );
358 foreach ( $info['config'] as $key => $val ) {
359 if ( $key[0] !== '@' ) {
360 $this->globals
["$prefix$key"] = $val;
366 protected function extractParserTestFiles( $dir, array $info ) {
367 if ( isset( $info['ParserTestFiles'] ) ) {
368 foreach ( $info['ParserTestFiles'] as $path ) {
369 $this->globals
['wgParserTestFiles'][] = "$dir/$path";
375 * @param string $path
376 * @param string $name
377 * @param array $value
378 * @param array &$array
379 * @throws InvalidArgumentException
381 protected function storeToArray( $path, $name, $value, &$array ) {
382 if ( !is_array( $value ) ) {
383 throw new InvalidArgumentException( "The value for '$name' should be an array (from $path)" );
385 if ( isset( $array[$name] ) ) {
386 $array[$name] = array_merge_recursive( $array[$name], $value );
388 $array[$name] = $value;
392 public function getExtraAutoloaderPaths( $dir, array $info ) {
394 if ( isset( $info['load_composer_autoloader'] ) && $info['load_composer_autoloader'] === true ) {
395 $path = "$dir/vendor/autoload.php";
396 if ( file_exists( $path ) ) {