wfMsgForContentNoTrans() was removed
[mediawiki.git] / includes / registration / ExtensionRegistry.php
blob86be86bba81f56f6b4b0c14e6db5b7d50d965850
1 <?php
3 /**
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.
10 * @since 1.25
12 class ExtensionRegistry {
14 /**
15 * "requires" key that applies to MediaWiki core/$wgVersion
17 const MEDIAWIKI_CORE = 'MediaWiki';
19 /**
20 * Version of the highest supported manifest version
22 const MANIFEST_VERSION = 1;
24 /**
25 * Version of the oldest supported manifest version
27 const OLDEST_MANIFEST_VERSION = 1;
29 /**
30 * Bump whenever the registration cache needs resetting
32 const CACHE_VERSION = 3;
34 /**
35 * Special key that defines the merge strategy
37 * @since 1.26
39 const MERGE_STRATEGY = '_merge_strategy';
41 /**
42 * @var BagOStuff
44 protected $cache;
46 /**
47 * Array of loaded things, keyed by name, values are credits information
49 * @var array
51 private $loaded = array();
53 /**
54 * List of paths that should be loaded
56 * @var array
58 protected $queued = array();
60 /**
61 * Items in the JSON file that aren't being
62 * set as globals
64 * @var array
66 protected $attributes = array();
68 /**
69 * @var ExtensionRegistry
71 private static $instance;
73 /**
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
88 try {
89 $this->cache = ObjectCache::getLocalServerInstance();
90 } catch ( MWException $e ) {
91 $this->cache = new EmptyBagOStuff();
95 /**
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 );
105 } else {
106 throw new Exception( "$path does not exist!" );
108 if ( !$mtime ) {
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 global $wgVersion;
118 if ( !$this->queued ) {
119 return;
122 // A few more things to vary the cache on
123 $versions = array(
124 'registration' => self::CACHE_VERSION,
125 'mediawiki' => $wgVersion
128 // See if this queue is in APC
129 $key = wfMemcKey(
130 'registration',
131 md5( json_encode( $this->queued + $versions ) )
133 $data = $this->cache->get( $key );
134 if ( $data ) {
135 $this->exportExtractedData( $data );
136 } else {
137 $data = $this->readFromQueue( $this->queued );
138 $this->exportExtractedData( $data );
139 // Do this late since we don't want to extract it since we already
140 // did that, but it should be cached
141 $data['globals']['wgAutoloadClasses'] += $data['autoload'];
142 unset( $data['autoload'] );
143 $this->cache->set( $key, $data, 60 * 60 * 24 );
145 $this->queued = array();
149 * Get the current load queue. Not intended to be used
150 * outside of the installer.
152 * @return array
154 public function getQueue() {
155 return $this->queued;
159 * Clear the current load queue. Not intended to be used
160 * outside of the installer.
162 public function clearQueue() {
163 $this->queued = array();
167 * Process a queue of extensions and return their extracted data
169 * @param array $queue keys are filenames, values are ignored
170 * @return array extracted info
171 * @throws Exception
173 public function readFromQueue( array $queue ) {
174 global $wgVersion;
175 $autoloadClasses = array();
176 $autoloaderPaths = array();
177 $processor = new ExtensionProcessor();
178 $incompatible = array();
179 $coreVersionParser = new CoreVersionChecker( $wgVersion );
180 foreach ( $queue as $path => $mtime ) {
181 $json = file_get_contents( $path );
182 if ( $json === false ) {
183 throw new Exception( "Unable to read $path, does it exist?" );
185 $info = json_decode( $json, /* $assoc = */ true );
186 if ( !is_array( $info ) ) {
187 throw new Exception( "$path is not a valid JSON file." );
189 if ( !isset( $info['manifest_version'] ) ) {
190 // For backwards-compatability, assume a version of 1
191 $info['manifest_version'] = 1;
193 $version = $info['manifest_version'];
194 if ( $version < self::OLDEST_MANIFEST_VERSION || $version > self::MANIFEST_VERSION ) {
195 throw new Exception( "$path: unsupported manifest_version: {$version}" );
197 $autoload = $this->processAutoLoader( dirname( $path ), $info );
198 // Set up the autoloader now so custom processors will work
199 $GLOBALS['wgAutoloadClasses'] += $autoload;
200 $autoloadClasses += $autoload;
201 // Check any constraints against MediaWiki core
202 $requires = $processor->getRequirements( $info );
203 if ( isset( $requires[self::MEDIAWIKI_CORE] )
204 && !$coreVersionParser->check( $requires[self::MEDIAWIKI_CORE] )
206 // Doesn't match, mark it as incompatible.
207 $incompatible[] = "{$info['name']} is not compatible with the current "
208 . "MediaWiki core (version {$wgVersion}), it requires: " . $requires[self::MEDIAWIKI_CORE]
209 . '.';
210 continue;
212 // Get extra paths for later inclusion
213 $autoloaderPaths = array_merge( $autoloaderPaths,
214 $processor->getExtraAutoloaderPaths( dirname( $path ), $info ) );
215 // Compatible, read and extract info
216 $processor->extractInfo( $path, $info, $version );
218 if ( $incompatible ) {
219 if ( count( $incompatible ) === 1 ) {
220 throw new Exception( $incompatible[0] );
221 } else {
222 throw new Exception( implode( "\n", $incompatible ) );
225 $data = $processor->getExtractedInfo();
226 // Need to set this so we can += to it later
227 $data['globals']['wgAutoloadClasses'] = array();
228 $data['autoload'] = $autoloadClasses;
229 $data['autoloaderPaths'] = $autoloaderPaths;
230 return $data;
233 protected function exportExtractedData( array $info ) {
234 foreach ( $info['globals'] as $key => $val ) {
235 // If a merge strategy is set, read it and remove it from the value
236 // so it doesn't accidentally end up getting set.
237 // Need to check $val is an array for PHP 5.3 which will return
238 // true on isset( 'string'['foo'] ).
239 if ( isset( $val[self::MERGE_STRATEGY] ) && is_array( $val ) ) {
240 $mergeStrategy = $val[self::MERGE_STRATEGY];
241 unset( $val[self::MERGE_STRATEGY] );
242 } else {
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;
250 continue;
253 if ( !is_array( $GLOBALS[$key] ) || !is_array( $val ) ) {
254 // config setting that has already been overridden, don't set it
255 continue;
258 switch ( $mergeStrategy ) {
259 case 'array_merge_recursive':
260 $GLOBALS[$key] = array_merge_recursive( $GLOBALS[$key], $val );
261 break;
262 case 'array_plus_2d':
263 $GLOBALS[$key] = wfArrayPlus2d( $GLOBALS[$key], $val );
264 break;
265 case 'array_plus':
266 $GLOBALS[$key] += $val;
267 break;
268 case 'array_merge':
269 $GLOBALS[$key] = array_merge( $val, $GLOBALS[$key] );
270 break;
271 default:
272 throw new UnexpectedValueException( "Unknown merge strategy '$mergeStrategy'" );
276 foreach ( $info['defines'] as $name => $val ) {
277 define( $name, $val );
279 foreach ( $info['callbacks'] as $cb ) {
280 call_user_func( $cb );
283 foreach ( $info['autoloaderPaths'] as $path ) {
284 require_once $path;
287 $this->loaded += $info['credits'];
288 if ( $info['attributes'] ) {
289 if ( !$this->attributes ) {
290 $this->attributes = $info['attributes'];
291 } else {
292 $this->attributes = array_merge_recursive( $this->attributes, $info['attributes'] );
298 * Loads and processes the given JSON file without delay
300 * If some extensions are already queued, this will load
301 * those as well.
303 * @param string $path Absolute path to the JSON file
305 public function load( $path ) {
306 $this->loadFromQueue(); // First clear the queue
307 $this->queue( $path );
308 $this->loadFromQueue();
312 * Whether a thing has been loaded
313 * @param string $name
314 * @return bool
316 public function isLoaded( $name ) {
317 return isset( $this->loaded[$name] );
321 * @param string $name
322 * @return array
324 public function getAttribute( $name ) {
325 if ( isset( $this->attributes[$name] ) ) {
326 return $this->attributes[$name];
327 } else {
328 return array();
333 * Get information about all things
335 * @return array
337 public function getAllThings() {
338 return $this->loaded;
342 * Mark a thing as loaded
344 * @param string $name
345 * @param array $credits
347 protected function markLoaded( $name, array $credits ) {
348 $this->loaded[$name] = $credits;
352 * Register classes with the autoloader
354 * @param string $dir
355 * @param array $info
356 * @return array
358 protected function processAutoLoader( $dir, array $info ) {
359 if ( isset( $info['AutoloadClasses'] ) ) {
360 // Make paths absolute, relative to the JSON file
361 return array_map( function( $file ) use ( $dir ) {
362 return "$dir/$file";
363 }, $info['AutoloadClasses'] );
364 } else {
365 return array();