MediaWiki master
ExtensionsProvider.php
Go to the documentation of this file.
1<?php
2
4
5use AutoLoader;
8use MediaWiki\MainConfigSchema;
12
18class ExtensionsProvider extends Task {
20 public function getName() {
21 return 'extensions';
22 }
23
25 public function getProvidedNames() {
26 return [ 'HookContainer', 'VirtualDomains', 'ExtensionTaskSpecs' ];
27 }
28
29 public function execute(): Status {
30 if ( !$this->getOption( 'Extensions' ) ) {
31 $this->getContext()->provide( 'VirtualDomains', [] );
32 $this->getContext()->provide( 'ExtensionTaskSpecs', [] );
33 return Status::newGood();
34 }
35
36 // Marker for DatabaseUpdater::loadExtensions so we don't
37 // double load extensions
38 define( 'MW_EXTENSIONS_LOADED', true );
39
40 $legacySchemaHooks = $this->getAutoExtensionLegacySchemaHooks();
41 $data = $this->getAutoExtensionData();
42 if ( isset( $data['globals']['wgHooks']['LoadExtensionSchemaUpdates'] ) ) {
43 $legacySchemaHooks = array_merge( $legacySchemaHooks,
44 $data['globals']['wgHooks']['LoadExtensionSchemaUpdates'] );
45 }
46 $extDeprecatedHooks = $data['attributes']['DeprecatedHooks'] ?? [];
47
48 $legacyHooks = $legacySchemaHooks ? [ 'LoadExtensionSchemaUpdates' => $legacySchemaHooks ] : [];
49 $this->getContext()->provide( 'HookContainer',
50 new HookContainer(
51 new StaticHookRegistry(
52 $legacyHooks,
53 $data['attributes']['Hooks'] ?? [],
54 $extDeprecatedHooks
55 ),
56 MediaWikiServices::getInstance()->getObjectFactory()
57 )
58 );
59 $this->getContext()->provide( 'VirtualDomains',
60 $data['attributes']['DatabaseVirtualDomains'] ?? [] );
61 $this->getContext()->provide( 'ExtensionTaskSpecs',
62 $data['attributes']['InstallerTasks'] ?? [] );
63
64 return Status::newGood();
65 }
66
70 protected function getExtensionsDir() {
71 return MW_INSTALL_PATH . '/extensions';
72 }
73
77 protected function getSkinsDir() {
78 return MW_INSTALL_PATH . '/skins';
79 }
80
87 private function getAutoExtensionLegacySchemaHooks() {
88 $exts = $this->getOption( 'Extensions' );
89 $extensionsDir = $this->getExtensionsDir();
90 $files = [];
91 foreach ( $exts as $e ) {
92 if ( file_exists( "$extensionsDir/$e/$e.php" ) ) {
93 $files[] = "$extensionsDir/$e/$e.php";
94 }
95 }
96
97 if ( $files ) {
98 return $this->includeExtensionFiles( $files );
99 } else {
100 return [];
101 }
102 }
103
111 private function includeExtensionFiles( $files ) {
120 // Extract the defaults into the current scope
121 foreach ( MainConfigSchema::listDefaultValues( 'wg' ) as $var => $value ) {
122 $$var = $value;
123 }
124
125 // phpcs:ignore MediaWiki.VariableAnalysis.UnusedGlobalVariables
127 $wgExtensionDirectory = $this->getExtensionsDir();
128 $wgStyleDirectory = $this->getSkinsDir();
129
130 foreach ( $files as $file ) {
131 require_once $file;
132 }
133
134 // Ignore everyone else's hooks. Lord knows what someone might be doing
135 // in ParserFirstCallInit (see T29171)
136 // @phpcs:disable MediaWiki.VariableAnalysis.MisleadingGlobalNames.Misleading$wgHooks
137 // @phpcs:ignore Generic.Files.LineLength.TooLong
138 // @phan-suppress-next-line PhanUndeclaredVariable,PhanCoalescingAlwaysNull $wgHooks is defined by MainConfigSchema
139 $hooksWeWant = $wgHooks['LoadExtensionSchemaUpdates'] ?? [];
140 // @phpcs:enable MediaWiki.VariableAnalysis.MisleadingGlobalNames.Misleading$wgHooks
141 return $hooksWeWant;
142 }
143
150 private function getAutoExtensionData() {
151 $exts = $this->getOption( 'Extensions' );
152
153 $extensionProcessor = new ExtensionProcessor();
154 foreach ( $exts as $e ) {
155 $jsonPath = $this->getExtensionsDir() . "/$e/extension.json";
156 if ( file_exists( $jsonPath ) ) {
157 $extensionProcessor->extractInfoFromFile( $jsonPath );
158 }
159 }
160
161 $autoload = $extensionProcessor->getExtractedAutoloadInfo();
162 AutoLoader::loadFiles( $autoload['files'] );
163 AutoLoader::registerClasses( $autoload['classes'] );
164 AutoLoader::registerNamespaces( $autoload['namespaces'] );
165
166 return $extensionProcessor->getExtractedInfo();
167 }
168
169}
if(!defined('MEDIAWIKI')) if(!defined( 'MW_ENTRY_POINT')) global $IP
Environment checks.
Definition Setup.php:90
$wgAutoloadClasses
Definition Setup.php:141
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:68
This initializes autoloading for MediaWiki core, extensions, and vendored libraries.
static registerClasses(array $files)
Register a file to load the given class from.
static loadFiles(array $files)
Batch version of loadFile()
static registerNamespaces(array $dirs)
Register a directory to load the classes of a given namespace from, per PSR4.
This is a simple immutable HookRegistry which can be used to set up a local HookContainer in tests an...
A scheduled provider which loads extensions.
getName()
Get the symbolic name of the task.string
getProvidedNames()
Get a list of names of objects that this task promises to provide via $this->getContext()->provide()....
Base class for installer tasks.
Definition Task.php:24
getOption(string $name)
Get an installer option value.
Definition Task.php:190
getContext()
Get the execution context.
Definition Task.php:169
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
Load extension manifests and then aggregate their contents.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:44
$wgStyleDirectory
Config variable stub for the StyleDirectory setting, for use by phpdoc and IDEs.
$wgHooks
Config variable stub for the Hooks setting, for use by phpdoc and IDEs.
$wgExtensionDirectory
Config variable stub for the ExtensionDirectory setting, for use by phpdoc and IDEs.