Merge "Special:Upload should not crash on failing previews"
[mediawiki.git] / includes / registration / ExtensionProcessor.php
blob1212f9972cba8ce42424f2bd385ce7d71ed7d76c
1 <?php
3 class ExtensionProcessor implements Processor {
5 /**
6 * Keys that should be set to $GLOBALS
8 * @var array
9 */
10 protected static $globalSettings = [
11 'ActionFilteredLogs',
12 'Actions',
13 'AddGroups',
14 'APIFormatModules',
15 'APIListModules',
16 'APIMetaModules',
17 'APIModules',
18 'APIPropModules',
19 'AuthManagerAutoConfig',
20 'AvailableRights',
21 'CentralIdLookupProviders',
22 'ChangeCredentialsBlacklist',
23 'ConfigRegistry',
24 'ContentHandlers',
25 'DefaultUserOptions',
26 'ExtensionEntryPointListFiles',
27 'ExtensionFunctions',
28 'FeedClasses',
29 'FileExtensions',
30 'FilterLogTypes',
31 'GrantPermissionGroups',
32 'GrantPermissions',
33 'GroupPermissions',
34 'GroupsAddToSelf',
35 'GroupsRemoveFromSelf',
36 'HiddenPrefs',
37 'ImplicitGroups',
38 'JobClasses',
39 'LogActions',
40 'LogActionsHandlers',
41 'LogHeaders',
42 'LogNames',
43 'LogRestrictions',
44 'LogTypes',
45 'MediaHandlers',
46 'PasswordPolicy',
47 'RateLimits',
48 'RecentChangesFlags',
49 'RemoveCredentialsBlacklist',
50 'RemoveGroups',
51 'ResourceLoaderLESSVars',
52 'ResourceLoaderSources',
53 'RevokePermissions',
54 'SessionProviders',
55 'SpecialPages',
56 'ValidSkinNames',
59 /**
60 * Mapping of global settings to their specific merge strategies.
62 * @see ExtensionRegistry::exportExtractedData
63 * @see getExtractedInfo
64 * @var array
66 protected static $mergeStrategies = [
67 'wgAuthManagerAutoConfig' => 'array_plus_2d',
68 'wgCapitalLinkOverrides' => 'array_plus',
69 'wgExtensionCredits' => 'array_merge_recursive',
70 'wgExtraGenderNamespaces' => 'array_plus',
71 'wgGrantPermissions' => 'array_plus_2d',
72 'wgGroupPermissions' => 'array_plus_2d',
73 'wgHooks' => 'array_merge_recursive',
74 'wgNamespaceContentModels' => 'array_plus',
75 'wgNamespaceProtection' => 'array_plus',
76 'wgNamespacesWithSubpages' => 'array_plus',
77 'wgPasswordPolicy' => 'array_merge_recursive',
78 'wgRateLimits' => 'array_plus_2d',
79 'wgRevokePermissions' => 'array_plus_2d',
82 /**
83 * Keys that are part of the extension credits
85 * @var array
87 protected static $creditsAttributes = [
88 'name',
89 'namemsg',
90 'author',
91 'version',
92 'url',
93 'description',
94 'descriptionmsg',
95 'license-name',
98 /**
99 * Things that are not 'attributes', but are not in
100 * $globalSettings or $creditsAttributes.
102 * @var array
104 protected static $notAttributes = [
105 'callback',
106 'Hooks',
107 'namespaces',
108 'ResourceFileModulePaths',
109 'ResourceModules',
110 'ResourceModuleSkinStyles',
111 'ExtensionMessagesFiles',
112 'MessagesDirs',
113 'type',
114 'config',
115 'config_prefix',
116 'ServiceWiringFiles',
117 'ParserTestFiles',
118 'AutoloadClasses',
119 'manifest_version',
120 'load_composer_autoloader',
124 * Stuff that is going to be set to $GLOBALS
126 * Some keys are pre-set to arrays so we can += to them
128 * @var array
130 protected $globals = [
131 'wgExtensionMessagesFiles' => [],
132 'wgMessagesDirs' => [],
136 * Things that should be define()'d
138 * @var array
140 protected $defines = [];
143 * Things to be called once registration of these extensions are done
144 * keyed by the name of the extension that it belongs to
146 * @var callable[]
148 protected $callbacks = [];
151 * @var array
153 protected $credits = [];
156 * Any thing else in the $info that hasn't
157 * already been processed
159 * @var array
161 protected $attributes = [];
164 * @param string $path
165 * @param array $info
166 * @param int $version manifest_version for info
167 * @return array
169 public function extractInfo( $path, array $info, $version ) {
170 $dir = dirname( $path );
171 if ( $version === 2 ) {
172 $this->extractConfig2( $info, $dir );
173 } else {
174 // $version === 1
175 $this->extractConfig1( $info );
177 $this->extractHooks( $info );
178 $this->extractExtensionMessagesFiles( $dir, $info );
179 $this->extractMessagesDirs( $dir, $info );
180 $this->extractNamespaces( $info );
181 $this->extractResourceLoaderModules( $dir, $info );
182 $this->extractServiceWiringFiles( $dir, $info );
183 $this->extractParserTestFiles( $dir, $info );
184 $name = $this->extractCredits( $path, $info );
185 if ( isset( $info['callback'] ) ) {
186 $this->callbacks[$name] = $info['callback'];
189 foreach ( $info as $key => $val ) {
190 if ( in_array( $key, self::$globalSettings ) ) {
191 $this->storeToArray( $path, "wg$key", $val, $this->globals );
192 // Ignore anything that starts with a @
193 } elseif ( $key[0] !== '@' && !in_array( $key, self::$notAttributes )
194 && !in_array( $key, self::$creditsAttributes )
196 $this->storeToArray( $path, $key, $val, $this->attributes );
201 public function getExtractedInfo() {
202 // Make sure the merge strategies are set
203 foreach ( $this->globals as $key => $val ) {
204 if ( isset( self::$mergeStrategies[$key] ) ) {
205 $this->globals[$key][ExtensionRegistry::MERGE_STRATEGY] = self::$mergeStrategies[$key];
209 return [
210 'globals' => $this->globals,
211 'defines' => $this->defines,
212 'callbacks' => $this->callbacks,
213 'credits' => $this->credits,
214 'attributes' => $this->attributes,
218 public function getRequirements( array $info ) {
219 return isset( $info['requires'] ) ? $info['requires'] : [];
222 protected function extractHooks( array $info ) {
223 if ( isset( $info['Hooks'] ) ) {
224 foreach ( $info['Hooks'] as $name => $value ) {
225 if ( is_array( $value ) ) {
226 foreach ( $value as $callback ) {
227 $this->globals['wgHooks'][$name][] = $callback;
229 } else {
230 $this->globals['wgHooks'][$name][] = $value;
237 * Register namespaces with the appropriate global settings
239 * @param array $info
241 protected function extractNamespaces( array $info ) {
242 if ( isset( $info['namespaces'] ) ) {
243 foreach ( $info['namespaces'] as $ns ) {
244 $id = $ns['id'];
245 $this->defines[$ns['constant']] = $id;
246 if ( !( isset( $ns['conditional'] ) && $ns['conditional'] ) ) {
247 // If it is not conditional, register it
248 $this->attributes['ExtensionNamespaces'][$id] = $ns['name'];
250 if ( isset( $ns['gender'] ) ) {
251 $this->globals['wgExtraGenderNamespaces'][$id] = $ns['gender'];
253 if ( isset( $ns['subpages'] ) && $ns['subpages'] ) {
254 $this->globals['wgNamespacesWithSubpages'][$id] = true;
256 if ( isset( $ns['content'] ) && $ns['content'] ) {
257 $this->globals['wgContentNamespaces'][] = $id;
259 if ( isset( $ns['defaultcontentmodel'] ) ) {
260 $this->globals['wgNamespaceContentModels'][$id] = $ns['defaultcontentmodel'];
262 if ( isset( $ns['protection'] ) ) {
263 $this->globals['wgNamespaceProtection'][$id] = $ns['protection'];
265 if ( isset( $ns['capitallinkoverride'] ) ) {
266 $this->globals['wgCapitalLinkOverrides'][$id] = $ns['capitallinkoverride'];
272 protected function extractResourceLoaderModules( $dir, array $info ) {
273 $defaultPaths = isset( $info['ResourceFileModulePaths'] )
274 ? $info['ResourceFileModulePaths']
275 : false;
276 if ( isset( $defaultPaths['localBasePath'] ) ) {
277 if ( $defaultPaths['localBasePath'] === '' ) {
278 // Avoid double slashes (e.g. /extensions/Example//path)
279 $defaultPaths['localBasePath'] = $dir;
280 } else {
281 $defaultPaths['localBasePath'] = "$dir/{$defaultPaths['localBasePath']}";
285 foreach ( [ 'ResourceModules', 'ResourceModuleSkinStyles' ] as $setting ) {
286 if ( isset( $info[$setting] ) ) {
287 foreach ( $info[$setting] as $name => $data ) {
288 if ( isset( $data['localBasePath'] ) ) {
289 if ( $data['localBasePath'] === '' ) {
290 // Avoid double slashes (e.g. /extensions/Example//path)
291 $data['localBasePath'] = $dir;
292 } else {
293 $data['localBasePath'] = "$dir/{$data['localBasePath']}";
296 if ( $defaultPaths ) {
297 $data += $defaultPaths;
299 $this->globals["wg$setting"][$name] = $data;
305 protected function extractExtensionMessagesFiles( $dir, array $info ) {
306 if ( isset( $info['ExtensionMessagesFiles'] ) ) {
307 $this->globals["wgExtensionMessagesFiles"] += array_map( function( $file ) use ( $dir ) {
308 return "$dir/$file";
309 }, $info['ExtensionMessagesFiles'] );
314 * Set message-related settings, which need to be expanded to use
315 * absolute paths
317 * @param string $dir
318 * @param array $info
320 protected function extractMessagesDirs( $dir, array $info ) {
321 if ( isset( $info['MessagesDirs'] ) ) {
322 foreach ( $info['MessagesDirs'] as $name => $files ) {
323 foreach ( (array)$files as $file ) {
324 $this->globals["wgMessagesDirs"][$name][] = "$dir/$file";
331 * @param string $path
332 * @param array $info
333 * @return string Name of thing
334 * @throws Exception
336 protected function extractCredits( $path, array $info ) {
337 $credits = [
338 'path' => $path,
339 'type' => isset( $info['type'] ) ? $info['type'] : 'other',
341 foreach ( self::$creditsAttributes as $attr ) {
342 if ( isset( $info[$attr] ) ) {
343 $credits[$attr] = $info[$attr];
347 $name = $credits['name'];
349 // If someone is loading the same thing twice, throw
350 // a nice error (T121493)
351 if ( isset( $this->credits[$name] ) ) {
352 $firstPath = $this->credits[$name]['path'];
353 $secondPath = $credits['path'];
354 throw new Exception( "It was attempted to load $name twice, from $firstPath and $secondPath." );
357 $this->credits[$name] = $credits;
358 $this->globals['wgExtensionCredits'][$credits['type']][] = $credits;
360 return $name;
364 * Set configuration settings for manifest_version == 1
365 * @todo In the future, this should be done via Config interfaces
367 * @param array $info
369 protected function extractConfig1( array $info ) {
370 if ( isset( $info['config'] ) ) {
371 if ( isset( $info['config']['_prefix'] ) ) {
372 $prefix = $info['config']['_prefix'];
373 unset( $info['config']['_prefix'] );
374 } else {
375 $prefix = 'wg';
377 foreach ( $info['config'] as $key => $val ) {
378 if ( $key[0] !== '@' ) {
379 $this->globals["$prefix$key"] = $val;
386 * Set configuration settings for manifest_version == 2
387 * @todo In the future, this should be done via Config interfaces
389 * @param array $info
390 * @param string $dir
392 protected function extractConfig2( array $info, $dir ) {
393 if ( isset( $info['config_prefix'] ) ) {
394 $prefix = $info['config_prefix'];
395 } else {
396 $prefix = 'wg';
398 if ( isset( $info['config'] ) ) {
399 foreach ( $info['config'] as $key => $data ) {
400 $value = $data['value'];
401 if ( isset( $data['merge_strategy'] ) ) {
402 $value[ExtensionRegistry::MERGE_STRATEGY] = $data['merge_strategy'];
404 if ( isset( $data['path'] ) && $data['path'] ) {
405 $value = "$dir/$value";
407 $this->globals["$prefix$key"] = $value;
412 protected function extractServiceWiringFiles( $dir, array $info ) {
413 if ( isset( $info['ServiceWiringFiles'] ) ) {
414 foreach ( $info['ServiceWiringFiles'] as $path ) {
415 $this->globals['wgServiceWiringFiles'][] = "$dir/$path";
420 protected function extractParserTestFiles( $dir, array $info ) {
421 if ( isset( $info['ParserTestFiles'] ) ) {
422 foreach ( $info['ParserTestFiles'] as $path ) {
423 $this->globals['wgParserTestFiles'][] = "$dir/$path";
429 * @param string $path
430 * @param string $name
431 * @param array $value
432 * @param array &$array
433 * @throws InvalidArgumentException
435 protected function storeToArray( $path, $name, $value, &$array ) {
436 if ( !is_array( $value ) ) {
437 throw new InvalidArgumentException( "The value for '$name' should be an array (from $path)" );
439 if ( isset( $array[$name] ) ) {
440 $array[$name] = array_merge_recursive( $array[$name], $value );
441 } else {
442 $array[$name] = $value;
446 public function getExtraAutoloaderPaths( $dir, array $info ) {
447 $paths = [];
448 if ( isset( $info['load_composer_autoloader'] ) && $info['load_composer_autoloader'] === true ) {
449 $path = "$dir/vendor/autoload.php";
450 if ( file_exists( $path ) ) {
451 $paths[] = $path;
454 return $paths;