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
= 4;
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 * 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;
83 * @return ExtensionRegistry
85 public static function getInstance() {
86 if ( self
::$instance === null ) {
87 self
::$instance = new self();
90 return self
::$instance;
93 public function __construct() {
94 // We use a try/catch because we don't want to fail here
95 // if $wgObjectCaches is not configured properly for APC setup
97 $this->cache
= MediaWikiServices
::getInstance()->getLocalServerObjectCache();
98 } catch ( MWException
$e ) {
99 $this->cache
= new EmptyBagOStuff();
104 * @param string $path Absolute path to the JSON file
106 public function queue( $path ) {
107 global $wgExtensionInfoMTime;
109 $mtime = $wgExtensionInfoMTime;
110 if ( $mtime === false ) {
111 if ( file_exists( $path ) ) {
112 $mtime = filemtime( $path );
114 throw new Exception( "$path does not exist!" );
117 $err = error_get_last();
118 throw new Exception( "Couldn't stat $path: {$err['message']}" );
121 $this->queued
[$path] = $mtime;
125 * @throws MWException If the queue is already marked as finished (no further things should
128 public function loadFromQueue() {
130 if ( !$this->queued
) {
134 if ( $this->finished
) {
135 throw new MWException(
136 "The following paths tried to load late: "
137 . implode( ', ', array_keys( $this->queued
) )
141 // A few more things to vary the cache on
143 'registration' => self
::CACHE_VERSION
,
144 'mediawiki' => $wgVersion
147 // See if this queue is in APC
150 md5( json_encode( $this->queued +
$versions ) )
152 $data = $this->cache
->get( $key );
154 $this->exportExtractedData( $data );
156 $data = $this->readFromQueue( $this->queued
);
157 $this->exportExtractedData( $data );
158 // Do this late since we don't want to extract it since we already
159 // did that, but it should be cached
160 $data['globals']['wgAutoloadClasses'] +
= $data['autoload'];
161 unset( $data['autoload'] );
162 $this->cache
->set( $key, $data, 60 * 60 * 24 );
168 * Get the current load queue. Not intended to be used
169 * outside of the installer.
173 public function getQueue() {
174 return $this->queued
;
178 * Clear the current load queue. Not intended to be used
179 * outside of the installer.
181 public function clearQueue() {
186 * After this is called, no more extensions can be loaded
190 public function finish() {
191 $this->finished
= true;
195 * Process a queue of extensions and return their extracted data
197 * @param array $queue keys are filenames, values are ignored
198 * @return array extracted info
201 public function readFromQueue( array $queue ) {
203 $autoloadClasses = [];
204 $autoloaderPaths = [];
205 $processor = new ExtensionProcessor();
207 $coreVersionParser = new CoreVersionChecker( $wgVersion );
208 foreach ( $queue as $path => $mtime ) {
209 $json = file_get_contents( $path );
210 if ( $json === false ) {
211 throw new Exception( "Unable to read $path, does it exist?" );
213 $info = json_decode( $json, /* $assoc = */ true );
214 if ( !is_array( $info ) ) {
215 throw new Exception( "$path is not a valid JSON file." );
218 // Check any constraints against MediaWiki core
219 $requires = $processor->getRequirements( $info );
220 if ( isset( $requires[self
::MEDIAWIKI_CORE
] )
221 && !$coreVersionParser->check( $requires[self
::MEDIAWIKI_CORE
] )
223 // Doesn't match, mark it as incompatible.
224 $incompatible[] = "{$info['name']} is not compatible with the current "
225 . "MediaWiki core (version {$wgVersion}), it requires: " . $requires[self
::MEDIAWIKI_CORE
]
230 if ( !isset( $info['manifest_version'] ) ) {
231 // For backwards-compatability, assume a version of 1
232 $info['manifest_version'] = 1;
234 $version = $info['manifest_version'];
235 if ( $version < self
::OLDEST_MANIFEST_VERSION ||
$version > self
::MANIFEST_VERSION
) {
236 throw new Exception( "$path: unsupported manifest_version: {$version}" );
239 $autoload = $this->processAutoLoader( dirname( $path ), $info );
240 // Set up the autoloader now so custom processors will work
241 $GLOBALS['wgAutoloadClasses'] +
= $autoload;
242 $autoloadClasses +
= $autoload;
244 // Get extra paths for later inclusion
245 $autoloaderPaths = array_merge( $autoloaderPaths,
246 $processor->getExtraAutoloaderPaths( dirname( $path ), $info ) );
247 // Compatible, read and extract info
248 $processor->extractInfo( $path, $info, $version );
250 if ( $incompatible ) {
251 if ( count( $incompatible ) === 1 ) {
252 throw new Exception( $incompatible[0] );
254 throw new Exception( implode( "\n", $incompatible ) );
257 $data = $processor->getExtractedInfo();
258 // Need to set this so we can += to it later
259 $data['globals']['wgAutoloadClasses'] = [];
260 $data['autoload'] = $autoloadClasses;
261 $data['autoloaderPaths'] = $autoloaderPaths;
265 protected function exportExtractedData( array $info ) {
266 foreach ( $info['globals'] as $key => $val ) {
267 // If a merge strategy is set, read it and remove it from the value
268 // so it doesn't accidentally end up getting set.
269 if ( is_array( $val ) && isset( $val[self
::MERGE_STRATEGY
] ) ) {
270 $mergeStrategy = $val[self
::MERGE_STRATEGY
];
271 unset( $val[self
::MERGE_STRATEGY
] );
273 $mergeStrategy = 'array_merge';
276 // Optimistic: If the global is not set, or is an empty array, replace it entirely.
277 // Will be O(1) performance.
278 if ( !isset( $GLOBALS[$key] ) ||
( is_array( $GLOBALS[$key] ) && !$GLOBALS[$key] ) ) {
279 $GLOBALS[$key] = $val;
283 if ( !is_array( $GLOBALS[$key] ) ||
!is_array( $val ) ) {
284 // config setting that has already been overridden, don't set it
288 switch ( $mergeStrategy ) {
289 case 'array_merge_recursive':
290 $GLOBALS[$key] = array_merge_recursive( $GLOBALS[$key], $val );
292 case 'array_replace_recursive':
293 $GLOBALS[$key] = array_replace_recursive( $GLOBALS[$key], $val );
295 case 'array_plus_2d':
296 $GLOBALS[$key] = wfArrayPlus2d( $GLOBALS[$key], $val );
299 $GLOBALS[$key] +
= $val;
302 $GLOBALS[$key] = array_merge( $val, $GLOBALS[$key] );
305 throw new UnexpectedValueException( "Unknown merge strategy '$mergeStrategy'" );
309 foreach ( $info['defines'] as $name => $val ) {
310 define( $name, $val );
312 foreach ( $info['autoloaderPaths'] as $path ) {
316 $this->loaded +
= $info['credits'];
317 if ( $info['attributes'] ) {
318 if ( !$this->attributes
) {
319 $this->attributes
= $info['attributes'];
321 $this->attributes
= array_merge_recursive( $this->attributes
, $info['attributes'] );
325 foreach ( $info['callbacks'] as $name => $cb ) {
326 call_user_func( $cb, $info['credits'][$name] );
331 * Loads and processes the given JSON file without delay
333 * If some extensions are already queued, this will load
336 * @param string $path Absolute path to the JSON file
338 public function load( $path ) {
339 $this->loadFromQueue(); // First clear the queue
340 $this->queue( $path );
341 $this->loadFromQueue();
345 * Whether a thing has been loaded
346 * @param string $name
349 public function isLoaded( $name ) {
350 return isset( $this->loaded
[$name] );
354 * @param string $name
357 public function getAttribute( $name ) {
358 if ( isset( $this->attributes
[$name] ) ) {
359 return $this->attributes
[$name];
366 * Get information about all things
370 public function getAllThings() {
371 return $this->loaded
;
375 * Mark a thing as loaded
377 * @param string $name
378 * @param array $credits
380 protected function markLoaded( $name, array $credits ) {
381 $this->loaded
[$name] = $credits;
385 * Register classes with the autoloader
391 protected function processAutoLoader( $dir, array $info ) {
392 if ( isset( $info['AutoloadClasses'] ) ) {
393 // Make paths absolute, relative to the JSON file
394 return array_map( function( $file ) use ( $dir ) {
396 }, $info['AutoloadClasses'] );