registration: Add support for $wgGrantPermissions & $wgGrantPermissionGroups
[mediawiki.git] / includes / registration / ExtensionProcessor.php
blob2205f9538f28b4ca645253a26ca5f8499fc2b2b2
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 'RateLimits',
32 'RecentChangesFlags',
33 'MediaHandlers',
34 'ExtensionFunctions',
35 'ExtensionEntryPointListFiles',
36 'SpecialPages',
37 'JobClasses',
38 'LogTypes',
39 'LogRestrictions',
40 'FilterLogTypes',
41 'ActionFilteredLogs',
42 'LogNames',
43 'LogHeaders',
44 'LogActions',
45 'LogActionsHandlers',
46 'Actions',
47 'APIModules',
48 'APIFormatModules',
49 'APIMetaModules',
50 'APIPropModules',
51 'APIListModules',
52 'ValidSkinNames',
53 'FeedClasses',
56 /**
57 * Mapping of global settings to their specific merge strategies.
59 * @see ExtensionRegistry::exportExtractedData
60 * @see getExtractedInfo
61 * @var array
63 protected static $mergeStrategies = [
64 'wgGroupPermissions' => 'array_plus_2d',
65 'wgRevokePermissions' => 'array_plus_2d',
66 'wgGrantPermissions' => 'array_plus_2d',
67 'wgHooks' => 'array_merge_recursive',
68 'wgExtensionCredits' => 'array_merge_recursive',
69 'wgExtraGenderNamespaces' => 'array_plus',
70 'wgNamespacesWithSubpages' => 'array_plus',
71 'wgNamespaceContentModels' => 'array_plus',
72 'wgNamespaceProtection' => 'array_plus',
73 'wgCapitalLinkOverrides' => 'array_plus',
74 'wgRateLimits' => 'array_plus_2d',
75 'wgAuthManagerAutoConfig' => 'array_plus_2d',
78 /**
79 * Keys that are part of the extension credits
81 * @var array
83 protected static $creditsAttributes = [
84 'name',
85 'namemsg',
86 'author',
87 'version',
88 'url',
89 'description',
90 'descriptionmsg',
91 'license-name',
94 /**
95 * Things that are not 'attributes', but are not in
96 * $globalSettings or $creditsAttributes.
98 * @var array
100 protected static $notAttributes = [
101 'callback',
102 'Hooks',
103 'namespaces',
104 'ResourceFileModulePaths',
105 'ResourceModules',
106 'ResourceModuleSkinStyles',
107 'ExtensionMessagesFiles',
108 'MessagesDirs',
109 'type',
110 'config',
111 'ParserTestFiles',
112 'AutoloadClasses',
113 'manifest_version',
114 'load_composer_autoloader',
118 * Stuff that is going to be set to $GLOBALS
120 * Some keys are pre-set to arrays so we can += to them
122 * @var array
124 protected $globals = [
125 'wgExtensionMessagesFiles' => [],
126 'wgMessagesDirs' => [],
130 * Things that should be define()'d
132 * @var array
134 protected $defines = [];
137 * Things to be called once registration of these extensions are done
139 * @var callable[]
141 protected $callbacks = [];
144 * @var array
146 protected $credits = [];
149 * Any thing else in the $info that hasn't
150 * already been processed
152 * @var array
154 protected $attributes = [];
157 * @param string $path
158 * @param array $info
159 * @param int $version manifest_version for info
160 * @return array
162 public function extractInfo( $path, array $info, $version ) {
163 $this->extractConfig( $info );
164 $this->extractHooks( $info );
165 $dir = dirname( $path );
166 $this->extractExtensionMessagesFiles( $dir, $info );
167 $this->extractMessagesDirs( $dir, $info );
168 $this->extractNamespaces( $info );
169 $this->extractResourceLoaderModules( $dir, $info );
170 $this->extractParserTestFiles( $dir, $info );
171 if ( isset( $info['callback'] ) ) {
172 $this->callbacks[] = $info['callback'];
175 $this->extractCredits( $path, $info );
176 foreach ( $info as $key => $val ) {
177 if ( in_array( $key, self::$globalSettings ) ) {
178 $this->storeToArray( $path, "wg$key", $val, $this->globals );
179 // Ignore anything that starts with a @
180 } elseif ( $key[0] !== '@' && !in_array( $key, self::$notAttributes )
181 && !in_array( $key, self::$creditsAttributes )
183 $this->storeToArray( $path, $key, $val, $this->attributes );
188 public function getExtractedInfo() {
189 // Make sure the merge strategies are set
190 foreach ( $this->globals as $key => $val ) {
191 if ( isset( self::$mergeStrategies[$key] ) ) {
192 $this->globals[$key][ExtensionRegistry::MERGE_STRATEGY] = self::$mergeStrategies[$key];
196 return [
197 'globals' => $this->globals,
198 'defines' => $this->defines,
199 'callbacks' => $this->callbacks,
200 'credits' => $this->credits,
201 'attributes' => $this->attributes,
205 public function getRequirements( array $info ) {
206 $requirements = [];
207 $key = ExtensionRegistry::MEDIAWIKI_CORE;
208 if ( isset( $info['requires'][$key] ) ) {
209 $requirements[$key] = $info['requires'][$key];
212 return $requirements;
215 protected function extractHooks( array $info ) {
216 if ( isset( $info['Hooks'] ) ) {
217 foreach ( $info['Hooks'] as $name => $value ) {
218 if ( is_array( $value ) ) {
219 foreach ( $value as $callback ) {
220 $this->globals['wgHooks'][$name][] = $callback;
222 } else {
223 $this->globals['wgHooks'][$name][] = $value;
230 * Register namespaces with the appropriate global settings
232 * @param array $info
234 protected function extractNamespaces( array $info ) {
235 if ( isset( $info['namespaces'] ) ) {
236 foreach ( $info['namespaces'] as $ns ) {
237 $id = $ns['id'];
238 $this->defines[$ns['constant']] = $id;
239 $this->attributes['ExtensionNamespaces'][$id] = $ns['name'];
240 if ( isset( $ns['gender'] ) ) {
241 $this->globals['wgExtraGenderNamespaces'][$id] = $ns['gender'];
243 if ( isset( $ns['subpages'] ) && $ns['subpages'] ) {
244 $this->globals['wgNamespacesWithSubpages'][$id] = true;
246 if ( isset( $ns['content'] ) && $ns['content'] ) {
247 $this->globals['wgContentNamespaces'][] = $id;
249 if ( isset( $ns['defaultcontentmodel'] ) ) {
250 $this->globals['wgNamespaceContentModels'][$id] = $ns['defaultcontentmodel'];
252 if ( isset( $ns['protection'] ) ) {
253 $this->globals['wgNamespaceProtection'][$id] = $ns['protection'];
255 if ( isset( $ns['capitallinkoverride'] ) ) {
256 $this->globals['wgCapitalLinkOverrides'][$id] = $ns['capitallinkoverride'];
262 protected function extractResourceLoaderModules( $dir, array $info ) {
263 $defaultPaths = isset( $info['ResourceFileModulePaths'] )
264 ? $info['ResourceFileModulePaths']
265 : false;
266 if ( isset( $defaultPaths['localBasePath'] ) ) {
267 if ( $defaultPaths['localBasePath'] === '' ) {
268 // Avoid double slashes (e.g. /extensions/Example//path)
269 $defaultPaths['localBasePath'] = $dir;
270 } else {
271 $defaultPaths['localBasePath'] = "$dir/{$defaultPaths['localBasePath']}";
275 foreach ( [ 'ResourceModules', 'ResourceModuleSkinStyles' ] as $setting ) {
276 if ( isset( $info[$setting] ) ) {
277 foreach ( $info[$setting] as $name => $data ) {
278 if ( isset( $data['localBasePath'] ) ) {
279 if ( $data['localBasePath'] === '' ) {
280 // Avoid double slashes (e.g. /extensions/Example//path)
281 $data['localBasePath'] = $dir;
282 } else {
283 $data['localBasePath'] = "$dir/{$data['localBasePath']}";
286 if ( $defaultPaths ) {
287 $data += $defaultPaths;
289 $this->globals["wg$setting"][$name] = $data;
295 protected function extractExtensionMessagesFiles( $dir, array $info ) {
296 if ( isset( $info['ExtensionMessagesFiles'] ) ) {
297 $this->globals["wgExtensionMessagesFiles"] += array_map( function( $file ) use ( $dir ) {
298 return "$dir/$file";
299 }, $info['ExtensionMessagesFiles'] );
304 * Set message-related settings, which need to be expanded to use
305 * absolute paths
307 * @param string $dir
308 * @param array $info
310 protected function extractMessagesDirs( $dir, array $info ) {
311 if ( isset( $info['MessagesDirs'] ) ) {
312 foreach ( $info['MessagesDirs'] as $name => $files ) {
313 foreach ( (array)$files as $file ) {
314 $this->globals["wgMessagesDirs"][$name][] = "$dir/$file";
321 * @param string $path
322 * @param array $info
323 * @throws Exception
325 protected function extractCredits( $path, array $info ) {
326 $credits = [
327 'path' => $path,
328 'type' => isset( $info['type'] ) ? $info['type'] : 'other',
330 foreach ( self::$creditsAttributes as $attr ) {
331 if ( isset( $info[$attr] ) ) {
332 $credits[$attr] = $info[$attr];
336 $name = $credits['name'];
338 // If someone is loading the same thing twice, throw
339 // a nice error (T121493)
340 if ( isset( $this->credits[$name] ) ) {
341 $firstPath = $this->credits[$name]['path'];
342 $secondPath = $credits['path'];
343 throw new Exception( "It was attempted to load $name twice, from $firstPath and $secondPath." );
346 $this->credits[$name] = $credits;
347 $this->globals['wgExtensionCredits'][$credits['type']][] = $credits;
351 * Set configuration settings
352 * @todo In the future, this should be done via Config interfaces
354 * @param array $info
356 protected function extractConfig( array $info ) {
357 if ( isset( $info['config'] ) ) {
358 if ( isset( $info['config']['_prefix'] ) ) {
359 $prefix = $info['config']['_prefix'];
360 unset( $info['config']['_prefix'] );
361 } else {
362 $prefix = 'wg';
364 foreach ( $info['config'] as $key => $val ) {
365 if ( $key[0] !== '@' ) {
366 $this->globals["$prefix$key"] = $val;
372 protected function extractParserTestFiles( $dir, array $info ) {
373 if ( isset( $info['ParserTestFiles'] ) ) {
374 foreach ( $info['ParserTestFiles'] as $path ) {
375 $this->globals['wgParserTestFiles'][] = "$dir/$path";
381 * @param string $path
382 * @param string $name
383 * @param array $value
384 * @param array &$array
385 * @throws InvalidArgumentException
387 protected function storeToArray( $path, $name, $value, &$array ) {
388 if ( !is_array( $value ) ) {
389 throw new InvalidArgumentException( "The value for '$name' should be an array (from $path)" );
391 if ( isset( $array[$name] ) ) {
392 $array[$name] = array_merge_recursive( $array[$name], $value );
393 } else {
394 $array[$name] = $value;
398 public function getExtraAutoloaderPaths( $dir, array $info ) {
399 $paths = [];
400 if ( isset( $info['load_composer_autoloader'] ) && $info['load_composer_autoloader'] === true ) {
401 $path = "$dir/vendor/autoload.php";
402 if ( file_exists( $path ) ) {
403 $paths[] = $path;
406 return $paths;