MediaWiki  master
ExtensionRegistry.php
Go to the documentation of this file.
1 <?php
2 
7 
18 
22  const MEDIAWIKI_CORE = 'MediaWiki';
23 
28  const MANIFEST_VERSION = 2;
29 
34  const MANIFEST_VERSION_MW_VERSION = '>= 1.29.0';
35 
40 
44  const CACHE_VERSION = 7;
45 
51  const MERGE_STRATEGY = '_merge_strategy';
52 
58  private $loaded = [];
59 
65  protected $queued = [];
66 
72  private $finished = false;
73 
80  protected $attributes = [];
81 
87  protected $testAttributes = [];
88 
94  protected $checkDev = false;
95 
99  private static $instance;
100 
105  public static function getInstance() {
106  if ( self::$instance === null ) {
107  self::$instance = new self();
108  }
109 
110  return self::$instance;
111  }
112 
117  public function setCheckDevRequires( $check ) {
118  $this->checkDev = $check;
119  }
120 
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 
148  public function loadFromQueue() {
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
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 
207  public function getQueue() {
208  return $this->queued;
209  }
210 
215  public function clearQueue() {
216  $this->queued = [];
217  }
218 
224  public function finish() {
225  $this->finished = true;
226  }
227 
232  private function getAbilities() {
233  return [
234  'shell' => !Shell::isDisabled(),
235  ];
236  }
237 
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 
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  if ( isset( $info['AutoloadClasses'] ) ) {
310  $autoload = $this->processAutoLoader( $dir, $info['AutoloadClasses'] );
311  $GLOBALS['wgAutoloadClasses'] += $autoload;
312  $autoloadClasses += $autoload;
313  }
314  if ( isset( $info['AutoloadNamespaces'] ) ) {
315  $autoloadNamespaces += $this->processAutoLoader( $dir, $info['AutoloadNamespaces'] );
316  AutoLoader::$psr4Namespaces += $autoloadNamespaces;
317  }
318 
319  // get all requirements/dependencies for this extension
320  $requires = $processor->getRequirements( $info, $this->checkDev );
321 
322  // validate the information needed and add the requirements
323  if ( is_array( $requires ) && $requires && isset( $info['name'] ) ) {
324  $extDependencies[$info['name']] = $requires;
325  }
326 
327  // Get extra paths for later inclusion
328  $autoloaderPaths = array_merge( $autoloaderPaths,
329  $processor->getExtraAutoloaderPaths( $dir, $info ) );
330  // Compatible, read and extract info
331  $processor->extractInfo( $path, $info, $version );
332  }
333  $data = $processor->getExtractedInfo();
334  $data['warnings'] = $warnings;
335 
336  // check for incompatible extensions
337  $incompatible = array_merge(
338  $incompatible,
339  $versionChecker
340  ->setLoadedExtensionsAndSkins( $data['credits'] )
341  ->checkArray( $extDependencies )
342  );
343 
344  if ( $incompatible ) {
345  throw new ExtensionDependencyError( $incompatible );
346  }
347 
348  // Need to set this so we can += to it later
349  $data['globals']['wgAutoloadClasses'] = [];
350  $data['autoload'] = $autoloadClasses;
351  $data['autoloaderPaths'] = $autoloaderPaths;
352  $data['autoloaderNS'] = $autoloadNamespaces;
353  return $data;
354  }
355 
364  public static function exportAutoloadClassesAndNamespaces(
365  $dir, $info, &$autoloadClasses = [], &$autoloadNamespaces = []
366  ) {
367  if ( isset( $info['AutoloadClasses'] ) ) {
368  $autoload = self::processAutoLoader( $dir, $info['AutoloadClasses'] );
369  $GLOBALS['wgAutoloadClasses'] += $autoload;
370  $autoloadClasses += $autoload;
371  }
372  if ( isset( $info['AutoloadNamespaces'] ) ) {
373  $autoloadNamespaces += self::processAutoLoader( $dir, $info['AutoloadNamespaces'] );
374  AutoLoader::$psr4Namespaces += $autoloadNamespaces;
375  }
376  }
377 
378  protected function exportExtractedData( array $info ) {
379  foreach ( $info['globals'] as $key => $val ) {
380  // If a merge strategy is set, read it and remove it from the value
381  // so it doesn't accidentally end up getting set.
382  if ( is_array( $val ) && isset( $val[self::MERGE_STRATEGY] ) ) {
383  $mergeStrategy = $val[self::MERGE_STRATEGY];
384  unset( $val[self::MERGE_STRATEGY] );
385  } else {
386  $mergeStrategy = 'array_merge';
387  }
388 
389  // Optimistic: If the global is not set, or is an empty array, replace it entirely.
390  // Will be O(1) performance.
391  if ( !array_key_exists( $key, $GLOBALS ) || ( is_array( $GLOBALS[$key] ) && !$GLOBALS[$key] ) ) {
392  $GLOBALS[$key] = $val;
393  continue;
394  }
395 
396  if ( !is_array( $GLOBALS[$key] ) || !is_array( $val ) ) {
397  // config setting that has already been overridden, don't set it
398  continue;
399  }
400 
401  switch ( $mergeStrategy ) {
402  case 'array_merge_recursive':
403  $GLOBALS[$key] = array_merge_recursive( $GLOBALS[$key], $val );
404  break;
405  case 'array_replace_recursive':
406  $GLOBALS[$key] = array_replace_recursive( $GLOBALS[$key], $val );
407  break;
408  case 'array_plus_2d':
409  $GLOBALS[$key] = wfArrayPlus2d( $GLOBALS[$key], $val );
410  break;
411  case 'array_plus':
412  $GLOBALS[$key] += $val;
413  break;
414  case 'array_merge':
415  $GLOBALS[$key] = array_merge( $val, $GLOBALS[$key] );
416  break;
417  default:
418  throw new UnexpectedValueException( "Unknown merge strategy '$mergeStrategy'" );
419  }
420  }
421 
422  if ( isset( $info['autoloaderNS'] ) ) {
423  AutoLoader::$psr4Namespaces += $info['autoloaderNS'];
424  }
425 
426  foreach ( $info['defines'] as $name => $val ) {
427  define( $name, $val );
428  }
429  foreach ( $info['autoloaderPaths'] as $path ) {
430  if ( file_exists( $path ) ) {
431  require_once $path;
432  }
433  }
434 
435  $this->loaded += $info['credits'];
436  if ( $info['attributes'] ) {
437  if ( !$this->attributes ) {
438  $this->attributes = $info['attributes'];
439  } else {
440  $this->attributes = array_merge_recursive( $this->attributes, $info['attributes'] );
441  }
442  }
443 
444  foreach ( $info['callbacks'] as $name => $cb ) {
445  if ( !is_callable( $cb ) ) {
446  if ( is_array( $cb ) ) {
447  $cb = '[ ' . implode( ', ', $cb ) . ' ]';
448  }
449  throw new UnexpectedValueException( "callback '$cb' is not callable" );
450  }
451  $cb( $info['credits'][$name] );
452  }
453  }
454 
464  public function load( $path ) {
465  wfDeprecated( __METHOD__, '1.34' );
466  $this->loadFromQueue(); // First clear the queue
467  $this->queue( $path );
468  $this->loadFromQueue();
469  }
470 
479  public function isLoaded( $name, $constraint = '*' ) {
480  $isLoaded = isset( $this->loaded[$name] );
481  if ( $constraint === '*' || !$isLoaded ) {
482  return $isLoaded;
483  }
484  // if a specific constraint is requested, but no version is set, throw an exception
485  if ( !isset( $this->loaded[$name]['version'] ) ) {
486  $msg = "{$name} does not expose its version, but an extension or a skin"
487  . " requires: {$constraint}.";
488  throw new LogicException( $msg );
489  }
490 
491  return SemVer::satisfies( $this->loaded[$name]['version'], $constraint );
492  }
493 
498  public function getAttribute( $name ) {
499  return $this->testAttributes[$name] ??
500  $this->attributes[$name] ?? [];
501  }
502 
511  public function setAttributeForTest( $name, array $value ) {
512  // @codeCoverageIgnoreStart
513  if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
514  throw new RuntimeException( __METHOD__ . ' can only be used in tests' );
515  }
516  // @codeCoverageIgnoreEnd
517  if ( isset( $this->testAttributes[$name] ) ) {
518  throw new Exception( "The attribute '$name' has already been overridden" );
519  }
520  $this->testAttributes[$name] = $value;
521  return new ScopedCallback( function () use ( $name ) {
522  unset( $this->testAttributes[$name] );
523  } );
524  }
525 
531  public function getAllThings() {
532  return $this->loaded;
533  }
534 
542  protected static function processAutoLoader( $dir, array $files ) {
543  // Make paths absolute, relative to the JSON file
544  foreach ( $files as &$file ) {
545  $file = "$dir/$file";
546  }
547  return $files;
548  }
549 }
static ExtensionRegistry $instance
const MANIFEST_VERSION
Version of the highest supported manifest version Note: Update MANIFEST_VERSION_MW_VERSION when chang...
$wgVersion
MediaWiki version number.
bool $finished
Whether we are done loading things.
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Copyright (C) 2018 Kunal Mehta legoktm@member.fsf.org
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Definition: router.php:42
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2158
static exportAutoloadClassesAndNamespaces( $dir, $info, &$autoloadClasses=[], &$autoloadNamespaces=[])
Export autoload classes and namespaces for a given directory and parsed JSON info file...
Provides functions to check a set of extensions with dependencies against a set of loaded extensions ...
exportExtractedData(array $info)
$value
setAttributeForTest( $name, array $value)
Force override the value of an attribute during tests.
getAllThings()
Get information about all things.
array $loaded
Array of loaded things, keyed by name, values are credits information.
const MERGE_STRATEGY
Special key that defines the merge strategy.
getAbilities()
Get the list of abilities and their values.
static processAutoLoader( $dir, array $files)
Fully expand autoloader paths.
clearQueue()
Clear the current load queue.
const MANIFEST_VERSION_MW_VERSION
MediaWiki version constraint representing what the current highest MANIFEST_VERSION is supported in...
const CACHE_VERSION
Bump whenever the registration cache needs resetting.
array $attributes
Items in the JSON file that aren&#39;t being set as globals.
load( $path)
Loads and processes the given JSON file without delay.
int bool $wgExtensionInfoMTime
When loading extensions through the extension registration system, this can be used to invalidate the...
$cache
Definition: mcc.php:33
const MEDIAWIKI_CORE
"requires" key that applies to MediaWiki core/$wgVersion
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:773
finish()
After this is called, no more extensions can be loaded.
$GLOBALS['IP']
const OLDEST_MANIFEST_VERSION
Version of the oldest supported manifest version.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
readFromQueue(array $queue)
Process a queue of extensions and return their extracted data.
array $queued
List of paths that should be loaded.
array $testAttributes
Attributes for testing.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
static newFromParams( $params)
Create a new cache object from parameters.
buildVersionChecker()
Queries information about the software environment and constructs an appropiate version checker...
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
bool $checkDev
Whether to check dev-requires.
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:277
$wgObjectCaches
Advanced object cache configuration.
static detectLocalServerCache()
Detects which local server cache library is present and returns a configuration for it...
wfArrayPlus2d(array $baseArray, array $newValues)
Merges two (possibly) 2 dimensional arrays into the target array ($baseArray).
static string [] $psr4Namespaces
Definition: AutoLoader.php:37
$wgDevelopmentWarnings
If set to true MediaWiki will throw notices for some possible error conditions and for deprecated fun...
isLoaded( $name, $constraint=' *')
Whether a thing has been loaded.
getQueue()
Get the current load queue.