MediaWiki  1.27.1
ExtensionRegistry.php
Go to the documentation of this file.
1 <?php
2 
13 
17  const MEDIAWIKI_CORE = 'MediaWiki';
18 
22  const MANIFEST_VERSION = 1;
23 
28 
32  const CACHE_VERSION = 3;
33 
39  const MERGE_STRATEGY = '_merge_strategy';
40 
44  protected $cache;
45 
51  private $loaded = [];
52 
58  protected $queued = [];
59 
66  protected $attributes = [];
67 
71  private static $instance;
72 
76  public static function getInstance() {
77  if ( self::$instance === null ) {
78  self::$instance = new self();
79  }
80 
81  return self::$instance;
82  }
83 
84  public function __construct() {
85  // We use a try/catch instead of the $fallback parameter because
86  // we don't want to fail here if $wgObjectCaches is not configured
87  // properly for APC setup
88  try {
90  } catch ( MWException $e ) {
91  $this->cache = new EmptyBagOStuff();
92  }
93  }
94 
98  public function queue( $path ) {
100 
101  $mtime = $wgExtensionInfoMTime;
102  if ( $mtime === false ) {
103  if ( file_exists( $path ) ) {
104  $mtime = filemtime( $path );
105  } else {
106  throw new Exception( "$path does not exist!" );
107  }
108  if ( !$mtime ) {
109  $err = error_get_last();
110  throw new Exception( "Couldn't stat $path: {$err['message']}" );
111  }
112  }
113  $this->queued[$path] = $mtime;
114  }
115 
116  public function loadFromQueue() {
118  if ( !$this->queued ) {
119  return;
120  }
121 
122  // A few more things to vary the cache on
123  $versions = [
124  'registration' => self::CACHE_VERSION,
125  'mediawiki' => $wgVersion
126  ];
127 
128  // See if this queue is in APC
129  $key = wfMemcKey(
130  'registration',
131  md5( json_encode( $this->queued + $versions ) )
132  );
133  $data = $this->cache->get( $key );
134  if ( $data ) {
135  $this->exportExtractedData( $data );
136  } else {
137  $data = $this->readFromQueue( $this->queued );
138  $this->exportExtractedData( $data );
139  // Do this late since we don't want to extract it since we already
140  // did that, but it should be cached
141  $data['globals']['wgAutoloadClasses'] += $data['autoload'];
142  unset( $data['autoload'] );
143  $this->cache->set( $key, $data, 60 * 60 * 24 );
144  }
145  $this->queued = [];
146  }
147 
154  public function getQueue() {
155  return $this->queued;
156  }
157 
162  public function clearQueue() {
163  $this->queued = [];
164  }
165 
173  public function readFromQueue( array $queue ) {
175  $autoloadClasses = [];
176  $autoloaderPaths = [];
177  $processor = new ExtensionProcessor();
178  $incompatible = [];
179  $coreVersionParser = new CoreVersionChecker( $wgVersion );
180  foreach ( $queue as $path => $mtime ) {
181  $json = file_get_contents( $path );
182  if ( $json === false ) {
183  throw new Exception( "Unable to read $path, does it exist?" );
184  }
185  $info = json_decode( $json, /* $assoc = */ true );
186  if ( !is_array( $info ) ) {
187  throw new Exception( "$path is not a valid JSON file." );
188  }
189  if ( !isset( $info['manifest_version'] ) ) {
190  // For backwards-compatability, assume a version of 1
191  $info['manifest_version'] = 1;
192  }
193  $version = $info['manifest_version'];
194  if ( $version < self::OLDEST_MANIFEST_VERSION || $version > self::MANIFEST_VERSION ) {
195  throw new Exception( "$path: unsupported manifest_version: {$version}" );
196  }
197  $autoload = $this->processAutoLoader( dirname( $path ), $info );
198  // Set up the autoloader now so custom processors will work
199  $GLOBALS['wgAutoloadClasses'] += $autoload;
200  $autoloadClasses += $autoload;
201  // Check any constraints against MediaWiki core
202  $requires = $processor->getRequirements( $info );
203  if ( isset( $requires[self::MEDIAWIKI_CORE] )
204  && !$coreVersionParser->check( $requires[self::MEDIAWIKI_CORE] )
205  ) {
206  // Doesn't match, mark it as incompatible.
207  $incompatible[] = "{$info['name']} is not compatible with the current "
208  . "MediaWiki core (version {$wgVersion}), it requires: " . $requires[self::MEDIAWIKI_CORE]
209  . '.';
210  continue;
211  }
212  // Get extra paths for later inclusion
213  $autoloaderPaths = array_merge( $autoloaderPaths,
214  $processor->getExtraAutoloaderPaths( dirname( $path ), $info ) );
215  // Compatible, read and extract info
216  $processor->extractInfo( $path, $info, $version );
217  }
218  if ( $incompatible ) {
219  if ( count( $incompatible ) === 1 ) {
220  throw new Exception( $incompatible[0] );
221  } else {
222  throw new Exception( implode( "\n", $incompatible ) );
223  }
224  }
225  $data = $processor->getExtractedInfo();
226  // Need to set this so we can += to it later
227  $data['globals']['wgAutoloadClasses'] = [];
228  $data['autoload'] = $autoloadClasses;
229  $data['autoloaderPaths'] = $autoloaderPaths;
230  return $data;
231  }
232 
233  protected function exportExtractedData( array $info ) {
234  foreach ( $info['globals'] as $key => $val ) {
235  // If a merge strategy is set, read it and remove it from the value
236  // so it doesn't accidentally end up getting set.
237  if ( is_array( $val ) && isset( $val[self::MERGE_STRATEGY] ) ) {
238  $mergeStrategy = $val[self::MERGE_STRATEGY];
239  unset( $val[self::MERGE_STRATEGY] );
240  } else {
241  $mergeStrategy = 'array_merge';
242  }
243 
244  // Optimistic: If the global is not set, or is an empty array, replace it entirely.
245  // Will be O(1) performance.
246  if ( !isset( $GLOBALS[$key] ) || ( is_array( $GLOBALS[$key] ) && !$GLOBALS[$key] ) ) {
247  $GLOBALS[$key] = $val;
248  continue;
249  }
250 
251  if ( !is_array( $GLOBALS[$key] ) || !is_array( $val ) ) {
252  // config setting that has already been overridden, don't set it
253  continue;
254  }
255 
256  switch ( $mergeStrategy ) {
257  case 'array_merge_recursive':
258  $GLOBALS[$key] = array_merge_recursive( $GLOBALS[$key], $val );
259  break;
260  case 'array_plus_2d':
261  $GLOBALS[$key] = wfArrayPlus2d( $GLOBALS[$key], $val );
262  break;
263  case 'array_plus':
264  $GLOBALS[$key] += $val;
265  break;
266  case 'array_merge':
267  $GLOBALS[$key] = array_merge( $val, $GLOBALS[$key] );
268  break;
269  default:
270  throw new UnexpectedValueException( "Unknown merge strategy '$mergeStrategy'" );
271  }
272  }
273 
274  foreach ( $info['defines'] as $name => $val ) {
275  define( $name, $val );
276  }
277  foreach ( $info['autoloaderPaths'] as $path ) {
278  require_once $path;
279  }
280  foreach ( $info['callbacks'] as $cb ) {
281  call_user_func( $cb );
282  }
283 
284  $this->loaded += $info['credits'];
285  if ( $info['attributes'] ) {
286  if ( !$this->attributes ) {
287  $this->attributes = $info['attributes'];
288  } else {
289  $this->attributes = array_merge_recursive( $this->attributes, $info['attributes'] );
290  }
291  }
292  }
293 
302  public function load( $path ) {
303  $this->loadFromQueue(); // First clear the queue
304  $this->queue( $path );
305  $this->loadFromQueue();
306  }
307 
313  public function isLoaded( $name ) {
314  return isset( $this->loaded[$name] );
315  }
316 
321  public function getAttribute( $name ) {
322  if ( isset( $this->attributes[$name] ) ) {
323  return $this->attributes[$name];
324  } else {
325  return [];
326  }
327  }
328 
334  public function getAllThings() {
335  return $this->loaded;
336  }
337 
344  protected function markLoaded( $name, array $credits ) {
345  $this->loaded[$name] = $credits;
346  }
347 
355  protected function processAutoLoader( $dir, array $info ) {
356  if ( isset( $info['AutoloadClasses'] ) ) {
357  // Make paths absolute, relative to the JSON file
358  return array_map( function( $file ) use ( $dir ) {
359  return "$dir/$file";
360  }, $info['AutoloadClasses'] );
361  } else {
362  return [];
363  }
364  }
365 }
static ExtensionRegistry $instance
const MANIFEST_VERSION
Version of the highest supported manifest version.
the array() calling protocol came about after MediaWiki 1.4rc1.
magic word the default is to use $key to get the and $key value or $key value text $key value html to format the value $key
Definition: hooks.txt:2321
$wgVersion
MediaWiki version number.
processAutoLoader($dir, array $info)
Register classes with the autoloader.
if(count($args)==0) $dir
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException'returning false will NOT prevent logging $e
Definition: hooks.txt:1932
exportExtractedData(array $info)
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
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.
isLoaded($name)
Whether a thing has been loaded.
you have access to all of the normal MediaWiki so you can get a DB use the cache
Definition: maintenance.txt:52
clearQueue()
Clear the current load queue.
$GLOBALS['IP']
const CACHE_VERSION
Bump whenever the registration cache needs resetting.
array $attributes
Items in the JSON file that aren't being set as globals.
int bool $wgExtensionInfoMTime
When loading extensions through the extension registration system, this can be used to invalidate the...
const MEDIAWIKI_CORE
"requires" key that applies to MediaWiki core/$wgVersion
A BagOStuff object with no objects in it.
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
markLoaded($name, array $credits)
Mark a thing as loaded.
ExtensionRegistry class.
readFromQueue(array $queue)
Process a queue of extensions and return their extracted data.
array $queued
List of paths that should be loaded.
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 getLocalServerInstance($fallback=CACHE_NONE)
Factory function for CACHE_ACCEL (referenced from DefaultSettings.php)
load($path)
Loads and processes the given JSON file without delay.
!html< table >< tr >< td > broken</td ></tr ></table >!end!test Table cell attributes
wfMemcKey()
Make a cache key for the local wiki.
$version
Definition: parserTests.php:85
wfArrayPlus2d(array $baseArray, array $newValues)
Merges two (possibly) 2 dimensional arrays into the target array ($baseArray).
getQueue()
Get the current load queue.
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:310