Merge "Remove incorrect timezone conversion from date parameters"
[lhc/web/wiklou.git] / includes / registration / ExtensionProcessor.php
1 <?php
2
3 class ExtensionProcessor implements Processor {
4
5 /**
6 * Keys that should be set to $GLOBALS
7 *
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 'ResourceLoaderSources',
52 'RevokePermissions',
53 'SessionProviders',
54 'SpecialPages',
55 'ValidSkinNames',
56 ];
57
58 /**
59 * Top-level attributes that come from MW core
60 *
61 * @var string[]
62 */
63 protected static $coreAttributes = [
64 'SkinOOUIThemes',
65 'TrackingCategories',
66 ];
67
68 /**
69 * Mapping of global settings to their specific merge strategies.
70 *
71 * @see ExtensionRegistry::exportExtractedData
72 * @see getExtractedInfo
73 * @var array
74 */
75 protected static $mergeStrategies = [
76 'wgAuthManagerAutoConfig' => 'array_plus_2d',
77 'wgCapitalLinkOverrides' => 'array_plus',
78 'wgExtensionCredits' => 'array_merge_recursive',
79 'wgExtraGenderNamespaces' => 'array_plus',
80 'wgGrantPermissions' => 'array_plus_2d',
81 'wgGroupPermissions' => 'array_plus_2d',
82 'wgHooks' => 'array_merge_recursive',
83 'wgNamespaceContentModels' => 'array_plus',
84 'wgNamespaceProtection' => 'array_plus',
85 'wgNamespacesWithSubpages' => 'array_plus',
86 'wgPasswordPolicy' => 'array_merge_recursive',
87 'wgRateLimits' => 'array_plus_2d',
88 'wgRevokePermissions' => 'array_plus_2d',
89 ];
90
91 /**
92 * Keys that are part of the extension credits
93 *
94 * @var array
95 */
96 protected static $creditsAttributes = [
97 'name',
98 'namemsg',
99 'author',
100 'version',
101 'url',
102 'description',
103 'descriptionmsg',
104 'license-name',
105 ];
106
107 /**
108 * Things that are not 'attributes', but are not in
109 * $globalSettings or $creditsAttributes.
110 *
111 * @var array
112 */
113 protected static $notAttributes = [
114 'callback',
115 'Hooks',
116 'namespaces',
117 'ResourceFileModulePaths',
118 'ResourceModules',
119 'ResourceModuleSkinStyles',
120 'ExtensionMessagesFiles',
121 'MessagesDirs',
122 'type',
123 'config',
124 'config_prefix',
125 'ServiceWiringFiles',
126 'ParserTestFiles',
127 'AutoloadClasses',
128 'manifest_version',
129 'load_composer_autoloader',
130 ];
131
132 /**
133 * Stuff that is going to be set to $GLOBALS
134 *
135 * Some keys are pre-set to arrays so we can += to them
136 *
137 * @var array
138 */
139 protected $globals = [
140 'wgExtensionMessagesFiles' => [],
141 'wgMessagesDirs' => [],
142 ];
143
144 /**
145 * Things that should be define()'d
146 *
147 * @var array
148 */
149 protected $defines = [];
150
151 /**
152 * Things to be called once registration of these extensions are done
153 * keyed by the name of the extension that it belongs to
154 *
155 * @var callable[]
156 */
157 protected $callbacks = [];
158
159 /**
160 * @var array
161 */
162 protected $credits = [];
163
164 /**
165 * @var array
166 */
167 protected $config = [];
168
169 /**
170 * Any thing else in the $info that hasn't
171 * already been processed
172 *
173 * @var array
174 */
175 protected $attributes = [];
176
177 /**
178 * Extension attributes, keyed by name =>
179 * settings.
180 *
181 * @var array
182 */
183 protected $extAttributes = [];
184
185 /**
186 * @param string $path
187 * @param array $info
188 * @param int $version manifest_version for info
189 * @return array
190 */
191 public function extractInfo( $path, array $info, $version ) {
192 $dir = dirname( $path );
193 $this->extractHooks( $info );
194 $this->extractExtensionMessagesFiles( $dir, $info );
195 $this->extractMessagesDirs( $dir, $info );
196 $this->extractNamespaces( $info );
197 $this->extractResourceLoaderModules( $dir, $info );
198 if ( isset( $info['ServiceWiringFiles'] ) ) {
199 $this->extractPathBasedGlobal(
200 'wgServiceWiringFiles',
201 $dir,
202 $info['ServiceWiringFiles']
203 );
204 }
205 if ( isset( $info['ParserTestFiles'] ) ) {
206 $this->extractPathBasedGlobal(
207 'wgParserTestFiles',
208 $dir,
209 $info['ParserTestFiles']
210 );
211 }
212 $name = $this->extractCredits( $path, $info );
213 if ( isset( $info['callback'] ) ) {
214 $this->callbacks[$name] = $info['callback'];
215 }
216
217 // config should be after all core globals are extracted,
218 // so duplicate setting detection will work fully
219 if ( $version === 2 ) {
220 $this->extractConfig2( $info, $dir );
221 } else {
222 // $version === 1
223 $this->extractConfig1( $info );
224 }
225
226 if ( $version === 2 ) {
227 $this->extractAttributes( $path, $info );
228 }
229
230 foreach ( $info as $key => $val ) {
231 // If it's a global setting,
232 if ( in_array( $key, self::$globalSettings ) ) {
233 $this->storeToArray( $path, "wg$key", $val, $this->globals );
234 continue;
235 }
236 // Ignore anything that starts with a @
237 if ( $key[0] === '@' ) {
238 continue;
239 }
240
241 if ( $version === 2 ) {
242 // Only whitelisted attributes are set
243 if ( in_array( $key, self::$coreAttributes ) ) {
244 $this->storeToArray( $path, $key, $val, $this->attributes );
245 }
246 } else {
247 // version === 1
248 if ( !in_array( $key, self::$notAttributes )
249 && !in_array( $key, self::$creditsAttributes )
250 ) {
251 // If it's not blacklisted, it's an attribute
252 $this->storeToArray( $path, $key, $val, $this->attributes );
253 }
254 }
255
256 }
257 }
258
259 /**
260 * @param string $path
261 * @param array $info
262 */
263 protected function extractAttributes( $path, array $info ) {
264 if ( isset( $info['attributes'] ) ) {
265 foreach ( $info['attributes'] as $extName => $value ) {
266 $this->storeToArray( $path, $extName, $value, $this->extAttributes );
267 }
268 }
269 }
270
271 public function getExtractedInfo() {
272 // Make sure the merge strategies are set
273 foreach ( $this->globals as $key => $val ) {
274 if ( isset( self::$mergeStrategies[$key] ) ) {
275 $this->globals[$key][ExtensionRegistry::MERGE_STRATEGY] = self::$mergeStrategies[$key];
276 }
277 }
278
279 // Merge $this->extAttributes into $this->attributes depending on what is loaded
280 foreach ( $this->extAttributes as $extName => $value ) {
281 // Only set the attribute if $extName is loaded (and hence present in credits)
282 if ( isset( $this->credits[$extName] ) ) {
283 foreach ( $value as $attrName => $attrValue ) {
284 $this->storeToArray(
285 '', // Don't provide a path since it's impossible to generate an error here
286 $extName . $attrName,
287 $attrValue,
288 $this->attributes
289 );
290 }
291 unset( $this->extAttributes[$extName] );
292 }
293 }
294
295 return [
296 'globals' => $this->globals,
297 'config' => $this->config,
298 'defines' => $this->defines,
299 'callbacks' => $this->callbacks,
300 'credits' => $this->credits,
301 'attributes' => $this->attributes,
302 ];
303 }
304
305 public function getRequirements( array $info ) {
306 return $info['requires'] ?? [];
307 }
308
309 protected function extractHooks( array $info ) {
310 if ( isset( $info['Hooks'] ) ) {
311 foreach ( $info['Hooks'] as $name => $value ) {
312 if ( is_array( $value ) ) {
313 foreach ( $value as $callback ) {
314 $this->globals['wgHooks'][$name][] = $callback;
315 }
316 } else {
317 $this->globals['wgHooks'][$name][] = $value;
318 }
319 }
320 }
321 }
322
323 /**
324 * Register namespaces with the appropriate global settings
325 *
326 * @param array $info
327 */
328 protected function extractNamespaces( array $info ) {
329 if ( isset( $info['namespaces'] ) ) {
330 foreach ( $info['namespaces'] as $ns ) {
331 if ( defined( $ns['constant'] ) ) {
332 // If the namespace constant is already defined, use it.
333 // This allows namespace IDs to be overwritten locally.
334 $id = constant( $ns['constant'] );
335 } else {
336 $id = $ns['id'];
337 $this->defines[ $ns['constant'] ] = $id;
338 }
339
340 if ( !( isset( $ns['conditional'] ) && $ns['conditional'] ) ) {
341 // If it is not conditional, register it
342 $this->attributes['ExtensionNamespaces'][$id] = $ns['name'];
343 }
344 if ( isset( $ns['gender'] ) ) {
345 $this->globals['wgExtraGenderNamespaces'][$id] = $ns['gender'];
346 }
347 if ( isset( $ns['subpages'] ) && $ns['subpages'] ) {
348 $this->globals['wgNamespacesWithSubpages'][$id] = true;
349 }
350 if ( isset( $ns['content'] ) && $ns['content'] ) {
351 $this->globals['wgContentNamespaces'][] = $id;
352 }
353 if ( isset( $ns['defaultcontentmodel'] ) ) {
354 $this->globals['wgNamespaceContentModels'][$id] = $ns['defaultcontentmodel'];
355 }
356 if ( isset( $ns['protection'] ) ) {
357 $this->globals['wgNamespaceProtection'][$id] = $ns['protection'];
358 }
359 if ( isset( $ns['capitallinkoverride'] ) ) {
360 $this->globals['wgCapitalLinkOverrides'][$id] = $ns['capitallinkoverride'];
361 }
362 }
363 }
364 }
365
366 protected function extractResourceLoaderModules( $dir, array $info ) {
367 $defaultPaths = $info['ResourceFileModulePaths'] ?? false;
368 if ( isset( $defaultPaths['localBasePath'] ) ) {
369 if ( $defaultPaths['localBasePath'] === '' ) {
370 // Avoid double slashes (e.g. /extensions/Example//path)
371 $defaultPaths['localBasePath'] = $dir;
372 } else {
373 $defaultPaths['localBasePath'] = "$dir/{$defaultPaths['localBasePath']}";
374 }
375 }
376
377 foreach ( [ 'ResourceModules', 'ResourceModuleSkinStyles' ] as $setting ) {
378 if ( isset( $info[$setting] ) ) {
379 foreach ( $info[$setting] as $name => $data ) {
380 if ( isset( $data['localBasePath'] ) ) {
381 if ( $data['localBasePath'] === '' ) {
382 // Avoid double slashes (e.g. /extensions/Example//path)
383 $data['localBasePath'] = $dir;
384 } else {
385 $data['localBasePath'] = "$dir/{$data['localBasePath']}";
386 }
387 }
388 if ( $defaultPaths ) {
389 $data += $defaultPaths;
390 }
391 $this->globals["wg$setting"][$name] = $data;
392 }
393 }
394 }
395 }
396
397 protected function extractExtensionMessagesFiles( $dir, array $info ) {
398 if ( isset( $info['ExtensionMessagesFiles'] ) ) {
399 foreach ( $info['ExtensionMessagesFiles'] as &$file ) {
400 $file = "$dir/$file";
401 }
402 $this->globals["wgExtensionMessagesFiles"] += $info['ExtensionMessagesFiles'];
403 }
404 }
405
406 /**
407 * Set message-related settings, which need to be expanded to use
408 * absolute paths
409 *
410 * @param string $dir
411 * @param array $info
412 */
413 protected function extractMessagesDirs( $dir, array $info ) {
414 if ( isset( $info['MessagesDirs'] ) ) {
415 foreach ( $info['MessagesDirs'] as $name => $files ) {
416 foreach ( (array)$files as $file ) {
417 $this->globals["wgMessagesDirs"][$name][] = "$dir/$file";
418 }
419 }
420 }
421 }
422
423 /**
424 * @param string $path
425 * @param array $info
426 * @return string Name of thing
427 * @throws Exception
428 */
429 protected function extractCredits( $path, array $info ) {
430 $credits = [
431 'path' => $path,
432 'type' => $info['type'] ?? 'other',
433 ];
434 foreach ( self::$creditsAttributes as $attr ) {
435 if ( isset( $info[$attr] ) ) {
436 $credits[$attr] = $info[$attr];
437 }
438 }
439
440 $name = $credits['name'];
441
442 // If someone is loading the same thing twice, throw
443 // a nice error (T121493)
444 if ( isset( $this->credits[$name] ) ) {
445 $firstPath = $this->credits[$name]['path'];
446 $secondPath = $credits['path'];
447 throw new Exception( "It was attempted to load $name twice, from $firstPath and $secondPath." );
448 }
449
450 $this->credits[$name] = $credits;
451 $this->globals['wgExtensionCredits'][$credits['type']][] = $credits;
452
453 return $name;
454 }
455
456 /**
457 * Set configuration settings for manifest_version == 1
458 * @todo In the future, this should be done via Config interfaces
459 *
460 * @param array $info
461 */
462 protected function extractConfig1( array $info ) {
463 if ( isset( $info['config'] ) ) {
464 if ( isset( $info['config']['_prefix'] ) ) {
465 $prefix = $info['config']['_prefix'];
466 unset( $info['config']['_prefix'] );
467 } else {
468 $prefix = 'wg';
469 }
470 foreach ( $info['config'] as $key => $val ) {
471 if ( $key[0] !== '@' ) {
472 $this->addConfigGlobal( "$prefix$key", $val, $info['name'] );
473 }
474 }
475 }
476 }
477
478 /**
479 * Set configuration settings for manifest_version == 2
480 * @todo In the future, this should be done via Config interfaces
481 *
482 * @param array $info
483 * @param string $dir
484 */
485 protected function extractConfig2( array $info, $dir ) {
486 if ( isset( $info['config_prefix'] ) ) {
487 $prefix = $info['config_prefix'];
488 } else {
489 $prefix = 'wg';
490 }
491 if ( isset( $info['config'] ) ) {
492 foreach ( $info['config'] as $key => $data ) {
493 $value = $data['value'];
494 if ( isset( $data['merge_strategy'] ) ) {
495 $value[ExtensionRegistry::MERGE_STRATEGY] = $data['merge_strategy'];
496 }
497 if ( isset( $data['path'] ) && $data['path'] ) {
498 $value = "$dir/$value";
499 }
500 $this->addConfigGlobal( "$prefix$key", $value, $info['name'] );
501 $data['providedby'] = $info['name'];
502 if ( isset( $info['ConfigRegistry'][0] ) ) {
503 $data['configregistry'] = array_keys( $info['ConfigRegistry'] )[0];
504 }
505 $this->config[$key] = $data;
506 }
507 }
508 }
509
510 /**
511 * Helper function to set a value to a specific global, if it isn't set already.
512 *
513 * @param string $key The config key with the prefix and anything
514 * @param mixed $value The value of the config
515 * @param string $extName Name of the extension
516 */
517 private function addConfigGlobal( $key, $value, $extName ) {
518 if ( array_key_exists( $key, $this->globals ) ) {
519 throw new RuntimeException(
520 "The configuration setting '$key' was already set by MediaWiki core or"
521 . " another extension, and cannot be set again by $extName." );
522 }
523 $this->globals[$key] = $value;
524 }
525
526 protected function extractPathBasedGlobal( $global, $dir, $paths ) {
527 foreach ( $paths as $path ) {
528 $this->globals[$global][] = "$dir/$path";
529 }
530 }
531
532 /**
533 * @param string $path
534 * @param string $name
535 * @param array $value
536 * @param array &$array
537 * @throws InvalidArgumentException
538 */
539 protected function storeToArray( $path, $name, $value, &$array ) {
540 if ( !is_array( $value ) ) {
541 throw new InvalidArgumentException( "The value for '$name' should be an array (from $path)" );
542 }
543 if ( isset( $array[$name] ) ) {
544 $array[$name] = array_merge_recursive( $array[$name], $value );
545 } else {
546 $array[$name] = $value;
547 }
548 }
549
550 public function getExtraAutoloaderPaths( $dir, array $info ) {
551 $paths = [];
552 if ( isset( $info['load_composer_autoloader'] ) && $info['load_composer_autoloader'] === true ) {
553 $paths[] = "$dir/vendor/autoload.php";
554 }
555 return $paths;
556 }
557 }