Merge "SpecialMovepage: Convert form to use OOUI controls"
[lhc/web/wiklou.git] / includes / registration / ExtensionRegistry.php
1 <?php
2
3 /**
4 * ExtensionRegistry class
5 *
6 * The Registry loads JSON files, and uses a Processor
7 * to extract information from them. It also registers
8 * classes with the autoloader.
9 *
10 * @since 1.25
11 */
12 class ExtensionRegistry {
13
14 /**
15 * Version of the highest supported manifest version
16 */
17 const MANIFEST_VERSION = 1;
18
19 /**
20 * Version of the oldest supported manifest version
21 */
22 const OLDEST_MANIFEST_VERSION = 1;
23
24 /**
25 * Bump whenever the registration cache needs resetting
26 */
27 const CACHE_VERSION = 1;
28
29 /**
30 * Special key that defines the merge strategy
31 *
32 * @since 1.26
33 */
34 const MERGE_STRATEGY = '_merge_strategy';
35
36 /**
37 * @var BagOStuff
38 */
39 protected $cache;
40
41 /**
42 * Array of loaded things, keyed by name, values are credits information
43 *
44 * @var array
45 */
46 private $loaded = array();
47
48 /**
49 * List of paths that should be loaded
50 *
51 * @var array
52 */
53 protected $queued = array();
54
55 /**
56 * Items in the JSON file that aren't being
57 * set as globals
58 *
59 * @var array
60 */
61 protected $attributes = array();
62
63 /**
64 * @var ExtensionRegistry
65 */
66 private static $instance;
67
68 /**
69 * @return ExtensionRegistry
70 */
71 public static function getInstance() {
72 if ( self::$instance === null ) {
73 self::$instance = new self();
74 }
75
76 return self::$instance;
77 }
78
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
83 try {
84 $this->cache = ObjectCache::newAccelerator();
85 } catch ( MWException $e ) {
86 $this->cache = new EmptyBagOStuff();
87 }
88 }
89
90 /**
91 * @param string $path Absolute path to the JSON file
92 */
93 public function queue( $path ) {
94 global $wgExtensionInfoMTime;
95
96 $mtime = $wgExtensionInfoMTime;
97 if ( $mtime === false ) {
98 if ( file_exists( $path ) ) {
99 $mtime = filemtime( $path );
100 } else {
101 throw new Exception( "$path does not exist!" );
102 }
103 if ( !$mtime ) {
104 $err = error_get_last();
105 throw new Exception( "Couldn't stat $path: {$err['message']}" );
106 }
107 }
108 $this->queued[$path] = $mtime;
109 }
110
111 public function loadFromQueue() {
112 if ( !$this->queued ) {
113 return;
114 }
115
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 );
119 if ( $data ) {
120 $this->exportExtractedData( $data );
121 } else {
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 );
129 }
130 $this->queued = array();
131 }
132
133 /**
134 * Get the current load queue. Not intended to be used
135 * outside of the installer.
136 *
137 * @return array
138 */
139 public function getQueue() {
140 return $this->queued;
141 }
142
143 /**
144 * Clear the current load queue. Not intended to be used
145 * outside of the installer.
146 */
147 public function clearQueue() {
148 $this->queued = array();
149 }
150
151 /**
152 * Process a queue of extensions and return their extracted data
153 *
154 * @param array $queue keys are filenames, values are ignored
155 * @return array extracted info
156 * @throws Exception
157 */
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?" );
165 }
166 $info = json_decode( $json, /* $assoc = */ true );
167 if ( !is_array( $info ) ) {
168 throw new Exception( "$path is not a valid JSON file." );
169 }
170 if ( !isset( $info['manifest_version'] ) ) {
171 // For backwards-compatability, assume a version of 1
172 $info['manifest_version'] = 1;
173 }
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}" );
177 }
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 );
183 }
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;
189 }
190 $data['globals']['wgExtensionCredits'][self::MERGE_STRATEGY] = 'array_merge_recursive';
191 $data['autoload'] = $autoloadClasses;
192 return $data;
193 }
194
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] );
204 } else {
205 $mergeStrategy = 'array_merge';
206 }
207
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;
212 continue;
213 }
214
215 if ( !is_array( $GLOBALS[$key] ) || !is_array( $val ) ) {
216 // config setting that has already been overridden, don't set it
217 continue;
218 }
219
220 switch ( $mergeStrategy ) {
221 case 'array_merge_recursive':
222 $GLOBALS[$key] = array_merge_recursive( $GLOBALS[$key], $val );
223 break;
224 case 'array_plus_2d':
225 $GLOBALS[$key] = wfArrayPlus2d( $GLOBALS[$key], $val );
226 break;
227 case 'array_plus':
228 $GLOBALS[$key] = $val + $GLOBALS[$key];
229 break;
230 case 'array_merge':
231 $GLOBALS[$key] = array_merge( $val, $GLOBALS[$key] );
232 break;
233 default:
234 throw new UnexpectedValueException( "Unknown merge strategy '$mergeStrategy'" );
235 }
236 }
237
238 foreach ( $info['defines'] as $name => $val ) {
239 define( $name, $val );
240 }
241 foreach ( $info['callbacks'] as $cb ) {
242 call_user_func( $cb );
243 }
244
245 $this->loaded += $info['credits'];
246
247 if ( $info['attributes'] ) {
248 if ( !$this->attributes ) {
249 $this->attributes = $info['attributes'];
250 } else {
251 $this->attributes = array_merge_recursive( $this->attributes, $info['attributes'] );
252 }
253 }
254 }
255
256 /**
257 * Loads and processes the given JSON file without delay
258 *
259 * If some extensions are already queued, this will load
260 * those as well.
261 *
262 * @param string $path Absolute path to the JSON file
263 */
264 public function load( $path ) {
265 $this->loadFromQueue(); // First clear the queue
266 $this->queue( $path );
267 $this->loadFromQueue();
268 }
269
270 /**
271 * Whether a thing has been loaded
272 * @param string $name
273 * @return bool
274 */
275 public function isLoaded( $name ) {
276 return isset( $this->loaded[$name] );
277 }
278
279 /**
280 * @param string $name
281 * @return array
282 */
283 public function getAttribute( $name ) {
284 if ( isset( $this->attributes[$name] ) ) {
285 return $this->attributes[$name];
286 } else {
287 return array();
288 }
289 }
290
291 /**
292 * Get information about all things
293 *
294 * @return array
295 */
296 public function getAllThings() {
297 return $this->loaded;
298 }
299
300 /**
301 * Mark a thing as loaded
302 *
303 * @param string $name
304 * @param array $credits
305 */
306 protected function markLoaded( $name, array $credits ) {
307 $this->loaded[$name] = $credits;
308 }
309
310 /**
311 * Register classes with the autoloader
312 *
313 * @param string $dir
314 * @param array $info
315 * @return array
316 */
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 ) {
321 return "$dir/$file";
322 }, $info['AutoloadClasses'] );
323 } else {
324 return array();
325 }
326 }
327 }