26058c9b68b39ce79be58ad6f23370b3891e7fc6
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 'CentralIdLookupProviders',
32 'ExtensionEntryPointListFiles',
54 * Mapping of global settings to their specific merge strategies.
56 * @see ExtensionRegistry::exportExtractedData
57 * @see getExtractedInfo
60 protected static $mergeStrategies = [
61 'wgGroupPermissions' => 'array_plus_2d',
62 'wgRevokePermissions' => 'array_plus_2d',
63 'wgHooks' => 'array_merge_recursive',
64 'wgExtensionCredits' => 'array_merge_recursive',
65 'wgExtraGenderNamespaces' => 'array_plus',
66 'wgNamespacesWithSubpages' => 'array_plus',
67 'wgNamespaceContentModels' => 'array_plus',
68 'wgNamespaceProtection' => 'array_plus',
69 'wgCapitalLinkOverrides' => 'array_plus',
70 'wgRateLimits' => 'array_plus_2d',
74 * Keys that are part of the extension credits
78 protected static $creditsAttributes = [
90 * Things that are not 'attributes', but are not in
91 * $globalSettings or $creditsAttributes.
95 protected static $notAttributes = [
99 'ResourceFileModulePaths',
101 'ResourceModuleSkinStyles',
102 'ExtensionMessagesFiles',
109 'load_composer_autoloader',
113 * Stuff that is going to be set to $GLOBALS
115 * Some keys are pre-set to arrays so we can += to them
119 protected $globals = [
120 'wgExtensionMessagesFiles' => [],
121 'wgMessagesDirs' => [],
125 * Things that should be define()'d
129 protected $defines = [];
132 * Things to be called once registration of these extensions are done
136 protected $callbacks = [];
141 protected $credits = [];
144 * Any thing else in the $info that hasn't
145 * already been processed
149 protected $attributes = [];
152 * @param string $path
154 * @param int $version manifest_version for info
157 public function extractInfo( $path, array $info, $version ) {
158 $this->extractConfig( $info );
159 $this->extractHooks( $info );
160 $dir = dirname( $path );
161 $this->extractExtensionMessagesFiles( $dir, $info );
162 $this->extractMessagesDirs( $dir, $info );
163 $this->extractNamespaces( $info );
164 $this->extractResourceLoaderModules( $dir, $info );
165 $this->extractParserTestFiles( $dir, $info );
166 if ( isset( $info['callback'] ) ) {
167 $this->callbacks
[] = $info['callback'];
170 $this->extractCredits( $path, $info );
171 foreach ( $info as $key => $val ) {
172 if ( in_array( $key, self
::$globalSettings ) ) {
173 $this->storeToArray( $path, "wg$key", $val, $this->globals
);
174 // Ignore anything that starts with a @
175 } elseif ( $key[0] !== '@' && !in_array( $key, self
::$notAttributes )
176 && !in_array( $key, self
::$creditsAttributes )
178 $this->storeToArray( $path, $key, $val, $this->attributes
);
183 public function getExtractedInfo() {
184 // Make sure the merge strategies are set
185 foreach ( $this->globals
as $key => $val ) {
186 if ( isset( self
::$mergeStrategies[$key] ) ) {
187 $this->globals
[$key][ExtensionRegistry
::MERGE_STRATEGY
] = self
::$mergeStrategies[$key];
192 'globals' => $this->globals
,
193 'defines' => $this->defines
,
194 'callbacks' => $this->callbacks
,
195 'credits' => $this->credits
,
196 'attributes' => $this->attributes
,
200 public function getRequirements( array $info ) {
202 $key = ExtensionRegistry
::MEDIAWIKI_CORE
;
203 if ( isset( $info['requires'][$key] ) ) {
204 $requirements[$key] = $info['requires'][$key];
207 return $requirements;
210 protected function extractHooks( array $info ) {
211 if ( isset( $info['Hooks'] ) ) {
212 foreach ( $info['Hooks'] as $name => $value ) {
213 if ( is_array( $value ) ) {
214 foreach ( $value as $callback ) {
215 $this->globals
['wgHooks'][$name][] = $callback;
218 $this->globals
['wgHooks'][$name][] = $value;
225 * Register namespaces with the appropriate global settings
229 protected function extractNamespaces( array $info ) {
230 if ( isset( $info['namespaces'] ) ) {
231 foreach ( $info['namespaces'] as $ns ) {
233 $this->defines
[$ns['constant']] = $id;
234 $this->attributes
['ExtensionNamespaces'][$id] = $ns['name'];
235 if ( isset( $ns['gender'] ) ) {
236 $this->globals
['wgExtraGenderNamespaces'][$id] = $ns['gender'];
238 if ( isset( $ns['subpages'] ) && $ns['subpages'] ) {
239 $this->globals
['wgNamespacesWithSubpages'][$id] = true;
241 if ( isset( $ns['content'] ) && $ns['content'] ) {
242 $this->globals
['wgContentNamespaces'][] = $id;
244 if ( isset( $ns['defaultcontentmodel'] ) ) {
245 $this->globals
['wgNamespaceContentModels'][$id] = $ns['defaultcontentmodel'];
247 if ( isset( $ns['protection'] ) ) {
248 $this->globals
['wgNamespaceProtection'][$id] = $ns['protection'];
250 if ( isset( $ns['capitallinkoverride'] ) ) {
251 $this->globals
['wgCapitalLinkOverrides'][$id] = $ns['capitallinkoverride'];
257 protected function extractResourceLoaderModules( $dir, array $info ) {
258 $defaultPaths = isset( $info['ResourceFileModulePaths'] )
259 ?
$info['ResourceFileModulePaths']
261 if ( isset( $defaultPaths['localBasePath'] ) ) {
262 if ( $defaultPaths['localBasePath'] === '' ) {
263 // Avoid double slashes (e.g. /extensions/Example//path)
264 $defaultPaths['localBasePath'] = $dir;
266 $defaultPaths['localBasePath'] = "$dir/{$defaultPaths['localBasePath']}";
270 foreach ( [ 'ResourceModules', 'ResourceModuleSkinStyles' ] as $setting ) {
271 if ( isset( $info[$setting] ) ) {
272 foreach ( $info[$setting] as $name => $data ) {
273 if ( isset( $data['localBasePath'] ) ) {
274 if ( $data['localBasePath'] === '' ) {
275 // Avoid double slashes (e.g. /extensions/Example//path)
276 $data['localBasePath'] = $dir;
278 $data['localBasePath'] = "$dir/{$data['localBasePath']}";
281 if ( $defaultPaths ) {
282 $data +
= $defaultPaths;
284 $this->globals
["wg$setting"][$name] = $data;
290 protected function extractExtensionMessagesFiles( $dir, array $info ) {
291 if ( isset( $info['ExtensionMessagesFiles'] ) ) {
292 $this->globals
["wgExtensionMessagesFiles"] +
= array_map( function( $file ) use ( $dir ) {
294 }, $info['ExtensionMessagesFiles'] );
299 * Set message-related settings, which need to be expanded to use
305 protected function extractMessagesDirs( $dir, array $info ) {
306 if ( isset( $info['MessagesDirs'] ) ) {
307 foreach ( $info['MessagesDirs'] as $name => $files ) {
308 foreach ( (array)$files as $file ) {
309 $this->globals
["wgMessagesDirs"][$name][] = "$dir/$file";
316 * @param string $path
320 protected function extractCredits( $path, array $info ) {
323 'type' => isset( $info['type'] ) ?
$info['type'] : 'other',
325 foreach ( self
::$creditsAttributes as $attr ) {
326 if ( isset( $info[$attr] ) ) {
327 $credits[$attr] = $info[$attr];
331 $name = $credits['name'];
333 // If someone is loading the same thing twice, throw
334 // a nice error (T121493)
335 if ( isset( $this->credits
[$name] ) ) {
336 $firstPath = $this->credits
[$name]['path'];
337 $secondPath = $credits['path'];
338 throw new Exception( "It was attempted to load $name twice, from $firstPath and $secondPath." );
341 $this->credits
[$name] = $credits;
342 $this->globals
['wgExtensionCredits'][$credits['type']][] = $credits;
346 * Set configuration settings
347 * @todo In the future, this should be done via Config interfaces
351 protected function extractConfig( array $info ) {
352 if ( isset( $info['config'] ) ) {
353 if ( isset( $info['config']['_prefix'] ) ) {
354 $prefix = $info['config']['_prefix'];
355 unset( $info['config']['_prefix'] );
359 foreach ( $info['config'] as $key => $val ) {
360 if ( $key[0] !== '@' ) {
361 $this->globals
["$prefix$key"] = $val;
367 protected function extractParserTestFiles( $dir, array $info ) {
368 if ( isset( $info['ParserTestFiles'] ) ) {
369 foreach ( $info['ParserTestFiles'] as $path ) {
370 $this->globals
['wgParserTestFiles'][] = "$dir/$path";
376 * @param string $path
377 * @param string $name
378 * @param array $value
379 * @param array &$array
380 * @throws InvalidArgumentException
382 protected function storeToArray( $path, $name, $value, &$array ) {
383 if ( !is_array( $value ) ) {
384 throw new InvalidArgumentException( "The value for '$name' should be an array (from $path)" );
386 if ( isset( $array[$name] ) ) {
387 $array[$name] = array_merge_recursive( $array[$name], $value );
389 $array[$name] = $value;
393 public function getExtraAutoloaderPaths( $dir, array $info ) {
395 if ( isset( $info['load_composer_autoloader'] ) && $info['load_composer_autoloader'] === true ) {
396 $path = "$dir/vendor/autoload.php";
397 if ( file_exists( $path ) ) {