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