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',
68 'wgRateLimits' => 'array_plus_2d',
72 * Keys that are part of the extension credits
76 protected static $creditsAttributes = array(
88 * Things that are not 'attributes', but are not in
89 * $globalSettings or $creditsAttributes.
93 protected static $notAttributes = array(
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 = array(
118 'wgExtensionMessagesFiles' => array(),
119 'wgMessagesDirs' => array(),
123 * Things that should be define()'d
127 protected $defines = array();
130 * Things to be called once registration of these extensions are done
134 protected $callbacks = array();
139 protected $credits = array();
142 * Any thing else in the $info that hasn't
143 * already been processed
147 protected $attributes = array();
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 ) {
199 $requirements = array();
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 $defaultPaths['localBasePath'] = "$dir/{$defaultPaths['localBasePath']}";
259 foreach ( array( 'ResourceModules', 'ResourceModuleSkinStyles' ) as $setting ) {
260 if ( isset( $info[$setting] ) ) {
261 foreach ( $info[$setting] as $name => $data ) {
262 if ( isset( $data['localBasePath'] ) ) {
263 $data['localBasePath'] = "$dir/{$data['localBasePath']}";
265 if ( $defaultPaths ) {
266 $data +
= $defaultPaths;
268 $this->globals
["wg$setting"][$name] = $data;
274 protected function extractExtensionMessagesFiles( $dir, array $info ) {
275 if ( isset( $info['ExtensionMessagesFiles'] ) ) {
276 $this->globals
["wgExtensionMessagesFiles"] +
= array_map( function( $file ) use ( $dir ) {
278 }, $info['ExtensionMessagesFiles'] );
283 * Set message-related settings, which need to be expanded to use
289 protected function extractMessagesDirs( $dir, array $info ) {
290 if ( isset( $info['MessagesDirs'] ) ) {
291 foreach ( $info['MessagesDirs'] as $name => $files ) {
292 foreach ( (array)$files as $file ) {
293 $this->globals
["wgMessagesDirs"][$name][] = "$dir/$file";
300 * @param string $path
304 protected function extractCredits( $path, array $info ) {
307 'type' => isset( $info['type'] ) ?
$info['type'] : 'other',
309 foreach ( self
::$creditsAttributes as $attr ) {
310 if ( isset( $info[$attr] ) ) {
311 $credits[$attr] = $info[$attr];
315 $name = $credits['name'];
317 // If someone is loading the same thing twice, throw
318 // a nice error (T121493)
319 if ( isset( $this->credits
[$name] ) ) {
320 $firstPath = $this->credits
[$name]['path'];
321 $secondPath = $credits['path'];
322 throw new Exception( "It was attempted to load $name twice, from $firstPath and $secondPath." );
325 $this->credits
[$name] = $credits;
326 $this->globals
['wgExtensionCredits'][$credits['type']][] = $credits;
330 * Set configuration settings
331 * @todo In the future, this should be done via Config interfaces
335 protected function extractConfig( array $info ) {
336 if ( isset( $info['config'] ) ) {
337 if ( isset( $info['config']['_prefix'] ) ) {
338 $prefix = $info['config']['_prefix'];
339 unset( $info['config']['_prefix'] );
343 foreach ( $info['config'] as $key => $val ) {
344 if ( $key[0] !== '@' ) {
345 $this->globals
["$prefix$key"] = $val;
351 protected function extractParserTestFiles( $dir, array $info ) {
352 if ( isset( $info['ParserTestFiles'] ) ) {
353 foreach ( $info['ParserTestFiles'] as $path ) {
354 $this->globals
['wgParserTestFiles'][] = "$dir/$path";
360 * @param string $name
361 * @param array $value
362 * @param array &$array
363 * @throws InvalidArgumentException
365 protected function storeToArray( $name, $value, &$array ) {
366 if ( !is_array( $value ) ) {
367 throw new InvalidArgumentException( "The value for '$name' should be an array" );
369 if ( isset( $array[$name] ) ) {
370 $array[$name] = array_merge_recursive( $array[$name], $value );
372 $array[$name] = $value;
376 public function getExtraAutoloaderPaths( $dir, array $info ) {
378 if ( isset( $info['load_composer_autoloader'] ) && $info['load_composer_autoloader'] === true ) {
379 $path = "$dir/vendor/autoload.php";
380 if ( file_exists( $path ) ) {