Merge "Only load gallery styling rules when galleries are on the page"
[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 // First merge items that are in both arrays
226 foreach ( $GLOBALS[$key] as $name => &$groupVal ) {
227 if ( isset( $val[$name] ) ) {
228 $groupVal += $val[$name];
229 }
230 }
231 // Now add items that didn't exist yet
232 $GLOBALS[$key] += $val;
233 break;
234 case 'array_plus':
235 $GLOBALS[$key] = $val + $GLOBALS[$key];
236 break;
237 case 'array_merge':
238 $GLOBALS[$key] = array_merge( $val, $GLOBALS[$key] );
239 break;
240 default:
241 throw new UnexpectedValueException( "Unknown merge strategy '$mergeStrategy'" );
242 }
243 }
244
245 foreach ( $info['defines'] as $name => $val ) {
246 define( $name, $val );
247 }
248 foreach ( $info['callbacks'] as $cb ) {
249 call_user_func( $cb );
250 }
251
252 $this->loaded += $info['credits'];
253
254 if ( $info['attributes'] ) {
255 if ( !$this->attributes ) {
256 $this->attributes = $info['attributes'];
257 } else {
258 $this->attributes = array_merge_recursive( $this->attributes, $info['attributes'] );
259 }
260 }
261 }
262
263 /**
264 * Loads and processes the given JSON file without delay
265 *
266 * If some extensions are already queued, this will load
267 * those as well.
268 *
269 * @param string $path Absolute path to the JSON file
270 */
271 public function load( $path ) {
272 $this->loadFromQueue(); // First clear the queue
273 $this->queue( $path );
274 $this->loadFromQueue();
275 }
276
277 /**
278 * Whether a thing has been loaded
279 * @param string $name
280 * @return bool
281 */
282 public function isLoaded( $name ) {
283 return isset( $this->loaded[$name] );
284 }
285
286 /**
287 * @param string $name
288 * @return array
289 */
290 public function getAttribute( $name ) {
291 if ( isset( $this->attributes[$name] ) ) {
292 return $this->attributes[$name];
293 } else {
294 return array();
295 }
296 }
297
298 /**
299 * Get information about all things
300 *
301 * @return array
302 */
303 public function getAllThings() {
304 return $this->loaded;
305 }
306
307 /**
308 * Mark a thing as loaded
309 *
310 * @param string $name
311 * @param array $credits
312 */
313 protected function markLoaded( $name, array $credits ) {
314 $this->loaded[$name] = $credits;
315 }
316
317 /**
318 * Register classes with the autoloader
319 *
320 * @param string $dir
321 * @param array $info
322 * @return array
323 */
324 protected function processAutoLoader( $dir, array $info ) {
325 if ( isset( $info['AutoloadClasses'] ) ) {
326 // Make paths absolute, relative to the JSON file
327 return array_map( function( $file ) use ( $dir ) {
328 return "$dir/$file";
329 }, $info['AutoloadClasses'] );
330 } else {
331 return array();
332 }
333 }
334 }