Merge "Allow partially blocked users to import images"
[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 * Whether to check dev-requires
91 *
92 * @var bool
93 */
94 protected $checkDev = false;
95
96 /**
97 * @var ExtensionRegistry
98 */
99 private static $instance;
100
101 /**
102 * @codeCoverageIgnore
103 * @return ExtensionRegistry
104 */
105 public static function getInstance() {
106 if ( self::$instance === null ) {
107 self::$instance = new self();
108 }
109
110 return self::$instance;
111 }
112
113 /**
114 * @since 1.34
115 * @param bool $check
116 */
117 public function setCheckDevRequires( $check ) {
118 $this->checkDev = $check;
119 }
120
121 /**
122 * @param string $path Absolute path to the JSON file
123 */
124 public function queue( $path ) {
125 global $wgExtensionInfoMTime;
126
127 $mtime = $wgExtensionInfoMTime;
128 if ( $mtime === false ) {
129 if ( file_exists( $path ) ) {
130 $mtime = filemtime( $path );
131 } else {
132 throw new Exception( "$path does not exist!" );
133 }
134 // @codeCoverageIgnoreStart
135 if ( $mtime === false ) {
136 $err = error_get_last();
137 throw new Exception( "Couldn't stat $path: {$err['message']}" );
138 // @codeCoverageIgnoreEnd
139 }
140 }
141 $this->queued[$path] = $mtime;
142 }
143
144 /**
145 * @throws MWException If the queue is already marked as finished (no further things should
146 * be loaded then).
147 */
148 public function loadFromQueue() {
149 global $wgVersion, $wgDevelopmentWarnings, $wgObjectCaches;
150 if ( !$this->queued ) {
151 return;
152 }
153
154 if ( $this->finished ) {
155 throw new MWException(
156 "The following paths tried to load late: "
157 . implode( ', ', array_keys( $this->queued ) )
158 );
159 }
160
161 // A few more things to vary the cache on
162 $versions = [
163 'registration' => self::CACHE_VERSION,
164 'mediawiki' => $wgVersion,
165 'abilities' => $this->getAbilities(),
166 'checkDev' => $this->checkDev,
167 ];
168
169 // We use a try/catch because we don't want to fail here
170 // if $wgObjectCaches is not configured properly for APC setup
171 try {
172 // Avoid MediaWikiServices to prevent instantiating it before extensions have loaded
173 $cacheId = ObjectCache::detectLocalServerCache();
174 $cache = ObjectCache::newFromParams( $wgObjectCaches[$cacheId] );
175 } catch ( InvalidArgumentException $e ) {
176 $cache = new EmptyBagOStuff();
177 }
178 // See if this queue is in APC
179 $key = $cache->makeKey(
180 'registration',
181 md5( json_encode( $this->queued + $versions ) )
182 );
183 $data = $cache->get( $key );
184 if ( $data ) {
185 $this->exportExtractedData( $data );
186 } else {
187 $data = $this->readFromQueue( $this->queued );
188 $this->exportExtractedData( $data );
189 // Do this late since we don't want to extract it since we already
190 // did that, but it should be cached
191 $data['globals']['wgAutoloadClasses'] += $data['autoload'];
192 unset( $data['autoload'] );
193 if ( !( $data['warnings'] && $wgDevelopmentWarnings ) ) {
194 // If there were no warnings that were shown, cache it
195 $cache->set( $key, $data, 60 * 60 * 24 );
196 }
197 }
198 $this->queued = [];
199 }
200
201 /**
202 * Get the current load queue. Not intended to be used
203 * outside of the installer.
204 *
205 * @return array
206 */
207 public function getQueue() {
208 return $this->queued;
209 }
210
211 /**
212 * Clear the current load queue. Not intended to be used
213 * outside of the installer.
214 */
215 public function clearQueue() {
216 $this->queued = [];
217 }
218
219 /**
220 * After this is called, no more extensions can be loaded
221 *
222 * @since 1.29
223 */
224 public function finish() {
225 $this->finished = true;
226 }
227
228 /**
229 * Get the list of abilities and their values
230 * @return bool[]
231 */
232 private function getAbilities() {
233 return [
234 'shell' => !Shell::isDisabled(),
235 ];
236 }
237
238 /**
239 * Queries information about the software environment and constructs an appropiate version checker
240 *
241 * @return VersionChecker
242 */
243 private function buildVersionChecker() {
244 global $wgVersion;
245 // array to optionally specify more verbose error messages for
246 // missing abilities
247 $abilityErrors = [
248 'shell' => ( new ShellDisabledError() )->getMessage(),
249 ];
250
251 return new VersionChecker(
252 $wgVersion,
253 PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION . '.' . PHP_RELEASE_VERSION,
254 get_loaded_extensions(),
255 $this->getAbilities(),
256 $abilityErrors
257 );
258 }
259
260 /**
261 * Process a queue of extensions and return their extracted data
262 *
263 * @param array $queue keys are filenames, values are ignored
264 * @return array extracted info
265 * @throws Exception
266 * @throws ExtensionDependencyError
267 */
268 public function readFromQueue( array $queue ) {
269 $autoloadClasses = [];
270 $autoloadNamespaces = [];
271 $autoloaderPaths = [];
272 $processor = new ExtensionProcessor();
273 $versionChecker = $this->buildVersionChecker();
274 $extDependencies = [];
275 $incompatible = [];
276 $warnings = false;
277 foreach ( $queue as $path => $mtime ) {
278 $json = file_get_contents( $path );
279 if ( $json === false ) {
280 throw new Exception( "Unable to read $path, does it exist?" );
281 }
282 $info = json_decode( $json, /* $assoc = */ true );
283 if ( !is_array( $info ) ) {
284 throw new Exception( "$path is not a valid JSON file." );
285 }
286
287 if ( !isset( $info['manifest_version'] ) ) {
288 wfDeprecated(
289 "{$info['name']}'s extension.json or skin.json does not have manifest_version",
290 '1.29'
291 );
292 $warnings = true;
293 // For backwards-compatability, assume a version of 1
294 $info['manifest_version'] = 1;
295 }
296 $version = $info['manifest_version'];
297 if ( $version < self::OLDEST_MANIFEST_VERSION || $version > self::MANIFEST_VERSION ) {
298 $incompatible[] = "$path: unsupported manifest_version: {$version}";
299 }
300
301 $dir = dirname( $path );
302 self::exportAutoloadClassesAndNamespaces(
303 $dir,
304 $info,
305 $autoloadClasses,
306 $autoloadNamespaces
307 );
308
309 // get all requirements/dependencies for this extension
310 $requires = $processor->getRequirements( $info, $this->checkDev );
311
312 // validate the information needed and add the requirements
313 if ( is_array( $requires ) && $requires && isset( $info['name'] ) ) {
314 $extDependencies[$info['name']] = $requires;
315 }
316
317 // Get extra paths for later inclusion
318 $autoloaderPaths = array_merge( $autoloaderPaths,
319 $processor->getExtraAutoloaderPaths( $dir, $info ) );
320 // Compatible, read and extract info
321 $processor->extractInfo( $path, $info, $version );
322 }
323 $data = $processor->getExtractedInfo();
324 $data['warnings'] = $warnings;
325
326 // check for incompatible extensions
327 $incompatible = array_merge(
328 $incompatible,
329 $versionChecker
330 ->setLoadedExtensionsAndSkins( $data['credits'] )
331 ->checkArray( $extDependencies )
332 );
333
334 if ( $incompatible ) {
335 throw new ExtensionDependencyError( $incompatible );
336 }
337
338 // Need to set this so we can += to it later
339 $data['globals']['wgAutoloadClasses'] = [];
340 $data['autoload'] = $autoloadClasses;
341 $data['autoloaderPaths'] = $autoloaderPaths;
342 $data['autoloaderNS'] = $autoloadNamespaces;
343 return $data;
344 }
345
346 /**
347 * Export autoload classes and namespaces for a given directory and parsed JSON info file.
348 *
349 * @param string $dir
350 * @param array $info
351 * @param array &$autoloadClasses
352 * @param array &$autoloadNamespaces
353 */
354 public static function exportAutoloadClassesAndNamespaces(
355 $dir, $info, &$autoloadClasses = [], &$autoloadNamespaces = []
356 ) {
357 if ( isset( $info['AutoloadClasses'] ) ) {
358 $autoload = self::processAutoLoader( $dir, $info['AutoloadClasses'] );
359 $GLOBALS['wgAutoloadClasses'] += $autoload;
360 $autoloadClasses += $autoload;
361 }
362 if ( isset( $info['AutoloadNamespaces'] ) ) {
363 $autoloadNamespaces += self::processAutoLoader( $dir, $info['AutoloadNamespaces'] );
364 AutoLoader::$psr4Namespaces += $autoloadNamespaces;
365 }
366 }
367
368 protected function exportExtractedData( array $info ) {
369 foreach ( $info['globals'] as $key => $val ) {
370 // If a merge strategy is set, read it and remove it from the value
371 // so it doesn't accidentally end up getting set.
372 if ( is_array( $val ) && isset( $val[self::MERGE_STRATEGY] ) ) {
373 $mergeStrategy = $val[self::MERGE_STRATEGY];
374 unset( $val[self::MERGE_STRATEGY] );
375 } else {
376 $mergeStrategy = 'array_merge';
377 }
378
379 // Optimistic: If the global is not set, or is an empty array, replace it entirely.
380 // Will be O(1) performance.
381 if ( !array_key_exists( $key, $GLOBALS ) || ( is_array( $GLOBALS[$key] ) && !$GLOBALS[$key] ) ) {
382 $GLOBALS[$key] = $val;
383 continue;
384 }
385
386 if ( !is_array( $GLOBALS[$key] ) || !is_array( $val ) ) {
387 // config setting that has already been overridden, don't set it
388 continue;
389 }
390
391 switch ( $mergeStrategy ) {
392 case 'array_merge_recursive':
393 $GLOBALS[$key] = array_merge_recursive( $GLOBALS[$key], $val );
394 break;
395 case 'array_replace_recursive':
396 $GLOBALS[$key] = array_replace_recursive( $GLOBALS[$key], $val );
397 break;
398 case 'array_plus_2d':
399 $GLOBALS[$key] = wfArrayPlus2d( $GLOBALS[$key], $val );
400 break;
401 case 'array_plus':
402 $GLOBALS[$key] += $val;
403 break;
404 case 'array_merge':
405 $GLOBALS[$key] = array_merge( $val, $GLOBALS[$key] );
406 break;
407 default:
408 throw new UnexpectedValueException( "Unknown merge strategy '$mergeStrategy'" );
409 }
410 }
411
412 if ( isset( $info['autoloaderNS'] ) ) {
413 AutoLoader::$psr4Namespaces += $info['autoloaderNS'];
414 }
415
416 foreach ( $info['defines'] as $name => $val ) {
417 define( $name, $val );
418 }
419 foreach ( $info['autoloaderPaths'] as $path ) {
420 if ( file_exists( $path ) ) {
421 require_once $path;
422 }
423 }
424
425 $this->loaded += $info['credits'];
426 if ( $info['attributes'] ) {
427 if ( !$this->attributes ) {
428 $this->attributes = $info['attributes'];
429 } else {
430 $this->attributes = array_merge_recursive( $this->attributes, $info['attributes'] );
431 }
432 }
433
434 foreach ( $info['callbacks'] as $name => $cb ) {
435 if ( !is_callable( $cb ) ) {
436 if ( is_array( $cb ) ) {
437 $cb = '[ ' . implode( ', ', $cb ) . ' ]';
438 }
439 throw new UnexpectedValueException( "callback '$cb' is not callable" );
440 }
441 $cb( $info['credits'][$name] );
442 }
443 }
444
445 /**
446 * Loads and processes the given JSON file without delay
447 *
448 * If some extensions are already queued, this will load
449 * those as well.
450 * TODO: Remove in MediaWiki 1.35
451 * @deprecated since 1.34, use ExtensionRegistry->queue() instead
452 * @param string $path Absolute path to the JSON file
453 */
454 public function load( $path ) {
455 wfDeprecated( __METHOD__, '1.34' );
456 $this->loadFromQueue(); // First clear the queue
457 $this->queue( $path );
458 $this->loadFromQueue();
459 }
460
461 /**
462 * Whether a thing has been loaded
463 * @param string $name
464 * @param string $constraint The required version constraint for this dependency
465 * @throws LogicException if a specific contraint is asked for,
466 * but the extension isn't versioned
467 * @return bool
468 */
469 public function isLoaded( $name, $constraint = '*' ) {
470 $isLoaded = isset( $this->loaded[$name] );
471 if ( $constraint === '*' || !$isLoaded ) {
472 return $isLoaded;
473 }
474 // if a specific constraint is requested, but no version is set, throw an exception
475 if ( !isset( $this->loaded[$name]['version'] ) ) {
476 $msg = "{$name} does not expose its version, but an extension or a skin"
477 . " requires: {$constraint}.";
478 throw new LogicException( $msg );
479 }
480
481 return SemVer::satisfies( $this->loaded[$name]['version'], $constraint );
482 }
483
484 /**
485 * @param string $name
486 * @return array
487 */
488 public function getAttribute( $name ) {
489 return $this->testAttributes[$name] ??
490 $this->attributes[$name] ?? [];
491 }
492
493 /**
494 * Force override the value of an attribute during tests
495 *
496 * @param string $name Name of attribute to override
497 * @param array $value Value to set
498 * @return ScopedCallback to reset
499 * @since 1.33
500 */
501 public function setAttributeForTest( $name, array $value ) {
502 // @codeCoverageIgnoreStart
503 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
504 throw new RuntimeException( __METHOD__ . ' can only be used in tests' );
505 }
506 // @codeCoverageIgnoreEnd
507 if ( isset( $this->testAttributes[$name] ) ) {
508 throw new Exception( "The attribute '$name' has already been overridden" );
509 }
510 $this->testAttributes[$name] = $value;
511 return new ScopedCallback( function () use ( $name ) {
512 unset( $this->testAttributes[$name] );
513 } );
514 }
515
516 /**
517 * Get information about all things
518 *
519 * @return array
520 */
521 public function getAllThings() {
522 return $this->loaded;
523 }
524
525 /**
526 * Fully expand autoloader paths
527 *
528 * @param string $dir
529 * @param array $files
530 * @return array
531 */
532 protected static function processAutoLoader( $dir, array $files ) {
533 // Make paths absolute, relative to the JSON file
534 foreach ( $files as &$file ) {
535 $file = "$dir/$file";
536 }
537 return $files;
538 }
539 }