Merge "Installer: Validate password against sysop/bureaucrat policies"
[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 * "requires" key that applies to MediaWiki core/$wgVersion
16 */
17 const MEDIAWIKI_CORE = 'MediaWiki';
18
19 /**
20 * Version of the highest supported manifest version
21 */
22 const MANIFEST_VERSION = 1;
23
24 /**
25 * Version of the oldest supported manifest version
26 */
27 const OLDEST_MANIFEST_VERSION = 1;
28
29 /**
30 * Bump whenever the registration cache needs resetting
31 */
32 const CACHE_VERSION = 1;
33
34 /**
35 * Special key that defines the merge strategy
36 *
37 * @since 1.26
38 */
39 const MERGE_STRATEGY = '_merge_strategy';
40
41 /**
42 * @var BagOStuff
43 */
44 protected $cache;
45
46 /**
47 * Array of loaded things, keyed by name, values are credits information
48 *
49 * @var array
50 */
51 private $loaded = array();
52
53 /**
54 * List of paths that should be loaded
55 *
56 * @var array
57 */
58 protected $queued = array();
59
60 /**
61 * Items in the JSON file that aren't being
62 * set as globals
63 *
64 * @var array
65 */
66 protected $attributes = array();
67
68 /**
69 * @var ExtensionRegistry
70 */
71 private static $instance;
72
73 /**
74 * @return ExtensionRegistry
75 */
76 public static function getInstance() {
77 if ( self::$instance === null ) {
78 self::$instance = new self();
79 }
80
81 return self::$instance;
82 }
83
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::newAccelerator();
90 } catch ( MWException $e ) {
91 $this->cache = new EmptyBagOStuff();
92 }
93 }
94
95 /**
96 * @param string $path Absolute path to the JSON file
97 */
98 public function queue( $path ) {
99 global $wgExtensionInfoMTime;
100
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!" );
107 }
108 if ( !$mtime ) {
109 $err = error_get_last();
110 throw new Exception( "Couldn't stat $path: {$err['message']}" );
111 }
112 }
113 $this->queued[$path] = $mtime;
114 }
115
116 public function loadFromQueue() {
117 global $wgVersion;
118 if ( !$this->queued ) {
119 return;
120 }
121
122 // A few more things to vary the cache on
123 $versions = array(
124 'registration' => self::CACHE_VERSION,
125 'mediawiki' => $wgVersion
126 );
127
128 // See if this queue is in APC
129 $key = wfMemcKey(
130 'registration',
131 md5( json_encode( $this->queued + $versions ) )
132 );
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 );
144 }
145 $this->queued = array();
146 }
147
148 /**
149 * Get the current load queue. Not intended to be used
150 * outside of the installer.
151 *
152 * @return array
153 */
154 public function getQueue() {
155 return $this->queued;
156 }
157
158 /**
159 * Clear the current load queue. Not intended to be used
160 * outside of the installer.
161 */
162 public function clearQueue() {
163 $this->queued = array();
164 }
165
166 /**
167 * Process a queue of extensions and return their extracted data
168 *
169 * @param array $queue keys are filenames, values are ignored
170 * @return array extracted info
171 * @throws Exception
172 */
173 public function readFromQueue( array $queue ) {
174 global $wgVersion;
175 $autoloadClasses = array();
176 $processor = new ExtensionProcessor();
177 $incompatible = array();
178 $coreVersionParser = new CoreVersionChecker( $wgVersion );
179 foreach ( $queue as $path => $mtime ) {
180 $json = file_get_contents( $path );
181 if ( $json === false ) {
182 throw new Exception( "Unable to read $path, does it exist?" );
183 }
184 $info = json_decode( $json, /* $assoc = */ true );
185 if ( !is_array( $info ) ) {
186 throw new Exception( "$path is not a valid JSON file." );
187 }
188 if ( !isset( $info['manifest_version'] ) ) {
189 // For backwards-compatability, assume a version of 1
190 $info['manifest_version'] = 1;
191 }
192 $version = $info['manifest_version'];
193 if ( $version < self::OLDEST_MANIFEST_VERSION || $version > self::MANIFEST_VERSION ) {
194 throw new Exception( "$path: unsupported manifest_version: {$version}" );
195 }
196 $autoload = $this->processAutoLoader( dirname( $path ), $info );
197 // Set up the autoloader now so custom processors will work
198 $GLOBALS['wgAutoloadClasses'] += $autoload;
199 $autoloadClasses += $autoload;
200 // Check any constraints against MediaWiki core
201 $requires = $processor->getRequirements( $info );
202 if ( isset( $requires[self::MEDIAWIKI_CORE] )
203 && !$coreVersionParser->check( $requires[self::MEDIAWIKI_CORE] )
204 ) {
205 // Doesn't match, mark it as incompatible.
206 $incompatible[] = "{$info['name']} is not compatible with the current "
207 . "MediaWiki core (version {$wgVersion}), it requires: " . $requires[self::MEDIAWIKI_CORE]
208 . '.';
209 continue;
210 }
211 // Compatible, read and extract info
212 $processor->extractInfo( $path, $info, $version );
213 }
214 if ( $incompatible ) {
215 if ( count( $incompatible ) === 1 ) {
216 throw new Exception( $incompatible[0] );
217 } else {
218 throw new Exception( implode( "\n", $incompatible ) );
219 }
220 }
221 $data = $processor->getExtractedInfo();
222 // Need to set this so we can += to it later
223 $data['globals']['wgAutoloadClasses'] = array();
224 foreach ( $data['credits'] as $credit ) {
225 $data['globals']['wgExtensionCredits'][$credit['type']][] = $credit;
226 }
227 $data['globals']['wgExtensionCredits'][self::MERGE_STRATEGY] = 'array_merge_recursive';
228 $data['autoload'] = $autoloadClasses;
229 return $data;
230 }
231
232 protected function exportExtractedData( array $info ) {
233 foreach ( $info['globals'] as $key => $val ) {
234 // If a merge strategy is set, read it and remove it from the value
235 // so it doesn't accidentally end up getting set.
236 // Need to check $val is an array for PHP 5.3 which will return
237 // true on isset( 'string'['foo'] ).
238 if ( isset( $val[self::MERGE_STRATEGY] ) && is_array( $val ) ) {
239 $mergeStrategy = $val[self::MERGE_STRATEGY];
240 unset( $val[self::MERGE_STRATEGY] );
241 } else {
242 $mergeStrategy = 'array_merge';
243 }
244
245 // Optimistic: If the global is not set, or is an empty array, replace it entirely.
246 // Will be O(1) performance.
247 if ( !isset( $GLOBALS[$key] ) || ( is_array( $GLOBALS[$key] ) && !$GLOBALS[$key] ) ) {
248 $GLOBALS[$key] = $val;
249 continue;
250 }
251
252 if ( !is_array( $GLOBALS[$key] ) || !is_array( $val ) ) {
253 // config setting that has already been overridden, don't set it
254 continue;
255 }
256
257 switch ( $mergeStrategy ) {
258 case 'array_merge_recursive':
259 $GLOBALS[$key] = array_merge_recursive( $GLOBALS[$key], $val );
260 break;
261 case 'array_plus_2d':
262 $GLOBALS[$key] = wfArrayPlus2d( $GLOBALS[$key], $val );
263 break;
264 case 'array_plus':
265 $GLOBALS[$key] += $val;
266 break;
267 case 'array_merge':
268 $GLOBALS[$key] = array_merge( $val, $GLOBALS[$key] );
269 break;
270 default:
271 throw new UnexpectedValueException( "Unknown merge strategy '$mergeStrategy'" );
272 }
273 }
274
275 foreach ( $info['defines'] as $name => $val ) {
276 define( $name, $val );
277 }
278 foreach ( $info['callbacks'] as $cb ) {
279 call_user_func( $cb );
280 }
281
282 $this->loaded += $info['credits'];
283
284 if ( $info['attributes'] ) {
285 if ( !$this->attributes ) {
286 $this->attributes = $info['attributes'];
287 } else {
288 $this->attributes = array_merge_recursive( $this->attributes, $info['attributes'] );
289 }
290 }
291 }
292
293 /**
294 * Loads and processes the given JSON file without delay
295 *
296 * If some extensions are already queued, this will load
297 * those as well.
298 *
299 * @param string $path Absolute path to the JSON file
300 */
301 public function load( $path ) {
302 $this->loadFromQueue(); // First clear the queue
303 $this->queue( $path );
304 $this->loadFromQueue();
305 }
306
307 /**
308 * Whether a thing has been loaded
309 * @param string $name
310 * @return bool
311 */
312 public function isLoaded( $name ) {
313 return isset( $this->loaded[$name] );
314 }
315
316 /**
317 * @param string $name
318 * @return array
319 */
320 public function getAttribute( $name ) {
321 if ( isset( $this->attributes[$name] ) ) {
322 return $this->attributes[$name];
323 } else {
324 return array();
325 }
326 }
327
328 /**
329 * Get information about all things
330 *
331 * @return array
332 */
333 public function getAllThings() {
334 return $this->loaded;
335 }
336
337 /**
338 * Mark a thing as loaded
339 *
340 * @param string $name
341 * @param array $credits
342 */
343 protected function markLoaded( $name, array $credits ) {
344 $this->loaded[$name] = $credits;
345 }
346
347 /**
348 * Register classes with the autoloader
349 *
350 * @param string $dir
351 * @param array $info
352 * @return array
353 */
354 protected function processAutoLoader( $dir, array $info ) {
355 if ( isset( $info['AutoloadClasses'] ) ) {
356 // Make paths absolute, relative to the JSON file
357 return array_map( function( $file ) use ( $dir ) {
358 return "$dir/$file";
359 }, $info['AutoloadClasses'] );
360 } else {
361 return array();
362 }
363 }
364 }