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
22 const MANIFEST_VERSION
= 1;
25 * Version of the oldest supported manifest version
27 const OLDEST_MANIFEST_VERSION
= 1;
30 * Bump whenever the registration cache needs resetting
32 const CACHE_VERSION
= 1;
35 * Special key that defines the merge strategy
39 const MERGE_STRATEGY
= '_merge_strategy';
47 * Array of loaded things, keyed by name, values are credits information
51 private $loaded = array();
54 * List of paths that should be loaded
58 protected $queued = array();
61 * Items in the JSON file that aren't being
66 protected $attributes = array();
69 * @var ExtensionRegistry
71 private static $instance;
74 * @return ExtensionRegistry
76 public static function getInstance() {
77 if ( self
::$instance === null ) {
78 self
::$instance = new self();
81 return self
::$instance;
84 public function __construct() {
85 // We use a try/catch instead of the $fallback parameter because
86 // we don't want to fail here if $wgObjectCaches is not configured
87 // properly for APC setup
89 $this->cache
= ObjectCache
::newAccelerator();
90 } catch ( MWException
$e ) {
91 $this->cache
= new EmptyBagOStuff();
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;
116 public function loadFromQueue() {
117 if ( !$this->queued
) {
121 // See if this queue is in APC
122 $key = wfMemcKey( 'registration', md5( json_encode( $this->queued
) ), self
::CACHE_VERSION
);
123 $data = $this->cache
->get( $key );
125 $this->exportExtractedData( $data );
127 $data = $this->readFromQueue( $this->queued
);
128 $this->exportExtractedData( $data );
129 // Do this late since we don't want to extract it since we already
130 // did that, but it should be cached
131 $data['globals']['wgAutoloadClasses'] +
= $data['autoload'];
132 unset( $data['autoload'] );
133 $this->cache
->set( $key, $data, 60 * 60 * 24 );
135 $this->queued
= array();
139 * Get the current load queue. Not intended to be used
140 * outside of the installer.
144 public function getQueue() {
145 return $this->queued
;
149 * Clear the current load queue. Not intended to be used
150 * outside of the installer.
152 public function clearQueue() {
153 $this->queued
= array();
157 * Process a queue of extensions and return their extracted data
159 * @param array $queue keys are filenames, values are ignored
160 * @return array extracted info
163 public function readFromQueue( array $queue ) {
165 $autoloadClasses = array();
166 $processor = new ExtensionProcessor();
167 $incompatible = array();
168 $coreVersionParser = new CoreVersionChecker( $wgVersion );
169 foreach ( $queue as $path => $mtime ) {
170 $json = file_get_contents( $path );
171 if ( $json === false ) {
172 throw new Exception( "Unable to read $path, does it exist?" );
174 $info = json_decode( $json, /* $assoc = */ true );
175 if ( !is_array( $info ) ) {
176 throw new Exception( "$path is not a valid JSON file." );
178 if ( !isset( $info['manifest_version'] ) ) {
179 // For backwards-compatability, assume a version of 1
180 $info['manifest_version'] = 1;
182 $version = $info['manifest_version'];
183 if ( $version < self
::OLDEST_MANIFEST_VERSION ||
$version > self
::MANIFEST_VERSION
) {
184 throw new Exception( "$path: unsupported manifest_version: {$version}" );
186 $autoload = $this->processAutoLoader( dirname( $path ), $info );
187 // Set up the autoloader now so custom processors will work
188 $GLOBALS['wgAutoloadClasses'] +
= $autoload;
189 $autoloadClasses +
= $autoload;
190 // Check any constraints against MediaWiki core
191 $requires = $processor->getRequirements( $info );
192 if ( isset( $requires[self
::MEDIAWIKI_CORE
] )
193 && !$coreVersionParser->check( $requires[self
::MEDIAWIKI_CORE
] )
195 // Doesn't match, mark it as incompatible.
196 $incompatible[] = "{$info['name']} is not compatible with the current "
197 . "MediaWiki core (version {$wgVersion}), it requires: ". $requires[self
::MEDIAWIKI_CORE
]
201 // Compatible, read and extract info
202 $processor->extractInfo( $path, $info, $version );
204 if ( $incompatible ) {
205 if ( count( $incompatible ) === 1 ) {
206 throw new Exception( $incompatible[0] );
208 throw new Exception( implode( "\n", $incompatible ) );
211 $data = $processor->getExtractedInfo();
212 // Need to set this so we can += to it later
213 $data['globals']['wgAutoloadClasses'] = array();
214 foreach ( $data['credits'] as $credit ) {
215 $data['globals']['wgExtensionCredits'][$credit['type']][] = $credit;
217 $data['globals']['wgExtensionCredits'][self
::MERGE_STRATEGY
] = 'array_merge_recursive';
218 $data['autoload'] = $autoloadClasses;
222 protected function exportExtractedData( array $info ) {
223 foreach ( $info['globals'] as $key => $val ) {
224 // If a merge strategy is set, read it and remove it from the value
225 // so it doesn't accidentally end up getting set.
226 // Need to check $val is an array for PHP 5.3 which will return
227 // true on isset( 'string'['foo'] ).
228 if ( isset( $val[self
::MERGE_STRATEGY
] ) && is_array( $val ) ) {
229 $mergeStrategy = $val[self
::MERGE_STRATEGY
];
230 unset( $val[self
::MERGE_STRATEGY
] );
232 $mergeStrategy = 'array_merge';
235 // Optimistic: If the global is not set, or is an empty array, replace it entirely.
236 // Will be O(1) performance.
237 if ( !isset( $GLOBALS[$key] ) ||
( is_array( $GLOBALS[$key] ) && !$GLOBALS[$key] ) ) {
238 $GLOBALS[$key] = $val;
242 if ( !is_array( $GLOBALS[$key] ) ||
!is_array( $val ) ) {
243 // config setting that has already been overridden, don't set it
247 switch ( $mergeStrategy ) {
248 case 'array_merge_recursive':
249 $GLOBALS[$key] = array_merge_recursive( $GLOBALS[$key], $val );
251 case 'array_plus_2d':
252 $GLOBALS[$key] = wfArrayPlus2d( $GLOBALS[$key], $val );
255 $GLOBALS[$key] +
= $val;
258 $GLOBALS[$key] = array_merge( $val, $GLOBALS[$key] );
261 throw new UnexpectedValueException( "Unknown merge strategy '$mergeStrategy'" );
265 foreach ( $info['defines'] as $name => $val ) {
266 define( $name, $val );
268 foreach ( $info['callbacks'] as $cb ) {
269 call_user_func( $cb );
272 $this->loaded +
= $info['credits'];
274 if ( $info['attributes'] ) {
275 if ( !$this->attributes
) {
276 $this->attributes
= $info['attributes'];
278 $this->attributes
= array_merge_recursive( $this->attributes
, $info['attributes'] );
284 * Loads and processes the given JSON file without delay
286 * If some extensions are already queued, this will load
289 * @param string $path Absolute path to the JSON file
291 public function load( $path ) {
292 $this->loadFromQueue(); // First clear the queue
293 $this->queue( $path );
294 $this->loadFromQueue();
298 * Whether a thing has been loaded
299 * @param string $name
302 public function isLoaded( $name ) {
303 return isset( $this->loaded
[$name] );
307 * @param string $name
310 public function getAttribute( $name ) {
311 if ( isset( $this->attributes
[$name] ) ) {
312 return $this->attributes
[$name];
319 * Get information about all things
323 public function getAllThings() {
324 return $this->loaded
;
328 * Mark a thing as loaded
330 * @param string $name
331 * @param array $credits
333 protected function markLoaded( $name, array $credits ) {
334 $this->loaded
[$name] = $credits;
338 * Register classes with the autoloader
344 protected function processAutoLoader( $dir, array $info ) {
345 if ( isset( $info['AutoloadClasses'] ) ) {
346 // Make paths absolute, relative to the JSON file
347 return array_map( function( $file ) use ( $dir ) {
349 }, $info['AutoloadClasses'] );