3 class ExtensionProcessor
implements Processor
{
6 * Keys that should be set to $GLOBALS
10 protected static $globalSettings = array(
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 = array(
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',
71 * Keys that are part of the extension credits
75 protected static $creditsAttributes = array(
87 * Things that are not 'attributes', but are not in
88 * $globalSettings or $creditsAttributes.
92 protected static $notAttributes = array(
96 'ResourceFileModulePaths',
98 'ResourceModuleSkinStyles',
99 'ExtensionMessagesFiles',
106 'load_composer_autoloader',
110 * Stuff that is going to be set to $GLOBALS
112 * Some keys are pre-set to arrays so we can += to them
116 protected $globals = array(
117 'wgExtensionMessagesFiles' => array(),
118 'wgMessagesDirs' => array(),
122 * Things that should be define()'d
126 protected $defines = array();
129 * Things to be called once registration of these extensions are done
133 protected $callbacks = array();
138 protected $credits = array();
141 * Any thing else in the $info that hasn't
142 * already been processed
146 protected $attributes = array();
149 * @param string $path
151 * @param int $version manifest_version for info
154 public function extractInfo( $path, array $info, $version ) {
155 $this->extractConfig( $info );
156 $this->extractHooks( $info );
157 $dir = dirname( $path );
158 $this->extractExtensionMessagesFiles( $dir, $info );
159 $this->extractMessagesDirs( $dir, $info );
160 $this->extractNamespaces( $info );
161 $this->extractResourceLoaderModules( $dir, $info );
162 $this->extractParserTestFiles( $dir, $info );
163 if ( isset( $info['callback'] ) ) {
164 $this->callbacks
[] = $info['callback'];
167 $this->extractCredits( $path, $info );
168 foreach ( $info as $key => $val ) {
169 if ( in_array( $key, self
::$globalSettings ) ) {
170 $this->storeToArray( "wg$key", $val, $this->globals
);
171 // Ignore anything that starts with a @
172 } elseif ( $key[0] !== '@' && !in_array( $key, self
::$notAttributes )
173 && !in_array( $key, self
::$creditsAttributes )
175 $this->storeToArray( $key, $val, $this->attributes
);
180 public function getExtractedInfo() {
181 // Make sure the merge strategies are set
182 foreach ( $this->globals
as $key => $val ) {
183 if ( isset( self
::$mergeStrategies[$key] ) ) {
184 $this->globals
[$key][ExtensionRegistry
::MERGE_STRATEGY
] = self
::$mergeStrategies[$key];
189 'globals' => $this->globals
,
190 'defines' => $this->defines
,
191 'callbacks' => $this->callbacks
,
192 'credits' => $this->credits
,
193 'attributes' => $this->attributes
,
197 public function getRequirements( array $info ) {
198 $requirements = array();
199 $key = ExtensionRegistry
::MEDIAWIKI_CORE
;
200 if ( isset( $info['requires'][$key] ) ) {
201 $requirements[$key] = $info['requires'][$key];
204 return $requirements;
207 protected function extractHooks( array $info ) {
208 if ( isset( $info['Hooks'] ) ) {
209 foreach ( $info['Hooks'] as $name => $value ) {
210 foreach ( (array)$value as $callback ) {
211 $this->globals
['wgHooks'][$name][] = $callback;
218 * Register namespaces with the appropriate global settings
222 protected function extractNamespaces( array $info ) {
223 if ( isset( $info['namespaces'] ) ) {
224 foreach ( $info['namespaces'] as $ns ) {
226 $this->defines
[$ns['constant']] = $id;
227 $this->attributes
['ExtensionNamespaces'][$id] = $ns['name'];
228 if ( isset( $ns['gender'] ) ) {
229 $this->globals
['wgExtraGenderNamespaces'][$id] = $ns['gender'];
231 if ( isset( $ns['subpages'] ) && $ns['subpages'] ) {
232 $this->globals
['wgNamespacesWithSubpages'][$id] = true;
234 if ( isset( $ns['content'] ) && $ns['content'] ) {
235 $this->globals
['wgContentNamespaces'][] = $id;
237 if ( isset( $ns['defaultcontentmodel'] ) ) {
238 $this->globals
['wgNamespaceContentModels'][$id] = $ns['defaultcontentmodel'];
240 if ( isset( $ns['protection'] ) ) {
241 $this->globals
['wgNamespaceProtection'][$id] = $ns['protection'];
243 if ( isset( $ns['capitallinkoverride'] ) ) {
244 $this->globals
['wgCapitalLinkOverrides'][$id] = $ns['capitallinkoverride'];
250 protected function extractResourceLoaderModules( $dir, array $info ) {
251 $defaultPaths = isset( $info['ResourceFileModulePaths'] )
252 ?
$info['ResourceFileModulePaths']
254 if ( isset( $defaultPaths['localBasePath'] ) ) {
255 $defaultPaths['localBasePath'] = "$dir/{$defaultPaths['localBasePath']}";
258 foreach ( array( 'ResourceModules', 'ResourceModuleSkinStyles' ) as $setting ) {
259 if ( isset( $info[$setting] ) ) {
260 foreach ( $info[$setting] as $name => $data ) {
261 if ( isset( $data['localBasePath'] ) ) {
262 $data['localBasePath'] = "$dir/{$data['localBasePath']}";
264 if ( $defaultPaths ) {
265 $data +
= $defaultPaths;
267 $this->globals
["wg$setting"][$name] = $data;
273 protected function extractExtensionMessagesFiles( $dir, array $info ) {
274 if ( isset( $info['ExtensionMessagesFiles'] ) ) {
275 $this->globals
["wgExtensionMessagesFiles"] +
= array_map( function( $file ) use ( $dir ) {
277 }, $info['ExtensionMessagesFiles'] );
282 * Set message-related settings, which need to be expanded to use
288 protected function extractMessagesDirs( $dir, array $info ) {
289 if ( isset( $info['MessagesDirs'] ) ) {
290 foreach ( $info['MessagesDirs'] as $name => $files ) {
291 foreach ( (array)$files as $file ) {
292 $this->globals
["wgMessagesDirs"][$name][] = "$dir/$file";
299 * @param string $path
303 protected function extractCredits( $path, array $info ) {
306 'type' => isset( $info['type'] ) ?
$info['type'] : 'other',
308 foreach ( self
::$creditsAttributes as $attr ) {
309 if ( isset( $info[$attr] ) ) {
310 $credits[$attr] = $info[$attr];
314 $name = $credits['name'];
316 // If someone is loading the same thing twice, throw
317 // a nice error (T121493)
318 if ( isset( $this->credits
[$name] ) ) {
319 $firstPath = $this->credits
[$name]['path'];
320 $secondPath = $credits['path'];
321 throw new Exception( "It was attempted to load $name twice, from $firstPath and $secondPath." );
324 $this->credits
[$name] = $credits;
325 $this->globals
['wgExtensionCredits'][$credits['type']][] = $credits;
329 * Set configuration settings
330 * @todo In the future, this should be done via Config interfaces
334 protected function extractConfig( array $info ) {
335 if ( isset( $info['config'] ) ) {
336 if ( isset( $info['config']['_prefix'] ) ) {
337 $prefix = $info['config']['_prefix'];
338 unset( $info['config']['_prefix'] );
342 foreach ( $info['config'] as $key => $val ) {
343 if ( $key[0] !== '@' ) {
344 $this->globals
["$prefix$key"] = $val;
350 protected function extractParserTestFiles( $dir, array $info ) {
351 if ( isset( $info['ParserTestFiles'] ) ) {
352 foreach ( $info['ParserTestFiles'] as $path ) {
353 $this->globals
['wgParserTestFiles'][] = "$dir/$path";
359 * @param string $name
360 * @param array $value
361 * @param array &$array
362 * @throws InvalidArgumentException
364 protected function storeToArray( $name, $value, &$array ) {
365 if ( !is_array( $value ) ) {
366 throw new InvalidArgumentException( "The value for '$name' should be an array" );
368 if ( isset( $array[$name] ) ) {
369 $array[$name] = array_merge_recursive( $array[$name], $value );
371 $array[$name] = $value;
375 public function getExtraAutoloaderPaths( $dir, array $info ) {
377 if ( isset( $info['load_composer_autoloader'] ) && $info['load_composer_autoloader'] === true ) {
378 $path = "$dir/vendor/autoload.php";
379 if ( file_exists( $path ) ) {