3 use Composer\Semver\Semver;
7 use Wikimedia\ScopedCallback;
50 private const CACHE_VERSION = 8;
52 private const CACHE_EXPIRY = 60 * 60 * 24;
64 private const LAZY_LOADED_ATTRIBUTES = [
67 'SkinLessImportPaths',
95 private $finished =
false;
143 private static $instance;
148 private $cache =
null;
155 private static bool $accessDisabledForUnitTests =
false;
162 if ( self::$accessDisabledForUnitTests ) {
163 throw new RuntimeException(
'Access is disabled in unit tests' );
165 if ( self::$instance ===
null ) {
166 self::$instance =
new self();
169 return self::$instance;
176 if ( !defined(
'MW_PHPUNIT_TEST' ) ) {
177 throw new RuntimeException(
'Can only be called in tests' );
179 self::$accessDisabledForUnitTests =
true;
186 if ( !defined(
'MW_PHPUNIT_TEST' ) ) {
187 throw new RuntimeException(
'Can only be called in tests' );
189 self::$accessDisabledForUnitTests =
false;
201 $this->cache = $cache;
209 $this->checkDev = $check;
210 $this->invalidateProcessCache();
221 $this->loadTestClassesAndNamespaces = $load;
231 if ( $mtime ===
false ) {
233 $mtime = @filemtime(
$path );
235 if ( $mtime ===
false ) {
236 $err = error_get_last();
241 $this->queued[
$path] = $mtime;
242 $this->invalidateProcessCache();
246 if ( !$this->cache ) {
255 private function makeCacheKey(
BagOStuff $cache, $component, ...$extra ) {
258 "registration-$component",
259 $this->getVaryHash(),
269 private function getVaryHash() {
270 if ( $this->varyHash ===
null ) {
274 'registration' => self::CACHE_VERSION,
276 'abilities' => $this->getAbilities(),
277 'checkDev' => $this->checkDev,
278 'queue' => $this->queued,
280 $this->varyHash = md5( json_encode( $vary ) );
282 return $this->varyHash;
288 private function invalidateProcessCache() {
289 $this->varyHash =
null;
290 $this->lazyAttributes = [];
298 if ( !$this->queued ) {
302 if ( $this->finished ) {
304 "The following paths tried to load late: "
305 . implode(
', ', array_keys( $this->queued ) )
309 $cache = $this->getCache();
311 $key = $this->makeCacheKey( $cache,
'main' );
312 $data = $cache->
get( $key );
314 $data = $this->readFromQueue( $this->queued );
315 $this->saveToCache( $cache, $data );
317 $this->exportExtractedData( $data );
334 foreach ( self::LAZY_LOADED_ATTRIBUTES as $attrib ) {
335 if ( isset( $data[
'attributes'][$attrib] ) ) {
336 $lazy[$attrib] = $data[
'attributes'][$attrib];
337 unset( $data[
'attributes'][$attrib] );
340 $mainKey = $this->makeCacheKey( $cache,
'main' );
341 $cache->
set( $mainKey, $data, self::CACHE_EXPIRY );
342 foreach ( $lazy as $attrib => $value ) {
344 $this->makeCacheKey( $cache,
'lazy-attrib', $attrib ),
358 return $this->queued;
367 $this->invalidateProcessCache();
376 $this->finished =
true;
383 private function getAbilities() {
385 'shell' => !Shell::isDisabled(),
394 private function buildVersionChecker() {
403 PHP_MAJOR_VERSION .
'.' . PHP_MINOR_VERSION .
'.' . PHP_RELEASE_VERSION,
404 get_loaded_extensions(),
405 $this->getAbilities(),
422 $versionChecker = $this->buildVersionChecker();
423 $extDependencies = [];
425 foreach ( $queue as
$path => $mtime ) {
426 $json = file_get_contents(
$path );
427 if ( $json ===
false ) {
428 throw new Exception(
"Unable to read $path, does it exist?" );
430 $info = json_decode( $json,
true );
431 if ( !is_array( $info ) ) {
432 throw new Exception(
"$path is not a valid JSON file." );
435 $version = $info[
'manifest_version'];
436 if ( $version < self::OLDEST_MANIFEST_VERSION || $version > self::MANIFEST_VERSION ) {
437 throw new Exception(
"$path: unsupported manifest_version: {$version}" );
441 $requires = $processor->getRequirements( $info, $this->checkDev );
444 if ( is_array( $requires ) && $requires && isset( $info[
'name'] ) ) {
445 $extDependencies[$info[
'name']] = $requires;
449 $processor->extractInfo(
$path, $info, $version );
451 $data = $processor->getExtractedInfo( $this->loadTestClassesAndNamespaces );
452 $data[
'warnings'] = $warnings;
455 $incompatible = $versionChecker
456 ->setLoadedExtensionsAndSkins( $data[
'credits'] )
457 ->checkArray( $extDependencies );
459 if ( $incompatible ) {
467 foreach ( $info[
'globals'] as $key => $val ) {
470 if ( is_array( $val ) && isset( $val[self::MERGE_STRATEGY] ) ) {
471 $mergeStrategy = $val[self::MERGE_STRATEGY];
472 unset( $val[self::MERGE_STRATEGY] );
474 $mergeStrategy =
'array_merge';
477 if ( $mergeStrategy ===
'provide_default' ) {
478 if ( !array_key_exists( $key, $GLOBALS ) ) {
479 $GLOBALS[$key] = $val;
486 if ( !array_key_exists( $key, $GLOBALS ) || ( is_array( $GLOBALS[$key] ) && !$GLOBALS[$key] ) ) {
487 $GLOBALS[$key] = $val;
491 if ( !is_array( $GLOBALS[$key] ) || !is_array( $val ) ) {
496 switch ( $mergeStrategy ) {
497 case 'array_merge_recursive':
498 $GLOBALS[$key] = array_merge_recursive( $GLOBALS[$key], $val );
500 case 'array_replace_recursive':
501 $GLOBALS[$key] = array_replace_recursive( $val, $GLOBALS[$key] );
503 case 'array_plus_2d':
507 $GLOBALS[$key] += $val;
510 $GLOBALS[$key] = array_merge( $val, $GLOBALS[$key] );
513 throw new UnexpectedValueException(
"Unknown merge strategy '$mergeStrategy'" );
517 if ( isset( $info[
'autoloaderNS'] ) ) {
518 AutoLoader::registerNamespaces( $info[
'autoloaderNS'] );
521 if ( isset( $info[
'autoloaderClasses'] ) ) {
522 AutoLoader::registerClasses( $info[
'autoloaderClasses'] );
525 foreach ( $info[
'defines'] as $name => $val ) {
526 if ( !defined( $name ) ) {
527 define( $name, $val );
528 } elseif ( constant( $name ) !== $val ) {
529 throw new UnexpectedValueException(
530 "$name cannot be re-defined with $val it has already been set with " . constant( $name )
535 if ( isset( $info[
'autoloaderPaths'] ) ) {
536 AutoLoader::loadFiles( $info[
'autoloaderPaths'] );
539 $this->loaded += $info[
'credits'];
540 if ( $info[
'attributes'] ) {
541 if ( !$this->attributes ) {
542 $this->attributes = $info[
'attributes'];
544 $this->attributes = array_merge_recursive( $this->attributes, $info[
'attributes'] );
549 $settings = $this->getSettingsBuilder();
551 foreach ( $info[
'callbacks'] as $name => $cb ) {
552 if ( !is_callable( $cb ) ) {
553 if ( is_array( $cb ) ) {
554 $cb =
'[ ' . implode(
', ', $cb ) .
' ]';
556 throw new UnexpectedValueException(
"callback '$cb' is not callable" );
558 $cb( $info[
'credits'][$name], $settings );
570 public function isLoaded( $name, $constraint =
'*' ) {
571 $isLoaded = isset( $this->loaded[$name] );
572 if ( $constraint ===
'*' || !$isLoaded ) {
576 if ( !isset( $this->loaded[$name][
'version'] ) ) {
577 $msg =
"{$name} does not expose its version, but an extension or a skin"
578 .
" requires: {$constraint}.";
579 throw new LogicException( $msg );
582 return Semver::satisfies( $this->loaded[$name][
'version'], $constraint );
590 if ( isset( $this->testAttributes[$name] ) ) {
591 return $this->testAttributes[$name];
594 if ( in_array( $name, self::LAZY_LOADED_ATTRIBUTES,
true ) ) {
595 return $this->getLazyLoadedAttribute( $name );
598 return $this->attributes[$name] ?? [];
608 if ( isset( $this->testAttributes[$name] ) ) {
609 return $this->testAttributes[$name];
611 if ( isset( $this->lazyAttributes[$name] ) ) {
612 return $this->lazyAttributes[$name];
616 $cache = $this->getCache();
617 $key = $this->makeCacheKey( $cache,
'lazy-attrib', $name );
618 $data = $cache->
get( $key );
619 if ( $data !==
false ) {
620 $this->lazyAttributes[$name] = $data;
625 foreach ( $this->loaded as $info ) {
628 $paths[$info[
'path']] = 1;
631 $result = $this->readFromQueue( $paths );
632 $data = $result[
'attributes'][$name] ?? [];
633 $this->saveToCache( $cache, $result );
634 $this->lazyAttributes[$name] = $data;
649 if ( !defined(
'MW_PHPUNIT_TEST' ) ) {
650 throw new LogicException( __METHOD__ .
' can only be used in tests' );
653 if ( isset( $this->testAttributes[$name] ) ) {
654 throw new Exception(
"The attribute '$name' has already been overridden" );
656 $this->testAttributes[$name] = $value;
657 return new ScopedCallback(
function () use ( $name ) {
658 unset( $this->testAttributes[$name] );
668 return $this->loaded;
680 foreach ( $files as &
$file ) {
681 $file =
"$dir/$file";
691 $this->settingsBuilder = $settingsBuilder;
695 if ( $this->settingsBuilder === null ) {
696 $this->settingsBuilder = SettingsBuilder::getInstance();
698 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'))
Class representing a cache/ephemeral data store.
get( $key, $flags=0)
Get an item.
set( $key, $value, $exptime=0, $flags=0)
Set an item.
makeGlobalKey( $keygroup,... $components)
Make a cache key from the given components, in the "global" keyspace.
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.
static makeLocalServerCache()
Create a new BagOStuff instance for local-server caching.
Check whether extensions and their dependencies meet certain version requirements.
$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.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.