Merge "Revert "Log the reason why revision->getContent() returns null""
[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 if ( isset( $info['ServiceWiringFiles'] ) ) {
201 $this->extractPathBasedGlobal(
202 'wgServiceWiringFiles',
203 $dir,
204 $info['ServiceWiringFiles']
205 );
206 }
207 if ( isset( $info['ParserTestFiles'] ) ) {
208 $this->extractPathBasedGlobal(
209 'wgParserTestFiles',
210 $dir,
211 $info['ParserTestFiles']
212 );
213 }
214 $name = $this->extractCredits( $path, $info );
215 if ( isset( $info['callback'] ) ) {
216 $this->callbacks[$name] = $info['callback'];
217 }
218
219 if ( $version === 2 ) {
220 $this->extractAttributes( $path, $info );
221 }
222
223 foreach ( $info as $key => $val ) {
224 // If it's a global setting,
225 if ( in_array( $key, self::$globalSettings ) ) {
226 $this->storeToArray( $path, "wg$key", $val, $this->globals );
227 continue;
228 }
229 // Ignore anything that starts with a @
230 if ( $key[0] === '@' ) {
231 continue;
232 }
233
234 if ( $version === 2 ) {
235 // Only whitelisted attributes are set
236 if ( in_array( $key, self::$coreAttributes ) ) {
237 $this->storeToArray( $path, $key, $val, $this->attributes );
238 }
239 } else {
240 // version === 1
241 if ( !in_array( $key, self::$notAttributes )
242 && !in_array( $key, self::$creditsAttributes )
243 ) {
244 // If it's not blacklisted, it's an attribute
245 $this->storeToArray( $path, $key, $val, $this->attributes );
246 }
247 }
248
249 }
250 }
251
252 /**
253 * @param string $path
254 * @param array $info
255 */
256 protected function extractAttributes( $path, array $info ) {
257 if ( isset( $info['attributes'] ) ) {
258 foreach ( $info['attributes'] as $extName => $value ) {
259 $this->storeToArray( $path, $extName, $value, $this->extAttributes );
260 }
261 }
262 }
263
264 public function getExtractedInfo() {
265 // Make sure the merge strategies are set
266 foreach ( $this->globals as $key => $val ) {
267 if ( isset( self::$mergeStrategies[$key] ) ) {
268 $this->globals[$key][ExtensionRegistry::MERGE_STRATEGY] = self::$mergeStrategies[$key];
269 }
270 }
271
272 // Merge $this->extAttributes into $this->attributes depending on what is loaded
273 foreach ( $this->extAttributes as $extName => $value ) {
274 // Only set the attribute if $extName is loaded (and hence present in credits)
275 if ( isset( $this->credits[$extName] ) ) {
276 foreach ( $value as $attrName => $attrValue ) {
277 $this->storeToArray(
278 '', // Don't provide a path since it's impossible to generate an error here
279 $extName . $attrName,
280 $attrValue,
281 $this->attributes
282 );
283 }
284 unset( $this->extAttributes[$extName] );
285 }
286 }
287
288 return [
289 'globals' => $this->globals,
290 'defines' => $this->defines,
291 'callbacks' => $this->callbacks,
292 'credits' => $this->credits,
293 'attributes' => $this->attributes,
294 ];
295 }
296
297 public function getRequirements( array $info ) {
298 return isset( $info['requires'] ) ? $info['requires'] : [];
299 }
300
301 protected function extractHooks( array $info ) {
302 if ( isset( $info['Hooks'] ) ) {
303 foreach ( $info['Hooks'] as $name => $value ) {
304 if ( is_array( $value ) ) {
305 foreach ( $value as $callback ) {
306 $this->globals['wgHooks'][$name][] = $callback;
307 }
308 } else {
309 $this->globals['wgHooks'][$name][] = $value;
310 }
311 }
312 }
313 }
314
315 /**
316 * Register namespaces with the appropriate global settings
317 *
318 * @param array $info
319 */
320 protected function extractNamespaces( array $info ) {
321 if ( isset( $info['namespaces'] ) ) {
322 foreach ( $info['namespaces'] as $ns ) {
323 if ( defined( $ns['constant'] ) ) {
324 // If the namespace constant is already defined, use it.
325 // This allows namespace IDs to be overwritten locally.
326 $id = constant( $ns['constant'] );
327 } else {
328 $id = $ns['id'];
329 $this->defines[ $ns['constant'] ] = $id;
330 }
331
332 if ( !( isset( $ns['conditional'] ) && $ns['conditional'] ) ) {
333 // If it is not conditional, register it
334 $this->attributes['ExtensionNamespaces'][$id] = $ns['name'];
335 }
336 if ( isset( $ns['gender'] ) ) {
337 $this->globals['wgExtraGenderNamespaces'][$id] = $ns['gender'];
338 }
339 if ( isset( $ns['subpages'] ) && $ns['subpages'] ) {
340 $this->globals['wgNamespacesWithSubpages'][$id] = true;
341 }
342 if ( isset( $ns['content'] ) && $ns['content'] ) {
343 $this->globals['wgContentNamespaces'][] = $id;
344 }
345 if ( isset( $ns['defaultcontentmodel'] ) ) {
346 $this->globals['wgNamespaceContentModels'][$id] = $ns['defaultcontentmodel'];
347 }
348 if ( isset( $ns['protection'] ) ) {
349 $this->globals['wgNamespaceProtection'][$id] = $ns['protection'];
350 }
351 if ( isset( $ns['capitallinkoverride'] ) ) {
352 $this->globals['wgCapitalLinkOverrides'][$id] = $ns['capitallinkoverride'];
353 }
354 }
355 }
356 }
357
358 protected function extractResourceLoaderModules( $dir, array $info ) {
359 $defaultPaths = isset( $info['ResourceFileModulePaths'] )
360 ? $info['ResourceFileModulePaths']
361 : false;
362 if ( isset( $defaultPaths['localBasePath'] ) ) {
363 if ( $defaultPaths['localBasePath'] === '' ) {
364 // Avoid double slashes (e.g. /extensions/Example//path)
365 $defaultPaths['localBasePath'] = $dir;
366 } else {
367 $defaultPaths['localBasePath'] = "$dir/{$defaultPaths['localBasePath']}";
368 }
369 }
370
371 foreach ( [ 'ResourceModules', 'ResourceModuleSkinStyles' ] as $setting ) {
372 if ( isset( $info[$setting] ) ) {
373 foreach ( $info[$setting] as $name => $data ) {
374 if ( isset( $data['localBasePath'] ) ) {
375 if ( $data['localBasePath'] === '' ) {
376 // Avoid double slashes (e.g. /extensions/Example//path)
377 $data['localBasePath'] = $dir;
378 } else {
379 $data['localBasePath'] = "$dir/{$data['localBasePath']}";
380 }
381 }
382 if ( $defaultPaths ) {
383 $data += $defaultPaths;
384 }
385 $this->globals["wg$setting"][$name] = $data;
386 }
387 }
388 }
389 }
390
391 protected function extractExtensionMessagesFiles( $dir, array $info ) {
392 if ( isset( $info['ExtensionMessagesFiles'] ) ) {
393 foreach ( $info['ExtensionMessagesFiles'] as &$file ) {
394 $file = "$dir/$file";
395 }
396 $this->globals["wgExtensionMessagesFiles"] += $info['ExtensionMessagesFiles'];
397 }
398 }
399
400 /**
401 * Set message-related settings, which need to be expanded to use
402 * absolute paths
403 *
404 * @param string $dir
405 * @param array $info
406 */
407 protected function extractMessagesDirs( $dir, array $info ) {
408 if ( isset( $info['MessagesDirs'] ) ) {
409 foreach ( $info['MessagesDirs'] as $name => $files ) {
410 foreach ( (array)$files as $file ) {
411 $this->globals["wgMessagesDirs"][$name][] = "$dir/$file";
412 }
413 }
414 }
415 }
416
417 /**
418 * @param string $path
419 * @param array $info
420 * @return string Name of thing
421 * @throws Exception
422 */
423 protected function extractCredits( $path, array $info ) {
424 $credits = [
425 'path' => $path,
426 'type' => isset( $info['type'] ) ? $info['type'] : 'other',
427 ];
428 foreach ( self::$creditsAttributes as $attr ) {
429 if ( isset( $info[$attr] ) ) {
430 $credits[$attr] = $info[$attr];
431 }
432 }
433
434 $name = $credits['name'];
435
436 // If someone is loading the same thing twice, throw
437 // a nice error (T121493)
438 if ( isset( $this->credits[$name] ) ) {
439 $firstPath = $this->credits[$name]['path'];
440 $secondPath = $credits['path'];
441 throw new Exception( "It was attempted to load $name twice, from $firstPath and $secondPath." );
442 }
443
444 $this->credits[$name] = $credits;
445 $this->globals['wgExtensionCredits'][$credits['type']][] = $credits;
446
447 return $name;
448 }
449
450 /**
451 * Set configuration settings for manifest_version == 1
452 * @todo In the future, this should be done via Config interfaces
453 *
454 * @param array $info
455 */
456 protected function extractConfig1( array $info ) {
457 if ( isset( $info['config'] ) ) {
458 if ( isset( $info['config']['_prefix'] ) ) {
459 $prefix = $info['config']['_prefix'];
460 unset( $info['config']['_prefix'] );
461 } else {
462 $prefix = 'wg';
463 }
464 foreach ( $info['config'] as $key => $val ) {
465 if ( $key[0] !== '@' ) {
466 $this->addConfigGlobal( "$prefix$key", $val );
467 }
468 }
469 }
470 }
471
472 /**
473 * Set configuration settings for manifest_version == 2
474 * @todo In the future, this should be done via Config interfaces
475 *
476 * @param array $info
477 * @param string $dir
478 */
479 protected function extractConfig2( array $info, $dir ) {
480 if ( isset( $info['config_prefix'] ) ) {
481 $prefix = $info['config_prefix'];
482 } else {
483 $prefix = 'wg';
484 }
485 if ( isset( $info['config'] ) ) {
486 foreach ( $info['config'] as $key => $data ) {
487 $value = $data['value'];
488 if ( isset( $data['merge_strategy'] ) ) {
489 $value[ExtensionRegistry::MERGE_STRATEGY] = $data['merge_strategy'];
490 }
491 if ( isset( $data['path'] ) && $data['path'] ) {
492 $value = "$dir/$value";
493 }
494 $this->addConfigGlobal( "$prefix$key", $value );
495 }
496 }
497 }
498
499 /**
500 * Helper function to set a value to a specific global, if it isn't set already.
501 *
502 * @param string $key The config key with the prefix and anything
503 * @param mixed $value The value of the config
504 */
505 private function addConfigGlobal( $key, $value ) {
506 if ( array_key_exists( $key, $this->globals ) ) {
507 throw new RuntimeException(
508 "The configuration setting '$key' was already set by another extension,"
509 . " and cannot be set again." );
510 }
511 $this->globals[$key] = $value;
512 }
513
514 protected function extractPathBasedGlobal( $global, $dir, $paths ) {
515 foreach ( $paths as $path ) {
516 $this->globals[$global][] = "$dir/$path";
517 }
518 }
519
520 /**
521 * @param string $path
522 * @param string $name
523 * @param array $value
524 * @param array &$array
525 * @throws InvalidArgumentException
526 */
527 protected function storeToArray( $path, $name, $value, &$array ) {
528 if ( !is_array( $value ) ) {
529 throw new InvalidArgumentException( "The value for '$name' should be an array (from $path)" );
530 }
531 if ( isset( $array[$name] ) ) {
532 $array[$name] = array_merge_recursive( $array[$name], $value );
533 } else {
534 $array[$name] = $value;
535 }
536 }
537
538 public function getExtraAutoloaderPaths( $dir, array $info ) {
539 $paths = [];
540 if ( isset( $info['load_composer_autoloader'] ) && $info['load_composer_autoloader'] === true ) {
541 $paths[] = "$dir/vendor/autoload.php";
542 }
543 return $paths;
544 }
545 }