MediaWiki  master
ExtensionRegistry.php
Go to the documentation of this file.
1 <?php
2 
3 use Composer\Semver\Semver;
6 use Wikimedia\ScopedCallback;
7 
18 
22  public const MEDIAWIKI_CORE = 'MediaWiki';
23 
28  public const MANIFEST_VERSION = 2;
29 
34  public const MANIFEST_VERSION_MW_VERSION = '>= 1.29.0';
35 
39  public const OLDEST_MANIFEST_VERSION = 1;
40 
44  private const CACHE_VERSION = 7;
45 
46  private const CACHE_EXPIRY = 60 * 60 * 24;
47 
53  public const MERGE_STRATEGY = '_merge_strategy';
54 
58  private const LAZY_LOADED_ATTRIBUTES = [
59  'TrackingCategories',
60  'QUnitTestModules',
61  'SkinLessImportPaths',
62  ];
63 
75  private $loaded = [];
76 
82  protected $queued = [];
83 
89  private $finished = false;
90 
97  protected $attributes = [];
98 
104  protected $testAttributes = [];
105 
111  protected $lazyAttributes = [];
112 
118  protected $checkDev = false;
119 
125  protected $loadTestClassesAndNamespaces = false;
126 
130  private static $instance;
131 
136  public static function getInstance() {
137  if ( self::$instance === null ) {
138  self::$instance = new self();
139  }
140 
141  return self::$instance;
142  }
143 
148  public function setCheckDevRequires( $check ) {
149  $this->checkDev = $check;
150  }
151 
159  public function setLoadTestClassesAndNamespaces( $load ) {
160  $this->loadTestClassesAndNamespaces = $load;
161  }
162 
166  public function queue( $path ) {
167  global $wgExtensionInfoMTime;
168 
169  $mtime = $wgExtensionInfoMTime;
170  if ( $mtime === false ) {
171  // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
172  $mtime = @filemtime( $path );
173  // @codeCoverageIgnoreStart
174  if ( $mtime === false ) {
175  $err = error_get_last();
176  throw new Exception( "Unable to open file $path: {$err['message']}" );
177  // @codeCoverageIgnoreEnd
178  }
179  }
180  $this->queued[$path] = $mtime;
181  }
182 
183  private function getCache() : BagOStuff {
184  // Can't call MediaWikiServices here, as we must not cause services
185  // to be instantiated before extensions have loaded.
187  }
188 
189  private function makeCacheKey( BagOStuff $cache, $component, ...$extra ) {
190  // Everything we vary the cache on
191  $vary = [
192  'registration' => self::CACHE_VERSION,
193  'mediawiki' => MW_VERSION,
194  'abilities' => $this->getAbilities(),
195  'checkDev' => $this->checkDev,
196  'queue' => $this->queued,
197  ];
198 
199  // Allow reusing cached ExtensionRegistry metadata between wikis (T274648)
200  return $cache->makeGlobalKey(
201  "registration-$component",
202  // We vary the cache on the current queue (what will be or already was loaded)
203  // plus various versions of stuff for VersionChecker
204  md5( json_encode( $vary ) ),
205  ...$extra
206  );
207  }
208 
213  public function loadFromQueue() {
214  if ( !$this->queued ) {
215  return;
216  }
217 
218  if ( $this->finished ) {
219  throw new MWException(
220  "The following paths tried to load late: "
221  . implode( ', ', array_keys( $this->queued ) )
222  );
223  }
224 
225  $cache = $this->getCache();
226  // See if this queue is in APC
227  $key = $this->makeCacheKey( $cache, 'main' );
228  $data = $cache->get( $key );
229  if ( !$data ) {
230  $data = $this->readFromQueue( $this->queued );
231  $this->saveToCache( $cache, $data );
232  }
233  $this->exportExtractedData( $data );
234  }
235 
242  protected function saveToCache( BagOStuff $cache, array $data ) {
243  global $wgDevelopmentWarnings;
244  if ( $data['warnings'] && $wgDevelopmentWarnings ) {
245  // If warnings were shown, don't cache it
246  return;
247  }
248  $lazy = [];
249  // Cache lazy-loaded attributes separately
250  foreach ( self::LAZY_LOADED_ATTRIBUTES as $attrib ) {
251  if ( isset( $data['attributes'][$attrib] ) ) {
252  $lazy[$attrib] = $data['attributes'][$attrib];
253  unset( $data['attributes'][$attrib] );
254  }
255  }
256  $mainKey = $this->makeCacheKey( $cache, 'main' );
257  $cache->set( $mainKey, $data, self::CACHE_EXPIRY );
258  foreach ( $lazy as $attrib => $value ) {
259  $cache->set(
260  $this->makeCacheKey( $cache, 'lazy-attrib', $attrib ),
261  $value,
262  self::CACHE_EXPIRY
263  );
264  }
265  }
266 
273  public function getQueue() {
274  return $this->queued;
275  }
276 
281  public function clearQueue() {
282  $this->queued = [];
283  }
284 
290  public function finish() {
291  $this->finished = true;
292  }
293 
298  private function getAbilities() {
299  return [
300  'shell' => !Shell::isDisabled(),
301  ];
302  }
303 
309  private function buildVersionChecker() {
310  // array to optionally specify more verbose error messages for
311  // missing abilities
312  $abilityErrors = [
313  'shell' => ( new ShellDisabledError() )->getMessage(),
314  ];
315 
316  return new VersionChecker(
317  MW_VERSION,
318  PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION . '.' . PHP_RELEASE_VERSION,
319  get_loaded_extensions(),
320  $this->getAbilities(),
321  $abilityErrors
322  );
323  }
324 
333  public function readFromQueue( array $queue ) {
334  $autoloadClasses = [];
335  $autoloadNamespaces = [];
336  $autoloaderPaths = [];
337  $processor = new ExtensionProcessor();
338  $versionChecker = $this->buildVersionChecker();
339  $extDependencies = [];
340  $warnings = false;
341  foreach ( $queue as $path => $mtime ) {
342  $json = file_get_contents( $path );
343  if ( $json === false ) {
344  throw new Exception( "Unable to read $path, does it exist?" );
345  }
346  $info = json_decode( $json, /* $assoc = */ true );
347  if ( !is_array( $info ) ) {
348  throw new Exception( "$path is not a valid JSON file." );
349  }
350 
351  if ( !isset( $info['manifest_version'] ) ) {
353  "{$info['name']}'s extension.json or skin.json does not have manifest_version, " .
354  'this is deprecated since MediaWiki 1.29',
355  '1.29', false, false
356  );
357  $warnings = true;
358  // For backwards-compatibility, assume a version of 1
359  $info['manifest_version'] = 1;
360  }
361  $version = $info['manifest_version'];
362  if ( $version < self::OLDEST_MANIFEST_VERSION || $version > self::MANIFEST_VERSION ) {
363  throw new Exception( "$path: unsupported manifest_version: {$version}" );
364  }
365 
366  $dir = dirname( $path );
368  $dir,
369  $info,
370  $autoloadClasses,
371  $autoloadNamespaces
372  );
373 
374  if ( $this->loadTestClassesAndNamespaces ) {
376  $dir,
377  $info,
378  $autoloadClasses,
379  $autoloadNamespaces
380  );
381  }
382 
383  // get all requirements/dependencies for this extension
384  $requires = $processor->getRequirements( $info, $this->checkDev );
385 
386  // validate the information needed and add the requirements
387  if ( is_array( $requires ) && $requires && isset( $info['name'] ) ) {
388  $extDependencies[$info['name']] = $requires;
389  }
390 
391  // Get extra paths for later inclusion
392  $autoloaderPaths = array_merge( $autoloaderPaths,
393  $processor->getExtraAutoloaderPaths( $dir, $info ) );
394  // Compatible, read and extract info
395  $processor->extractInfo( $path, $info, $version );
396  }
397  $data = $processor->getExtractedInfo();
398  $data['warnings'] = $warnings;
399 
400  // check for incompatible extensions
401  $incompatible = $versionChecker
402  ->setLoadedExtensionsAndSkins( $data['credits'] )
403  ->checkArray( $extDependencies );
404 
405  if ( $incompatible ) {
406  throw new ExtensionDependencyError( $incompatible );
407  }
408 
409  // FIXME: It was a design mistake to handle autoloading separately (T240535)
410  $data['globals']['wgAutoloadClasses'] = $autoloadClasses;
411  $data['autoloaderPaths'] = $autoloaderPaths;
412  $data['autoloaderNS'] = $autoloadNamespaces;
413  return $data;
414  }
415 
424  public static function exportAutoloadClassesAndNamespaces(
425  $dir, $info, &$autoloadClasses = [], &$autoloadNamespaces = []
426  ) {
427  if ( isset( $info['AutoloadClasses'] ) ) {
428  $autoload = self::processAutoLoader( $dir, $info['AutoloadClasses'] );
429  // @phan-suppress-next-line PhanUndeclaredVariableAssignOp
430  $GLOBALS['wgAutoloadClasses'] += $autoload;
431  $autoloadClasses += $autoload;
432  }
433  if ( isset( $info['AutoloadNamespaces'] ) ) {
434  $autoloadNamespaces += self::processAutoLoader( $dir, $info['AutoloadNamespaces'] );
435  AutoLoader::$psr4Namespaces += $autoloadNamespaces;
436  }
437  }
438 
449  $dir, $info, &$autoloadClasses = [], &$autoloadNamespaces = []
450  ) {
451  if ( isset( $info['TestAutoloadClasses'] ) ) {
452  $autoload = self::processAutoLoader( $dir, $info['TestAutoloadClasses'] );
453  $GLOBALS['wgAutoloadClasses'] += $autoload;
454  $autoloadClasses += $autoload;
455  }
456  if ( isset( $info['TestAutoloadNamespaces'] ) ) {
457  $autoloadNamespaces += self::processAutoLoader( $dir, $info['TestAutoloadNamespaces'] );
458  AutoLoader::$psr4Namespaces += $autoloadNamespaces;
459  }
460  }
461 
462  protected function exportExtractedData( array $info ) {
463  foreach ( $info['globals'] as $key => $val ) {
464  // If a merge strategy is set, read it and remove it from the value
465  // so it doesn't accidentally end up getting set.
466  if ( is_array( $val ) && isset( $val[self::MERGE_STRATEGY] ) ) {
467  $mergeStrategy = $val[self::MERGE_STRATEGY];
468  unset( $val[self::MERGE_STRATEGY] );
469  } else {
470  $mergeStrategy = 'array_merge';
471  }
472 
473  // Optimistic: If the global is not set, or is an empty array, replace it entirely.
474  // Will be O(1) performance.
475  if ( !array_key_exists( $key, $GLOBALS ) || ( is_array( $GLOBALS[$key] ) && !$GLOBALS[$key] ) ) {
476  $GLOBALS[$key] = $val;
477  continue;
478  }
479 
480  if ( !is_array( $GLOBALS[$key] ) || !is_array( $val ) ) {
481  // config setting that has already been overridden, don't set it
482  continue;
483  }
484 
485  switch ( $mergeStrategy ) {
486  case 'array_merge_recursive':
487  $GLOBALS[$key] = array_merge_recursive( $GLOBALS[$key], $val );
488  break;
489  case 'array_replace_recursive':
490  $GLOBALS[$key] = array_replace_recursive( $GLOBALS[$key], $val );
491  break;
492  case 'array_plus_2d':
493  $GLOBALS[$key] = wfArrayPlus2d( $GLOBALS[$key], $val );
494  break;
495  case 'array_plus':
496  $GLOBALS[$key] += $val;
497  break;
498  case 'array_merge':
499  $GLOBALS[$key] = array_merge( $val, $GLOBALS[$key] );
500  break;
501  default:
502  throw new UnexpectedValueException( "Unknown merge strategy '$mergeStrategy'" );
503  }
504  }
505 
506  if ( isset( $info['autoloaderNS'] ) ) {
507  AutoLoader::$psr4Namespaces += $info['autoloaderNS'];
508  }
509 
510  foreach ( $info['defines'] as $name => $val ) {
511  if ( !defined( $name ) ) {
512  define( $name, $val );
513  } elseif ( constant( $name ) !== $val ) {
514  throw new UnexpectedValueException(
515  "$name cannot be re-defined with $val it has already been set with " . constant( $name )
516  );
517  }
518  }
519 
520  foreach ( $info['autoloaderPaths'] as $path ) {
521  if ( file_exists( $path ) ) {
522  require_once $path;
523  }
524  }
525 
526  $this->loaded += $info['credits'];
527  if ( $info['attributes'] ) {
528  if ( !$this->attributes ) {
529  $this->attributes = $info['attributes'];
530  } else {
531  $this->attributes = array_merge_recursive( $this->attributes, $info['attributes'] );
532  }
533  }
534 
535  foreach ( $info['callbacks'] as $name => $cb ) {
536  if ( !is_callable( $cb ) ) {
537  if ( is_array( $cb ) ) {
538  $cb = '[ ' . implode( ', ', $cb ) . ' ]';
539  }
540  throw new UnexpectedValueException( "callback '$cb' is not callable" );
541  }
542  $cb( $info['credits'][$name] );
543  }
544  }
545 
554  public function isLoaded( $name, $constraint = '*' ) {
555  $isLoaded = isset( $this->loaded[$name] );
556  if ( $constraint === '*' || !$isLoaded ) {
557  return $isLoaded;
558  }
559  // if a specific constraint is requested, but no version is set, throw an exception
560  if ( !isset( $this->loaded[$name]['version'] ) ) {
561  $msg = "{$name} does not expose its version, but an extension or a skin"
562  . " requires: {$constraint}.";
563  throw new LogicException( $msg );
564  }
565 
566  return Semver::satisfies( $this->loaded[$name]['version'], $constraint );
567  }
568 
573  public function getAttribute( $name ) {
574  if ( isset( $this->testAttributes[$name] ) ) {
575  return $this->testAttributes[$name];
576  }
577 
578  if ( in_array( $name, self::LAZY_LOADED_ATTRIBUTES, true ) ) {
579  return $this->getLazyLoadedAttribute( $name );
580  }
581 
582  return $this->attributes[$name] ?? [];
583  }
584 
591  protected function getLazyLoadedAttribute( $name ) {
592  if ( isset( $this->testAttributes[$name] ) ) {
593  return $this->testAttributes[$name];
594  }
595 
596  // See if it's in the cache
597  $cache = $this->getCache();
598  $key = $this->makeCacheKey( $cache, 'lazy-attrib', $name );
599  $data = $cache->get( $key );
600  if ( $data !== false ) {
601  return $data;
602  }
603 
604  $paths = [];
605  foreach ( $this->loaded as $info ) {
606  // mtime (array value) doesn't matter here since
607  // we're skipping cache, so use a dummy time
608  $paths[$info['path']] = 1;
609  }
610 
611  $result = $this->readFromQueue( $paths );
612  $data = $result['attributes'][$name] ?? [];
613  $this->saveToCache( $cache, $result );
614 
615  return $data;
616  }
617 
626  public function setAttributeForTest( $name, array $value ) {
627  // @codeCoverageIgnoreStart
628  if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
629  throw new RuntimeException( __METHOD__ . ' can only be used in tests' );
630  }
631  // @codeCoverageIgnoreEnd
632  if ( isset( $this->testAttributes[$name] ) ) {
633  throw new Exception( "The attribute '$name' has already been overridden" );
634  }
635  $this->testAttributes[$name] = $value;
636  return new ScopedCallback( function () use ( $name ) {
637  unset( $this->testAttributes[$name] );
638  } );
639  }
640 
646  public function getAllThings() {
647  return $this->loaded;
648  }
649 
657  protected static function processAutoLoader( $dir, array $files ) {
658  // Make paths absolute, relative to the JSON file
659  foreach ( $files as &$file ) {
660  $file = "$dir/$file";
661  }
662  return $files;
663  }
664 }
ExtensionRegistry\isLoaded
isLoaded( $name, $constraint=' *')
Whether a thing has been loaded.
Definition: ExtensionRegistry.php:554
ExtensionRegistry\setLoadTestClassesAndNamespaces
setLoadTestClassesAndNamespaces( $load)
Controls if classes and namespaces defined under the keys TestAutoloadClasses and TestAutoloadNamespa...
Definition: ExtensionRegistry.php:159
MediaWiki\Shell\Shell
Executes shell commands.
Definition: Shell.php:45
ExtensionRegistry\getQueue
getQueue()
Get the current load queue.
Definition: ExtensionRegistry.php:273
ExtensionRegistry\$finished
bool $finished
Whether we are done loading things.
Definition: ExtensionRegistry.php:89
ExtensionRegistry\MANIFEST_VERSION
const MANIFEST_VERSION
Version of the highest supported manifest version Note: Update MANIFEST_VERSION_MW_VERSION when chang...
Definition: ExtensionRegistry.php:28
ExtensionRegistry\queue
queue( $path)
Definition: ExtensionRegistry.php:166
wfArrayPlus2d
wfArrayPlus2d(array $baseArray, array $newValues)
Merges two (possibly) 2 dimensional arrays into the target array ($baseArray).
Definition: GlobalFunctions.php:2878
ExtensionDependencyError
Copyright (C) 2018 Kunal Mehta legoktm@member.fsf.org
Definition: ExtensionDependencyError.php:25
ExtensionRegistry\exportAutoloadClassesAndNamespaces
static exportAutoloadClassesAndNamespaces( $dir, $info, &$autoloadClasses=[], &$autoloadNamespaces=[])
Export autoload classes and namespaces for a given directory and parsed JSON info file.
Definition: ExtensionRegistry.php:424
ObjectCache\makeLocalServerCache
static makeLocalServerCache()
Create a new BagOStuff instance for local-server caching.
Definition: ObjectCache.php:299
ExtensionRegistry\exportExtractedData
exportExtractedData(array $info)
Definition: ExtensionRegistry.php:462
ExtensionRegistry\makeCacheKey
makeCacheKey(BagOStuff $cache, $component,... $extra)
Definition: ExtensionRegistry.php:189
ExtensionRegistry
ExtensionRegistry class.
Definition: ExtensionRegistry.php:17
MW_VERSION
const MW_VERSION
The running version of MediaWiki.
Definition: Defines.php:36
ExtensionRegistry\$loadTestClassesAndNamespaces
bool $loadTestClassesAndNamespaces
Whether test classes and namespaces should be added to the auto loader.
Definition: ExtensionRegistry.php:125
ExtensionRegistry\getLazyLoadedAttribute
getLazyLoadedAttribute( $name)
Get an attribute value that isn't cached by reading each extension.json file again.
Definition: ExtensionRegistry.php:591
ExtensionRegistry\getAllThings
getAllThings()
Get credits information about all installed extensions and skins.
Definition: ExtensionRegistry.php:646
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
BagOStuff
Class representing a cache/ephemeral data store.
Definition: BagOStuff.php:86
ExtensionRegistry\setCheckDevRequires
setCheckDevRequires( $check)
Definition: ExtensionRegistry.php:148
ExtensionRegistry\exportTestAutoloadClassesAndNamespaces
static exportTestAutoloadClassesAndNamespaces( $dir, $info, &$autoloadClasses=[], &$autoloadNamespaces=[])
Export test autoload classes and namespaces for a given directory and parsed JSON info file.
Definition: ExtensionRegistry.php:448
ExtensionRegistry\getCache
getCache()
Definition: ExtensionRegistry.php:183
ExtensionRegistry\getAbilities
getAbilities()
Get the list of abilities and their values.
Definition: ExtensionRegistry.php:298
ExtensionRegistry\setAttributeForTest
setAttributeForTest( $name, array $value)
Force override the value of an attribute during tests.
Definition: ExtensionRegistry.php:626
ExtensionRegistry\clearQueue
clearQueue()
Clear the current load queue.
Definition: ExtensionRegistry.php:281
ExtensionRegistry\MERGE_STRATEGY
const MERGE_STRATEGY
Special key that defines the merge strategy.
Definition: ExtensionRegistry.php:53
ExtensionRegistry\getInstance
static getInstance()
Definition: ExtensionRegistry.php:136
ExtensionProcessor
Definition: ExtensionProcessor.php:3
ExtensionRegistry\CACHE_VERSION
const CACHE_VERSION
Bump whenever the registration cache needs resetting.
Definition: ExtensionRegistry.php:44
wfDeprecatedMsg
wfDeprecatedMsg( $msg, $version=false, $component=false, $callerOffset=2)
Log a deprecation warning with arbitrary message text.
Definition: GlobalFunctions.php:1066
MWException
MediaWiki exception.
Definition: MWException.php:29
ExtensionRegistry\$attributes
array $attributes
Items in the JSON file that aren't being set as globals.
Definition: ExtensionRegistry.php:97
ExtensionRegistry\processAutoLoader
static processAutoLoader( $dir, array $files)
Fully expand autoloader paths.
Definition: ExtensionRegistry.php:657
$queue
$queue
Definition: mergeMessageFileList.php:176
ExtensionRegistry\MANIFEST_VERSION_MW_VERSION
const MANIFEST_VERSION_MW_VERSION
MediaWiki version constraint representing what the current highest MANIFEST_VERSION is supported in.
Definition: ExtensionRegistry.php:34
AutoLoader\$psr4Namespaces
static string[] $psr4Namespaces
Definition: AutoLoader.php:39
ExtensionRegistry\finish
finish()
After this is called, no more extensions can be loaded.
Definition: ExtensionRegistry.php:290
ExtensionRegistry\loadFromQueue
loadFromQueue()
Definition: ExtensionRegistry.php:213
ExtensionRegistry\$queued
int[] $queued
List of paths that should be loaded.
Definition: ExtensionRegistry.php:82
ExtensionRegistry\OLDEST_MANIFEST_VERSION
const OLDEST_MANIFEST_VERSION
Version of the oldest supported manifest version.
Definition: ExtensionRegistry.php:39
ExtensionRegistry\MEDIAWIKI_CORE
const MEDIAWIKI_CORE
"requires" key that applies to MediaWiki core
Definition: ExtensionRegistry.php:22
ExtensionRegistry\readFromQueue
readFromQueue(array $queue)
Process a queue of extensions and return their extracted data.
Definition: ExtensionRegistry.php:333
ExtensionRegistry\LAZY_LOADED_ATTRIBUTES
const LAZY_LOADED_ATTRIBUTES
Attributes that should be lazy-loaded.
Definition: ExtensionRegistry.php:58
ExtensionRegistry\$testAttributes
array $testAttributes
Attributes for testing.
Definition: ExtensionRegistry.php:104
$wgDevelopmentWarnings
$wgDevelopmentWarnings
If set to true MediaWiki will throw notices for some possible error conditions and for deprecated fun...
Definition: DefaultSettings.php:6881
MediaWiki\ShellDisabledError
@newable
Definition: ShellDisabledError.php:29
ExtensionRegistry\$loaded
array[] $loaded
Array of loaded things, keyed by name, values are credits information.
Definition: ExtensionRegistry.php:75
VersionChecker
Provides functions to check a set of extensions with dependencies against a set of loaded extensions ...
Definition: VersionChecker.php:32
$cache
$cache
Definition: mcc.php:33
ExtensionRegistry\buildVersionChecker
buildVersionChecker()
Queries information about the software environment and constructs an appropiate version checker.
Definition: ExtensionRegistry.php:309
ExtensionRegistry\$lazyAttributes
array $lazyAttributes
Lazy-loaded attributes.
Definition: ExtensionRegistry.php:111
$path
$path
Definition: NoLocalSettings.php:25
$wgExtensionInfoMTime
int bool $wgExtensionInfoMTime
When loading extensions through the extension registration system, this can be used to invalidate the...
Definition: DefaultSettings.php:2959
ExtensionRegistry\CACHE_EXPIRY
const CACHE_EXPIRY
Definition: ExtensionRegistry.php:46
ExtensionRegistry\getAttribute
getAttribute( $name)
Definition: ExtensionRegistry.php:573
ExtensionRegistry\$checkDev
bool $checkDev
Whether to check dev-requires.
Definition: ExtensionRegistry.php:118
BagOStuff\makeGlobalKey
makeGlobalKey( $collection,... $components)
Make a cache key for the default keyspace and given components.
ExtensionRegistry\$instance
static ExtensionRegistry $instance
Definition: ExtensionRegistry.php:130
ExtensionRegistry\saveToCache
saveToCache(BagOStuff $cache, array $data)
Save data in the cache.
Definition: ExtensionRegistry.php:242