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',
27 'AuthManagerAutoConfig',
28 'CentralIdLookupProviders',
33 'ExtensionEntryPointListFiles',
55 * Mapping of global settings to their specific merge strategies.
57 * @see ExtensionRegistry::exportExtractedData
58 * @see getExtractedInfo
61 protected static $mergeStrategies = [
62 'wgGroupPermissions' => 'array_plus_2d',
63 'wgRevokePermissions' => 'array_plus_2d',
64 'wgHooks' => 'array_merge_recursive',
65 'wgExtensionCredits' => 'array_merge_recursive',
66 'wgExtraGenderNamespaces' => 'array_plus',
67 'wgNamespacesWithSubpages' => 'array_plus',
68 'wgNamespaceContentModels' => 'array_plus',
69 'wgNamespaceProtection' => 'array_plus',
70 'wgCapitalLinkOverrides' => 'array_plus',
71 'wgRateLimits' => 'array_plus_2d',
72 'wgAuthManagerAutoConfig' => 'array_plus_2d',
76 * Keys that are part of the extension credits
80 protected static $creditsAttributes = [
92 * Things that are not 'attributes', but are not in
93 * $globalSettings or $creditsAttributes.
97 protected static $notAttributes = [
101 'ResourceFileModulePaths',
103 'ResourceModuleSkinStyles',
104 'ExtensionMessagesFiles',
111 'load_composer_autoloader',
115 * Stuff that is going to be set to $GLOBALS
117 * Some keys are pre-set to arrays so we can += to them
121 protected $globals = [
122 'wgExtensionMessagesFiles' => [],
123 'wgMessagesDirs' => [],
127 * Things that should be define()'d
131 protected $defines = [];
134 * Things to be called once registration of these extensions are done
138 protected $callbacks = [];
143 protected $credits = [];
146 * Any thing else in the $info that hasn't
147 * already been processed
151 protected $attributes = [];
154 * @param string $path
156 * @param int $version manifest_version for info
159 public function extractInfo( $path, array $info, $version ) {
160 $this->extractConfig( $info );
161 $this->extractHooks( $info );
162 $dir = dirname( $path );
163 $this->extractExtensionMessagesFiles( $dir, $info );
164 $this->extractMessagesDirs( $dir, $info );
165 $this->extractNamespaces( $info );
166 $this->extractResourceLoaderModules( $dir, $info );
167 $this->extractParserTestFiles( $dir, $info );
168 if ( isset( $info['callback'] ) ) {
169 $this->callbacks
[] = $info['callback'];
172 $this->extractCredits( $path, $info );
173 foreach ( $info as $key => $val ) {
174 if ( in_array( $key, self
::$globalSettings ) ) {
175 $this->storeToArray( $path, "wg$key", $val, $this->globals
);
176 // Ignore anything that starts with a @
177 } elseif ( $key[0] !== '@' && !in_array( $key, self
::$notAttributes )
178 && !in_array( $key, self
::$creditsAttributes )
180 $this->storeToArray( $path, $key, $val, $this->attributes
);
185 public function getExtractedInfo() {
186 // Make sure the merge strategies are set
187 foreach ( $this->globals
as $key => $val ) {
188 if ( isset( self
::$mergeStrategies[$key] ) ) {
189 $this->globals
[$key][ExtensionRegistry
::MERGE_STRATEGY
] = self
::$mergeStrategies[$key];
194 'globals' => $this->globals
,
195 'defines' => $this->defines
,
196 'callbacks' => $this->callbacks
,
197 'credits' => $this->credits
,
198 'attributes' => $this->attributes
,
202 public function getRequirements( array $info ) {
204 $key = ExtensionRegistry
::MEDIAWIKI_CORE
;
205 if ( isset( $info['requires'][$key] ) ) {
206 $requirements[$key] = $info['requires'][$key];
209 return $requirements;
212 protected function extractHooks( array $info ) {
213 if ( isset( $info['Hooks'] ) ) {
214 foreach ( $info['Hooks'] as $name => $value ) {
215 if ( is_array( $value ) ) {
216 foreach ( $value as $callback ) {
217 $this->globals
['wgHooks'][$name][] = $callback;
220 $this->globals
['wgHooks'][$name][] = $value;
227 * Register namespaces with the appropriate global settings
231 protected function extractNamespaces( array $info ) {
232 if ( isset( $info['namespaces'] ) ) {
233 foreach ( $info['namespaces'] as $ns ) {
235 $this->defines
[$ns['constant']] = $id;
236 $this->attributes
['ExtensionNamespaces'][$id] = $ns['name'];
237 if ( isset( $ns['gender'] ) ) {
238 $this->globals
['wgExtraGenderNamespaces'][$id] = $ns['gender'];
240 if ( isset( $ns['subpages'] ) && $ns['subpages'] ) {
241 $this->globals
['wgNamespacesWithSubpages'][$id] = true;
243 if ( isset( $ns['content'] ) && $ns['content'] ) {
244 $this->globals
['wgContentNamespaces'][] = $id;
246 if ( isset( $ns['defaultcontentmodel'] ) ) {
247 $this->globals
['wgNamespaceContentModels'][$id] = $ns['defaultcontentmodel'];
249 if ( isset( $ns['protection'] ) ) {
250 $this->globals
['wgNamespaceProtection'][$id] = $ns['protection'];
252 if ( isset( $ns['capitallinkoverride'] ) ) {
253 $this->globals
['wgCapitalLinkOverrides'][$id] = $ns['capitallinkoverride'];
259 protected function extractResourceLoaderModules( $dir, array $info ) {
260 $defaultPaths = isset( $info['ResourceFileModulePaths'] )
261 ?
$info['ResourceFileModulePaths']
263 if ( isset( $defaultPaths['localBasePath'] ) ) {
264 if ( $defaultPaths['localBasePath'] === '' ) {
265 // Avoid double slashes (e.g. /extensions/Example//path)
266 $defaultPaths['localBasePath'] = $dir;
268 $defaultPaths['localBasePath'] = "$dir/{$defaultPaths['localBasePath']}";
272 foreach ( [ 'ResourceModules', 'ResourceModuleSkinStyles' ] as $setting ) {
273 if ( isset( $info[$setting] ) ) {
274 foreach ( $info[$setting] as $name => $data ) {
275 if ( isset( $data['localBasePath'] ) ) {
276 if ( $data['localBasePath'] === '' ) {
277 // Avoid double slashes (e.g. /extensions/Example//path)
278 $data['localBasePath'] = $dir;
280 $data['localBasePath'] = "$dir/{$data['localBasePath']}";
283 if ( $defaultPaths ) {
284 $data +
= $defaultPaths;
286 $this->globals
["wg$setting"][$name] = $data;
292 protected function extractExtensionMessagesFiles( $dir, array $info ) {
293 if ( isset( $info['ExtensionMessagesFiles'] ) ) {
294 $this->globals
["wgExtensionMessagesFiles"] +
= array_map( function( $file ) use ( $dir ) {
296 }, $info['ExtensionMessagesFiles'] );
301 * Set message-related settings, which need to be expanded to use
307 protected function extractMessagesDirs( $dir, array $info ) {
308 if ( isset( $info['MessagesDirs'] ) ) {
309 foreach ( $info['MessagesDirs'] as $name => $files ) {
310 foreach ( (array)$files as $file ) {
311 $this->globals
["wgMessagesDirs"][$name][] = "$dir/$file";
318 * @param string $path
322 protected function extractCredits( $path, array $info ) {
325 'type' => isset( $info['type'] ) ?
$info['type'] : 'other',
327 foreach ( self
::$creditsAttributes as $attr ) {
328 if ( isset( $info[$attr] ) ) {
329 $credits[$attr] = $info[$attr];
333 $name = $credits['name'];
335 // If someone is loading the same thing twice, throw
336 // a nice error (T121493)
337 if ( isset( $this->credits
[$name] ) ) {
338 $firstPath = $this->credits
[$name]['path'];
339 $secondPath = $credits['path'];
340 throw new Exception( "It was attempted to load $name twice, from $firstPath and $secondPath." );
343 $this->credits
[$name] = $credits;
344 $this->globals
['wgExtensionCredits'][$credits['type']][] = $credits;
348 * Set configuration settings
349 * @todo In the future, this should be done via Config interfaces
353 protected function extractConfig( array $info ) {
354 if ( isset( $info['config'] ) ) {
355 if ( isset( $info['config']['_prefix'] ) ) {
356 $prefix = $info['config']['_prefix'];
357 unset( $info['config']['_prefix'] );
361 foreach ( $info['config'] as $key => $val ) {
362 if ( $key[0] !== '@' ) {
363 $this->globals
["$prefix$key"] = $val;
369 protected function extractParserTestFiles( $dir, array $info ) {
370 if ( isset( $info['ParserTestFiles'] ) ) {
371 foreach ( $info['ParserTestFiles'] as $path ) {
372 $this->globals
['wgParserTestFiles'][] = "$dir/$path";
378 * @param string $path
379 * @param string $name
380 * @param array $value
381 * @param array &$array
382 * @throws InvalidArgumentException
384 protected function storeToArray( $path, $name, $value, &$array ) {
385 if ( !is_array( $value ) ) {
386 throw new InvalidArgumentException( "The value for '$name' should be an array (from $path)" );
388 if ( isset( $array[$name] ) ) {
389 $array[$name] = array_merge_recursive( $array[$name], $value );
391 $array[$name] = $value;
395 public function getExtraAutoloaderPaths( $dir, array $info ) {
397 if ( isset( $info['load_composer_autoloader'] ) && $info['load_composer_autoloader'] === true ) {
398 $path = "$dir/vendor/autoload.php";
399 if ( file_exists( $path ) ) {