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
24 const MANIFEST_VERSION
= 2;
27 * Version of the oldest supported manifest version
29 const OLDEST_MANIFEST_VERSION
= 1;
32 * Bump whenever the registration cache needs resetting
34 const CACHE_VERSION
= 6;
37 * Special key that defines the merge strategy
41 const MERGE_STRATEGY
= '_merge_strategy';
44 * Array of loaded things, keyed by name, values are credits information
51 * List of paths that should be loaded
55 protected $queued = [];
58 * Whether we are done loading things
62 private $finished = false;
65 * Items in the JSON file that aren't being
70 protected $attributes = [];
73 * @var ExtensionRegistry
75 private static $instance;
78 * @return ExtensionRegistry
80 public static function getInstance() {
81 if ( self
::$instance === null ) {
82 self
::$instance = new self();
85 return self
::$instance;
89 * @param string $path Absolute path to the JSON file
91 public function queue( $path ) {
92 global $wgExtensionInfoMTime;
94 $mtime = $wgExtensionInfoMTime;
95 if ( $mtime === false ) {
96 if ( file_exists( $path ) ) {
97 $mtime = filemtime( $path );
99 throw new Exception( "$path does not exist!" );
102 $err = error_get_last();
103 throw new Exception( "Couldn't stat $path: {$err['message']}" );
106 $this->queued
[$path] = $mtime;
110 * @throws MWException If the queue is already marked as finished (no further things should
113 public function loadFromQueue() {
114 global $wgVersion, $wgDevelopmentWarnings;
115 if ( !$this->queued
) {
119 if ( $this->finished
) {
120 throw new MWException(
121 "The following paths tried to load late: "
122 . implode( ', ', array_keys( $this->queued
) )
126 // A few more things to vary the cache on
128 'registration' => self
::CACHE_VERSION
,
129 'mediawiki' => $wgVersion
132 // We use a try/catch because we don't want to fail here
133 // if $wgObjectCaches is not configured properly for APC setup
135 $cache = MediaWikiServices
::getInstance()->getLocalServerObjectCache();
136 } catch ( MWException
$e ) {
137 $cache = new EmptyBagOStuff();
139 // See if this queue is in APC
140 $key = $cache->makeKey(
142 md5( json_encode( $this->queued +
$versions ) )
144 $data = $cache->get( $key );
146 $this->exportExtractedData( $data );
148 $data = $this->readFromQueue( $this->queued
);
149 $this->exportExtractedData( $data );
150 // Do this late since we don't want to extract it since we already
151 // did that, but it should be cached
152 $data['globals']['wgAutoloadClasses'] +
= $data['autoload'];
153 unset( $data['autoload'] );
154 if ( !( $data['warnings'] && $wgDevelopmentWarnings ) ) {
155 // If there were no warnings that were shown, cache it
156 $cache->set( $key, $data, 60 * 60 * 24 );
163 * Get the current load queue. Not intended to be used
164 * outside of the installer.
168 public function getQueue() {
169 return $this->queued
;
173 * Clear the current load queue. Not intended to be used
174 * outside of the installer.
176 public function clearQueue() {
181 * After this is called, no more extensions can be loaded
185 public function finish() {
186 $this->finished
= true;
190 * Process a queue of extensions and return their extracted data
192 * @param array $queue keys are filenames, values are ignored
193 * @return array extracted info
196 public function readFromQueue( array $queue ) {
198 $autoloadClasses = [];
199 $autoloaderPaths = [];
200 $processor = new ExtensionProcessor();
201 $versionChecker = new VersionChecker( $wgVersion );
202 $extDependencies = [];
205 foreach ( $queue as $path => $mtime ) {
206 $json = file_get_contents( $path );
207 if ( $json === false ) {
208 throw new Exception( "Unable to read $path, does it exist?" );
210 $info = json_decode( $json, /* $assoc = */ true );
211 if ( !is_array( $info ) ) {
212 throw new Exception( "$path is not a valid JSON file." );
215 if ( !isset( $info['manifest_version'] ) ) {
217 "{$info['name']}'s extension.json or skin.json does not have manifest_version",
221 // For backwards-compatability, assume a version of 1
222 $info['manifest_version'] = 1;
224 $version = $info['manifest_version'];
225 if ( $version < self
::OLDEST_MANIFEST_VERSION ||
$version > self
::MANIFEST_VERSION
) {
226 $incompatible[] = "$path: unsupported manifest_version: {$version}";
229 $autoload = $this->processAutoLoader( dirname( $path ), $info );
230 // Set up the autoloader now so custom processors will work
231 $GLOBALS['wgAutoloadClasses'] +
= $autoload;
232 $autoloadClasses +
= $autoload;
234 // get all requirements/dependencies for this extension
235 $requires = $processor->getRequirements( $info );
237 // validate the information needed and add the requirements
238 if ( is_array( $requires ) && $requires && isset( $info['name'] ) ) {
239 $extDependencies[$info['name']] = $requires;
242 // Get extra paths for later inclusion
243 $autoloaderPaths = array_merge( $autoloaderPaths,
244 $processor->getExtraAutoloaderPaths( dirname( $path ), $info ) );
245 // Compatible, read and extract info
246 $processor->extractInfo( $path, $info, $version );
248 $data = $processor->getExtractedInfo();
249 $data['warnings'] = $warnings;
251 // check for incompatible extensions
252 $incompatible = array_merge(
255 ->setLoadedExtensionsAndSkins( $data['credits'] )
256 ->checkArray( $extDependencies )
259 if ( $incompatible ) {
260 if ( count( $incompatible ) === 1 ) {
261 throw new Exception( $incompatible[0] );
263 throw new Exception( implode( "\n", $incompatible ) );
267 // Need to set this so we can += to it later
268 $data['globals']['wgAutoloadClasses'] = [];
269 $data['autoload'] = $autoloadClasses;
270 $data['autoloaderPaths'] = $autoloaderPaths;
274 protected function exportExtractedData( array $info ) {
275 foreach ( $info['globals'] as $key => $val ) {
276 // If a merge strategy is set, read it and remove it from the value
277 // so it doesn't accidentally end up getting set.
278 if ( is_array( $val ) && isset( $val[self
::MERGE_STRATEGY
] ) ) {
279 $mergeStrategy = $val[self
::MERGE_STRATEGY
];
280 unset( $val[self
::MERGE_STRATEGY
] );
282 $mergeStrategy = 'array_merge';
285 // Optimistic: If the global is not set, or is an empty array, replace it entirely.
286 // Will be O(1) performance.
287 if ( !isset( $GLOBALS[$key] ) ||
( is_array( $GLOBALS[$key] ) && !$GLOBALS[$key] ) ) {
288 $GLOBALS[$key] = $val;
292 if ( !is_array( $GLOBALS[$key] ) ||
!is_array( $val ) ) {
293 // config setting that has already been overridden, don't set it
297 switch ( $mergeStrategy ) {
298 case 'array_merge_recursive':
299 $GLOBALS[$key] = array_merge_recursive( $GLOBALS[$key], $val );
301 case 'array_replace_recursive':
302 $GLOBALS[$key] = array_replace_recursive( $GLOBALS[$key], $val );
304 case 'array_plus_2d':
305 $GLOBALS[$key] = wfArrayPlus2d( $GLOBALS[$key], $val );
308 $GLOBALS[$key] +
= $val;
311 $GLOBALS[$key] = array_merge( $val, $GLOBALS[$key] );
314 throw new UnexpectedValueException( "Unknown merge strategy '$mergeStrategy'" );
318 foreach ( $info['defines'] as $name => $val ) {
319 define( $name, $val );
321 foreach ( $info['autoloaderPaths'] as $path ) {
325 $this->loaded +
= $info['credits'];
326 if ( $info['attributes'] ) {
327 if ( !$this->attributes
) {
328 $this->attributes
= $info['attributes'];
330 $this->attributes
= array_merge_recursive( $this->attributes
, $info['attributes'] );
334 foreach ( $info['callbacks'] as $name => $cb ) {
335 if ( !is_callable( $cb ) ) {
336 if ( is_array( $cb ) ) {
337 $cb = '[ ' . implode( ', ', $cb ) . ' ]';
339 throw new UnexpectedValueException( "callback '$cb' is not callable" );
341 call_user_func( $cb, $info['credits'][$name] );
346 * Loads and processes the given JSON file without delay
348 * If some extensions are already queued, this will load
351 * @param string $path Absolute path to the JSON file
353 public function load( $path ) {
354 $this->loadFromQueue(); // First clear the queue
355 $this->queue( $path );
356 $this->loadFromQueue();
360 * Whether a thing has been loaded
361 * @param string $name
364 public function isLoaded( $name ) {
365 return isset( $this->loaded
[$name] );
369 * @param string $name
372 public function getAttribute( $name ) {
373 if ( isset( $this->attributes
[$name] ) ) {
374 return $this->attributes
[$name];
381 * Get information about all things
385 public function getAllThings() {
386 return $this->loaded
;
390 * Mark a thing as loaded
392 * @param string $name
393 * @param array $credits
395 protected function markLoaded( $name, array $credits ) {
396 $this->loaded
[$name] = $credits;
400 * Register classes with the autoloader
406 protected function processAutoLoader( $dir, array $info ) {
407 if ( isset( $info['AutoloadClasses'] ) ) {
408 // Make paths absolute, relative to the JSON file
409 return array_map( function ( $file ) use ( $dir ) {
411 }, $info['AutoloadClasses'] );