3 use Composer\Semver\Semver;
7 use Wikimedia\ScopedCallback;
43 private const CACHE_VERSION = 8;
45 private const CACHE_EXPIRY = 60 * 60 * 24;
57 private const LAZY_LOADED_ATTRIBUTES = [
60 'SkinLessImportPaths',
88 private $finished =
false;
136 private static $instance;
141 private $cache =
null;
153 if ( self::$instance ===
null ) {
154 self::$instance =
new self();
157 return self::$instance;
169 $this->cache = $cache;
177 $this->checkDev = $check;
178 $this->invalidateProcessCache();
189 $this->loadTestClassesAndNamespaces = $load;
199 if ( $mtime ===
false ) {
201 $mtime = @filemtime(
$path );
203 if ( $mtime ===
false ) {
204 $err = error_get_last();
209 $this->queued[
$path] = $mtime;
210 $this->invalidateProcessCache();
214 if ( !$this->cache ) {
223 private function makeCacheKey(
BagOStuff $cache, $component, ...$extra ) {
226 "registration-$component",
227 $this->getVaryHash(),
237 private function getVaryHash() {
238 if ( $this->varyHash ===
null ) {
242 'registration' => self::CACHE_VERSION,
244 'abilities' => $this->getAbilities(),
245 'checkDev' => $this->checkDev,
246 'queue' => $this->queued,
248 $this->varyHash = md5( json_encode( $vary ) );
250 return $this->varyHash;
256 private function invalidateProcessCache() {
257 $this->varyHash =
null;
258 $this->lazyAttributes = [];
266 if ( !$this->queued ) {
270 if ( $this->finished ) {
272 "The following paths tried to load late: "
273 . implode(
', ', array_keys( $this->queued ) )
277 $cache = $this->getCache();
279 $key = $this->makeCacheKey( $cache,
'main' );
280 $data = $cache->
get( $key );
282 $data = $this->readFromQueue( $this->queued );
283 $this->saveToCache( $cache, $data );
285 $this->exportExtractedData( $data );
302 foreach ( self::LAZY_LOADED_ATTRIBUTES as $attrib ) {
303 if ( isset( $data[
'attributes'][$attrib] ) ) {
304 $lazy[$attrib] = $data[
'attributes'][$attrib];
305 unset( $data[
'attributes'][$attrib] );
308 $mainKey = $this->makeCacheKey( $cache,
'main' );
309 $cache->
set( $mainKey, $data, self::CACHE_EXPIRY );
310 foreach ( $lazy as $attrib => $value ) {
312 $this->makeCacheKey( $cache,
'lazy-attrib', $attrib ),
326 return $this->queued;
335 $this->invalidateProcessCache();
344 $this->finished =
true;
351 private function getAbilities() {
353 'shell' => !Shell::isDisabled(),
362 private function buildVersionChecker() {
371 PHP_MAJOR_VERSION .
'.' . PHP_MINOR_VERSION .
'.' . PHP_RELEASE_VERSION,
372 get_loaded_extensions(),
373 $this->getAbilities(),
390 $versionChecker = $this->buildVersionChecker();
391 $extDependencies = [];
394 $json = file_get_contents(
$path );
395 if ( $json ===
false ) {
396 throw new Exception(
"Unable to read $path, does it exist?" );
398 $info = json_decode( $json,
true );
399 if ( !is_array( $info ) ) {
400 throw new Exception(
"$path is not a valid JSON file." );
403 if ( !isset( $info[
'manifest_version'] ) ) {
405 "{$info['name']}'s extension.json or skin.json does not have manifest_version, " .
406 'this is deprecated since MediaWiki 1.29',
411 $info[
'manifest_version'] = 1;
413 $version = $info[
'manifest_version'];
414 if ( $version < self::OLDEST_MANIFEST_VERSION || $version > self::MANIFEST_VERSION ) {
415 throw new Exception(
"$path: unsupported manifest_version: {$version}" );
419 $requires = $processor->getRequirements( $info, $this->checkDev );
422 if ( is_array( $requires ) && $requires && isset( $info[
'name'] ) ) {
423 $extDependencies[$info[
'name']] = $requires;
427 $processor->extractInfo(
$path, $info, $version );
429 $data = $processor->getExtractedInfo( $this->loadTestClassesAndNamespaces );
430 $data[
'warnings'] = $warnings;
433 $incompatible = $versionChecker
434 ->setLoadedExtensionsAndSkins( $data[
'credits'] )
435 ->checkArray( $extDependencies );
437 if ( $incompatible ) {
445 foreach ( $info[
'globals'] as $key => $val ) {
448 if ( is_array( $val ) && isset( $val[self::MERGE_STRATEGY] ) ) {
449 $mergeStrategy = $val[self::MERGE_STRATEGY];
450 unset( $val[self::MERGE_STRATEGY] );
452 $mergeStrategy =
'array_merge';
455 if ( $mergeStrategy ===
'provide_default' ) {
456 if ( !array_key_exists( $key, $GLOBALS ) ) {
457 $GLOBALS[$key] = $val;
464 if ( !array_key_exists( $key, $GLOBALS ) || ( is_array( $GLOBALS[$key] ) && !$GLOBALS[$key] ) ) {
465 $GLOBALS[$key] = $val;
469 if ( !is_array( $GLOBALS[$key] ) || !is_array( $val ) ) {
474 switch ( $mergeStrategy ) {
475 case 'array_merge_recursive':
476 $GLOBALS[$key] = array_merge_recursive( $GLOBALS[$key], $val );
478 case 'array_replace_recursive':
479 $GLOBALS[$key] = array_replace_recursive( $val, $GLOBALS[$key] );
481 case 'array_plus_2d':
485 $GLOBALS[$key] += $val;
488 $GLOBALS[$key] = array_merge( $val, $GLOBALS[$key] );
491 throw new UnexpectedValueException(
"Unknown merge strategy '$mergeStrategy'" );
495 if ( isset( $info[
'autoloaderNS'] ) ) {
496 AutoLoader::registerNamespaces( $info[
'autoloaderNS'] );
499 if ( isset( $info[
'autoloaderClasses'] ) ) {
500 AutoLoader::registerClasses( $info[
'autoloaderClasses'] );
503 foreach ( $info[
'defines'] as $name => $val ) {
504 if ( !defined( $name ) ) {
505 define( $name, $val );
506 } elseif ( constant( $name ) !== $val ) {
507 throw new UnexpectedValueException(
508 "$name cannot be re-defined with $val it has already been set with " . constant( $name )
513 if ( isset( $info[
'autoloaderPaths'] ) ) {
514 AutoLoader::loadFiles( $info[
'autoloaderPaths'] );
517 $this->loaded += $info[
'credits'];
518 if ( $info[
'attributes'] ) {
519 if ( !$this->attributes ) {
520 $this->attributes = $info[
'attributes'];
522 $this->attributes = array_merge_recursive( $this->attributes, $info[
'attributes'] );
527 $settings = $this->getSettingsBuilder();
529 foreach ( $info[
'callbacks'] as $name => $cb ) {
530 if ( !is_callable( $cb ) ) {
531 if ( is_array( $cb ) ) {
532 $cb =
'[ ' . implode(
', ', $cb ) .
' ]';
534 throw new UnexpectedValueException(
"callback '$cb' is not callable" );
536 $cb( $info[
'credits'][$name], $settings );
548 public function isLoaded( $name, $constraint =
'*' ) {
549 $isLoaded = isset( $this->loaded[$name] );
550 if ( $constraint ===
'*' || !$isLoaded ) {
554 if ( !isset( $this->loaded[$name][
'version'] ) ) {
555 $msg =
"{$name} does not expose its version, but an extension or a skin"
556 .
" requires: {$constraint}.";
557 throw new LogicException( $msg );
560 return Semver::satisfies( $this->loaded[$name][
'version'], $constraint );
568 if ( isset( $this->testAttributes[$name] ) ) {
569 return $this->testAttributes[$name];
572 if ( in_array( $name, self::LAZY_LOADED_ATTRIBUTES,
true ) ) {
573 return $this->getLazyLoadedAttribute( $name );
576 return $this->attributes[$name] ?? [];
586 if ( isset( $this->testAttributes[$name] ) ) {
587 return $this->testAttributes[$name];
589 if ( isset( $this->lazyAttributes[$name] ) ) {
590 return $this->lazyAttributes[$name];
594 $cache = $this->getCache();
595 $key = $this->makeCacheKey( $cache,
'lazy-attrib', $name );
596 $data = $cache->
get( $key );
597 if ( $data !==
false ) {
598 $this->lazyAttributes[$name] = $data;
603 foreach ( $this->loaded as $info ) {
606 $paths[$info[
'path']] = 1;
609 $result = $this->readFromQueue( $paths );
610 $data = $result[
'attributes'][$name] ?? [];
611 $this->saveToCache( $cache, $result );
612 $this->lazyAttributes[$name] = $data;
627 if ( !defined(
'MW_PHPUNIT_TEST' ) ) {
628 throw new RuntimeException( __METHOD__ .
' can only be used in tests' );
631 if ( isset( $this->testAttributes[$name] ) ) {
632 throw new Exception(
"The attribute '$name' has already been overridden" );
634 $this->testAttributes[$name] = $value;
635 return new ScopedCallback(
function () use ( $name ) {
636 unset( $this->testAttributes[$name] );
646 return $this->loaded;
658 foreach ( $files as &
$file ) {
659 $file =
"$dir/$file";
669 $this->settingsBuilder = $settingsBuilder;
673 if ( $this->settingsBuilder === null ) {
674 $this->settingsBuilder = SettingsBuilder::getInstance();
676 return $this->settingsBuilder;
const MW_VERSION
The running version of MediaWiki.
wfDeprecatedMsg( $msg, $version=false, $component=false, $callerOffset=2)
Log a deprecation warning with arbitrary message text.
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.
makeGlobalKey( $collection,... $components)
Make a cache key for the default keyspace and given components.
set( $key, $value, $exptime=0, $flags=0)
Set an item.
Copyright (C) 2018 Kunal Mehta legoktm@debian.org
Utility class for loading extension manifests and aggregating their contents.
The Registry loads JSON files, and uses a Processor to extract information from them.
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.
static makeLocalServerCache()
Create a new BagOStuff instance for local-server caching.
Provides functions to check a set of extensions with dependencies against a set of loaded extensions ...
$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.