3use Composer\Semver\Semver;
9use Wikimedia\ScopedCallback;
52 private const CACHE_VERSION = 8;
54 private const CACHE_EXPIRY = 60 * 60 * 24;
66 private const LAZY_LOADED_ATTRIBUTES = [
69 'SkinLessImportPaths',
97 private $finished =
false;
145 private static $instance;
150 private $cache =
null;
157 private static bool $accessDisabledForUnitTests =
false;
164 if ( self::$accessDisabledForUnitTests ) {
165 throw new RuntimeException(
'Access is disabled in unit tests' );
167 if ( self::$instance ===
null ) {
168 self::$instance =
new self();
171 return self::$instance;
178 if ( !defined(
'MW_PHPUNIT_TEST' ) ) {
179 throw new RuntimeException(
'Can only be called in tests' );
181 self::$accessDisabledForUnitTests =
true;
188 if ( !defined(
'MW_PHPUNIT_TEST' ) ) {
189 throw new RuntimeException(
'Can only be called in tests' );
191 self::$accessDisabledForUnitTests =
false;
203 $this->cache = $cache;
211 $this->checkDev = $check;
212 $this->invalidateProcessCache();
223 $this->loadTestClassesAndNamespaces = $load;
233 if ( $mtime ===
false ) {
235 $mtime = @filemtime(
$path );
237 if ( $mtime ===
false ) {
238 $err = error_get_last();
243 $this->queued[
$path] = $mtime;
244 $this->invalidateProcessCache();
248 if ( !$this->cache ) {
256 :
WikiMap::getCurrentWikiDbDomain()->getId();
257 return ObjectCacheFactory::makeLocalServerCache( $keyspace );
263 private function makeCacheKey(
BagOStuff $cache, $component, ...$extra ) {
266 "registration-$component",
267 $this->getVaryHash(),
277 private function getVaryHash() {
278 if ( $this->varyHash ===
null ) {
282 'registration' => self::CACHE_VERSION,
284 'abilities' => $this->getAbilities(),
285 'checkDev' => $this->checkDev,
286 'queue' => $this->queued,
288 $this->varyHash = md5( json_encode( $vary ) );
290 return $this->varyHash;
296 private function invalidateProcessCache() {
297 $this->varyHash =
null;
298 $this->lazyAttributes = [];
302 if ( !$this->queued ) {
306 if ( $this->finished ) {
307 throw new LogicException(
308 "The following paths tried to load late: "
309 . implode(
', ', array_keys( $this->queued ) )
313 $cache = $this->getCache();
315 $key = $this->makeCacheKey( $cache,
'main' );
316 $data = $cache->
get( $key );
318 $data = $this->readFromQueue( $this->queued );
319 $this->saveToCache( $cache, $data );
321 $this->exportExtractedData( $data );
338 foreach ( self::LAZY_LOADED_ATTRIBUTES as $attrib ) {
339 if ( isset( $data[
'attributes'][$attrib] ) ) {
340 $lazy[$attrib] = $data[
'attributes'][$attrib];
341 unset( $data[
'attributes'][$attrib] );
344 $mainKey = $this->makeCacheKey( $cache,
'main' );
345 $cache->
set( $mainKey, $data, self::CACHE_EXPIRY );
346 foreach ( $lazy as $attrib => $value ) {
348 $this->makeCacheKey( $cache,
'lazy-attrib', $attrib ),
362 return $this->queued;
371 $this->invalidateProcessCache();
380 $this->finished =
true;
387 private function getAbilities() {
389 'shell' => !Shell::isDisabled(),
398 private function buildVersionChecker() {
407 PHP_MAJOR_VERSION .
'.' . PHP_MINOR_VERSION .
'.' . PHP_RELEASE_VERSION,
408 get_loaded_extensions(),
409 $this->getAbilities(),
426 $versionChecker = $this->buildVersionChecker();
427 $extDependencies = [];
429 foreach ( $queue as
$path => $mtime ) {
430 $json = file_get_contents(
$path );
431 if ( $json ===
false ) {
432 throw new InvalidArgumentException(
"Unable to read $path, does it exist?" );
434 $info = json_decode( $json,
true );
435 if ( !is_array( $info ) ) {
436 throw new InvalidArgumentException(
"$path is not a valid JSON file." );
439 $version = $info[
'manifest_version'];
440 if ( $version < self::OLDEST_MANIFEST_VERSION || $version > self::MANIFEST_VERSION ) {
441 throw new InvalidArgumentException(
"$path: unsupported manifest_version: {$version}" );
445 $requires = $processor->getRequirements( $info, $this->checkDev );
448 if ( is_array( $requires ) && $requires && isset( $info[
'name'] ) ) {
449 $extDependencies[$info[
'name']] = $requires;
453 $processor->extractInfo(
$path, $info, $version );
455 $data = $processor->getExtractedInfo( $this->loadTestClassesAndNamespaces );
456 $data[
'warnings'] = $warnings;
459 $incompatible = $versionChecker
460 ->setLoadedExtensionsAndSkins( $data[
'credits'] )
461 ->checkArray( $extDependencies );
463 if ( $incompatible ) {
471 if ( $info[
'globals'] ) {
475 $knownGlobals = array_fill_keys( array_keys( $GLOBALS ),
true );
477 foreach ( $info[
'globals'] as $key => $val ) {
480 if ( is_array( $val ) && isset( $val[self::MERGE_STRATEGY] ) ) {
481 $mergeStrategy = $val[self::MERGE_STRATEGY];
482 unset( $val[self::MERGE_STRATEGY] );
484 $mergeStrategy =
'array_merge';
487 if ( $mergeStrategy ===
'provide_default' ) {
488 if ( !isset( $knownGlobals[$key] ) ) {
489 $GLOBALS[$key] = $val;
490 $knownGlobals[$key] =
true;
496 if ( !isset( $knownGlobals[$key] ) ) {
497 $GLOBALS[$key] = $val;
498 $knownGlobals[$key] =
true;
500 } elseif ( !is_array( $val ) || !is_array( $GLOBALS[$key] ) ) {
504 } elseif ( !$GLOBALS[$key] ) {
506 $GLOBALS[$key] = $val;
510 switch ( $mergeStrategy ) {
511 case 'array_merge_recursive':
512 $GLOBALS[$key] = array_merge_recursive( $GLOBALS[$key], $val );
514 case 'array_replace_recursive':
515 $GLOBALS[$key] = array_replace_recursive( $val, $GLOBALS[$key] );
517 case 'array_plus_2d':
521 $GLOBALS[$key] += $val;
524 $GLOBALS[$key] = array_merge( $val, $GLOBALS[$key] );
527 throw new UnexpectedValueException(
"Unknown merge strategy '$mergeStrategy'" );
532 if ( isset( $info[
'autoloaderNS'] ) ) {
533 AutoLoader::registerNamespaces( $info[
'autoloaderNS'] );
536 if ( isset( $info[
'autoloaderClasses'] ) ) {
537 AutoLoader::registerClasses( $info[
'autoloaderClasses'] );
540 foreach ( $info[
'defines'] as $name => $val ) {
541 if ( !defined( $name ) ) {
542 define( $name, $val );
543 } elseif ( constant( $name ) !== $val ) {
544 throw new UnexpectedValueException(
545 "$name cannot be re-defined with $val it has already been set with " . constant( $name )
550 if ( isset( $info[
'autoloaderPaths'] ) ) {
551 AutoLoader::loadFiles( $info[
'autoloaderPaths'] );
554 $this->loaded += $info[
'credits'];
555 if ( $info[
'attributes'] ) {
556 if ( !$this->attributes ) {
557 $this->attributes = $info[
'attributes'];
559 $this->attributes = array_merge_recursive( $this->attributes, $info[
'attributes'] );
564 $settings = $this->getSettingsBuilder();
566 foreach ( $info[
'callbacks'] as $name => $cb ) {
567 if ( !is_callable( $cb ) ) {
568 if ( is_array( $cb ) ) {
569 $cb =
'[ ' . implode(
', ', $cb ) .
' ]';
571 throw new UnexpectedValueException(
"callback '$cb' is not callable" );
573 $cb( $info[
'credits'][$name], $settings );
585 public function isLoaded( $name, $constraint =
'*' ) {
586 $isLoaded = isset( $this->loaded[$name] );
587 if ( $constraint ===
'*' || !$isLoaded ) {
591 if ( !isset( $this->loaded[$name][
'version'] ) ) {
592 $msg =
"{$name} does not expose its version, but an extension or a skin"
593 .
" requires: {$constraint}.";
594 throw new LogicException( $msg );
597 return Semver::satisfies( $this->loaded[$name][
'version'], $constraint );
605 if ( isset( $this->testAttributes[$name] ) ) {
606 return $this->testAttributes[$name];
609 if ( in_array( $name, self::LAZY_LOADED_ATTRIBUTES,
true ) ) {
610 return $this->getLazyLoadedAttribute( $name );
613 return $this->attributes[$name] ?? [];
623 if ( isset( $this->testAttributes[$name] ) ) {
624 return $this->testAttributes[$name];
626 if ( isset( $this->lazyAttributes[$name] ) ) {
627 return $this->lazyAttributes[$name];
631 $cache = $this->getCache();
632 $key = $this->makeCacheKey( $cache,
'lazy-attrib', $name );
633 $data = $cache->
get( $key );
634 if ( $data !==
false ) {
635 $this->lazyAttributes[$name] = $data;
640 foreach ( $this->loaded as $info ) {
643 $paths[$info[
'path']] = 1;
646 $result = $this->readFromQueue( $paths );
647 $data = $result[
'attributes'][$name] ?? [];
648 $this->saveToCache( $cache, $result );
649 $this->lazyAttributes[$name] = $data;
664 if ( !defined(
'MW_PHPUNIT_TEST' ) ) {
665 throw new LogicException( __METHOD__ .
' can only be used in tests' );
668 if ( isset( $this->testAttributes[$name] ) ) {
669 throw new InvalidArgumentException(
"The attribute '$name' has already been overridden" );
671 $this->testAttributes[$name] = $value;
672 return new ScopedCallback(
function () use ( $name ) {
673 unset( $this->testAttributes[$name] );
683 return $this->loaded;
695 foreach ( $files as &$file ) {
696 $file =
"$dir/$file";
706 $this->settingsBuilder = $settingsBuilder;
710 if ( $this->settingsBuilder === null ) {
711 $this->settingsBuilder = SettingsBuilder::getInstance();
713 return $this->settingsBuilder;
const MW_VERSION
The running version of MediaWiki.
wfArrayPlus2d(array $baseArray, array $newValues)
Merges two (possibly) 2 dimensional arrays into the target array ($baseArray).
if(!defined('MW_SETUP_CALLBACK'))
Load extension manifests and then aggregate their contents.
Load JSON files, and uses a Processor to extract information.
setLoadTestClassesAndNamespaces( $load)
Controls if classes and namespaces defined under the keys TestAutoloadClasses and TestAutoloadNamespa...
static processAutoLoader( $dir, array $files)
Fully expand autoloader paths.
isLoaded( $name, $constraint=' *')
Whether a thing has been loaded.
const MERGE_STRATEGY
Special key that defines the merge strategy.
getQueue()
Get the current load queue.
getLazyLoadedAttribute( $name)
Get an attribute value that isn't cached by reading each extension.json file again.
setSettingsBuilder(SettingsBuilder $settingsBuilder)
saveToCache(BagOStuff $cache, array $data)
Save data in the cache.
const MANIFEST_VERSION
Version of the highest supported manifest version Note: Update MANIFEST_VERSION_MW_VERSION when chang...
setAttributeForTest( $name, array $value)
Force override the value of an attribute during tests.
const OLDEST_MANIFEST_VERSION
Version of the oldest supported manifest version.
array $testAttributes
Attributes for testing.
clearQueue()
Clear the current load queue.
array $lazyAttributes
Lazy-loaded attributes.
setCache(BagOStuff $cache)
Set the cache to use for extension info.
const MANIFEST_VERSION_MW_VERSION
MediaWiki version constraint representing what the current highest MANIFEST_VERSION is supported in.
int[] $queued
List of paths that should be loaded.
const MEDIAWIKI_CORE
"requires" key that applies to MediaWiki core
bool $loadTestClassesAndNamespaces
Whether test classes and namespaces should be added to the auto loader.
readFromQueue(array $queue)
Process a queue of extensions and return their extracted data.
exportExtractedData(array $info)
setCheckDevRequires( $check)
getAllThings()
Get credits information about all installed extensions and skins.
finish()
After this is called, no more extensions can be loaded.
array $attributes
Items in the JSON file that aren't being set as globals.
bool $checkDev
Whether to check dev-requires.
Thrown when ExtensionRegistry cannot open the extension.json or skin.json file.
Check whether extensions and their dependencies meet certain version requirements.
$wgCachePrefix
Config variable stub for the CachePrefix setting, for use by phpdoc and IDEs.
$wgExtensionInfoMTime
Config variable stub for the ExtensionInfoMTime setting, for use by phpdoc and IDEs.
$wgDevelopmentWarnings
Config variable stub for the DevelopmentWarnings setting, for use by phpdoc and IDEs.