4 * ExtensionRegistry class
6 * The Registry loads JSON files, and uses a Processor
7 * to extract information from them. It also registers
8 * classes with the autoloader.
12 class ExtensionRegistry
{
15 * "requires" key that applies to MediaWiki core/$wgVersion
17 const MEDIAWIKI_CORE
= 'MediaWiki';
20 * Version of the highest supported manifest version
21 * Note: Update MANIFEST_VERSION_MW_VERSION when changing this
23 const MANIFEST_VERSION
= 2;
26 * MediaWiki version constraint representing what the current
27 * highest MANIFEST_VERSION is supported in
29 const MANIFEST_VERSION_MW_VERSION
= '>= 1.29.0';
32 * Version of the oldest supported manifest version
34 const OLDEST_MANIFEST_VERSION
= 1;
37 * Bump whenever the registration cache needs resetting
39 const CACHE_VERSION
= 6;
42 * Special key that defines the merge strategy
46 const MERGE_STRATEGY
= '_merge_strategy';
49 * Array of loaded things, keyed by name, values are credits information
56 * List of paths that should be loaded
60 protected $queued = [];
63 * Whether we are done loading things
67 private $finished = false;
70 * Items in the JSON file that aren't being
75 protected $attributes = [];
78 * @var ExtensionRegistry
80 private static $instance;
84 * @return ExtensionRegistry
86 public static function getInstance() {
87 if ( self
::$instance === null ) {
88 self
::$instance = new self();
91 return self
::$instance;
95 * @param string $path Absolute path to the JSON file
97 public function queue( $path ) {
98 global $wgExtensionInfoMTime;
100 $mtime = $wgExtensionInfoMTime;
101 if ( $mtime === false ) {
102 if ( file_exists( $path ) ) {
103 $mtime = filemtime( $path );
105 throw new Exception( "$path does not exist!" );
107 // @codeCoverageIgnoreStart
108 if ( $mtime === false ) {
109 $err = error_get_last();
110 throw new Exception( "Couldn't stat $path: {$err['message']}" );
111 // @codeCoverageIgnoreEnd
114 $this->queued
[$path] = $mtime;
118 * @throws MWException If the queue is already marked as finished (no further things should
121 public function loadFromQueue() {
122 global $wgVersion, $wgDevelopmentWarnings;
123 if ( !$this->queued
) {
127 if ( $this->finished
) {
128 throw new MWException(
129 "The following paths tried to load late: "
130 . implode( ', ', array_keys( $this->queued
) )
134 // A few more things to vary the cache on
136 'registration' => self
::CACHE_VERSION
,
137 'mediawiki' => $wgVersion
140 // We use a try/catch because we don't want to fail here
141 // if $wgObjectCaches is not configured properly for APC setup
143 // Don't use MediaWikiServices here to prevent instantiating it before extensions have
145 $cacheId = ObjectCache
::detectLocalServerCache();
146 $cache = ObjectCache
::newFromId( $cacheId );
147 } catch ( MWException
$e ) {
148 $cache = new EmptyBagOStuff();
150 // See if this queue is in APC
151 $key = $cache->makeKey(
153 md5( json_encode( $this->queued +
$versions ) )
155 $data = $cache->get( $key );
157 $this->exportExtractedData( $data );
159 $data = $this->readFromQueue( $this->queued
);
160 $this->exportExtractedData( $data );
161 // Do this late since we don't want to extract it since we already
162 // did that, but it should be cached
163 $data['globals']['wgAutoloadClasses'] +
= $data['autoload'];
164 unset( $data['autoload'] );
165 if ( !( $data['warnings'] && $wgDevelopmentWarnings ) ) {
166 // If there were no warnings that were shown, cache it
167 $cache->set( $key, $data, 60 * 60 * 24 );
174 * Get the current load queue. Not intended to be used
175 * outside of the installer.
179 public function getQueue() {
180 return $this->queued
;
184 * Clear the current load queue. Not intended to be used
185 * outside of the installer.
187 public function clearQueue() {
192 * After this is called, no more extensions can be loaded
196 public function finish() {
197 $this->finished
= true;
201 * Process a queue of extensions and return their extracted data
203 * @param array $queue keys are filenames, values are ignored
204 * @return array extracted info
206 * @throws ExtensionDependencyError
208 public function readFromQueue( array $queue ) {
210 $autoloadClasses = [];
211 $autoloadNamespaces = [];
212 $autoloaderPaths = [];
213 $processor = new ExtensionProcessor();
214 $versionChecker = new VersionChecker( $wgVersion );
215 $extDependencies = [];
218 foreach ( $queue as $path => $mtime ) {
219 $json = file_get_contents( $path );
220 if ( $json === false ) {
221 throw new Exception( "Unable to read $path, does it exist?" );
223 $info = json_decode( $json, /* $assoc = */ true );
224 if ( !is_array( $info ) ) {
225 throw new Exception( "$path is not a valid JSON file." );
228 if ( !isset( $info['manifest_version'] ) ) {
230 "{$info['name']}'s extension.json or skin.json does not have manifest_version",
234 // For backwards-compatability, assume a version of 1
235 $info['manifest_version'] = 1;
237 $version = $info['manifest_version'];
238 if ( $version < self
::OLDEST_MANIFEST_VERSION ||
$version > self
::MANIFEST_VERSION
) {
239 $incompatible[] = "$path: unsupported manifest_version: {$version}";
242 $dir = dirname( $path );
243 if ( isset( $info['AutoloadClasses'] ) ) {
244 $autoload = $this->processAutoLoader( $dir, $info['AutoloadClasses'] );
245 $GLOBALS['wgAutoloadClasses'] +
= $autoload;
246 $autoloadClasses +
= $autoload;
248 if ( isset( $info['AutoloadNamespaces'] ) ) {
249 $autoloadNamespaces +
= $this->processAutoLoader( $dir, $info['AutoloadNamespaces'] );
250 AutoLoader
::$psr4Namespaces +
= $autoloadNamespaces;
253 // get all requirements/dependencies for this extension
254 $requires = $processor->getRequirements( $info );
256 // validate the information needed and add the requirements
257 if ( is_array( $requires ) && $requires && isset( $info['name'] ) ) {
258 $extDependencies[$info['name']] = $requires;
261 // Get extra paths for later inclusion
262 $autoloaderPaths = array_merge( $autoloaderPaths,
263 $processor->getExtraAutoloaderPaths( $dir, $info ) );
264 // Compatible, read and extract info
265 $processor->extractInfo( $path, $info, $version );
267 $data = $processor->getExtractedInfo();
268 $data['warnings'] = $warnings;
270 // check for incompatible extensions
271 $incompatible = array_merge(
274 ->setLoadedExtensionsAndSkins( $data['credits'] )
275 ->checkArray( $extDependencies )
278 if ( $incompatible ) {
279 throw new ExtensionDependencyError( $incompatible );
282 // Need to set this so we can += to it later
283 $data['globals']['wgAutoloadClasses'] = [];
284 $data['autoload'] = $autoloadClasses;
285 $data['autoloaderPaths'] = $autoloaderPaths;
286 $data['autoloaderNS'] = $autoloadNamespaces;
290 protected function exportExtractedData( array $info ) {
291 foreach ( $info['globals'] as $key => $val ) {
292 // If a merge strategy is set, read it and remove it from the value
293 // so it doesn't accidentally end up getting set.
294 if ( is_array( $val ) && isset( $val[self
::MERGE_STRATEGY
] ) ) {
295 $mergeStrategy = $val[self
::MERGE_STRATEGY
];
296 unset( $val[self
::MERGE_STRATEGY
] );
298 $mergeStrategy = 'array_merge';
301 // Optimistic: If the global is not set, or is an empty array, replace it entirely.
302 // Will be O(1) performance.
303 if ( !array_key_exists( $key, $GLOBALS ) ||
( is_array( $GLOBALS[$key] ) && !$GLOBALS[$key] ) ) {
304 $GLOBALS[$key] = $val;
308 if ( !is_array( $GLOBALS[$key] ) ||
!is_array( $val ) ) {
309 // config setting that has already been overridden, don't set it
313 switch ( $mergeStrategy ) {
314 case 'array_merge_recursive':
315 $GLOBALS[$key] = array_merge_recursive( $GLOBALS[$key], $val );
317 case 'array_replace_recursive':
318 $GLOBALS[$key] = array_replace_recursive( $GLOBALS[$key], $val );
320 case 'array_plus_2d':
321 $GLOBALS[$key] = wfArrayPlus2d( $GLOBALS[$key], $val );
324 $GLOBALS[$key] +
= $val;
327 $GLOBALS[$key] = array_merge( $val, $GLOBALS[$key] );
330 throw new UnexpectedValueException( "Unknown merge strategy '$mergeStrategy'" );
334 if ( isset( $info['autoloaderNS'] ) ) {
335 AutoLoader
::$psr4Namespaces +
= $info['autoloaderNS'];
338 foreach ( $info['defines'] as $name => $val ) {
339 define( $name, $val );
341 foreach ( $info['autoloaderPaths'] as $path ) {
342 if ( file_exists( $path ) ) {
347 $this->loaded +
= $info['credits'];
348 if ( $info['attributes'] ) {
349 if ( !$this->attributes
) {
350 $this->attributes
= $info['attributes'];
352 $this->attributes
= array_merge_recursive( $this->attributes
, $info['attributes'] );
356 foreach ( $info['callbacks'] as $name => $cb ) {
357 if ( !is_callable( $cb ) ) {
358 if ( is_array( $cb ) ) {
359 $cb = '[ ' . implode( ', ', $cb ) . ' ]';
361 throw new UnexpectedValueException( "callback '$cb' is not callable" );
363 call_user_func( $cb, $info['credits'][$name] );
368 * Loads and processes the given JSON file without delay
370 * If some extensions are already queued, this will load
373 * @param string $path Absolute path to the JSON file
375 public function load( $path ) {
376 $this->loadFromQueue(); // First clear the queue
377 $this->queue( $path );
378 $this->loadFromQueue();
382 * Whether a thing has been loaded
383 * @param string $name
386 public function isLoaded( $name ) {
387 return isset( $this->loaded
[$name] );
391 * @param string $name
394 public function getAttribute( $name ) {
395 if ( isset( $this->attributes
[$name] ) ) {
396 return $this->attributes
[$name];
403 * Get information about all things
407 public function getAllThings() {
408 return $this->loaded
;
412 * Fully expand autoloader paths
415 * @param array $files
418 protected function processAutoLoader( $dir, array $files ) {
419 // Make paths absolute, relative to the JSON file
420 foreach ( $files as &$file ) {
421 $file = "$dir/$file";