3 use MediaWiki\MediaWikiServices
;
6 * ExtensionRegistry class
8 * The Registry loads JSON files, and uses a Processor
9 * to extract information from them. It also registers
10 * classes with the autoloader.
14 class ExtensionRegistry
{
17 * "requires" key that applies to MediaWiki core/$wgVersion
19 const MEDIAWIKI_CORE
= 'MediaWiki';
22 * Version of the highest supported manifest version
23 * Note: Update MANIFEST_VERSION_MW_VERSION when changing this
25 const MANIFEST_VERSION
= 2;
28 * MediaWiki version constraint representing what the current
29 * highest MANIFEST_VERSION is supported in
31 const MANIFEST_VERSION_MW_VERSION
= '>= 1.29.0';
34 * Version of the oldest supported manifest version
36 const OLDEST_MANIFEST_VERSION
= 1;
39 * Bump whenever the registration cache needs resetting
41 const CACHE_VERSION
= 6;
44 * Special key that defines the merge strategy
48 const MERGE_STRATEGY
= '_merge_strategy';
51 * Array of loaded things, keyed by name, values are credits information
58 * List of paths that should be loaded
62 protected $queued = [];
65 * Whether we are done loading things
69 private $finished = false;
72 * Items in the JSON file that aren't being
77 protected $attributes = [];
80 * @var ExtensionRegistry
82 private static $instance;
86 * @return ExtensionRegistry
88 public static function getInstance() {
89 if ( self
::$instance === null ) {
90 self
::$instance = new self();
93 return self
::$instance;
97 * @param string $path Absolute path to the JSON file
99 public function queue( $path ) {
100 global $wgExtensionInfoMTime;
102 $mtime = $wgExtensionInfoMTime;
103 if ( $mtime === false ) {
104 if ( file_exists( $path ) ) {
105 $mtime = filemtime( $path );
107 throw new Exception( "$path does not exist!" );
109 // @codeCoverageIgnoreStart
111 $err = error_get_last();
112 throw new Exception( "Couldn't stat $path: {$err['message']}" );
113 // @codeCoverageIgnoreEnd
116 $this->queued
[$path] = $mtime;
120 * @throws MWException If the queue is already marked as finished (no further things should
123 public function loadFromQueue() {
124 global $wgVersion, $wgDevelopmentWarnings;
125 if ( !$this->queued
) {
129 if ( $this->finished
) {
130 throw new MWException(
131 "The following paths tried to load late: "
132 . implode( ', ', array_keys( $this->queued
) )
136 // A few more things to vary the cache on
138 'registration' => self
::CACHE_VERSION
,
139 'mediawiki' => $wgVersion
142 // We use a try/catch because we don't want to fail here
143 // if $wgObjectCaches is not configured properly for APC setup
145 $cache = MediaWikiServices
::getInstance()->getLocalServerObjectCache();
146 } catch ( MWException
$e ) {
147 $cache = new EmptyBagOStuff();
149 // See if this queue is in APC
150 $key = $cache->makeKey(
152 md5( json_encode( $this->queued +
$versions ) )
154 $data = $cache->get( $key );
156 $this->exportExtractedData( $data );
158 $data = $this->readFromQueue( $this->queued
);
159 $this->exportExtractedData( $data );
160 // Do this late since we don't want to extract it since we already
161 // did that, but it should be cached
162 $data['globals']['wgAutoloadClasses'] +
= $data['autoload'];
163 unset( $data['autoload'] );
164 if ( !( $data['warnings'] && $wgDevelopmentWarnings ) ) {
165 // If there were no warnings that were shown, cache it
166 $cache->set( $key, $data, 60 * 60 * 24 );
173 * Get the current load queue. Not intended to be used
174 * outside of the installer.
178 public function getQueue() {
179 return $this->queued
;
183 * Clear the current load queue. Not intended to be used
184 * outside of the installer.
186 public function clearQueue() {
191 * After this is called, no more extensions can be loaded
195 public function finish() {
196 $this->finished
= true;
200 * Process a queue of extensions and return their extracted data
202 * @param array $queue keys are filenames, values are ignored
203 * @return array extracted info
206 public function readFromQueue( array $queue ) {
208 $autoloadClasses = [];
209 $autoloadNamespaces = [];
210 $autoloaderPaths = [];
211 $processor = new ExtensionProcessor();
212 $versionChecker = new VersionChecker( $wgVersion );
213 $extDependencies = [];
216 foreach ( $queue as $path => $mtime ) {
217 $json = file_get_contents( $path );
218 if ( $json === false ) {
219 throw new Exception( "Unable to read $path, does it exist?" );
221 $info = json_decode( $json, /* $assoc = */ true );
222 if ( !is_array( $info ) ) {
223 throw new Exception( "$path is not a valid JSON file." );
226 if ( !isset( $info['manifest_version'] ) ) {
228 "{$info['name']}'s extension.json or skin.json does not have manifest_version",
232 // For backwards-compatability, assume a version of 1
233 $info['manifest_version'] = 1;
235 $version = $info['manifest_version'];
236 if ( $version < self
::OLDEST_MANIFEST_VERSION ||
$version > self
::MANIFEST_VERSION
) {
237 $incompatible[] = "$path: unsupported manifest_version: {$version}";
240 $dir = dirname( $path );
241 if ( isset( $info['AutoloadClasses'] ) ) {
242 $autoload = $this->processAutoLoader( $dir, $info['AutoloadClasses'] );
243 $GLOBALS['wgAutoloadClasses'] +
= $autoload;
244 $autoloadClasses +
= $autoload;
246 if ( isset( $info['AutoloadNamespaces'] ) ) {
247 $autoloadNamespaces +
= $this->processAutoLoader( $dir, $info['AutoloadNamespaces'] );
250 // get all requirements/dependencies for this extension
251 $requires = $processor->getRequirements( $info );
253 // validate the information needed and add the requirements
254 if ( is_array( $requires ) && $requires && isset( $info['name'] ) ) {
255 $extDependencies[$info['name']] = $requires;
258 // Get extra paths for later inclusion
259 $autoloaderPaths = array_merge( $autoloaderPaths,
260 $processor->getExtraAutoloaderPaths( $dir, $info ) );
261 // Compatible, read and extract info
262 $processor->extractInfo( $path, $info, $version );
264 $data = $processor->getExtractedInfo();
265 $data['warnings'] = $warnings;
267 // check for incompatible extensions
268 $incompatible = array_merge(
271 ->setLoadedExtensionsAndSkins( $data['credits'] )
272 ->checkArray( $extDependencies )
275 if ( $incompatible ) {
276 if ( count( $incompatible ) === 1 ) {
277 throw new Exception( $incompatible[0] );
279 throw new Exception( implode( "\n", $incompatible ) );
283 // Need to set this so we can += to it later
284 $data['globals']['wgAutoloadClasses'] = [];
285 $data['autoload'] = $autoloadClasses;
286 $data['autoloaderPaths'] = $autoloaderPaths;
287 $data['autoloaderNS'] = $autoloadNamespaces;
291 protected function exportExtractedData( array $info ) {
292 foreach ( $info['globals'] as $key => $val ) {
293 // If a merge strategy is set, read it and remove it from the value
294 // so it doesn't accidentally end up getting set.
295 if ( is_array( $val ) && isset( $val[self
::MERGE_STRATEGY
] ) ) {
296 $mergeStrategy = $val[self
::MERGE_STRATEGY
];
297 unset( $val[self
::MERGE_STRATEGY
] );
299 $mergeStrategy = 'array_merge';
302 // Optimistic: If the global is not set, or is an empty array, replace it entirely.
303 // Will be O(1) performance.
304 if ( !array_key_exists( $key, $GLOBALS ) ||
( is_array( $GLOBALS[$key] ) && !$GLOBALS[$key] ) ) {
305 $GLOBALS[$key] = $val;
309 if ( !is_array( $GLOBALS[$key] ) ||
!is_array( $val ) ) {
310 // config setting that has already been overridden, don't set it
314 switch ( $mergeStrategy ) {
315 case 'array_merge_recursive':
316 $GLOBALS[$key] = array_merge_recursive( $GLOBALS[$key], $val );
318 case 'array_replace_recursive':
319 $GLOBALS[$key] = array_replace_recursive( $GLOBALS[$key], $val );
321 case 'array_plus_2d':
322 $GLOBALS[$key] = wfArrayPlus2d( $GLOBALS[$key], $val );
325 $GLOBALS[$key] +
= $val;
328 $GLOBALS[$key] = array_merge( $val, $GLOBALS[$key] );
331 throw new UnexpectedValueException( "Unknown merge strategy '$mergeStrategy'" );
335 if ( isset( $info['autoloaderNS'] ) ) {
336 AutoLoader
::$psr4Namespaces +
= $info['autoloaderNS'];
339 foreach ( $info['defines'] as $name => $val ) {
340 define( $name, $val );
342 foreach ( $info['autoloaderPaths'] as $path ) {
343 if ( file_exists( $path ) ) {
348 $this->loaded +
= $info['credits'];
349 if ( $info['attributes'] ) {
350 if ( !$this->attributes
) {
351 $this->attributes
= $info['attributes'];
353 $this->attributes
= array_merge_recursive( $this->attributes
, $info['attributes'] );
357 foreach ( $info['callbacks'] as $name => $cb ) {
358 if ( !is_callable( $cb ) ) {
359 if ( is_array( $cb ) ) {
360 $cb = '[ ' . implode( ', ', $cb ) . ' ]';
362 throw new UnexpectedValueException( "callback '$cb' is not callable" );
364 call_user_func( $cb, $info['credits'][$name] );
369 * Loads and processes the given JSON file without delay
371 * If some extensions are already queued, this will load
374 * @param string $path Absolute path to the JSON file
376 public function load( $path ) {
377 $this->loadFromQueue(); // First clear the queue
378 $this->queue( $path );
379 $this->loadFromQueue();
383 * Whether a thing has been loaded
384 * @param string $name
387 public function isLoaded( $name ) {
388 return isset( $this->loaded
[$name] );
392 * @param string $name
395 public function getAttribute( $name ) {
396 if ( isset( $this->attributes
[$name] ) ) {
397 return $this->attributes
[$name];
404 * Get information about all things
408 public function getAllThings() {
409 return $this->loaded
;
413 * Fully expand autoloader paths
416 * @param array $files
419 protected function processAutoLoader( $dir, array $files ) {
420 // Make paths absolute, relative to the JSON file
421 foreach ( $files as &$file ) {
422 $file = "$dir/$file";