DatabaseMysqlBase visibility cleanups
[mediawiki.git] / includes / registration / ExtensionProcessor.php
blobd613b2eeb700d69851a897e3dba8f41c565af68f
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 'DefaultUserOptions',
14 'HiddenPrefs',
15 'GroupPermissions',
16 'RevokePermissions',
17 'GrantPermissions',
18 'GrantPermissionGroups',
19 'ImplicitGroups',
20 'GroupsAddToSelf',
21 'GroupsRemoveFromSelf',
22 'AddGroups',
23 'RemoveGroups',
24 'AvailableRights',
25 'ContentHandlers',
26 'ConfigRegistry',
27 'SessionProviders',
28 'AuthManagerAutoConfig',
29 'CentralIdLookupProviders',
30 'ChangeCredentialsBlacklist',
31 'RemoveCredentialsBlacklist',
32 'RateLimits',
33 'RecentChangesFlags',
34 'MediaHandlers',
35 'ExtensionFunctions',
36 'ExtensionEntryPointListFiles',
37 'SpecialPages',
38 'JobClasses',
39 'LogTypes',
40 'LogRestrictions',
41 'FilterLogTypes',
42 'ActionFilteredLogs',
43 'LogNames',
44 'LogHeaders',
45 'LogActions',
46 'LogActionsHandlers',
47 'Actions',
48 'APIModules',
49 'APIFormatModules',
50 'APIMetaModules',
51 'APIPropModules',
52 'APIListModules',
53 'ValidSkinNames',
54 'FeedClasses',
57 /**
58 * Mapping of global settings to their specific merge strategies.
60 * @see ExtensionRegistry::exportExtractedData
61 * @see getExtractedInfo
62 * @var array
64 protected static $mergeStrategies = [
65 'wgGroupPermissions' => 'array_plus_2d',
66 'wgRevokePermissions' => 'array_plus_2d',
67 'wgGrantPermissions' => 'array_plus_2d',
68 'wgHooks' => 'array_merge_recursive',
69 'wgExtensionCredits' => 'array_merge_recursive',
70 'wgExtraGenderNamespaces' => 'array_plus',
71 'wgNamespacesWithSubpages' => 'array_plus',
72 'wgNamespaceContentModels' => 'array_plus',
73 'wgNamespaceProtection' => 'array_plus',
74 'wgCapitalLinkOverrides' => 'array_plus',
75 'wgRateLimits' => 'array_plus_2d',
76 'wgAuthManagerAutoConfig' => 'array_plus_2d',
79 /**
80 * Keys that are part of the extension credits
82 * @var array
84 protected static $creditsAttributes = [
85 'name',
86 'namemsg',
87 'author',
88 'version',
89 'url',
90 'description',
91 'descriptionmsg',
92 'license-name',
95 /**
96 * Things that are not 'attributes', but are not in
97 * $globalSettings or $creditsAttributes.
99 * @var array
101 protected static $notAttributes = [
102 'callback',
103 'Hooks',
104 'namespaces',
105 'ResourceFileModulePaths',
106 'ResourceModules',
107 'ResourceModuleSkinStyles',
108 'ExtensionMessagesFiles',
109 'MessagesDirs',
110 'type',
111 'config',
112 'config_prefix',
113 'ServiceWiringFiles',
114 'ParserTestFiles',
115 'AutoloadClasses',
116 'manifest_version',
117 'load_composer_autoloader',
121 * Stuff that is going to be set to $GLOBALS
123 * Some keys are pre-set to arrays so we can += to them
125 * @var array
127 protected $globals = [
128 'wgExtensionMessagesFiles' => [],
129 'wgMessagesDirs' => [],
133 * Things that should be define()'d
135 * @var array
137 protected $defines = [];
140 * Things to be called once registration of these extensions are done
142 * @var callable[]
144 protected $callbacks = [];
147 * @var array
149 protected $credits = [];
152 * Any thing else in the $info that hasn't
153 * already been processed
155 * @var array
157 protected $attributes = [];
160 * @param string $path
161 * @param array $info
162 * @param int $version manifest_version for info
163 * @return array
165 public function extractInfo( $path, array $info, $version ) {
166 $dir = dirname( $path );
167 if ( $version === 2 ) {
168 $this->extractConfig2( $info, $dir );
169 } else {
170 // $version === 1
171 $this->extractConfig1( $info );
173 $this->extractHooks( $info );
174 $this->extractExtensionMessagesFiles( $dir, $info );
175 $this->extractMessagesDirs( $dir, $info );
176 $this->extractNamespaces( $info );
177 $this->extractResourceLoaderModules( $dir, $info );
178 $this->extractServiceWiringFiles( $dir, $info );
179 $this->extractParserTestFiles( $dir, $info );
180 if ( isset( $info['callback'] ) ) {
181 $this->callbacks[] = $info['callback'];
184 $this->extractCredits( $path, $info );
185 foreach ( $info as $key => $val ) {
186 if ( in_array( $key, self::$globalSettings ) ) {
187 $this->storeToArray( $path, "wg$key", $val, $this->globals );
188 // Ignore anything that starts with a @
189 } elseif ( $key[0] !== '@' && !in_array( $key, self::$notAttributes )
190 && !in_array( $key, self::$creditsAttributes )
192 $this->storeToArray( $path, $key, $val, $this->attributes );
197 public function getExtractedInfo() {
198 // Make sure the merge strategies are set
199 foreach ( $this->globals as $key => $val ) {
200 if ( isset( self::$mergeStrategies[$key] ) ) {
201 $this->globals[$key][ExtensionRegistry::MERGE_STRATEGY] = self::$mergeStrategies[$key];
205 return [
206 'globals' => $this->globals,
207 'defines' => $this->defines,
208 'callbacks' => $this->callbacks,
209 'credits' => $this->credits,
210 'attributes' => $this->attributes,
214 public function getRequirements( array $info ) {
215 $requirements = [];
216 $key = ExtensionRegistry::MEDIAWIKI_CORE;
217 if ( isset( $info['requires'][$key] ) ) {
218 $requirements[$key] = $info['requires'][$key];
221 return $requirements;
224 protected function extractHooks( array $info ) {
225 if ( isset( $info['Hooks'] ) ) {
226 foreach ( $info['Hooks'] as $name => $value ) {
227 if ( is_array( $value ) ) {
228 foreach ( $value as $callback ) {
229 $this->globals['wgHooks'][$name][] = $callback;
231 } else {
232 $this->globals['wgHooks'][$name][] = $value;
239 * Register namespaces with the appropriate global settings
241 * @param array $info
243 protected function extractNamespaces( array $info ) {
244 if ( isset( $info['namespaces'] ) ) {
245 foreach ( $info['namespaces'] as $ns ) {
246 $id = $ns['id'];
247 $this->defines[$ns['constant']] = $id;
248 if ( !( isset( $ns['conditional'] ) && $ns['conditional'] ) ) {
249 // If it is not conditional, register it
250 $this->attributes['ExtensionNamespaces'][$id] = $ns['name'];
252 if ( isset( $ns['gender'] ) ) {
253 $this->globals['wgExtraGenderNamespaces'][$id] = $ns['gender'];
255 if ( isset( $ns['subpages'] ) && $ns['subpages'] ) {
256 $this->globals['wgNamespacesWithSubpages'][$id] = true;
258 if ( isset( $ns['content'] ) && $ns['content'] ) {
259 $this->globals['wgContentNamespaces'][] = $id;
261 if ( isset( $ns['defaultcontentmodel'] ) ) {
262 $this->globals['wgNamespaceContentModels'][$id] = $ns['defaultcontentmodel'];
264 if ( isset( $ns['protection'] ) ) {
265 $this->globals['wgNamespaceProtection'][$id] = $ns['protection'];
267 if ( isset( $ns['capitallinkoverride'] ) ) {
268 $this->globals['wgCapitalLinkOverrides'][$id] = $ns['capitallinkoverride'];
274 protected function extractResourceLoaderModules( $dir, array $info ) {
275 $defaultPaths = isset( $info['ResourceFileModulePaths'] )
276 ? $info['ResourceFileModulePaths']
277 : false;
278 if ( isset( $defaultPaths['localBasePath'] ) ) {
279 if ( $defaultPaths['localBasePath'] === '' ) {
280 // Avoid double slashes (e.g. /extensions/Example//path)
281 $defaultPaths['localBasePath'] = $dir;
282 } else {
283 $defaultPaths['localBasePath'] = "$dir/{$defaultPaths['localBasePath']}";
287 foreach ( [ 'ResourceModules', 'ResourceModuleSkinStyles' ] as $setting ) {
288 if ( isset( $info[$setting] ) ) {
289 foreach ( $info[$setting] as $name => $data ) {
290 if ( isset( $data['localBasePath'] ) ) {
291 if ( $data['localBasePath'] === '' ) {
292 // Avoid double slashes (e.g. /extensions/Example//path)
293 $data['localBasePath'] = $dir;
294 } else {
295 $data['localBasePath'] = "$dir/{$data['localBasePath']}";
298 if ( $defaultPaths ) {
299 $data += $defaultPaths;
301 $this->globals["wg$setting"][$name] = $data;
307 protected function extractExtensionMessagesFiles( $dir, array $info ) {
308 if ( isset( $info['ExtensionMessagesFiles'] ) ) {
309 $this->globals["wgExtensionMessagesFiles"] += array_map( function( $file ) use ( $dir ) {
310 return "$dir/$file";
311 }, $info['ExtensionMessagesFiles'] );
316 * Set message-related settings, which need to be expanded to use
317 * absolute paths
319 * @param string $dir
320 * @param array $info
322 protected function extractMessagesDirs( $dir, array $info ) {
323 if ( isset( $info['MessagesDirs'] ) ) {
324 foreach ( $info['MessagesDirs'] as $name => $files ) {
325 foreach ( (array)$files as $file ) {
326 $this->globals["wgMessagesDirs"][$name][] = "$dir/$file";
333 * @param string $path
334 * @param array $info
335 * @throws Exception
337 protected function extractCredits( $path, array $info ) {
338 $credits = [
339 'path' => $path,
340 'type' => isset( $info['type'] ) ? $info['type'] : 'other',
342 foreach ( self::$creditsAttributes as $attr ) {
343 if ( isset( $info[$attr] ) ) {
344 $credits[$attr] = $info[$attr];
348 $name = $credits['name'];
350 // If someone is loading the same thing twice, throw
351 // a nice error (T121493)
352 if ( isset( $this->credits[$name] ) ) {
353 $firstPath = $this->credits[$name]['path'];
354 $secondPath = $credits['path'];
355 throw new Exception( "It was attempted to load $name twice, from $firstPath and $secondPath." );
358 $this->credits[$name] = $credits;
359 $this->globals['wgExtensionCredits'][$credits['type']][] = $credits;
363 * Set configuration settings for manifest_version == 1
364 * @todo In the future, this should be done via Config interfaces
366 * @param array $info
368 protected function extractConfig1( array $info ) {
369 if ( isset( $info['config'] ) ) {
370 if ( isset( $info['config']['_prefix'] ) ) {
371 $prefix = $info['config']['_prefix'];
372 unset( $info['config']['_prefix'] );
373 } else {
374 $prefix = 'wg';
376 foreach ( $info['config'] as $key => $val ) {
377 if ( $key[0] !== '@' ) {
378 $this->globals["$prefix$key"] = $val;
385 * Set configuration settings for manifest_version == 2
386 * @todo In the future, this should be done via Config interfaces
388 * @param array $info
389 * @param string $dir
391 protected function extractConfig2( array $info, $dir ) {
392 if ( isset( $info['config_prefix'] ) ) {
393 $prefix = $info['config_prefix'];
394 } else {
395 $prefix = 'wg';
397 if ( isset( $info['config'] ) ) {
398 foreach ( $info['config'] as $key => $data ) {
399 $value = $data['value'];
400 if ( isset( $data['merge_strategy'] ) ) {
401 $value[ExtensionRegistry::MERGE_STRATEGY] = $data['merge_strategy'];
403 if ( isset( $data['path'] ) && $data['path'] ) {
404 $value = "$dir/$value";
406 $this->globals["$prefix$key"] = $value;
411 protected function extractServiceWiringFiles( $dir, array $info ) {
412 if ( isset( $info['ServiceWiringFiles'] ) ) {
413 foreach ( $info['ServiceWiringFiles'] as $path ) {
414 $this->globals['wgServiceWiringFiles'][] = "$dir/$path";
419 protected function extractParserTestFiles( $dir, array $info ) {
420 if ( isset( $info['ParserTestFiles'] ) ) {
421 foreach ( $info['ParserTestFiles'] as $path ) {
422 $this->globals['wgParserTestFiles'][] = "$dir/$path";
428 * @param string $path
429 * @param string $name
430 * @param array $value
431 * @param array &$array
432 * @throws InvalidArgumentException
434 protected function storeToArray( $path, $name, $value, &$array ) {
435 if ( !is_array( $value ) ) {
436 throw new InvalidArgumentException( "The value for '$name' should be an array (from $path)" );
438 if ( isset( $array[$name] ) ) {
439 $array[$name] = array_merge_recursive( $array[$name], $value );
440 } else {
441 $array[$name] = $value;
445 public function getExtraAutoloaderPaths( $dir, array $info ) {
446 $paths = [];
447 if ( isset( $info['load_composer_autoloader'] ) && $info['load_composer_autoloader'] === true ) {
448 $path = "$dir/vendor/autoload.php";
449 if ( file_exists( $path ) ) {
450 $paths[] = $path;
453 return $paths;