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 * Version of the highest supported manifest version
17 const MANIFEST_VERSION
= 1;
20 * Version of the oldest supported manifest version
22 const OLDEST_MANIFEST_VERSION
= 1;
25 * Bump whenever the registration cache needs resetting
27 const CACHE_VERSION
= 1;
30 * Special key that defines the merge strategy
34 const MERGE_STRATEGY
= '_merge_strategy';
42 * Array of loaded things, keyed by name, values are credits information
46 private $loaded = array();
49 * List of paths that should be loaded
53 protected $queued = array();
56 * Items in the JSON file that aren't being
61 protected $attributes = array();
64 * @var ExtensionRegistry
66 private static $instance;
69 * @return ExtensionRegistry
71 public static function getInstance() {
72 if ( self
::$instance === null ) {
73 self
::$instance = new self();
76 return self
::$instance;
79 public function __construct() {
80 // We use a try/catch instead of the $fallback parameter because
81 // we don't want to fail here if $wgObjectCaches is not configured
82 // properly for APC setup
84 $this->cache
= ObjectCache
::newAccelerator();
85 } catch ( MWException
$e ) {
86 $this->cache
= new EmptyBagOStuff();
91 * @param string $path Absolute path to the JSON file
93 public function queue( $path ) {
94 global $wgExtensionInfoMTime;
96 $mtime = $wgExtensionInfoMTime;
97 if ( $mtime === false ) {
98 if ( file_exists( $path ) ) {
99 $mtime = filemtime( $path );
101 throw new Exception( "$path does not exist!" );
104 $err = error_get_last();
105 throw new Exception( "Couldn't stat $path: {$err['message']}" );
108 $this->queued
[$path] = $mtime;
111 public function loadFromQueue() {
112 if ( !$this->queued
) {
116 // See if this queue is in APC
117 $key = wfMemcKey( 'registration', md5( json_encode( $this->queued
) ), self
::CACHE_VERSION
);
118 $data = $this->cache
->get( $key );
120 $this->exportExtractedData( $data );
122 $data = $this->readFromQueue( $this->queued
);
123 $this->exportExtractedData( $data );
124 // Do this late since we don't want to extract it since we already
125 // did that, but it should be cached
126 $data['globals']['wgAutoloadClasses'] +
= $data['autoload'];
127 unset( $data['autoload'] );
128 $this->cache
->set( $key, $data, 60 * 60 * 24 );
130 $this->queued
= array();
134 * Get the current load queue. Not intended to be used
135 * outside of the installer.
139 public function getQueue() {
140 return $this->queued
;
144 * Clear the current load queue. Not intended to be used
145 * outside of the installer.
147 public function clearQueue() {
148 $this->queued
= array();
152 * Process a queue of extensions and return their extracted data
154 * @param array $queue keys are filenames, values are ignored
155 * @return array extracted info
158 public function readFromQueue( array $queue ) {
159 $autoloadClasses = array();
160 $processor = new ExtensionProcessor();
161 foreach ( $queue as $path => $mtime ) {
162 $json = file_get_contents( $path );
163 if ( $json === false ) {
164 throw new Exception( "Unable to read $path, does it exist?" );
166 $info = json_decode( $json, /* $assoc = */ true );
167 if ( !is_array( $info ) ) {
168 throw new Exception( "$path is not a valid JSON file." );
170 if ( !isset( $info['manifest_version'] ) ) {
171 // For backwards-compatability, assume a version of 1
172 $info['manifest_version'] = 1;
174 $version = $info['manifest_version'];
175 if ( $version < self
::OLDEST_MANIFEST_VERSION ||
$version > self
::MANIFEST_VERSION
) {
176 throw new Exception( "$path: unsupported manifest_version: {$version}" );
178 $autoload = $this->processAutoLoader( dirname( $path ), $info );
179 // Set up the autoloader now so custom processors will work
180 $GLOBALS['wgAutoloadClasses'] +
= $autoload;
181 $autoloadClasses +
= $autoload;
182 $processor->extractInfo( $path, $info, $version );
184 $data = $processor->getExtractedInfo();
185 // Need to set this so we can += to it later
186 $data['globals']['wgAutoloadClasses'] = array();
187 foreach ( $data['credits'] as $credit ) {
188 $data['globals']['wgExtensionCredits'][$credit['type']][] = $credit;
190 $data['globals']['wgExtensionCredits'][self
::MERGE_STRATEGY
] = 'array_merge_recursive';
191 $data['autoload'] = $autoloadClasses;
195 protected function exportExtractedData( array $info ) {
196 foreach ( $info['globals'] as $key => $val ) {
197 // If a merge strategy is set, read it and remove it from the value
198 // so it doesn't accidentally end up getting set.
199 // Need to check $val is an array for PHP 5.3 which will return
200 // true on isset( 'string'['foo'] ).
201 if ( isset( $val[self
::MERGE_STRATEGY
] ) && is_array( $val ) ) {
202 $mergeStrategy = $val[self
::MERGE_STRATEGY
];
203 unset( $val[self
::MERGE_STRATEGY
] );
205 $mergeStrategy = 'array_merge';
208 // Optimistic: If the global is not set, or is an empty array, replace it entirely.
209 // Will be O(1) performance.
210 if ( !isset( $GLOBALS[$key] ) ||
( is_array( $GLOBALS[$key] ) && !$GLOBALS[$key] ) ) {
211 $GLOBALS[$key] = $val;
215 if ( !is_array( $GLOBALS[$key] ) ||
!is_array( $val ) ) {
216 // config setting that has already been overridden, don't set it
220 switch ( $mergeStrategy ) {
221 case 'array_merge_recursive':
222 $GLOBALS[$key] = array_merge_recursive( $GLOBALS[$key], $val );
224 case 'array_plus_2d':
225 $GLOBALS[$key] = wfArrayPlus2d( $GLOBALS[$key], $val );
228 $GLOBALS[$key] +
= $val;
231 $GLOBALS[$key] = array_merge( $val, $GLOBALS[$key] );
234 throw new UnexpectedValueException( "Unknown merge strategy '$mergeStrategy'" );
238 foreach ( $info['defines'] as $name => $val ) {
239 define( $name, $val );
241 foreach ( $info['callbacks'] as $cb ) {
242 call_user_func( $cb );
245 $this->loaded +
= $info['credits'];
247 if ( $info['attributes'] ) {
248 if ( !$this->attributes
) {
249 $this->attributes
= $info['attributes'];
251 $this->attributes
= array_merge_recursive( $this->attributes
, $info['attributes'] );
257 * Loads and processes the given JSON file without delay
259 * If some extensions are already queued, this will load
262 * @param string $path Absolute path to the JSON file
264 public function load( $path ) {
265 $this->loadFromQueue(); // First clear the queue
266 $this->queue( $path );
267 $this->loadFromQueue();
271 * Whether a thing has been loaded
272 * @param string $name
275 public function isLoaded( $name ) {
276 return isset( $this->loaded
[$name] );
280 * @param string $name
283 public function getAttribute( $name ) {
284 if ( isset( $this->attributes
[$name] ) ) {
285 return $this->attributes
[$name];
292 * Get information about all things
296 public function getAllThings() {
297 return $this->loaded
;
301 * Mark a thing as loaded
303 * @param string $name
304 * @param array $credits
306 protected function markLoaded( $name, array $credits ) {
307 $this->loaded
[$name] = $credits;
311 * Register classes with the autoloader
317 protected function processAutoLoader( $dir, array $info ) {
318 if ( isset( $info['AutoloadClasses'] ) ) {
319 // Make paths absolute, relative to the JSON file
320 return array_map( function( $file ) use ( $dir ) {
322 }, $info['AutoloadClasses'] );