MediaWiki master
ExtensionsProvider.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Installer\Task;
4
5use AutoLoader;
6use MediaWiki\HookContainer\HookContainer;
7use MediaWiki\HookContainer\StaticHookRegistry;
8use MediaWiki\MainConfigSchema;
9use MediaWiki\MediaWikiServices;
10use MediaWiki\Registration\ExtensionProcessor;
11use MediaWiki\Status\Status;
12
18class ExtensionsProvider extends Task {
20 public function getName() {
21 return 'extensions';
22 }
23
25 public function getProvidedNames() {
26 return [ 'HookContainer', 'ExtensionTaskSpecs' ];
27 }
28
29 public function execute(): Status {
30 if ( !$this->getOption( 'Extensions' ) ) {
31 $this->getContext()->provide( 'ExtensionTaskSpecs', [] );
32 return Status::newGood();
33 }
34
35 // Marker for DatabaseUpdater::loadExtensions so we don't
36 // double load extensions
37 define( 'MW_EXTENSIONS_LOADED', true );
38
39 $legacySchemaHooks = $this->getAutoExtensionLegacySchemaHooks();
40 $data = $this->getAutoExtensionData();
41 if ( isset( $data['globals']['wgHooks']['LoadExtensionSchemaUpdates'] ) ) {
42 $legacySchemaHooks = array_merge( $legacySchemaHooks,
43 $data['globals']['wgHooks']['LoadExtensionSchemaUpdates'] );
44 }
45 $extDeprecatedHooks = $data['attributes']['DeprecatedHooks'] ?? [];
46
47 $legacyHooks = $legacySchemaHooks ? [ 'LoadExtensionSchemaUpdates' => $legacySchemaHooks ] : [];
48 $this->getContext()->provide( 'HookContainer',
49 new HookContainer(
50 new StaticHookRegistry(
51 $legacyHooks,
52 $data['attributes']['Hooks'] ?? [],
53 $extDeprecatedHooks
54 ),
55 MediaWikiServices::getInstance()->getObjectFactory()
56 )
57 );
58 $this->getContext()->provide( 'ExtensionTaskSpecs',
59 $data['attributes']['InstallerTasks'] ?? [] );
60
61 return Status::newGood();
62 }
63
67 protected function getExtensionsDir() {
68 return MW_INSTALL_PATH . '/extensions';
69 }
70
74 protected function getSkinsDir() {
75 return MW_INSTALL_PATH . '/skins';
76 }
77
84 private function getAutoExtensionLegacySchemaHooks() {
85 $exts = $this->getOption( 'Extensions' );
86 $extensionsDir = $this->getExtensionsDir();
87 $files = [];
88 foreach ( $exts as $e ) {
89 if ( file_exists( "$extensionsDir/$e/$e.php" ) ) {
90 $files[] = "$extensionsDir/$e/$e.php";
91 }
92 }
93
94 if ( $files ) {
95 return $this->includeExtensionFiles( $files );
96 } else {
97 return [];
98 }
99 }
100
108 private function includeExtensionFiles( $files ) {
117 // Extract the defaults into the current scope
118 foreach ( MainConfigSchema::listDefaultValues( 'wg' ) as $var => $value ) {
119 $$var = $value;
120 }
121
122 // phpcs:ignore MediaWiki.VariableAnalysis.UnusedGlobalVariables
124 $wgExtensionDirectory = $this->getExtensionsDir();
125 $wgStyleDirectory = $this->getSkinsDir();
126
127 foreach ( $files as $file ) {
128 require_once $file;
129 }
130
131 // Ignore everyone else's hooks. Lord knows what someone might be doing
132 // in ParserFirstCallInit (see T29171)
133 // @phpcs:disable MediaWiki.VariableAnalysis.MisleadingGlobalNames.Misleading$wgHooks
134 // @phpcs:ignore Generic.Files.LineLength.TooLong
135 // @phan-suppress-next-line PhanUndeclaredVariable,PhanCoalescingAlwaysNull $wgHooks is defined by MainConfigSchema
136 $hooksWeWant = $wgHooks['LoadExtensionSchemaUpdates'] ?? [];
137 // @phpcs:enable MediaWiki.VariableAnalysis.MisleadingGlobalNames.Misleading$wgHooks
138 return $hooksWeWant;
139 }
140
147 private function getAutoExtensionData() {
148 $exts = $this->getOption( 'Extensions' );
149
150 $extensionProcessor = new ExtensionProcessor();
151 foreach ( $exts as $e ) {
152 $jsonPath = $this->getExtensionsDir() . "/$e/extension.json";
153 if ( file_exists( $jsonPath ) ) {
154 $extensionProcessor->extractInfoFromFile( $jsonPath );
155 }
156 }
157
158 $autoload = $extensionProcessor->getExtractedAutoloadInfo();
159 AutoLoader::loadFiles( $autoload['files'] );
160 AutoLoader::registerClasses( $autoload['classes'] );
161 AutoLoader::registerNamespaces( $autoload['namespaces'] );
162
163 return $extensionProcessor->getExtractedInfo();
164 }
165
166}
$wgAutoloadClasses
Definition Setup.php:139
if(!defined('MEDIAWIKI')) if(!defined( 'MW_ENTRY_POINT')) $IP
Environment checks.
Definition Setup.php:102
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:72
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.
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
static listDefaultValues(string $prefix='')
Returns a generator for iterating over all config settings and their default values.
static getInstance()
Returns the global default instance of the top level service locator.
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.