MediaWiki master
ExtensionProcessor.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Registration;
4
5use Exception;
6use InvalidArgumentException;
7use MediaWiki\MainConfigNames;
8use MediaWiki\ResourceLoader\FilePath;
9use RuntimeException;
10use UnexpectedValueException;
11
18class ExtensionProcessor implements Processor {
19
25 protected static $globalSettings = [
26 MainConfigNames::ActionFilteredLogs,
27 MainConfigNames::Actions,
28 MainConfigNames::AddGroups,
29 MainConfigNames::APIFormatModules,
30 MainConfigNames::APIListModules,
31 MainConfigNames::APIMetaModules,
32 MainConfigNames::APIModules,
33 MainConfigNames::APIPropModules,
34 MainConfigNames::AuthManagerAutoConfig,
35 MainConfigNames::AvailableRights,
36 MainConfigNames::CentralIdLookupProviders,
37 MainConfigNames::ChangeCredentialsBlacklist,
38 MainConfigNames::ConditionalUserOptions,
39 MainConfigNames::ConfigRegistry,
40 MainConfigNames::ContentHandlers,
41 MainConfigNames::DefaultUserOptions,
42 MainConfigNames::ExtensionEntryPointListFiles,
43 MainConfigNames::ExtensionFunctions,
44 MainConfigNames::FeedClasses,
45 MainConfigNames::FileExtensions,
46 MainConfigNames::FilterLogTypes,
47 MainConfigNames::GrantPermissionGroups,
48 MainConfigNames::GrantPermissions,
49 MainConfigNames::GrantRiskGroups,
50 MainConfigNames::GroupPermissions,
51 MainConfigNames::GroupsAddToSelf,
52 MainConfigNames::GroupsRemoveFromSelf,
53 MainConfigNames::HiddenPrefs,
54 MainConfigNames::ImplicitGroups,
55 MainConfigNames::JobClasses,
56 MainConfigNames::LogActions,
57 MainConfigNames::LogActionsHandlers,
58 MainConfigNames::LogHeaders,
59 MainConfigNames::LogNames,
60 MainConfigNames::LogRestrictions,
61 MainConfigNames::LogTypes,
62 MainConfigNames::MediaHandlers,
63 MainConfigNames::OutputPipelineStages,
64 MainConfigNames::PasswordPolicy,
65 MainConfigNames::PrivilegedGroups,
66 MainConfigNames::RateLimits,
67 MainConfigNames::RawHtmlMessages,
68 MainConfigNames::ReauthenticateTime,
69 MainConfigNames::RecentChangesFlags,
70 MainConfigNames::RemoveCredentialsBlacklist,
71 MainConfigNames::RemoveGroups,
72 MainConfigNames::ResourceLoaderSources,
73 MainConfigNames::RevokePermissions,
74 MainConfigNames::SessionProviders,
75 MainConfigNames::SpecialPages,
76 MainConfigNames::UserRegistrationProviders,
77 ];
78
82 protected const CORE_ATTRIBS = [
83 'ParsoidModules',
84 'RestModuleFiles',
85 'RestRoutes',
86 'SkinOOUIThemes',
87 'SkinCodexThemes',
88 'SearchMappings',
89 'TrackingCategories',
90 'LateJSConfigVarNames',
91 'TempUserSerialProviders',
92 'TempUserSerialMappings',
93 'DatabaseVirtualDomains',
94 'UserOptionsStoreProviders',
95 'NotificationHandlers',
96 'NotificationMiddleware',
97 'RecentChangeSources',
98 'ShadowPageProviders',
99 'CodeHighlightProviders',
100 'LogParamsAllowedClasses',
101 ];
102
109 protected const MERGE_STRATEGIES = [
110 'wgAddGroups' => 'array_merge_recursive',
111 'wgAuthManagerAutoConfig' => 'array_plus_2d',
112 'wgCapitalLinkOverrides' => 'array_plus',
113 'wgExtraGenderNamespaces' => 'array_plus',
114 'wgGrantPermissions' => 'array_plus_2d',
115 'wgGroupPermissions' => 'array_plus_2d',
116 'wgHooks' => 'array_merge_recursive',
117 'wgNamespaceContentModels' => 'array_plus',
118 'wgNamespaceProtection' => 'array_plus',
119 'wgNamespacesWithSubpages' => 'array_plus',
120 'wgPasswordPolicy' => 'array_merge_recursive',
121 'wgRateLimits' => 'array_plus_2d',
122 'wgRemoveGroups' => 'array_merge_recursive',
123 'wgRevokePermissions' => 'array_plus_2d',
124 ];
125
129 protected const CREDIT_ATTRIBS = [
130 'type',
131 'author',
132 'description',
133 'descriptionmsg',
134 'license-name',
135 'name',
136 'namemsg',
137 'url',
138 'version',
139 ];
140
145 protected const NOT_ATTRIBS = [
146 'callback',
147 'config',
148 'config_prefix',
149 'load_composer_autoloader',
150 'manifest_version',
151 'namespaces',
152 'requires',
153 'AutoloadClasses',
154 'AutoloadNamespaces',
155 'ExtensionMessagesFiles',
156 'TranslationAliasesDirs',
157 'ForeignResourcesDir',
158 'Hooks',
159 'DomainEventIngresses',
160 'MessagePosterModule',
161 'MessagesDirs',
162 'OOUIThemePaths',
163 'QUnitTestModule',
164 'ResourceFileModulePaths',
165 'ResourceModuleSkinStyles',
166 'ResourceModules',
167 'ServiceWiringFiles',
168 ];
169
177 protected $globals = [
178 'wgExtensionMessagesFiles' => [],
179 'wgRestAPIAdditionalRouteFiles' => [],
180 'wgMessagesDirs' => [],
181 'TranslationAliasesDirs' => [],
182 ];
183
189 protected $defines = [];
190
198 protected $callbacks = [];
199
203 protected $credits = [];
204
212 protected $autoload = [
213 'files' => [],
214 'classes' => [],
215 'namespaces' => [],
216 ];
217
224 protected $autoloadDev = [
225 'files' => [],
226 'classes' => [],
227 'namespaces' => [],
228 ];
229
236 protected $attributes = [];
237
244 protected $extAttributes = [];
245
253 public function extractInfoFromFile( string $path ) {
254 $json = file_get_contents( $path );
255 $info = json_decode( $json, true );
256
257 if ( !$info ) {
258 throw new RuntimeException( "Failed to load JSON data from $path" );
259 }
260
261 $this->extractInfo( $path, $info, $info['manifest_version'] );
262 }
263
269 public function extractInfo( $path, array $info, $version ) {
270 $dir = dirname( $path );
271 $this->extractHooks( $info, $path );
272 $this->extractDomainEventIngresses( $info, $path );
273 $this->extractExtensionMessagesFiles( $dir, $info );
274 $this->extractMessagesDirs( $dir, $info );
275 $this->extractTranslationAliasesDirs( $dir, $info );
276 $this->extractSkins( $dir, $info );
277 $this->extractSkinImportPaths( $dir, $info );
278 $this->extractNamespaces( $info );
279 $this->extractImplicitRights( $info );
280 $this->extractResourceLoaderModules( $dir, $info );
281 $this->extractInstallerTasks( $dir, $info );
282 if ( isset( $info['ServiceWiringFiles'] ) ) {
283 $this->extractPathBasedGlobal(
284 'wgServiceWiringFiles',
285 $dir,
286 $info['ServiceWiringFiles']
287 );
288 }
289 $name = $this->extractCredits( $path, $info );
290 if ( isset( $info['callback'] ) ) {
291 $this->callbacks[$name] = $info['callback'];
292 }
293
294 $this->extractAutoload( $info, $dir );
295
296 // config should be after all core globals are extracted,
297 // so duplicate setting detection will work fully
298 if ( $version >= 2 ) {
299 $this->extractConfig2( $info, $dir );
300 } else {
301 // $version === 1
302 $this->extractConfig1( $info );
303 }
304
305 // Record the extension name in the ParsoidModules property
306 if ( isset( $info['ParsoidModules'] ) ) {
307 foreach ( $info['ParsoidModules'] as &$module ) {
308 if ( is_string( $module ) ) {
309 $className = $module;
310 $module = [
311 'class' => $className,
312 ];
313 }
314 $module['name'] ??= $name;
315 $module['extension-name'] = $name;
316 }
317 }
318
319 // Prepend path to REST module files
320 if ( isset( $info['RestModuleFiles'] ) ) {
321 foreach ( $info['RestModuleFiles'] as &$file ) {
322 $file = "$dir/$file";
323 }
324 }
325
326 $this->extractForeignResourcesDir( $info, $name, $dir );
327
328 if ( $version >= 2 ) {
329 $this->extractAttributes( $path, $info );
330 }
331
332 foreach ( $info as $key => $val ) {
333 // If it's a global setting,
334 if ( in_array( $key, self::$globalSettings ) ) {
335 $this->storeToArrayRecursive( $path, "wg$key", $val, $this->globals );
336 continue;
337 }
338 // Ignore anything that starts with a @
339 if ( $key[0] === '@' ) {
340 continue;
341 }
342
343 if ( $version >= 2 ) {
344 // Only allowed attributes are set
345 if ( in_array( $key, self::CORE_ATTRIBS ) ) {
346 $this->storeToArray( $path, $key, $val, $this->attributes );
347 }
348 } else {
349 // version === 1
350 if ( !in_array( $key, self::NOT_ATTRIBS )
351 && !in_array( $key, self::CREDIT_ATTRIBS )
352 ) {
353 // If it's not disallowed, it's an attribute
354 $this->storeToArrayRecursive( $path, $key, $val, $this->attributes );
355 }
356 }
357 }
358 }
359
364 protected function extractAttributes( $path, array $info ) {
365 if ( isset( $info['attributes'] ) ) {
366 foreach ( $info['attributes'] as $extName => $value ) {
367 $this->storeToArrayRecursive( $path, $extName, $value, $this->extAttributes );
368 }
369 }
370 }
371
373 public function getExtractedInfo( bool $includeDev = false ) {
374 // Make sure the merge strategies are set
375 foreach ( $this->globals as $key => $val ) {
376 if ( isset( self::MERGE_STRATEGIES[$key] ) ) {
377 $this->globals[$key][ExtensionRegistry::MERGE_STRATEGY] = self::MERGE_STRATEGIES[$key];
378 }
379 }
380
381 // Merge $this->extAttributes into $this->attributes depending on what is loaded
382 foreach ( $this->extAttributes as $extName => $value ) {
383 // Only set the attribute if $extName is loaded (and hence present in credits)
384 if ( isset( $this->credits[$extName] ) ) {
385 foreach ( $value as $attrName => $attrValue ) {
386 $this->storeToArrayRecursive(
387 '', // Don't provide a path since it's impossible to generate an error here
388 $extName . $attrName,
389 $attrValue,
390 $this->attributes
391 );
392 }
393 unset( $this->extAttributes[$extName] );
394 }
395 }
396
397 $autoload = $this->getExtractedAutoloadInfo( $includeDev );
398
399 return [
400 'globals' => $this->globals,
401 'defines' => $this->defines,
402 'callbacks' => $this->callbacks,
403 'credits' => $this->credits,
404 'attributes' => $this->attributes,
405 'autoloaderPaths' => $autoload['files'],
406 'autoloaderClasses' => $autoload['classes'],
407 'autoloaderNS' => $autoload['namespaces'],
408 ];
409 }
410
412 public function getRequirements( array $info, $includeDev ) {
413 // Quick shortcuts
414 if ( !$includeDev || !isset( $info['dev-requires'] ) ) {
415 return $info['requires'] ?? [];
416 }
417
418 if ( !isset( $info['requires'] ) ) {
419 return $info['dev-requires'] ?? [];
420 }
421
422 // OK, we actually have to merge everything
423 $merged = [];
424
425 // Helper that combines version requirements by
426 // picking the non-null if one is, or combines
427 // the two. Note that it is not possible for
428 // both inputs to be null.
429 $pick = static function ( $a, $b ) {
430 if ( $a === null ) {
431 return $b;
432 } elseif ( $b === null ) {
433 return $a;
434 } else {
435 return "$a $b";
436 }
437 };
438
439 $req = $info['requires'];
440 $dev = $info['dev-requires'];
441 if ( isset( $req['MediaWiki'] ) || isset( $dev['MediaWiki'] ) ) {
442 $merged['MediaWiki'] = $pick(
443 $req['MediaWiki'] ?? null,
444 $dev['MediaWiki'] ?? null
445 );
446 }
447
448 $platform = array_merge(
449 array_keys( $req['platform'] ?? [] ),
450 array_keys( $dev['platform'] ?? [] )
451 );
452 if ( $platform ) {
453 foreach ( $platform as $pkey ) {
454 if ( $pkey === 'php' ) {
455 $value = $pick(
456 $req['platform']['php'] ?? null,
457 $dev['platform']['php'] ?? null
458 );
459 } else {
460 // Prefer dev value, but these should be constant
461 // anyway (ext-* and ability-*)
462 $value = $dev['platform'][$pkey] ?? $req['platform'][$pkey];
463 }
464 $merged['platform'][$pkey] = $value;
465 }
466 }
467
468 foreach ( [ 'extensions', 'skins' ] as $thing ) {
469 $things = array_merge(
470 array_keys( $req[$thing] ?? [] ),
471 array_keys( $dev[$thing] ?? [] )
472 );
473 foreach ( $things as $name ) {
474 $merged[$thing][$name] = $pick(
475 $req[$thing][$name] ?? null,
476 $dev[$thing][$name] ?? null
477 );
478 }
479 }
480
481 return $merged;
482 }
483
496 private function setArrayHookHandler(
497 array $callback,
498 array $hookHandlersAttr,
499 string $name,
500 string $path
501 ) {
502 if ( isset( $callback['handler'] ) ) {
503 $handlerName = $callback['handler'];
504 $handlerDefinition = $hookHandlersAttr[$handlerName] ?? false;
505 if ( !$handlerDefinition ) {
506 throw new UnexpectedValueException(
507 "Missing handler definition for $name in HookHandlers attribute in $path"
508 );
509 }
510 $callback['handler'] = $handlerDefinition;
511 $callback['extensionPath'] = $path;
512 $this->attributes['Hooks'][$name][] = $callback;
513 } else {
514 foreach ( $callback as $callable ) {
515 if ( is_array( $callable ) ) {
516 if ( isset( $callable['handler'] ) ) { // Non-legacy style handler
517 $this->setArrayHookHandler( $callable, $hookHandlersAttr, $name, $path );
518 } else { // Legacy style handler array
519 $this->globals['wgHooks'][$name][] = $callable;
520 }
521 } elseif ( is_string( $callable ) ) {
522 $this->setStringHookHandler( $callable, $hookHandlersAttr, $name, $path );
523 }
524 }
525 }
526 }
527
538 private function setStringHookHandler(
539 string $callback,
540 array $hookHandlersAttr,
541 string $name,
542 string $path
543 ) {
544 if ( isset( $hookHandlersAttr[$callback] ) ) {
545 $handler = [
546 'handler' => $hookHandlersAttr[$callback],
547 'extensionPath' => $path
548 ];
549 $this->attributes['Hooks'][$name][] = $handler;
550 } else { // legacy style handler
551 $this->globals['wgHooks'][$name][] = $callback;
552 }
553 }
554
563 protected function extractHooks( array $info, string $path ) {
564 $extName = $info['name'];
565 if ( isset( $info['Hooks'] ) ) {
566 $hookHandlersAttr = [];
567 foreach ( $info['HookHandlers'] ?? [] as $name => $def ) {
568 $hookHandlersAttr[$name] = [ 'name' => "$extName-$name" ] + $def;
569 }
570 foreach ( $info['Hooks'] as $name => $callback ) {
571 if ( is_string( $callback ) ) {
572 $this->setStringHookHandler( $callback, $hookHandlersAttr, $name, $path );
573 } elseif ( is_array( $callback ) ) {
574 $this->setArrayHookHandler( $callback, $hookHandlersAttr, $name, $path );
575 }
576 }
577 }
578 if ( isset( $info['DeprecatedHooks'] ) ) {
579 $deprecatedHooks = [];
580 foreach ( $info['DeprecatedHooks'] as $name => $deprecatedHookInfo ) {
581 $deprecatedHookInfo += [ 'component' => $extName ];
582 $deprecatedHooks[$name] = $deprecatedHookInfo;
583 }
584 if ( isset( $this->attributes['DeprecatedHooks'] ) ) {
585 $this->attributes['DeprecatedHooks'] += $deprecatedHooks;
586 } else {
587 $this->attributes['DeprecatedHooks'] = $deprecatedHooks;
588 }
589 }
590 }
591
598 protected function extractDomainEventIngresses( array $info, string $path ) {
599 $this->attributes['DomainEventIngresses'] ??= [];
600 foreach ( $info['DomainEventIngresses'] ?? [] as $subscriber ) {
601 $subscriber['extensionPath'] = $path;
602 $this->attributes['DomainEventIngresses'][] = $subscriber;
603 }
604 }
605
609 protected function extractNamespaces( array $info ) {
610 if ( isset( $info['namespaces'] ) ) {
611 foreach ( $info['namespaces'] as $ns ) {
612 if ( defined( $ns['constant'] ) ) {
613 // If the namespace constant is already defined, use it.
614 // This allows namespace IDs to be overwritten locally.
615 $id = constant( $ns['constant'] );
616 } else {
617 $id = $ns['id'];
618 }
619 $this->defines[ $ns['constant'] ] = $id;
620
621 if ( !( isset( $ns['conditional'] ) && $ns['conditional'] ) ) {
622 // If it is not conditional, register it
623 $this->attributes['ExtensionNamespaces'][$id] = $ns['name'];
624 }
625 if ( isset( $ns['movable'] ) && !$ns['movable'] ) {
626 $this->attributes['ImmovableNamespaces'][] = $id;
627 }
628 if ( isset( $ns['gender'] ) ) {
629 $this->globals['wgExtraGenderNamespaces'][$id] = $ns['gender'];
630 }
631 if ( isset( $ns['subpages'] ) && $ns['subpages'] ) {
632 $this->globals['wgNamespacesWithSubpages'][$id] = true;
633 }
634 if ( isset( $ns['content'] ) && $ns['content'] ) {
635 $this->globals['wgContentNamespaces'][] = $id;
636 }
637 if ( isset( $ns['defaultcontentmodel'] ) ) {
638 $this->globals['wgNamespaceContentModels'][$id] = $ns['defaultcontentmodel'];
639 }
640 if ( isset( $ns['protection'] ) ) {
641 $this->globals['wgNamespaceProtection'][$id] = $ns['protection'];
642 }
643 if ( isset( $ns['capitallinkoverride'] ) ) {
644 $this->globals['wgCapitalLinkOverrides'][$id] = $ns['capitallinkoverride'];
645 }
646 if ( isset( $ns['includable'] ) && !$ns['includable'] ) {
647 $this->globals['wgNonincludableNamespaces'][] = $id;
648 }
649 }
650 }
651 }
652
653 protected function extractResourceLoaderModules( string $dir, array $info ) {
654 $defaultPaths = $info['ResourceFileModulePaths'] ?? false;
655 if ( isset( $defaultPaths['localBasePath'] ) ) {
656 if ( $defaultPaths['localBasePath'] === '' ) {
657 // Avoid double slashes (e.g. /extensions/Example//path)
658 $defaultPaths['localBasePath'] = $dir;
659 } else {
660 $defaultPaths['localBasePath'] = "$dir/{$defaultPaths['localBasePath']}";
661 }
662 }
663
664 foreach ( [ 'ResourceModules', 'ResourceModuleSkinStyles', 'OOUIThemePaths' ] as $setting ) {
665 if ( isset( $info[$setting] ) ) {
666 foreach ( $info[$setting] as $name => $data ) {
667 if ( isset( $data['localBasePath'] ) ) {
668 if ( $data['localBasePath'] === '' ) {
669 // Avoid double slashes (e.g. /extensions/Example//path)
670 $data['localBasePath'] = $dir;
671 } else {
672 $data['localBasePath'] = "$dir/{$data['localBasePath']}";
673 }
674 }
675 if ( $defaultPaths ) {
676 $data += $defaultPaths;
677 }
678 $this->attributes[$setting][$name] = $data;
679 }
680 }
681 }
682
683 if ( isset( $info['QUnitTestModule'] ) ) {
684 $data = $info['QUnitTestModule'];
685 if ( isset( $data['localBasePath'] ) ) {
686 if ( $data['localBasePath'] === '' ) {
687 // Avoid double slashes (e.g. /extensions/Example//path)
688 $data['localBasePath'] = $dir;
689 } else {
690 $data['localBasePath'] = "$dir/{$data['localBasePath']}";
691 }
692 }
693 $this->attributes['QUnitTestModule']["test.{$info['name']}"] = $data;
694 }
695
696 if ( isset( $info['MessagePosterModule'] ) ) {
697 $data = $info['MessagePosterModule'];
698 $basePath = $data['localBasePath'] ?? '';
699 $baseDir = $basePath === '' ? $dir : "$dir/$basePath";
700 foreach ( $data['scripts'] ?? [] as $scripts ) {
701 $this->attributes['MessagePosterModule']['scripts'][] =
702 new FilePath( $scripts, $baseDir );
703 }
704 foreach ( $data['dependencies'] ?? [] as $dependency ) {
705 $this->attributes['MessagePosterModule']['dependencies'][] = $dependency;
706 }
707 }
708 }
709
710 protected function extractExtensionMessagesFiles( string $dir, array $info ) {
711 if ( isset( $info['ExtensionMessagesFiles'] ) ) {
712 foreach ( $info['ExtensionMessagesFiles'] as &$file ) {
713 $file = "$dir/$file";
714 }
715 $this->globals["wgExtensionMessagesFiles"] += $info['ExtensionMessagesFiles'];
716 }
717 }
718
726 protected function extractMessagesDirs( $dir, array $info ) {
727 if ( isset( $info['MessagesDirs'] ) ) {
728 foreach ( $info['MessagesDirs'] as $name => $files ) {
729 foreach ( (array)$files as $file ) {
730 $this->globals["wgMessagesDirs"][$name][] = "$dir/$file";
731 }
732 }
733 }
734 }
735
743 protected function extractTranslationAliasesDirs( $dir, array $info ) {
744 foreach ( $info['TranslationAliasesDirs'] ?? [] as $name => $files ) {
745 foreach ( (array)$files as $file ) {
746 $this->globals['wgTranslationAliasesDirs'][$name][] = "$dir/$file";
747 }
748 }
749 }
750
757 protected function extractSkins( $dir, array $info ) {
758 if ( isset( $info['ValidSkinNames'] ) ) {
759 foreach ( $info['ValidSkinNames'] as $skinKey => $data ) {
760 if ( isset( $data['args'][0] ) ) {
761 $templateDirectory = $data['args'][0]['templateDirectory'] ?? 'templates';
762 $data['args'][0]['templateDirectory'] = $dir . '/' . $templateDirectory;
763 }
764 $this->globals['wgValidSkinNames'][$skinKey] = $data;
765 }
766 }
767 }
768
772 protected function extractImplicitRights( array $info ) {
773 // Rate limits are only configurable for rights that are either in wgImplicitRights
774 // or in wgAvailableRights. Extensions that define rate limits should not have to
775 // explicitly add them to wgImplicitRights as well, we can do that automatically.
776
777 if ( isset( $info['RateLimits'] ) ) {
778 $rights = array_keys( $info['RateLimits'] );
779
780 if ( isset( $info['AvailableRights'] ) ) {
781 $rights = array_diff( $rights, $info['AvailableRights'] );
782 }
783
784 $this->globals['wgImplicitRights'] = array_merge(
785 $this->globals['wgImplicitRights'] ?? [],
786 $rights
787 );
788 }
789 }
790
795 protected function extractSkinImportPaths( $dir, array $info ) {
796 if ( isset( $info['SkinLessImportPaths'] ) ) {
797 foreach ( $info['SkinLessImportPaths'] as $skin => $subpath ) {
798 $this->attributes['SkinLessImportPaths'][$skin] = "$dir/$subpath";
799 }
800 }
801 }
802
810 protected function extractCredits( $path, array $info ) {
811 $credits = [
812 'path' => $path,
813 'type' => 'other',
814 ];
815 foreach ( self::CREDIT_ATTRIBS as $attr ) {
816 if ( isset( $info[$attr] ) ) {
817 $credits[$attr] = $info[$attr];
818 }
819 }
820
821 $name = $credits['name'];
822
823 // If someone is loading the same thing twice, throw
824 // a nice error (T121493)
825 if ( isset( $this->credits[$name] ) ) {
826 $firstPath = $this->credits[$name]['path'];
827 $secondPath = $credits['path'];
828 throw new InvalidArgumentException(
829 "It was attempted to load $name twice, from $firstPath and $secondPath."
830 );
831 }
832
833 $this->credits[$name] = $credits;
834
835 return $name;
836 }
837
838 protected function extractForeignResourcesDir( array $info, string $name, string $dir ): void {
839 if ( array_key_exists( 'ForeignResourcesDir', $info ) ) {
840 if ( !is_string( $info['ForeignResourcesDir'] ) ) {
841 throw new InvalidArgumentException( "Incorrect ForeignResourcesDir type, must be a string (in $name)" );
842 }
843 $this->attributes['ForeignResourcesDir'][$name] = "{$dir}/{$info['ForeignResourcesDir']}";
844 }
845 }
846
847 protected function extractInstallerTasks( string $path, array $info ): void {
848 if ( isset( $info['InstallerTasks'] ) ) {
849 // Use a fixed path for the schema base path for now. This could be
850 // made configurable if there were a use case for that.
851 $schemaBasePath = $path . '/sql';
852 foreach ( $info['InstallerTasks'] as $taskSpec ) {
853 $this->attributes['InstallerTasks'][]
854 = $taskSpec + [ 'schemaBasePath' => $schemaBasePath ];
855 }
856 }
857 }
858
866 protected function extractConfig1( array $info ) {
867 if ( isset( $info['config'] ) ) {
868 if ( isset( $info['config']['_prefix'] ) ) {
869 $prefix = $info['config']['_prefix'];
870 unset( $info['config']['_prefix'] );
871 } else {
872 $prefix = 'wg';
873 }
874 foreach ( $info['config'] as $key => $val ) {
875 if ( $key[0] !== '@' ) {
876 $this->addConfigGlobal( "$prefix$key", $val, $info['name'] );
877 }
878 }
879 }
880 }
881
891 private function applyPath( array $value, string $dir, bool $ensurePathSuffix = false ): array {
892 $result = [];
893
894 foreach ( $value as $k => $v ) {
895 if ( $ensurePathSuffix && !str_ends_with( $v, '/' ) ) {
896 $v .= '/';
897 }
898 $result[$k] = $dir . '/' . $v;
899 }
900
901 return $result;
902 }
903
912 protected function extractConfig2( array $info, $dir ) {
913 $prefix = $info['config_prefix'] ?? 'wg';
914 if ( isset( $info['config'] ) ) {
915 foreach ( $info['config'] as $key => $data ) {
916 if ( !array_key_exists( 'value', $data ) ) {
917 throw new UnexpectedValueException( "Missing value for config $key" );
918 }
919
920 $value = $data['value'];
921 if ( isset( $data['path'] ) && $data['path'] ) {
922 if ( is_array( $value ) ) {
923 $value = $this->applyPath( $value, $dir );
924 } else {
925 $value = "$dir/$value";
926 }
927 }
928 if ( isset( $data['merge_strategy'] ) ) {
929 $value[ExtensionRegistry::MERGE_STRATEGY] = $data['merge_strategy'];
930 }
931 $this->addConfigGlobal( "$prefix$key", $value, $info['name'] );
932 }
933 }
934 }
935
943 private function addConfigGlobal( $key, $value, $extName ) {
944 if ( array_key_exists( $key, $this->globals ) ) {
945 throw new RuntimeException(
946 "The configuration setting '$key' was already set by MediaWiki core or"
947 . " another extension, and cannot be set again by $extName." );
948 }
949 if ( isset( $value[ExtensionRegistry::MERGE_STRATEGY] ) &&
950 $value[ExtensionRegistry::MERGE_STRATEGY] === 'array_merge_recursive' ) {
952 "Using the array_merge_recursive merge strategy in extension.json and skin.json" .
953 " was deprecated in MediaWiki 1.42",
954 "1.42"
955 );
956 }
957 $this->globals[$key] = $value;
958 }
959
960 protected function extractPathBasedGlobal( string $global, string $dir, array $paths ) {
961 foreach ( $paths as $path ) {
962 $this->globals[$global][] = "$dir/$path";
963 }
964 }
965
974 protected function storeToArrayRecursive( $path, $name, $value, &$array ) {
975 if ( !is_array( $value ) ) {
976 throw new InvalidArgumentException( "The value for '$name' should be an array (from $path)" );
977 }
978 if ( isset( $array[$name] ) ) {
979 $array[$name] = array_merge_recursive( $array[$name], $value );
980 } else {
981 $array[$name] = $value;
982 }
983 }
984
995 protected function storeToArray( $path, $name, $value, &$array ) {
996 if ( !is_array( $value ) ) {
997 throw new InvalidArgumentException( "The value for '$name' should be an array (from $path)" );
998 }
999 if ( isset( $array[$name] ) ) {
1000 $array[$name] = array_merge( $array[$name], $value );
1001 } else {
1002 $array[$name] = $value;
1003 }
1004 }
1005
1020 public function getExtractedAutoloadInfo( bool $includeDev = false ): array {
1021 $autoload = $this->autoload;
1022
1023 if ( $includeDev ) {
1024 $autoload['classes'] += $this->autoloadDev['classes'];
1025 $autoload['namespaces'] += $this->autoloadDev['namespaces'];
1026
1027 // NOTE: This is here for completeness. Per MW 1.39,
1028 // $this->autoloadDev['files'] is always empty.
1029 // So avoid the performance hit of array_merge().
1030 if ( !empty( $this->autoloadDev['files'] ) ) {
1031 // NOTE: Don't use += with numeric keys!
1032 // Could use PHPUtils::pushArray.
1033 $autoload['files'] = array_merge(
1034 $autoload['files'],
1035 $this->autoloadDev['files']
1036 );
1037 }
1038 }
1039
1040 return $autoload;
1041 }
1042
1043 private function extractAutoload( array $info, string $dir ) {
1044 if ( isset( $info['load_composer_autoloader'] ) && $info['load_composer_autoloader'] === true ) {
1045 $file = "$dir/vendor/autoload.php";
1046 if ( file_exists( $file ) ) {
1047 $this->autoload['files'][] = $file;
1048 }
1049 }
1050
1051 if ( isset( $info['AutoloadClasses'] ) ) {
1052 $paths = $this->applyPath( $info['AutoloadClasses'], $dir );
1053 $this->autoload['classes'] += $paths;
1054 }
1055
1056 if ( isset( $info['AutoloadNamespaces'] ) ) {
1057 $paths = $this->applyPath( $info['AutoloadNamespaces'], $dir, true );
1058 $this->autoload['namespaces'] += $paths;
1059 }
1060
1061 if ( isset( $info['TestAutoloadClasses'] ) ) {
1062 $paths = $this->applyPath( $info['TestAutoloadClasses'], $dir );
1063 $this->autoloadDev['classes'] += $paths;
1064 }
1065
1066 if ( isset( $info['TestAutoloadNamespaces'] ) ) {
1067 $paths = $this->applyPath( $info['TestAutoloadNamespaces'], $dir, true );
1068 $this->autoloadDev['namespaces'] += $paths;
1069 }
1070 }
1071}
1072
1074class_alias( ExtensionProcessor::class, 'ExtensionProcessor' );
wfDeprecatedMsg( $msg, $version=false, $component=false, $callerOffset=2)
Log a deprecation warning with arbitrary message text.
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:72
array< string, array > $data
The cache data.
string $key
The message key.
Definition Message.php:192
const MERGE_STRATEGY
Special key that defines the merge strategy.
Generic processor that reads associated arrays and registers whatever is required.
Definition Processor.php:11