Expose $wgMaxArticleSize in siteinfo query api
[mediawiki.git] / includes / registration / ExtensionProcessor.php
blobf1f7c2e18ec5c3a38d92c35a7391e489c300d512
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 'ResourceLoaderSources',
12 'ResourceLoaderLESSVars',
13 'ResourceLoaderLESSImportPaths',
14 'DefaultUserOptions',
15 'HiddenPrefs',
16 'GroupPermissions',
17 'RevokePermissions',
18 'GrantPermissions',
19 'GrantPermissionGroups',
20 'ImplicitGroups',
21 'GroupsAddToSelf',
22 'GroupsRemoveFromSelf',
23 'AddGroups',
24 'RemoveGroups',
25 'AvailableRights',
26 'ContentHandlers',
27 'ConfigRegistry',
28 'SessionProviders',
29 'AuthManagerAutoConfig',
30 'CentralIdLookupProviders',
31 'ChangeCredentialsBlacklist',
32 'RemoveCredentialsBlacklist',
33 'RateLimits',
34 'RecentChangesFlags',
35 'MediaHandlers',
36 'ExtensionFunctions',
37 'ExtensionEntryPointListFiles',
38 'SpecialPages',
39 'JobClasses',
40 'LogTypes',
41 'LogRestrictions',
42 'FilterLogTypes',
43 'ActionFilteredLogs',
44 'LogNames',
45 'LogHeaders',
46 'LogActions',
47 'LogActionsHandlers',
48 'Actions',
49 'APIModules',
50 'APIFormatModules',
51 'APIMetaModules',
52 'APIPropModules',
53 'APIListModules',
54 'ValidSkinNames',
55 'FeedClasses',
58 /**
59 * Mapping of global settings to their specific merge strategies.
61 * @see ExtensionRegistry::exportExtractedData
62 * @see getExtractedInfo
63 * @var array
65 protected static $mergeStrategies = [
66 'wgGroupPermissions' => 'array_plus_2d',
67 'wgRevokePermissions' => 'array_plus_2d',
68 'wgGrantPermissions' => 'array_plus_2d',
69 'wgHooks' => 'array_merge_recursive',
70 'wgExtensionCredits' => 'array_merge_recursive',
71 'wgExtraGenderNamespaces' => 'array_plus',
72 'wgNamespacesWithSubpages' => 'array_plus',
73 'wgNamespaceContentModels' => 'array_plus',
74 'wgNamespaceProtection' => 'array_plus',
75 'wgCapitalLinkOverrides' => 'array_plus',
76 'wgRateLimits' => 'array_plus_2d',
77 'wgAuthManagerAutoConfig' => 'array_plus_2d',
80 /**
81 * Keys that are part of the extension credits
83 * @var array
85 protected static $creditsAttributes = [
86 'name',
87 'namemsg',
88 'author',
89 'version',
90 'url',
91 'description',
92 'descriptionmsg',
93 'license-name',
96 /**
97 * Things that are not 'attributes', but are not in
98 * $globalSettings or $creditsAttributes.
100 * @var array
102 protected static $notAttributes = [
103 'callback',
104 'Hooks',
105 'namespaces',
106 'ResourceFileModulePaths',
107 'ResourceModules',
108 'ResourceModuleSkinStyles',
109 'ExtensionMessagesFiles',
110 'MessagesDirs',
111 'type',
112 'config',
113 'ParserTestFiles',
114 'AutoloadClasses',
115 'manifest_version',
116 'load_composer_autoloader',
120 * Stuff that is going to be set to $GLOBALS
122 * Some keys are pre-set to arrays so we can += to them
124 * @var array
126 protected $globals = [
127 'wgExtensionMessagesFiles' => [],
128 'wgMessagesDirs' => [],
132 * Things that should be define()'d
134 * @var array
136 protected $defines = [];
139 * Things to be called once registration of these extensions are done
141 * @var callable[]
143 protected $callbacks = [];
146 * @var array
148 protected $credits = [];
151 * Any thing else in the $info that hasn't
152 * already been processed
154 * @var array
156 protected $attributes = [];
159 * @param string $path
160 * @param array $info
161 * @param int $version manifest_version for info
162 * @return array
164 public function extractInfo( $path, array $info, $version ) {
165 $this->extractConfig( $info );
166 $this->extractHooks( $info );
167 $dir = dirname( $path );
168 $this->extractExtensionMessagesFiles( $dir, $info );
169 $this->extractMessagesDirs( $dir, $info );
170 $this->extractNamespaces( $info );
171 $this->extractResourceLoaderModules( $dir, $info );
172 $this->extractParserTestFiles( $dir, $info );
173 if ( isset( $info['callback'] ) ) {
174 $this->callbacks[] = $info['callback'];
177 $this->extractCredits( $path, $info );
178 foreach ( $info as $key => $val ) {
179 if ( in_array( $key, self::$globalSettings ) ) {
180 $this->storeToArray( $path, "wg$key", $val, $this->globals );
181 // Ignore anything that starts with a @
182 } elseif ( $key[0] !== '@' && !in_array( $key, self::$notAttributes )
183 && !in_array( $key, self::$creditsAttributes )
185 $this->storeToArray( $path, $key, $val, $this->attributes );
190 public function getExtractedInfo() {
191 // Make sure the merge strategies are set
192 foreach ( $this->globals as $key => $val ) {
193 if ( isset( self::$mergeStrategies[$key] ) ) {
194 $this->globals[$key][ExtensionRegistry::MERGE_STRATEGY] = self::$mergeStrategies[$key];
198 return [
199 'globals' => $this->globals,
200 'defines' => $this->defines,
201 'callbacks' => $this->callbacks,
202 'credits' => $this->credits,
203 'attributes' => $this->attributes,
207 public function getRequirements( array $info ) {
208 $requirements = [];
209 $key = ExtensionRegistry::MEDIAWIKI_CORE;
210 if ( isset( $info['requires'][$key] ) ) {
211 $requirements[$key] = $info['requires'][$key];
214 return $requirements;
217 protected function extractHooks( array $info ) {
218 if ( isset( $info['Hooks'] ) ) {
219 foreach ( $info['Hooks'] as $name => $value ) {
220 if ( is_array( $value ) ) {
221 foreach ( $value as $callback ) {
222 $this->globals['wgHooks'][$name][] = $callback;
224 } else {
225 $this->globals['wgHooks'][$name][] = $value;
232 * Register namespaces with the appropriate global settings
234 * @param array $info
236 protected function extractNamespaces( array $info ) {
237 if ( isset( $info['namespaces'] ) ) {
238 foreach ( $info['namespaces'] as $ns ) {
239 $id = $ns['id'];
240 $this->defines[$ns['constant']] = $id;
241 $this->attributes['ExtensionNamespaces'][$id] = $ns['name'];
242 if ( isset( $ns['gender'] ) ) {
243 $this->globals['wgExtraGenderNamespaces'][$id] = $ns['gender'];
245 if ( isset( $ns['subpages'] ) && $ns['subpages'] ) {
246 $this->globals['wgNamespacesWithSubpages'][$id] = true;
248 if ( isset( $ns['content'] ) && $ns['content'] ) {
249 $this->globals['wgContentNamespaces'][] = $id;
251 if ( isset( $ns['defaultcontentmodel'] ) ) {
252 $this->globals['wgNamespaceContentModels'][$id] = $ns['defaultcontentmodel'];
254 if ( isset( $ns['protection'] ) ) {
255 $this->globals['wgNamespaceProtection'][$id] = $ns['protection'];
257 if ( isset( $ns['capitallinkoverride'] ) ) {
258 $this->globals['wgCapitalLinkOverrides'][$id] = $ns['capitallinkoverride'];
264 protected function extractResourceLoaderModules( $dir, array $info ) {
265 $defaultPaths = isset( $info['ResourceFileModulePaths'] )
266 ? $info['ResourceFileModulePaths']
267 : false;
268 if ( isset( $defaultPaths['localBasePath'] ) ) {
269 if ( $defaultPaths['localBasePath'] === '' ) {
270 // Avoid double slashes (e.g. /extensions/Example//path)
271 $defaultPaths['localBasePath'] = $dir;
272 } else {
273 $defaultPaths['localBasePath'] = "$dir/{$defaultPaths['localBasePath']}";
277 foreach ( [ 'ResourceModules', 'ResourceModuleSkinStyles' ] as $setting ) {
278 if ( isset( $info[$setting] ) ) {
279 foreach ( $info[$setting] as $name => $data ) {
280 if ( isset( $data['localBasePath'] ) ) {
281 if ( $data['localBasePath'] === '' ) {
282 // Avoid double slashes (e.g. /extensions/Example//path)
283 $data['localBasePath'] = $dir;
284 } else {
285 $data['localBasePath'] = "$dir/{$data['localBasePath']}";
288 if ( $defaultPaths ) {
289 $data += $defaultPaths;
291 $this->globals["wg$setting"][$name] = $data;
297 protected function extractExtensionMessagesFiles( $dir, array $info ) {
298 if ( isset( $info['ExtensionMessagesFiles'] ) ) {
299 $this->globals["wgExtensionMessagesFiles"] += array_map( function( $file ) use ( $dir ) {
300 return "$dir/$file";
301 }, $info['ExtensionMessagesFiles'] );
306 * Set message-related settings, which need to be expanded to use
307 * absolute paths
309 * @param string $dir
310 * @param array $info
312 protected function extractMessagesDirs( $dir, array $info ) {
313 if ( isset( $info['MessagesDirs'] ) ) {
314 foreach ( $info['MessagesDirs'] as $name => $files ) {
315 foreach ( (array)$files as $file ) {
316 $this->globals["wgMessagesDirs"][$name][] = "$dir/$file";
323 * @param string $path
324 * @param array $info
325 * @throws Exception
327 protected function extractCredits( $path, array $info ) {
328 $credits = [
329 'path' => $path,
330 'type' => isset( $info['type'] ) ? $info['type'] : 'other',
332 foreach ( self::$creditsAttributes as $attr ) {
333 if ( isset( $info[$attr] ) ) {
334 $credits[$attr] = $info[$attr];
338 $name = $credits['name'];
340 // If someone is loading the same thing twice, throw
341 // a nice error (T121493)
342 if ( isset( $this->credits[$name] ) ) {
343 $firstPath = $this->credits[$name]['path'];
344 $secondPath = $credits['path'];
345 throw new Exception( "It was attempted to load $name twice, from $firstPath and $secondPath." );
348 $this->credits[$name] = $credits;
349 $this->globals['wgExtensionCredits'][$credits['type']][] = $credits;
353 * Set configuration settings
354 * @todo In the future, this should be done via Config interfaces
356 * @param array $info
358 protected function extractConfig( array $info ) {
359 if ( isset( $info['config'] ) ) {
360 if ( isset( $info['config']['_prefix'] ) ) {
361 $prefix = $info['config']['_prefix'];
362 unset( $info['config']['_prefix'] );
363 } else {
364 $prefix = 'wg';
366 foreach ( $info['config'] as $key => $val ) {
367 if ( $key[0] !== '@' ) {
368 $this->globals["$prefix$key"] = $val;
374 protected function extractParserTestFiles( $dir, array $info ) {
375 if ( isset( $info['ParserTestFiles'] ) ) {
376 foreach ( $info['ParserTestFiles'] as $path ) {
377 $this->globals['wgParserTestFiles'][] = "$dir/$path";
383 * @param string $path
384 * @param string $name
385 * @param array $value
386 * @param array &$array
387 * @throws InvalidArgumentException
389 protected function storeToArray( $path, $name, $value, &$array ) {
390 if ( !is_array( $value ) ) {
391 throw new InvalidArgumentException( "The value for '$name' should be an array (from $path)" );
393 if ( isset( $array[$name] ) ) {
394 $array[$name] = array_merge_recursive( $array[$name], $value );
395 } else {
396 $array[$name] = $value;
400 public function getExtraAutoloaderPaths( $dir, array $info ) {
401 $paths = [];
402 if ( isset( $info['load_composer_autoloader'] ) && $info['load_composer_autoloader'] === true ) {
403 $path = "$dir/vendor/autoload.php";
404 if ( file_exists( $path ) ) {
405 $paths[] = $path;
408 return $paths;