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
= 3;
37 * Special key that defines the merge strategy
41 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 * Items in the JSON file that aren't being
68 protected $attributes = [];
71 * @var ExtensionRegistry
73 private static $instance;
76 * @return ExtensionRegistry
78 public static function getInstance() {
79 if ( self
::$instance === null ) {
80 self
::$instance = new self();
83 return self
::$instance;
86 public function __construct() {
87 // We use a try/catch instead of the $fallback parameter because
88 // we don't want to fail here if $wgObjectCaches is not configured
89 // properly for APC setup
91 $this->cache
= MediaWikiServices
::getInstance()->getLocalServerObjectCache();
92 } catch ( MWException
$e ) {
93 $this->cache
= new EmptyBagOStuff();
98 * @param string $path Absolute path to the JSON file
100 public function queue( $path ) {
101 global $wgExtensionInfoMTime;
103 $mtime = $wgExtensionInfoMTime;
104 if ( $mtime === false ) {
105 if ( file_exists( $path ) ) {
106 $mtime = filemtime( $path );
108 throw new Exception( "$path does not exist!" );
111 $err = error_get_last();
112 throw new Exception( "Couldn't stat $path: {$err['message']}" );
115 $this->queued
[$path] = $mtime;
118 public function loadFromQueue() {
120 if ( !$this->queued
) {
124 // A few more things to vary the cache on
126 'registration' => self
::CACHE_VERSION
,
127 'mediawiki' => $wgVersion
130 // See if this queue is in APC
133 md5( json_encode( $this->queued +
$versions ) )
135 $data = $this->cache
->get( $key );
137 $this->exportExtractedData( $data );
139 $data = $this->readFromQueue( $this->queued
);
140 $this->exportExtractedData( $data );
141 // Do this late since we don't want to extract it since we already
142 // did that, but it should be cached
143 $data['globals']['wgAutoloadClasses'] +
= $data['autoload'];
144 unset( $data['autoload'] );
145 $this->cache
->set( $key, $data, 60 * 60 * 24 );
151 * Get the current load queue. Not intended to be used
152 * outside of the installer.
156 public function getQueue() {
157 return $this->queued
;
161 * Clear the current load queue. Not intended to be used
162 * outside of the installer.
164 public function clearQueue() {
169 * Process a queue of extensions and return their extracted data
171 * @param array $queue keys are filenames, values are ignored
172 * @return array extracted info
175 public function readFromQueue( array $queue ) {
177 $autoloadClasses = [];
178 $autoloaderPaths = [];
179 $processor = new ExtensionProcessor();
181 $coreVersionParser = new CoreVersionChecker( $wgVersion );
182 foreach ( $queue as $path => $mtime ) {
183 $json = file_get_contents( $path );
184 if ( $json === false ) {
185 throw new Exception( "Unable to read $path, does it exist?" );
187 $info = json_decode( $json, /* $assoc = */ true );
188 if ( !is_array( $info ) ) {
189 throw new Exception( "$path is not a valid JSON file." );
191 if ( !isset( $info['manifest_version'] ) ) {
192 // For backwards-compatability, assume a version of 1
193 $info['manifest_version'] = 1;
195 $version = $info['manifest_version'];
196 if ( $version < self
::OLDEST_MANIFEST_VERSION ||
$version > self
::MANIFEST_VERSION
) {
197 throw new Exception( "$path: unsupported manifest_version: {$version}" );
199 $autoload = $this->processAutoLoader( dirname( $path ), $info );
200 // Set up the autoloader now so custom processors will work
201 $GLOBALS['wgAutoloadClasses'] +
= $autoload;
202 $autoloadClasses +
= $autoload;
203 // Check any constraints against MediaWiki core
204 $requires = $processor->getRequirements( $info );
205 if ( isset( $requires[self
::MEDIAWIKI_CORE
] )
206 && !$coreVersionParser->check( $requires[self
::MEDIAWIKI_CORE
] )
208 // Doesn't match, mark it as incompatible.
209 $incompatible[] = "{$info['name']} is not compatible with the current "
210 . "MediaWiki core (version {$wgVersion}), it requires: " . $requires[self
::MEDIAWIKI_CORE
]
214 // Get extra paths for later inclusion
215 $autoloaderPaths = array_merge( $autoloaderPaths,
216 $processor->getExtraAutoloaderPaths( dirname( $path ), $info ) );
217 // Compatible, read and extract info
218 $processor->extractInfo( $path, $info, $version );
220 if ( $incompatible ) {
221 if ( count( $incompatible ) === 1 ) {
222 throw new Exception( $incompatible[0] );
224 throw new Exception( implode( "\n", $incompatible ) );
227 $data = $processor->getExtractedInfo();
228 // Need to set this so we can += to it later
229 $data['globals']['wgAutoloadClasses'] = [];
230 $data['autoload'] = $autoloadClasses;
231 $data['autoloaderPaths'] = $autoloaderPaths;
235 protected function exportExtractedData( array $info ) {
236 foreach ( $info['globals'] as $key => $val ) {
237 // If a merge strategy is set, read it and remove it from the value
238 // so it doesn't accidentally end up getting set.
239 if ( is_array( $val ) && isset( $val[self
::MERGE_STRATEGY
] ) ) {
240 $mergeStrategy = $val[self
::MERGE_STRATEGY
];
241 unset( $val[self
::MERGE_STRATEGY
] );
243 $mergeStrategy = 'array_merge';
246 // Optimistic: If the global is not set, or is an empty array, replace it entirely.
247 // Will be O(1) performance.
248 if ( !isset( $GLOBALS[$key] ) ||
( is_array( $GLOBALS[$key] ) && !$GLOBALS[$key] ) ) {
249 $GLOBALS[$key] = $val;
253 if ( !is_array( $GLOBALS[$key] ) ||
!is_array( $val ) ) {
254 // config setting that has already been overridden, don't set it
258 switch ( $mergeStrategy ) {
259 case 'array_merge_recursive':
260 $GLOBALS[$key] = array_merge_recursive( $GLOBALS[$key], $val );
262 case 'array_plus_2d':
263 $GLOBALS[$key] = wfArrayPlus2d( $GLOBALS[$key], $val );
266 $GLOBALS[$key] +
= $val;
269 $GLOBALS[$key] = array_merge( $val, $GLOBALS[$key] );
272 throw new UnexpectedValueException( "Unknown merge strategy '$mergeStrategy'" );
276 foreach ( $info['defines'] as $name => $val ) {
277 define( $name, $val );
279 foreach ( $info['autoloaderPaths'] as $path ) {
282 foreach ( $info['callbacks'] as $cb ) {
283 call_user_func( $cb );
286 $this->loaded +
= $info['credits'];
287 if ( $info['attributes'] ) {
288 if ( !$this->attributes
) {
289 $this->attributes
= $info['attributes'];
291 $this->attributes
= array_merge_recursive( $this->attributes
, $info['attributes'] );
297 * Loads and processes the given JSON file without delay
299 * If some extensions are already queued, this will load
302 * @param string $path Absolute path to the JSON file
304 public function load( $path ) {
305 $this->loadFromQueue(); // First clear the queue
306 $this->queue( $path );
307 $this->loadFromQueue();
311 * Whether a thing has been loaded
312 * @param string $name
315 public function isLoaded( $name ) {
316 return isset( $this->loaded
[$name] );
320 * @param string $name
323 public function getAttribute( $name ) {
324 if ( isset( $this->attributes
[$name] ) ) {
325 return $this->attributes
[$name];
332 * Get information about all things
336 public function getAllThings() {
337 return $this->loaded
;
341 * Mark a thing as loaded
343 * @param string $name
344 * @param array $credits
346 protected function markLoaded( $name, array $credits ) {
347 $this->loaded
[$name] = $credits;
351 * Register classes with the autoloader
357 protected function processAutoLoader( $dir, array $info ) {
358 if ( isset( $info['AutoloadClasses'] ) ) {
359 // Make paths absolute, relative to the JSON file
360 return array_map( function( $file ) use ( $dir ) {
362 }, $info['AutoloadClasses'] );