Merge "registration: Overhaul merging of globals"
[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 * Special key that defines the merge strategy
26 *
27 * @since 1.26
28 */
29 const MERGE_STRATEGY = '_merge_strategy';
30
31 /**
32 * @var BagOStuff
33 */
34 protected $cache;
35
36 /**
37 * Array of loaded things, keyed by name, values are credits information
38 *
39 * @var array
40 */
41 private $loaded = array();
42
43 /**
44 * List of paths that should be loaded
45 *
46 * @var array
47 */
48 protected $queued = array();
49
50 /**
51 * Items in the JSON file that aren't being
52 * set as globals
53 *
54 * @var array
55 */
56 protected $attributes = array();
57
58 /**
59 * @var ExtensionRegistry
60 */
61 private static $instance;
62
63 /**
64 * @return ExtensionRegistry
65 */
66 public static function getInstance() {
67 if ( self::$instance === null ) {
68 self::$instance = new self();
69 }
70
71 return self::$instance;
72 }
73
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
78 try {
79 $this->cache = ObjectCache::newAccelerator( array() );
80 } catch ( MWException $e ) {
81 $this->cache = new EmptyBagOStuff();
82 }
83 }
84
85 /**
86 * @param string $path Absolute path to the JSON file
87 */
88 public function queue( $path ) {
89 global $wgExtensionInfoMTime;
90
91 $mtime = $wgExtensionInfoMTime;
92 if ( $mtime === false ) {
93 if ( file_exists( $path ) ) {
94 $mtime = filemtime( $path );
95 } else {
96 throw new Exception( "$path does not exist!" );
97 }
98 if ( !$mtime ) {
99 $err = error_get_last();
100 throw new Exception( "Couldn't stat $path: {$err['message']}" );
101 }
102 }
103 $this->queued[$path] = $mtime;
104 }
105
106 public function loadFromQueue() {
107 if ( !$this->queued ) {
108 return;
109 }
110
111 // See if this queue is in APC
112 $key = wfMemcKey( 'registration', md5( json_encode( $this->queued ) ) );
113 $data = $this->cache->get( $key );
114 if ( $data ) {
115 $this->exportExtractedData( $data );
116 } else {
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 );
124 }
125 $this->queued = array();
126 }
127
128 /**
129 * Get the current load queue. Not intended to be used
130 * outside of the installer.
131 *
132 * @return array
133 */
134 public function getQueue() {
135 return $this->queued;
136 }
137
138 /**
139 * Clear the current load queue. Not intended to be used
140 * outside of the installer.
141 */
142 public function clearQueue() {
143 $this->queued = array();
144 }
145
146 /**
147 * Process a queue of extensions and return their extracted data
148 *
149 * @param array $queue keys are filenames, values are ignored
150 * @return array extracted info
151 * @throws Exception
152 */
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?" );
160 }
161 $info = json_decode( $json, /* $assoc = */ true );
162 if ( !is_array( $info ) ) {
163 throw new Exception( "$path is not a valid JSON file." );
164 }
165 if ( !isset( $info['manifest_version'] ) ) {
166 // For backwards-compatability, assume a version of 1
167 $info['manifest_version'] = 1;
168 }
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}" );
172 }
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 );
178 }
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;
184 }
185 $data['autoload'] = $autoloadClasses;
186 return $data;
187 }
188
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] );
198 } else {
199 $mergeStrategy = 'array_merge';
200 }
201
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;
206 continue;
207 }
208
209 if ( !is_array( $GLOBALS[$key] ) || !is_array( $val ) ) {
210 // config setting that has already been overridden, don't set it
211 continue;
212 }
213
214 switch ( $mergeStrategy ) {
215 case 'array_merge_recursive':
216 $GLOBALS[$key] = array_merge_recursive( $GLOBALS[$key], $val );
217 break;
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];
223 }
224 }
225 // Now add items that didn't exist yet
226 $GLOBALS[$key] += $val;
227 break;
228 case 'array_plus':
229 $GLOBALS[$key] = $val + $GLOBALS[$key];
230 break;
231 case 'array_merge':
232 $GLOBALS[$key] = array_merge( $val, $GLOBALS[$key] );
233 break;
234 default:
235 throw new UnexpectedValueException( "Unknown merge strategy '$mergeStrategy'" );
236 }
237 }
238
239 foreach ( $info['defines'] as $name => $val ) {
240 define( $name, $val );
241 }
242 foreach ( $info['callbacks'] as $cb ) {
243 call_user_func( $cb );
244 }
245
246 $this->loaded += $info['credits'];
247
248 if ( $info['attributes'] ) {
249 if ( !$this->attributes ) {
250 $this->attributes = $info['attributes'];
251 } else {
252 $this->attributes = array_merge_recursive( $this->attributes, $info['attributes'] );
253 }
254 }
255 }
256
257 /**
258 * Loads and processes the given JSON file without delay
259 *
260 * If some extensions are already queued, this will load
261 * those as well.
262 *
263 * @param string $path Absolute path to the JSON file
264 */
265 public function load( $path ) {
266 $this->loadFromQueue(); // First clear the queue
267 $this->queue( $path );
268 $this->loadFromQueue();
269 }
270
271 /**
272 * Whether a thing has been loaded
273 * @param string $name
274 * @return bool
275 */
276 public function isLoaded( $name ) {
277 return isset( $this->loaded[$name] );
278 }
279
280 /**
281 * @param string $name
282 * @return array
283 */
284 public function getAttribute( $name ) {
285 if ( isset( $this->attributes[$name] ) ) {
286 return $this->attributes[$name];
287 } else {
288 return array();
289 }
290 }
291
292 /**
293 * Get information about all things
294 *
295 * @return array
296 */
297 public function getAllThings() {
298 return $this->loaded;
299 }
300
301 /**
302 * Mark a thing as loaded
303 *
304 * @param string $name
305 * @param array $credits
306 */
307 protected function markLoaded( $name, array $credits ) {
308 $this->loaded[$name] = $credits;
309 }
310
311 /**
312 * Register classes with the autoloader
313 *
314 * @param string $dir
315 * @param array $info
316 * @return array
317 */
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 ) {
322 return "$dir/$file";
323 }, $info['AutoloadClasses'] );
324 } else {
325 return array();
326 }
327 }
328 }