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;
85 * @return ExtensionRegistry
87 public static function getInstance() {
88 if ( self
::$instance === null ) {
89 self
::$instance = new self();
92 return self
::$instance;
96 * @param string $path Absolute path to the JSON file
98 public function queue( $path ) {
99 global $wgExtensionInfoMTime;
101 $mtime = $wgExtensionInfoMTime;
102 if ( $mtime === false ) {
103 if ( file_exists( $path ) ) {
104 $mtime = filemtime( $path );
106 throw new Exception( "$path does not exist!" );
109 $err = error_get_last();
110 throw new Exception( "Couldn't stat $path: {$err['message']}" );
113 $this->queued
[$path] = $mtime;
117 * @throws MWException If the queue is already marked as finished (no further things should
120 public function loadFromQueue() {
121 global $wgVersion, $wgDevelopmentWarnings;
122 if ( !$this->queued
) {
126 if ( $this->finished
) {
127 throw new MWException(
128 "The following paths tried to load late: "
129 . implode( ', ', array_keys( $this->queued
) )
133 // A few more things to vary the cache on
135 'registration' => self
::CACHE_VERSION
,
136 'mediawiki' => $wgVersion
139 // We use a try/catch because we don't want to fail here
140 // if $wgObjectCaches is not configured properly for APC setup
142 $cache = MediaWikiServices
::getInstance()->getLocalServerObjectCache();
143 } catch ( MWException
$e ) {
144 $cache = new EmptyBagOStuff();
146 // See if this queue is in APC
147 $key = $cache->makeKey(
149 md5( json_encode( $this->queued +
$versions ) )
151 $data = $cache->get( $key );
153 $this->exportExtractedData( $data );
155 $data = $this->readFromQueue( $this->queued
);
156 $this->exportExtractedData( $data );
157 // Do this late since we don't want to extract it since we already
158 // did that, but it should be cached
159 $data['globals']['wgAutoloadClasses'] +
= $data['autoload'];
160 unset( $data['autoload'] );
161 if ( !( $data['warnings'] && $wgDevelopmentWarnings ) ) {
162 // If there were no warnings that were shown, cache it
163 $cache->set( $key, $data, 60 * 60 * 24 );
170 * Get the current load queue. Not intended to be used
171 * outside of the installer.
175 public function getQueue() {
176 return $this->queued
;
180 * Clear the current load queue. Not intended to be used
181 * outside of the installer.
183 public function clearQueue() {
188 * After this is called, no more extensions can be loaded
192 public function finish() {
193 $this->finished
= true;
197 * Process a queue of extensions and return their extracted data
199 * @param array $queue keys are filenames, values are ignored
200 * @return array extracted info
203 public function readFromQueue( array $queue ) {
205 $autoloadClasses = [];
206 $autoloadNamespaces = [];
207 $autoloaderPaths = [];
208 $processor = new ExtensionProcessor();
209 $versionChecker = new VersionChecker( $wgVersion );
210 $extDependencies = [];
213 foreach ( $queue as $path => $mtime ) {
214 $json = file_get_contents( $path );
215 if ( $json === false ) {
216 throw new Exception( "Unable to read $path, does it exist?" );
218 $info = json_decode( $json, /* $assoc = */ true );
219 if ( !is_array( $info ) ) {
220 throw new Exception( "$path is not a valid JSON file." );
223 if ( !isset( $info['manifest_version'] ) ) {
225 "{$info['name']}'s extension.json or skin.json does not have manifest_version",
229 // For backwards-compatability, assume a version of 1
230 $info['manifest_version'] = 1;
232 $version = $info['manifest_version'];
233 if ( $version < self
::OLDEST_MANIFEST_VERSION ||
$version > self
::MANIFEST_VERSION
) {
234 $incompatible[] = "$path: unsupported manifest_version: {$version}";
237 $dir = dirname( $path );
238 if ( isset( $info['AutoloadClasses'] ) ) {
239 $autoload = $this->processAutoLoader( $dir, $info['AutoloadClasses'] );
240 $GLOBALS['wgAutoloadClasses'] +
= $autoload;
241 $autoloadClasses +
= $autoload;
243 if ( isset( $info['AutoloadNamespaces'] ) ) {
244 $autoloadNamespaces +
= $this->processAutoLoader( $dir, $info['AutoloadNamespaces'] );
247 // get all requirements/dependencies for this extension
248 $requires = $processor->getRequirements( $info );
250 // validate the information needed and add the requirements
251 if ( is_array( $requires ) && $requires && isset( $info['name'] ) ) {
252 $extDependencies[$info['name']] = $requires;
255 // Get extra paths for later inclusion
256 $autoloaderPaths = array_merge( $autoloaderPaths,
257 $processor->getExtraAutoloaderPaths( $dir, $info ) );
258 // Compatible, read and extract info
259 $processor->extractInfo( $path, $info, $version );
261 $data = $processor->getExtractedInfo();
262 $data['warnings'] = $warnings;
264 // check for incompatible extensions
265 $incompatible = array_merge(
268 ->setLoadedExtensionsAndSkins( $data['credits'] )
269 ->checkArray( $extDependencies )
272 if ( $incompatible ) {
273 if ( count( $incompatible ) === 1 ) {
274 throw new Exception( $incompatible[0] );
276 throw new Exception( implode( "\n", $incompatible ) );
280 // Need to set this so we can += to it later
281 $data['globals']['wgAutoloadClasses'] = [];
282 $data['autoload'] = $autoloadClasses;
283 $data['autoloaderPaths'] = $autoloaderPaths;
284 $data['autoloaderNS'] = $autoloadNamespaces;
288 protected function exportExtractedData( array $info ) {
289 foreach ( $info['globals'] as $key => $val ) {
290 // If a merge strategy is set, read it and remove it from the value
291 // so it doesn't accidentally end up getting set.
292 if ( is_array( $val ) && isset( $val[self
::MERGE_STRATEGY
] ) ) {
293 $mergeStrategy = $val[self
::MERGE_STRATEGY
];
294 unset( $val[self
::MERGE_STRATEGY
] );
296 $mergeStrategy = 'array_merge';
299 // Optimistic: If the global is not set, or is an empty array, replace it entirely.
300 // Will be O(1) performance.
301 if ( !array_key_exists( $key, $GLOBALS ) ||
( is_array( $GLOBALS[$key] ) && !$GLOBALS[$key] ) ) {
302 $GLOBALS[$key] = $val;
306 if ( !is_array( $GLOBALS[$key] ) ||
!is_array( $val ) ) {
307 // config setting that has already been overridden, don't set it
311 switch ( $mergeStrategy ) {
312 case 'array_merge_recursive':
313 $GLOBALS[$key] = array_merge_recursive( $GLOBALS[$key], $val );
315 case 'array_replace_recursive':
316 $GLOBALS[$key] = array_replace_recursive( $GLOBALS[$key], $val );
318 case 'array_plus_2d':
319 $GLOBALS[$key] = wfArrayPlus2d( $GLOBALS[$key], $val );
322 $GLOBALS[$key] +
= $val;
325 $GLOBALS[$key] = array_merge( $val, $GLOBALS[$key] );
328 throw new UnexpectedValueException( "Unknown merge strategy '$mergeStrategy'" );
332 if ( isset( $info['autoloaderNS'] ) ) {
333 AutoLoader
::$psr4Namespaces +
= $info['autoloaderNS'];
336 foreach ( $info['defines'] as $name => $val ) {
337 define( $name, $val );
339 foreach ( $info['autoloaderPaths'] as $path ) {
340 if ( file_exists( $path ) ) {
345 $this->loaded +
= $info['credits'];
346 if ( $info['attributes'] ) {
347 if ( !$this->attributes
) {
348 $this->attributes
= $info['attributes'];
350 $this->attributes
= array_merge_recursive( $this->attributes
, $info['attributes'] );
354 foreach ( $info['callbacks'] as $name => $cb ) {
355 if ( !is_callable( $cb ) ) {
356 if ( is_array( $cb ) ) {
357 $cb = '[ ' . implode( ', ', $cb ) . ' ]';
359 throw new UnexpectedValueException( "callback '$cb' is not callable" );
361 call_user_func( $cb, $info['credits'][$name] );
366 * Loads and processes the given JSON file without delay
368 * If some extensions are already queued, this will load
371 * @param string $path Absolute path to the JSON file
373 public function load( $path ) {
374 $this->loadFromQueue(); // First clear the queue
375 $this->queue( $path );
376 $this->loadFromQueue();
380 * Whether a thing has been loaded
381 * @param string $name
384 public function isLoaded( $name ) {
385 return isset( $this->loaded
[$name] );
389 * @param string $name
392 public function getAttribute( $name ) {
393 if ( isset( $this->attributes
[$name] ) ) {
394 return $this->attributes
[$name];
401 * Get information about all things
405 public function getAllThings() {
406 return $this->loaded
;
410 * Mark a thing as loaded
412 * @param string $name
413 * @param array $credits
415 protected function markLoaded( $name, array $credits ) {
416 $this->loaded
[$name] = $credits;
420 * Fully expand autoloader paths
423 * @param array $files
426 protected function processAutoLoader( $dir, array $files ) {
427 // Make paths absolute, relative to the JSON file
428 foreach ( $files as &$file ) {
429 $file = "$dir/$file";