Merge "registration: Allow to require environment abilities"
[lhc/web/wiklou.git] / includes / registration / ExtensionRegistry.php
1 <?php
2
3 use Composer\Semver\Semver;
4 use Wikimedia\ScopedCallback;
5 use MediaWiki\Shell\Shell;
6 use MediaWiki\ShellDisabledError;
7
8 /**
9 * ExtensionRegistry class
10 *
11 * The Registry loads JSON files, and uses a Processor
12 * to extract information from them. It also registers
13 * classes with the autoloader.
14 *
15 * @since 1.25
16 */
17 class ExtensionRegistry {
18
19 /**
20 * "requires" key that applies to MediaWiki core/$wgVersion
21 */
22 const MEDIAWIKI_CORE = 'MediaWiki';
23
24 /**
25 * Version of the highest supported manifest version
26 * Note: Update MANIFEST_VERSION_MW_VERSION when changing this
27 */
28 const MANIFEST_VERSION = 2;
29
30 /**
31 * MediaWiki version constraint representing what the current
32 * highest MANIFEST_VERSION is supported in
33 */
34 const MANIFEST_VERSION_MW_VERSION = '>= 1.29.0';
35
36 /**
37 * Version of the oldest supported manifest version
38 */
39 const OLDEST_MANIFEST_VERSION = 1;
40
41 /**
42 * Bump whenever the registration cache needs resetting
43 */
44 const CACHE_VERSION = 7;
45
46 /**
47 * Special key that defines the merge strategy
48 *
49 * @since 1.26
50 */
51 const MERGE_STRATEGY = '_merge_strategy';
52
53 /**
54 * Array of loaded things, keyed by name, values are credits information
55 *
56 * @var array
57 */
58 private $loaded = [];
59
60 /**
61 * List of paths that should be loaded
62 *
63 * @var array
64 */
65 protected $queued = [];
66
67 /**
68 * Whether we are done loading things
69 *
70 * @var bool
71 */
72 private $finished = false;
73
74 /**
75 * Items in the JSON file that aren't being
76 * set as globals
77 *
78 * @var array
79 */
80 protected $attributes = [];
81
82 /**
83 * Attributes for testing
84 *
85 * @var array
86 */
87 protected $testAttributes = [];
88
89 /**
90 * @var ExtensionRegistry
91 */
92 private static $instance;
93
94 /**
95 * @codeCoverageIgnore
96 * @return ExtensionRegistry
97 */
98 public static function getInstance() {
99 if ( self::$instance === null ) {
100 self::$instance = new self();
101 }
102
103 return self::$instance;
104 }
105
106 /**
107 * @param string $path Absolute path to the JSON file
108 */
109 public function queue( $path ) {
110 global $wgExtensionInfoMTime;
111
112 $mtime = $wgExtensionInfoMTime;
113 if ( $mtime === false ) {
114 if ( file_exists( $path ) ) {
115 $mtime = filemtime( $path );
116 } else {
117 throw new Exception( "$path does not exist!" );
118 }
119 // @codeCoverageIgnoreStart
120 if ( $mtime === false ) {
121 $err = error_get_last();
122 throw new Exception( "Couldn't stat $path: {$err['message']}" );
123 // @codeCoverageIgnoreEnd
124 }
125 }
126 $this->queued[$path] = $mtime;
127 }
128
129 /**
130 * @throws MWException If the queue is already marked as finished (no further things should
131 * be loaded then).
132 */
133 public function loadFromQueue() {
134 global $wgVersion, $wgDevelopmentWarnings;
135 if ( !$this->queued ) {
136 return;
137 }
138
139 if ( $this->finished ) {
140 throw new MWException(
141 "The following paths tried to load late: "
142 . implode( ', ', array_keys( $this->queued ) )
143 );
144 }
145
146 // A few more things to vary the cache on
147 $versions = [
148 'registration' => self::CACHE_VERSION,
149 'mediawiki' => $wgVersion,
150 'abilities' => $this->getAbilities(),
151 ];
152
153 // We use a try/catch because we don't want to fail here
154 // if $wgObjectCaches is not configured properly for APC setup
155 try {
156 // Don't use MediaWikiServices here to prevent instantiating it before extensions have
157 // been loaded
158 $cacheId = ObjectCache::detectLocalServerCache();
159 $cache = ObjectCache::newFromId( $cacheId );
160 } catch ( InvalidArgumentException $e ) {
161 $cache = new EmptyBagOStuff();
162 }
163 // See if this queue is in APC
164 $key = $cache->makeKey(
165 'registration',
166 md5( json_encode( $this->queued + $versions ) )
167 );
168 $data = $cache->get( $key );
169 if ( $data ) {
170 $this->exportExtractedData( $data );
171 } else {
172 $data = $this->readFromQueue( $this->queued );
173 $this->exportExtractedData( $data );
174 // Do this late since we don't want to extract it since we already
175 // did that, but it should be cached
176 $data['globals']['wgAutoloadClasses'] += $data['autoload'];
177 unset( $data['autoload'] );
178 if ( !( $data['warnings'] && $wgDevelopmentWarnings ) ) {
179 // If there were no warnings that were shown, cache it
180 $cache->set( $key, $data, 60 * 60 * 24 );
181 }
182 }
183 $this->queued = [];
184 }
185
186 /**
187 * Get the current load queue. Not intended to be used
188 * outside of the installer.
189 *
190 * @return array
191 */
192 public function getQueue() {
193 return $this->queued;
194 }
195
196 /**
197 * Clear the current load queue. Not intended to be used
198 * outside of the installer.
199 */
200 public function clearQueue() {
201 $this->queued = [];
202 }
203
204 /**
205 * After this is called, no more extensions can be loaded
206 *
207 * @since 1.29
208 */
209 public function finish() {
210 $this->finished = true;
211 }
212
213 /**
214 * Get the list of abilities and their values
215 * @return bool[]
216 */
217 private function getAbilities() {
218 return [
219 'shell' => !Shell::isDisabled(),
220 ];
221 }
222
223 /**
224 * Queries information about the software environment and constructs an appropiate version checker
225 *
226 * @return VersionChecker
227 */
228 private function buildVersionChecker() {
229 global $wgVersion;
230 // array to optionally specify more verbose error messages for
231 // missing abilities
232 $abilityErrors = [
233 'shell' => ( new ShellDisabledError() )->getMessage(),
234 ];
235
236 return new VersionChecker(
237 $wgVersion,
238 PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION . '.' . PHP_RELEASE_VERSION,
239 get_loaded_extensions(),
240 $this->getAbilities(),
241 $abilityErrors
242 );
243 }
244
245 /**
246 * Process a queue of extensions and return their extracted data
247 *
248 * @param array $queue keys are filenames, values are ignored
249 * @return array extracted info
250 * @throws Exception
251 * @throws ExtensionDependencyError
252 */
253 public function readFromQueue( array $queue ) {
254 $autoloadClasses = [];
255 $autoloadNamespaces = [];
256 $autoloaderPaths = [];
257 $processor = new ExtensionProcessor();
258 $versionChecker = $this->buildVersionChecker();
259 $extDependencies = [];
260 $incompatible = [];
261 $warnings = false;
262 foreach ( $queue as $path => $mtime ) {
263 $json = file_get_contents( $path );
264 if ( $json === false ) {
265 throw new Exception( "Unable to read $path, does it exist?" );
266 }
267 $info = json_decode( $json, /* $assoc = */ true );
268 if ( !is_array( $info ) ) {
269 throw new Exception( "$path is not a valid JSON file." );
270 }
271
272 if ( !isset( $info['manifest_version'] ) ) {
273 wfDeprecated(
274 "{$info['name']}'s extension.json or skin.json does not have manifest_version",
275 '1.29'
276 );
277 $warnings = true;
278 // For backwards-compatability, assume a version of 1
279 $info['manifest_version'] = 1;
280 }
281 $version = $info['manifest_version'];
282 if ( $version < self::OLDEST_MANIFEST_VERSION || $version > self::MANIFEST_VERSION ) {
283 $incompatible[] = "$path: unsupported manifest_version: {$version}";
284 }
285
286 $dir = dirname( $path );
287 if ( isset( $info['AutoloadClasses'] ) ) {
288 $autoload = $this->processAutoLoader( $dir, $info['AutoloadClasses'] );
289 $GLOBALS['wgAutoloadClasses'] += $autoload;
290 $autoloadClasses += $autoload;
291 }
292 if ( isset( $info['AutoloadNamespaces'] ) ) {
293 $autoloadNamespaces += $this->processAutoLoader( $dir, $info['AutoloadNamespaces'] );
294 AutoLoader::$psr4Namespaces += $autoloadNamespaces;
295 }
296
297 // get all requirements/dependencies for this extension
298 $requires = $processor->getRequirements( $info );
299
300 // validate the information needed and add the requirements
301 if ( is_array( $requires ) && $requires && isset( $info['name'] ) ) {
302 $extDependencies[$info['name']] = $requires;
303 }
304
305 // Get extra paths for later inclusion
306 $autoloaderPaths = array_merge( $autoloaderPaths,
307 $processor->getExtraAutoloaderPaths( $dir, $info ) );
308 // Compatible, read and extract info
309 $processor->extractInfo( $path, $info, $version );
310 }
311 $data = $processor->getExtractedInfo();
312 $data['warnings'] = $warnings;
313
314 // check for incompatible extensions
315 $incompatible = array_merge(
316 $incompatible,
317 $versionChecker
318 ->setLoadedExtensionsAndSkins( $data['credits'] )
319 ->checkArray( $extDependencies )
320 );
321
322 if ( $incompatible ) {
323 throw new ExtensionDependencyError( $incompatible );
324 }
325
326 // Need to set this so we can += to it later
327 $data['globals']['wgAutoloadClasses'] = [];
328 $data['autoload'] = $autoloadClasses;
329 $data['autoloaderPaths'] = $autoloaderPaths;
330 $data['autoloaderNS'] = $autoloadNamespaces;
331 return $data;
332 }
333
334 protected function exportExtractedData( array $info ) {
335 foreach ( $info['globals'] as $key => $val ) {
336 // If a merge strategy is set, read it and remove it from the value
337 // so it doesn't accidentally end up getting set.
338 if ( is_array( $val ) && isset( $val[self::MERGE_STRATEGY] ) ) {
339 $mergeStrategy = $val[self::MERGE_STRATEGY];
340 unset( $val[self::MERGE_STRATEGY] );
341 } else {
342 $mergeStrategy = 'array_merge';
343 }
344
345 // Optimistic: If the global is not set, or is an empty array, replace it entirely.
346 // Will be O(1) performance.
347 if ( !array_key_exists( $key, $GLOBALS ) || ( is_array( $GLOBALS[$key] ) && !$GLOBALS[$key] ) ) {
348 $GLOBALS[$key] = $val;
349 continue;
350 }
351
352 if ( !is_array( $GLOBALS[$key] ) || !is_array( $val ) ) {
353 // config setting that has already been overridden, don't set it
354 continue;
355 }
356
357 switch ( $mergeStrategy ) {
358 case 'array_merge_recursive':
359 $GLOBALS[$key] = array_merge_recursive( $GLOBALS[$key], $val );
360 break;
361 case 'array_replace_recursive':
362 $GLOBALS[$key] = array_replace_recursive( $GLOBALS[$key], $val );
363 break;
364 case 'array_plus_2d':
365 $GLOBALS[$key] = wfArrayPlus2d( $GLOBALS[$key], $val );
366 break;
367 case 'array_plus':
368 $GLOBALS[$key] += $val;
369 break;
370 case 'array_merge':
371 $GLOBALS[$key] = array_merge( $val, $GLOBALS[$key] );
372 break;
373 default:
374 throw new UnexpectedValueException( "Unknown merge strategy '$mergeStrategy'" );
375 }
376 }
377
378 if ( isset( $info['autoloaderNS'] ) ) {
379 AutoLoader::$psr4Namespaces += $info['autoloaderNS'];
380 }
381
382 foreach ( $info['defines'] as $name => $val ) {
383 define( $name, $val );
384 }
385 foreach ( $info['autoloaderPaths'] as $path ) {
386 if ( file_exists( $path ) ) {
387 require_once $path;
388 }
389 }
390
391 $this->loaded += $info['credits'];
392 if ( $info['attributes'] ) {
393 if ( !$this->attributes ) {
394 $this->attributes = $info['attributes'];
395 } else {
396 $this->attributes = array_merge_recursive( $this->attributes, $info['attributes'] );
397 }
398 }
399
400 foreach ( $info['callbacks'] as $name => $cb ) {
401 if ( !is_callable( $cb ) ) {
402 if ( is_array( $cb ) ) {
403 $cb = '[ ' . implode( ', ', $cb ) . ' ]';
404 }
405 throw new UnexpectedValueException( "callback '$cb' is not callable" );
406 }
407 $cb( $info['credits'][$name] );
408 }
409 }
410
411 /**
412 * Loads and processes the given JSON file without delay
413 *
414 * If some extensions are already queued, this will load
415 * those as well.
416 *
417 * @param string $path Absolute path to the JSON file
418 */
419 public function load( $path ) {
420 $this->loadFromQueue(); // First clear the queue
421 $this->queue( $path );
422 $this->loadFromQueue();
423 }
424
425 /**
426 * Whether a thing has been loaded
427 * @param string $name
428 * @param string $constraint The required version constraint for this dependency
429 * @throws LogicException if a specific contraint is asked for,
430 * but the extension isn't versioned
431 * @return bool
432 */
433 public function isLoaded( $name, $constraint = '*' ) {
434 $isLoaded = isset( $this->loaded[$name] );
435 if ( $constraint === '*' || !$isLoaded ) {
436 return $isLoaded;
437 }
438 // if a specific constraint is requested, but no version is set, throw an exception
439 if ( !isset( $this->loaded[$name]['version'] ) ) {
440 $msg = "{$name} does not expose its version, but an extension or a skin"
441 . " requires: {$constraint}.";
442 throw new LogicException( $msg );
443 }
444
445 return SemVer::satisfies( $this->loaded[$name]['version'], $constraint );
446 }
447
448 /**
449 * @param string $name
450 * @return array
451 */
452 public function getAttribute( $name ) {
453 return $this->testAttributes[$name] ??
454 $this->attributes[$name] ?? [];
455 }
456
457 /**
458 * Force override the value of an attribute during tests
459 *
460 * @param string $name Name of attribute to override
461 * @param array $value Value to set
462 * @return ScopedCallback to reset
463 * @since 1.33
464 */
465 public function setAttributeForTest( $name, array $value ) {
466 // @codeCoverageIgnoreStart
467 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
468 throw new RuntimeException( __METHOD__ . ' can only be used in tests' );
469 }
470 // @codeCoverageIgnoreEnd
471 if ( isset( $this->testAttributes[$name] ) ) {
472 throw new Exception( "The attribute '$name' has already been overridden" );
473 }
474 $this->testAttributes[$name] = $value;
475 return new ScopedCallback( function () use ( $name ) {
476 unset( $this->testAttributes[$name] );
477 } );
478 }
479
480 /**
481 * Get information about all things
482 *
483 * @return array
484 */
485 public function getAllThings() {
486 return $this->loaded;
487 }
488
489 /**
490 * Fully expand autoloader paths
491 *
492 * @param string $dir
493 * @param array $files
494 * @return array
495 */
496 protected function processAutoLoader( $dir, array $files ) {
497 // Make paths absolute, relative to the JSON file
498 foreach ( $files as &$file ) {
499 $file = "$dir/$file";
500 }
501 return $files;
502 }
503 }