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',
52 * Mapping of global settings to their specific merge strategies.
54 * @see ExtensionRegistry::exportExtractedData
55 * @see getExtractedInfo
58 protected static $mergeStrategies = [
59 'wgGroupPermissions' => 'array_plus_2d',
60 'wgRevokePermissions' => 'array_plus_2d',
61 'wgHooks' => 'array_merge_recursive',
62 'wgExtensionCredits' => 'array_merge_recursive',
63 'wgExtraGenderNamespaces' => 'array_plus',
64 'wgNamespacesWithSubpages' => 'array_plus',
65 'wgNamespaceContentModels' => 'array_plus',
66 'wgNamespaceProtection' => 'array_plus',
67 'wgCapitalLinkOverrides' => 'array_plus',
68 'wgRateLimits' => 'array_plus_2d',
72 * Keys that are part of the extension credits
76 protected static $creditsAttributes = [
88 * Things that are not 'attributes', but are not in
89 * $globalSettings or $creditsAttributes.
93 protected static $notAttributes = [
97 'ResourceFileModulePaths',
99 'ResourceModuleSkinStyles',
100 'ExtensionMessagesFiles',
107 'load_composer_autoloader',
111 * Stuff that is going to be set to $GLOBALS
113 * Some keys are pre-set to arrays so we can += to them
117 protected $globals = [
118 'wgExtensionMessagesFiles' => [],
119 'wgMessagesDirs' => [],
123 * Things that should be define()'d
127 protected $defines = [];
130 * Things to be called once registration of these extensions are done
134 protected $callbacks = [];
139 protected $credits = [];
142 * Any thing else in the $info that hasn't
143 * already been processed
147 protected $attributes = [];
150 * @param string $path
152 * @param int $version manifest_version for info
155 public function extractInfo( $path, array $info, $version ) {
156 $this->extractConfig( $info );
157 $this->extractHooks( $info );
158 $dir = dirname( $path );
159 $this->extractExtensionMessagesFiles( $dir, $info );
160 $this->extractMessagesDirs( $dir, $info );
161 $this->extractNamespaces( $info );
162 $this->extractResourceLoaderModules( $dir, $info );
163 $this->extractParserTestFiles( $dir, $info );
164 if ( isset( $info['callback'] ) ) {
165 $this->callbacks
[] = $info['callback'];
168 $this->extractCredits( $path, $info );
169 foreach ( $info as $key => $val ) {
170 if ( in_array( $key, self
::$globalSettings ) ) {
171 $this->storeToArray( "wg$key", $val, $this->globals
);
172 // Ignore anything that starts with a @
173 } elseif ( $key[0] !== '@' && !in_array( $key, self
::$notAttributes )
174 && !in_array( $key, self
::$creditsAttributes )
176 $this->storeToArray( $key, $val, $this->attributes
);
181 public function getExtractedInfo() {
182 // Make sure the merge strategies are set
183 foreach ( $this->globals
as $key => $val ) {
184 if ( isset( self
::$mergeStrategies[$key] ) ) {
185 $this->globals
[$key][ExtensionRegistry
::MERGE_STRATEGY
] = self
::$mergeStrategies[$key];
190 'globals' => $this->globals
,
191 'defines' => $this->defines
,
192 'callbacks' => $this->callbacks
,
193 'credits' => $this->credits
,
194 'attributes' => $this->attributes
,
198 public function getRequirements( array $info ) {
200 $key = ExtensionRegistry
::MEDIAWIKI_CORE
;
201 if ( isset( $info['requires'][$key] ) ) {
202 $requirements[$key] = $info['requires'][$key];
205 return $requirements;
208 protected function extractHooks( array $info ) {
209 if ( isset( $info['Hooks'] ) ) {
210 foreach ( $info['Hooks'] as $name => $value ) {
211 foreach ( (array)$value as $callback ) {
212 $this->globals
['wgHooks'][$name][] = $callback;
219 * Register namespaces with the appropriate global settings
223 protected function extractNamespaces( array $info ) {
224 if ( isset( $info['namespaces'] ) ) {
225 foreach ( $info['namespaces'] as $ns ) {
227 $this->defines
[$ns['constant']] = $id;
228 $this->attributes
['ExtensionNamespaces'][$id] = $ns['name'];
229 if ( isset( $ns['gender'] ) ) {
230 $this->globals
['wgExtraGenderNamespaces'][$id] = $ns['gender'];
232 if ( isset( $ns['subpages'] ) && $ns['subpages'] ) {
233 $this->globals
['wgNamespacesWithSubpages'][$id] = true;
235 if ( isset( $ns['content'] ) && $ns['content'] ) {
236 $this->globals
['wgContentNamespaces'][] = $id;
238 if ( isset( $ns['defaultcontentmodel'] ) ) {
239 $this->globals
['wgNamespaceContentModels'][$id] = $ns['defaultcontentmodel'];
241 if ( isset( $ns['protection'] ) ) {
242 $this->globals
['wgNamespaceProtection'][$id] = $ns['protection'];
244 if ( isset( $ns['capitallinkoverride'] ) ) {
245 $this->globals
['wgCapitalLinkOverrides'][$id] = $ns['capitallinkoverride'];
251 protected function extractResourceLoaderModules( $dir, array $info ) {
252 $defaultPaths = isset( $info['ResourceFileModulePaths'] )
253 ?
$info['ResourceFileModulePaths']
255 if ( isset( $defaultPaths['localBasePath'] ) ) {
256 if ( $defaultPaths['localBasePath'] === '' ) {
257 // Avoid double slashes (e.g. /extensions/Example//path)
258 $defaultPaths['localBasePath'] = $dir;
260 $defaultPaths['localBasePath'] = "$dir/{$defaultPaths['localBasePath']}";
264 foreach ( [ 'ResourceModules', 'ResourceModuleSkinStyles' ] as $setting ) {
265 if ( isset( $info[$setting] ) ) {
266 foreach ( $info[$setting] as $name => $data ) {
267 if ( isset( $data['localBasePath'] ) ) {
268 if ( $data['localBasePath'] === '' ) {
269 // Avoid double slashes (e.g. /extensions/Example//path)
270 $data['localBasePath'] = $dir;
272 $data['localBasePath'] = "$dir/{$data['localBasePath']}";
275 if ( $defaultPaths ) {
276 $data +
= $defaultPaths;
278 $this->globals
["wg$setting"][$name] = $data;
284 protected function extractExtensionMessagesFiles( $dir, array $info ) {
285 if ( isset( $info['ExtensionMessagesFiles'] ) ) {
286 $this->globals
["wgExtensionMessagesFiles"] +
= array_map( function( $file ) use ( $dir ) {
288 }, $info['ExtensionMessagesFiles'] );
293 * Set message-related settings, which need to be expanded to use
299 protected function extractMessagesDirs( $dir, array $info ) {
300 if ( isset( $info['MessagesDirs'] ) ) {
301 foreach ( $info['MessagesDirs'] as $name => $files ) {
302 foreach ( (array)$files as $file ) {
303 $this->globals
["wgMessagesDirs"][$name][] = "$dir/$file";
310 * @param string $path
314 protected function extractCredits( $path, array $info ) {
317 'type' => isset( $info['type'] ) ?
$info['type'] : 'other',
319 foreach ( self
::$creditsAttributes as $attr ) {
320 if ( isset( $info[$attr] ) ) {
321 $credits[$attr] = $info[$attr];
325 $name = $credits['name'];
327 // If someone is loading the same thing twice, throw
328 // a nice error (T121493)
329 if ( isset( $this->credits
[$name] ) ) {
330 $firstPath = $this->credits
[$name]['path'];
331 $secondPath = $credits['path'];
332 throw new Exception( "It was attempted to load $name twice, from $firstPath and $secondPath." );
335 $this->credits
[$name] = $credits;
336 $this->globals
['wgExtensionCredits'][$credits['type']][] = $credits;
340 * Set configuration settings
341 * @todo In the future, this should be done via Config interfaces
345 protected function extractConfig( array $info ) {
346 if ( isset( $info['config'] ) ) {
347 if ( isset( $info['config']['_prefix'] ) ) {
348 $prefix = $info['config']['_prefix'];
349 unset( $info['config']['_prefix'] );
353 foreach ( $info['config'] as $key => $val ) {
354 if ( $key[0] !== '@' ) {
355 $this->globals
["$prefix$key"] = $val;
361 protected function extractParserTestFiles( $dir, array $info ) {
362 if ( isset( $info['ParserTestFiles'] ) ) {
363 foreach ( $info['ParserTestFiles'] as $path ) {
364 $this->globals
['wgParserTestFiles'][] = "$dir/$path";
370 * @param string $name
371 * @param array $value
372 * @param array &$array
373 * @throws InvalidArgumentException
375 protected function storeToArray( $name, $value, &$array ) {
376 if ( !is_array( $value ) ) {
377 throw new InvalidArgumentException( "The value for '$name' should be an array" );
379 if ( isset( $array[$name] ) ) {
380 $array[$name] = array_merge_recursive( $array[$name], $value );
382 $array[$name] = $value;
386 public function getExtraAutoloaderPaths( $dir, array $info ) {
388 if ( isset( $info['load_composer_autoloader'] ) && $info['load_composer_autoloader'] === true ) {
389 $path = "$dir/vendor/autoload.php";
390 if ( file_exists( $path ) ) {