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 * Special key that defines the merge strategy
29 const MERGE_STRATEGY
= '_merge_strategy';
37 * Array of loaded things, keyed by name, values are credits information
41 private $loaded = array();
44 * List of paths that should be loaded
48 protected $queued = array();
51 * Items in the JSON file that aren't being
56 protected $attributes = array();
59 * @var ExtensionRegistry
61 private static $instance;
64 * @return ExtensionRegistry
66 public static function getInstance() {
67 if ( self
::$instance === null ) {
68 self
::$instance = new self();
71 return self
::$instance;
74 public function __construct() {
75 // We use a try/catch instead of the $fallback parameter because
76 // we don't want to fail here if $wgObjectCaches is not configured
77 // properly for APC setup
79 $this->cache
= ObjectCache
::newAccelerator( array() );
80 } catch ( MWException
$e ) {
81 $this->cache
= new EmptyBagOStuff();
86 * @param string $path Absolute path to the JSON file
88 public function queue( $path ) {
89 global $wgExtensionInfoMTime;
91 $mtime = $wgExtensionInfoMTime;
92 if ( $mtime === false ) {
93 if ( file_exists( $path ) ) {
94 $mtime = filemtime( $path );
96 throw new Exception( "$path does not exist!" );
99 $err = error_get_last();
100 throw new Exception( "Couldn't stat $path: {$err['message']}" );
103 $this->queued
[$path] = $mtime;
106 public function loadFromQueue() {
107 if ( !$this->queued
) {
111 // See if this queue is in APC
112 $key = wfMemcKey( 'registration', md5( json_encode( $this->queued
) ) );
113 $data = $this->cache
->get( $key );
115 $this->exportExtractedData( $data );
117 $data = $this->readFromQueue( $this->queued
);
118 $this->exportExtractedData( $data );
119 // Do this late since we don't want to extract it since we already
120 // did that, but it should be cached
121 $data['globals']['wgAutoloadClasses'] +
= $data['autoload'];
122 unset( $data['autoload'] );
123 $this->cache
->set( $key, $data, 60 * 60 * 24 );
125 $this->queued
= array();
129 * Get the current load queue. Not intended to be used
130 * outside of the installer.
134 public function getQueue() {
135 return $this->queued
;
139 * Clear the current load queue. Not intended to be used
140 * outside of the installer.
142 public function clearQueue() {
143 $this->queued
= array();
147 * Process a queue of extensions and return their extracted data
149 * @param array $queue keys are filenames, values are ignored
150 * @return array extracted info
153 public function readFromQueue( array $queue ) {
154 $autoloadClasses = array();
155 $processor = new ExtensionProcessor();
156 foreach ( $queue as $path => $mtime ) {
157 $json = file_get_contents( $path );
158 if ( $json === false ) {
159 throw new Exception( "Unable to read $path, does it exist?" );
161 $info = json_decode( $json, /* $assoc = */ true );
162 if ( !is_array( $info ) ) {
163 throw new Exception( "$path is not a valid JSON file." );
165 if ( !isset( $info['manifest_version'] ) ) {
166 // For backwards-compatability, assume a version of 1
167 $info['manifest_version'] = 1;
169 $version = $info['manifest_version'];
170 if ( $version < self
::OLDEST_MANIFEST_VERSION ||
$version > self
::MANIFEST_VERSION
) {
171 throw new Exception( "$path: unsupported manifest_version: {$version}" );
173 $autoload = $this->processAutoLoader( dirname( $path ), $info );
174 // Set up the autoloader now so custom processors will work
175 $GLOBALS['wgAutoloadClasses'] +
= $autoload;
176 $autoloadClasses +
= $autoload;
177 $processor->extractInfo( $path, $info, $version );
179 $data = $processor->getExtractedInfo();
180 // Need to set this so we can += to it later
181 $data['globals']['wgAutoloadClasses'] = array();
182 foreach ( $data['credits'] as $credit ) {
183 $data['globals']['wgExtensionCredits'][$credit['type']][] = $credit;
185 $data['autoload'] = $autoloadClasses;
189 protected function exportExtractedData( array $info ) {
190 foreach ( $info['globals'] as $key => $val ) {
191 // If a merge strategy is set, read it and remove it from the value
192 // so it doesn't accidentally end up getting set.
193 // Need to check $val is an array for PHP 5.3 which will return
194 // true on isset( 'string'['foo'] ).
195 if ( isset( $val[self
::MERGE_STRATEGY
] ) && is_array( $val ) ) {
196 $mergeStrategy = $val[self
::MERGE_STRATEGY
];
197 unset( $val[self
::MERGE_STRATEGY
] );
199 $mergeStrategy = 'array_merge';
202 // Optimistic: If the global is not set, or is an empty array, replace it entirely.
203 // Will be O(1) performance.
204 if ( !isset( $GLOBALS[$key] ) ||
( is_array( $GLOBALS[$key] ) && !$GLOBALS[$key] ) ) {
205 $GLOBALS[$key] = $val;
209 if ( !is_array( $GLOBALS[$key] ) ||
!is_array( $val ) ) {
210 // config setting that has already been overridden, don't set it
214 switch ( $mergeStrategy ) {
215 case 'array_merge_recursive':
216 $GLOBALS[$key] = array_merge_recursive( $GLOBALS[$key], $val );
218 case 'array_plus_2d':
219 // First merge items that are in both arrays
220 foreach ( $GLOBALS[$key] as $name => &$groupVal ) {
221 if ( isset( $val[$name] ) ) {
222 $groupVal +
= $val[$name];
225 // Now add items that didn't exist yet
226 $GLOBALS[$key] +
= $val;
229 $GLOBALS[$key] = $val +
$GLOBALS[$key];
232 $GLOBALS[$key] = array_merge( $val, $GLOBALS[$key] );
235 throw new UnexpectedValueException( "Unknown merge strategy '$mergeStrategy'" );
239 foreach ( $info['defines'] as $name => $val ) {
240 define( $name, $val );
242 foreach ( $info['callbacks'] as $cb ) {
243 call_user_func( $cb );
246 $this->loaded +
= $info['credits'];
248 if ( $info['attributes'] ) {
249 if ( !$this->attributes
) {
250 $this->attributes
= $info['attributes'];
252 $this->attributes
= array_merge_recursive( $this->attributes
, $info['attributes'] );
258 * Loads and processes the given JSON file without delay
260 * If some extensions are already queued, this will load
263 * @param string $path Absolute path to the JSON file
265 public function load( $path ) {
266 $this->loadFromQueue(); // First clear the queue
267 $this->queue( $path );
268 $this->loadFromQueue();
272 * Whether a thing has been loaded
273 * @param string $name
276 public function isLoaded( $name ) {
277 return isset( $this->loaded
[$name] );
281 * @param string $name
284 public function getAttribute( $name ) {
285 if ( isset( $this->attributes
[$name] ) ) {
286 return $this->attributes
[$name];
293 * Get information about all things
297 public function getAllThings() {
298 return $this->loaded
;
302 * Mark a thing as loaded
304 * @param string $name
305 * @param array $credits
307 protected function markLoaded( $name, array $credits ) {
308 $this->loaded
[$name] = $credits;
312 * Register classes with the autoloader
318 protected function processAutoLoader( $dir, array $info ) {
319 if ( isset( $info['AutoloadClasses'] ) ) {
320 // Make paths absolute, relative to the JSON file
321 return array_map( function( $file ) use ( $dir ) {
323 }, $info['AutoloadClasses'] );