f977124cd4da3a00835ee31457d128d4a0353ab6
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',
53 * Mapping of global settings to their specific merge strategies.
55 * @see ExtensionRegistry::exportExtractedData
56 * @see getExtractedInfo
59 protected static $mergeStrategies = [
60 'wgGroupPermissions' => 'array_plus_2d',
61 'wgRevokePermissions' => 'array_plus_2d',
62 'wgHooks' => 'array_merge_recursive',
63 'wgExtensionCredits' => 'array_merge_recursive',
64 'wgExtraGenderNamespaces' => 'array_plus',
65 'wgNamespacesWithSubpages' => 'array_plus',
66 'wgNamespaceContentModels' => 'array_plus',
67 'wgNamespaceProtection' => 'array_plus',
68 'wgCapitalLinkOverrides' => 'array_plus',
69 'wgRateLimits' => 'array_plus_2d',
73 * Keys that are part of the extension credits
77 protected static $creditsAttributes = [
89 * Things that are not 'attributes', but are not in
90 * $globalSettings or $creditsAttributes.
94 protected static $notAttributes = [
98 'ResourceFileModulePaths',
100 'ResourceModuleSkinStyles',
101 'ExtensionMessagesFiles',
108 'load_composer_autoloader',
112 * Stuff that is going to be set to $GLOBALS
114 * Some keys are pre-set to arrays so we can += to them
118 protected $globals = [
119 'wgExtensionMessagesFiles' => [],
120 'wgMessagesDirs' => [],
124 * Things that should be define()'d
128 protected $defines = [];
131 * Things to be called once registration of these extensions are done
135 protected $callbacks = [];
140 protected $credits = [];
143 * Any thing else in the $info that hasn't
144 * already been processed
148 protected $attributes = [];
151 * @param string $path
153 * @param int $version manifest_version for info
156 public function extractInfo( $path, array $info, $version ) {
157 $this->extractConfig( $info );
158 $this->extractHooks( $info );
159 $dir = dirname( $path );
160 $this->extractExtensionMessagesFiles( $dir, $info );
161 $this->extractMessagesDirs( $dir, $info );
162 $this->extractNamespaces( $info );
163 $this->extractResourceLoaderModules( $dir, $info );
164 $this->extractParserTestFiles( $dir, $info );
165 if ( isset( $info['callback'] ) ) {
166 $this->callbacks
[] = $info['callback'];
169 $this->extractCredits( $path, $info );
170 foreach ( $info as $key => $val ) {
171 if ( in_array( $key, self
::$globalSettings ) ) {
172 $this->storeToArray( $path, "wg$key", $val, $this->globals
);
173 // Ignore anything that starts with a @
174 } elseif ( $key[0] !== '@' && !in_array( $key, self
::$notAttributes )
175 && !in_array( $key, self
::$creditsAttributes )
177 $this->storeToArray( $path, $key, $val, $this->attributes
);
182 public function getExtractedInfo() {
183 // Make sure the merge strategies are set
184 foreach ( $this->globals
as $key => $val ) {
185 if ( isset( self
::$mergeStrategies[$key] ) ) {
186 $this->globals
[$key][ExtensionRegistry
::MERGE_STRATEGY
] = self
::$mergeStrategies[$key];
191 'globals' => $this->globals
,
192 'defines' => $this->defines
,
193 'callbacks' => $this->callbacks
,
194 'credits' => $this->credits
,
195 'attributes' => $this->attributes
,
199 public function getRequirements( array $info ) {
201 $key = ExtensionRegistry
::MEDIAWIKI_CORE
;
202 if ( isset( $info['requires'][$key] ) ) {
203 $requirements[$key] = $info['requires'][$key];
206 return $requirements;
209 protected function extractHooks( array $info ) {
210 if ( isset( $info['Hooks'] ) ) {
211 foreach ( $info['Hooks'] as $name => $value ) {
212 foreach ( (array)$value as $callback ) {
213 $this->globals
['wgHooks'][$name][] = $callback;
220 * Register namespaces with the appropriate global settings
224 protected function extractNamespaces( array $info ) {
225 if ( isset( $info['namespaces'] ) ) {
226 foreach ( $info['namespaces'] as $ns ) {
228 $this->defines
[$ns['constant']] = $id;
229 $this->attributes
['ExtensionNamespaces'][$id] = $ns['name'];
230 if ( isset( $ns['gender'] ) ) {
231 $this->globals
['wgExtraGenderNamespaces'][$id] = $ns['gender'];
233 if ( isset( $ns['subpages'] ) && $ns['subpages'] ) {
234 $this->globals
['wgNamespacesWithSubpages'][$id] = true;
236 if ( isset( $ns['content'] ) && $ns['content'] ) {
237 $this->globals
['wgContentNamespaces'][] = $id;
239 if ( isset( $ns['defaultcontentmodel'] ) ) {
240 $this->globals
['wgNamespaceContentModels'][$id] = $ns['defaultcontentmodel'];
242 if ( isset( $ns['protection'] ) ) {
243 $this->globals
['wgNamespaceProtection'][$id] = $ns['protection'];
245 if ( isset( $ns['capitallinkoverride'] ) ) {
246 $this->globals
['wgCapitalLinkOverrides'][$id] = $ns['capitallinkoverride'];
252 protected function extractResourceLoaderModules( $dir, array $info ) {
253 $defaultPaths = isset( $info['ResourceFileModulePaths'] )
254 ?
$info['ResourceFileModulePaths']
256 if ( isset( $defaultPaths['localBasePath'] ) ) {
257 if ( $defaultPaths['localBasePath'] === '' ) {
258 // Avoid double slashes (e.g. /extensions/Example//path)
259 $defaultPaths['localBasePath'] = $dir;
261 $defaultPaths['localBasePath'] = "$dir/{$defaultPaths['localBasePath']}";
265 foreach ( [ 'ResourceModules', 'ResourceModuleSkinStyles' ] as $setting ) {
266 if ( isset( $info[$setting] ) ) {
267 foreach ( $info[$setting] as $name => $data ) {
268 if ( isset( $data['localBasePath'] ) ) {
269 if ( $data['localBasePath'] === '' ) {
270 // Avoid double slashes (e.g. /extensions/Example//path)
271 $data['localBasePath'] = $dir;
273 $data['localBasePath'] = "$dir/{$data['localBasePath']}";
276 if ( $defaultPaths ) {
277 $data +
= $defaultPaths;
279 $this->globals
["wg$setting"][$name] = $data;
285 protected function extractExtensionMessagesFiles( $dir, array $info ) {
286 if ( isset( $info['ExtensionMessagesFiles'] ) ) {
287 $this->globals
["wgExtensionMessagesFiles"] +
= array_map( function( $file ) use ( $dir ) {
289 }, $info['ExtensionMessagesFiles'] );
294 * Set message-related settings, which need to be expanded to use
300 protected function extractMessagesDirs( $dir, array $info ) {
301 if ( isset( $info['MessagesDirs'] ) ) {
302 foreach ( $info['MessagesDirs'] as $name => $files ) {
303 foreach ( (array)$files as $file ) {
304 $this->globals
["wgMessagesDirs"][$name][] = "$dir/$file";
311 * @param string $path
315 protected function extractCredits( $path, array $info ) {
318 'type' => isset( $info['type'] ) ?
$info['type'] : 'other',
320 foreach ( self
::$creditsAttributes as $attr ) {
321 if ( isset( $info[$attr] ) ) {
322 $credits[$attr] = $info[$attr];
326 $name = $credits['name'];
328 // If someone is loading the same thing twice, throw
329 // a nice error (T121493)
330 if ( isset( $this->credits
[$name] ) ) {
331 $firstPath = $this->credits
[$name]['path'];
332 $secondPath = $credits['path'];
333 throw new Exception( "It was attempted to load $name twice, from $firstPath and $secondPath." );
336 $this->credits
[$name] = $credits;
337 $this->globals
['wgExtensionCredits'][$credits['type']][] = $credits;
341 * Set configuration settings
342 * @todo In the future, this should be done via Config interfaces
346 protected function extractConfig( array $info ) {
347 if ( isset( $info['config'] ) ) {
348 if ( isset( $info['config']['_prefix'] ) ) {
349 $prefix = $info['config']['_prefix'];
350 unset( $info['config']['_prefix'] );
354 foreach ( $info['config'] as $key => $val ) {
355 if ( $key[0] !== '@' ) {
356 $this->globals
["$prefix$key"] = $val;
362 protected function extractParserTestFiles( $dir, array $info ) {
363 if ( isset( $info['ParserTestFiles'] ) ) {
364 foreach ( $info['ParserTestFiles'] as $path ) {
365 $this->globals
['wgParserTestFiles'][] = "$dir/$path";
371 * @param string $path
372 * @param string $name
373 * @param array $value
374 * @param array &$array
375 * @throws InvalidArgumentException
377 protected function storeToArray( $path, $name, $value, &$array ) {
378 if ( !is_array( $value ) ) {
379 throw new InvalidArgumentException( "The value for '$name' should be an array (from $path)" );
381 if ( isset( $array[$name] ) ) {
382 $array[$name] = array_merge_recursive( $array[$name], $value );
384 $array[$name] = $value;
388 public function getExtraAutoloaderPaths( $dir, array $info ) {
390 if ( isset( $info['load_composer_autoloader'] ) && $info['load_composer_autoloader'] === true ) {
391 $path = "$dir/vendor/autoload.php";
392 if ( file_exists( $path ) ) {