MediaWiki  1.30.0
ExtensionRegistry.php
Go to the documentation of this file.
1 <?php
2 
4 
15 
19  const MEDIAWIKI_CORE = 'MediaWiki';
20 
24  const MANIFEST_VERSION = 2;
25 
30 
34  const CACHE_VERSION = 6;
35 
41  const MERGE_STRATEGY = '_merge_strategy';
42 
48  private $loaded = [];
49 
55  protected $queued = [];
56 
62  private $finished = false;
63 
70  protected $attributes = [];
71 
75  private static $instance;
76 
80  public static function getInstance() {
81  if ( self::$instance === null ) {
82  self::$instance = new self();
83  }
84 
85  return self::$instance;
86  }
87 
91  public function queue( $path ) {
93 
94  $mtime = $wgExtensionInfoMTime;
95  if ( $mtime === false ) {
96  if ( file_exists( $path ) ) {
97  $mtime = filemtime( $path );
98  } else {
99  throw new Exception( "$path does not exist!" );
100  }
101  if ( !$mtime ) {
102  $err = error_get_last();
103  throw new Exception( "Couldn't stat $path: {$err['message']}" );
104  }
105  }
106  $this->queued[$path] = $mtime;
107  }
108 
113  public function loadFromQueue() {
115  if ( !$this->queued ) {
116  return;
117  }
118 
119  if ( $this->finished ) {
120  throw new MWException(
121  "The following paths tried to load late: "
122  . implode( ', ', array_keys( $this->queued ) )
123  );
124  }
125 
126  // A few more things to vary the cache on
127  $versions = [
128  'registration' => self::CACHE_VERSION,
129  'mediawiki' => $wgVersion
130  ];
131 
132  // We use a try/catch because we don't want to fail here
133  // if $wgObjectCaches is not configured properly for APC setup
134  try {
135  $cache = MediaWikiServices::getInstance()->getLocalServerObjectCache();
136  } catch ( MWException $e ) {
137  $cache = new EmptyBagOStuff();
138  }
139  // See if this queue is in APC
140  $key = $cache->makeKey(
141  'registration',
142  md5( json_encode( $this->queued + $versions ) )
143  );
144  $data = $cache->get( $key );
145  if ( $data ) {
146  $this->exportExtractedData( $data );
147  } else {
148  $data = $this->readFromQueue( $this->queued );
149  $this->exportExtractedData( $data );
150  // Do this late since we don't want to extract it since we already
151  // did that, but it should be cached
152  $data['globals']['wgAutoloadClasses'] += $data['autoload'];
153  unset( $data['autoload'] );
154  if ( !( $data['warnings'] && $wgDevelopmentWarnings ) ) {
155  // If there were no warnings that were shown, cache it
156  $cache->set( $key, $data, 60 * 60 * 24 );
157  }
158  }
159  $this->queued = [];
160  }
161 
168  public function getQueue() {
169  return $this->queued;
170  }
171 
176  public function clearQueue() {
177  $this->queued = [];
178  }
179 
185  public function finish() {
186  $this->finished = true;
187  }
188 
196  public function readFromQueue( array $queue ) {
198  $autoloadClasses = [];
199  $autoloaderPaths = [];
200  $processor = new ExtensionProcessor();
201  $versionChecker = new VersionChecker( $wgVersion );
202  $extDependencies = [];
203  $incompatible = [];
204  $warnings = false;
205  foreach ( $queue as $path => $mtime ) {
206  $json = file_get_contents( $path );
207  if ( $json === false ) {
208  throw new Exception( "Unable to read $path, does it exist?" );
209  }
210  $info = json_decode( $json, /* $assoc = */ true );
211  if ( !is_array( $info ) ) {
212  throw new Exception( "$path is not a valid JSON file." );
213  }
214 
215  if ( !isset( $info['manifest_version'] ) ) {
216  wfDeprecated(
217  "{$info['name']}'s extension.json or skin.json does not have manifest_version",
218  '1.29'
219  );
220  $warnings = true;
221  // For backwards-compatability, assume a version of 1
222  $info['manifest_version'] = 1;
223  }
224  $version = $info['manifest_version'];
225  if ( $version < self::OLDEST_MANIFEST_VERSION || $version > self::MANIFEST_VERSION ) {
226  $incompatible[] = "$path: unsupported manifest_version: {$version}";
227  }
228 
229  $autoload = $this->processAutoLoader( dirname( $path ), $info );
230  // Set up the autoloader now so custom processors will work
231  $GLOBALS['wgAutoloadClasses'] += $autoload;
232  $autoloadClasses += $autoload;
233 
234  // get all requirements/dependencies for this extension
235  $requires = $processor->getRequirements( $info );
236 
237  // validate the information needed and add the requirements
238  if ( is_array( $requires ) && $requires && isset( $info['name'] ) ) {
239  $extDependencies[$info['name']] = $requires;
240  }
241 
242  // Get extra paths for later inclusion
243  $autoloaderPaths = array_merge( $autoloaderPaths,
244  $processor->getExtraAutoloaderPaths( dirname( $path ), $info ) );
245  // Compatible, read and extract info
246  $processor->extractInfo( $path, $info, $version );
247  }
248  $data = $processor->getExtractedInfo();
249  $data['warnings'] = $warnings;
250 
251  // check for incompatible extensions
252  $incompatible = array_merge(
253  $incompatible,
254  $versionChecker
255  ->setLoadedExtensionsAndSkins( $data['credits'] )
256  ->checkArray( $extDependencies )
257  );
258 
259  if ( $incompatible ) {
260  if ( count( $incompatible ) === 1 ) {
261  throw new Exception( $incompatible[0] );
262  } else {
263  throw new Exception( implode( "\n", $incompatible ) );
264  }
265  }
266 
267  // Need to set this so we can += to it later
268  $data['globals']['wgAutoloadClasses'] = [];
269  $data['autoload'] = $autoloadClasses;
270  $data['autoloaderPaths'] = $autoloaderPaths;
271  return $data;
272  }
273 
274  protected function exportExtractedData( array $info ) {
275  foreach ( $info['globals'] as $key => $val ) {
276  // If a merge strategy is set, read it and remove it from the value
277  // so it doesn't accidentally end up getting set.
278  if ( is_array( $val ) && isset( $val[self::MERGE_STRATEGY] ) ) {
279  $mergeStrategy = $val[self::MERGE_STRATEGY];
280  unset( $val[self::MERGE_STRATEGY] );
281  } else {
282  $mergeStrategy = 'array_merge';
283  }
284 
285  // Optimistic: If the global is not set, or is an empty array, replace it entirely.
286  // Will be O(1) performance.
287  if ( !isset( $GLOBALS[$key] ) || ( is_array( $GLOBALS[$key] ) && !$GLOBALS[$key] ) ) {
288  $GLOBALS[$key] = $val;
289  continue;
290  }
291 
292  if ( !is_array( $GLOBALS[$key] ) || !is_array( $val ) ) {
293  // config setting that has already been overridden, don't set it
294  continue;
295  }
296 
297  switch ( $mergeStrategy ) {
298  case 'array_merge_recursive':
299  $GLOBALS[$key] = array_merge_recursive( $GLOBALS[$key], $val );
300  break;
301  case 'array_replace_recursive':
302  $GLOBALS[$key] = array_replace_recursive( $GLOBALS[$key], $val );
303  break;
304  case 'array_plus_2d':
305  $GLOBALS[$key] = wfArrayPlus2d( $GLOBALS[$key], $val );
306  break;
307  case 'array_plus':
308  $GLOBALS[$key] += $val;
309  break;
310  case 'array_merge':
311  $GLOBALS[$key] = array_merge( $val, $GLOBALS[$key] );
312  break;
313  default:
314  throw new UnexpectedValueException( "Unknown merge strategy '$mergeStrategy'" );
315  }
316  }
317 
318  foreach ( $info['defines'] as $name => $val ) {
319  define( $name, $val );
320  }
321  foreach ( $info['autoloaderPaths'] as $path ) {
322  require_once $path;
323  }
324 
325  $this->loaded += $info['credits'];
326  if ( $info['attributes'] ) {
327  if ( !$this->attributes ) {
328  $this->attributes = $info['attributes'];
329  } else {
330  $this->attributes = array_merge_recursive( $this->attributes, $info['attributes'] );
331  }
332  }
333 
334  foreach ( $info['callbacks'] as $name => $cb ) {
335  if ( !is_callable( $cb ) ) {
336  if ( is_array( $cb ) ) {
337  $cb = '[ ' . implode( ', ', $cb ) . ' ]';
338  }
339  throw new UnexpectedValueException( "callback '$cb' is not callable" );
340  }
341  call_user_func( $cb, $info['credits'][$name] );
342  }
343  }
344 
353  public function load( $path ) {
354  $this->loadFromQueue(); // First clear the queue
355  $this->queue( $path );
356  $this->loadFromQueue();
357  }
358 
364  public function isLoaded( $name ) {
365  return isset( $this->loaded[$name] );
366  }
367 
372  public function getAttribute( $name ) {
373  if ( isset( $this->attributes[$name] ) ) {
374  return $this->attributes[$name];
375  } else {
376  return [];
377  }
378  }
379 
385  public function getAllThings() {
386  return $this->loaded;
387  }
388 
395  protected function markLoaded( $name, array $credits ) {
396  $this->loaded[$name] = $credits;
397  }
398 
406  protected function processAutoLoader( $dir, array $info ) {
407  if ( isset( $info['AutoloadClasses'] ) ) {
408  // Make paths absolute, relative to the JSON file
409  return array_map( function ( $file ) use ( $dir ) {
410  return "$dir/$file";
411  }, $info['AutoloadClasses'] );
412  } else {
413  return [];
414  }
415  }
416 }
ExtensionRegistry\getQueue
getQueue()
Get the current load queue.
Definition: ExtensionRegistry.php:168
ExtensionRegistry\$finished
bool $finished
Whether we are done loading things.
Definition: ExtensionRegistry.php:62
ExtensionRegistry\MANIFEST_VERSION
const MANIFEST_VERSION
Version of the highest supported manifest version.
Definition: ExtensionRegistry.php:24
ExtensionRegistry\queue
queue( $path)
Definition: ExtensionRegistry.php:91
EmptyBagOStuff
A BagOStuff object with no objects in it.
Definition: EmptyBagOStuff.php:29
wfArrayPlus2d
wfArrayPlus2d(array $baseArray, array $newValues)
Merges two (possibly) 2 dimensional arrays into the target array ($baseArray).
Definition: GlobalFunctions.php:3483
captcha-old.count
count
Definition: captcha-old.py:249
ExtensionRegistry\exportExtractedData
exportExtractedData(array $info)
Definition: ExtensionRegistry.php:274
ExtensionRegistry\processAutoLoader
processAutoLoader( $dir, array $info)
Register classes with the autoloader.
Definition: ExtensionRegistry.php:406
ExtensionRegistry
ExtensionRegistry class.
Definition: ExtensionRegistry.php:14
$wgVersion
$wgVersion
MediaWiki version number.
Definition: DefaultSettings.php:78
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
ExtensionRegistry\getAllThings
getAllThings()
Get information about all things.
Definition: ExtensionRegistry.php:385
ExtensionRegistry\$loaded
array $loaded
Array of loaded things, keyed by name, values are credits information.
Definition: ExtensionRegistry.php:48
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:302
ExtensionRegistry\clearQueue
clearQueue()
Clear the current load queue.
Definition: ExtensionRegistry.php:176
php
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
ExtensionRegistry\MERGE_STRATEGY
const MERGE_STRATEGY
Special key that defines the merge strategy.
Definition: ExtensionRegistry.php:41
ExtensionRegistry\getInstance
static getInstance()
Definition: ExtensionRegistry.php:80
ExtensionProcessor
Definition: ExtensionProcessor.php:3
ExtensionRegistry\isLoaded
isLoaded( $name)
Whether a thing has been loaded.
Definition: ExtensionRegistry.php:364
ExtensionRegistry\CACHE_VERSION
const CACHE_VERSION
Bump whenever the registration cache needs resetting.
Definition: ExtensionRegistry.php:34
MWException
MediaWiki exception.
Definition: MWException.php:26
ExtensionRegistry\$attributes
array $attributes
Items in the JSON file that aren't being set as globals.
Definition: ExtensionRegistry.php:70
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
Definition: GlobalFunctions.php:1176
ExtensionRegistry\load
load( $path)
Loads and processes the given JSON file without delay.
Definition: ExtensionRegistry.php:353
$queue
$queue
Definition: mergeMessageFileList.php:161
ExtensionRegistry\finish
finish()
After this is called, no more extensions can be loaded.
Definition: ExtensionRegistry.php:185
ExtensionRegistry\loadFromQueue
loadFromQueue()
Definition: ExtensionRegistry.php:113
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
$GLOBALS
$GLOBALS['wgAutoloadClasses']['LocalisationUpdate']
Definition: Autoload.php:10
$dir
$dir
Definition: Autoload.php:8
ExtensionRegistry\OLDEST_MANIFEST_VERSION
const OLDEST_MANIFEST_VERSION
Version of the oldest supported manifest version.
Definition: ExtensionRegistry.php:29
ExtensionRegistry\MEDIAWIKI_CORE
const MEDIAWIKI_CORE
"requires" key that applies to MediaWiki core/$wgVersion
Definition: ExtensionRegistry.php:19
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2141
ExtensionRegistry\readFromQueue
readFromQueue(array $queue)
Process a queue of extensions and return their extracted data.
Definition: ExtensionRegistry.php:196
ExtensionRegistry\$queued
array $queued
List of paths that should be loaded.
Definition: ExtensionRegistry.php:55
$wgDevelopmentWarnings
$wgDevelopmentWarnings
If set to true MediaWiki will throw notices for some possible error conditions and for deprecated fun...
Definition: DefaultSettings.php:6297
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
$path
$path
Definition: NoLocalSettings.php:26
as
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
$wgExtensionInfoMTime
int bool $wgExtensionInfoMTime
When loading extensions through the extension registration system, this can be used to invalidate the...
Definition: DefaultSettings.php:2634
ExtensionRegistry\getAttribute
getAttribute( $name)
Definition: ExtensionRegistry.php:372
ExtensionRegistry\markLoaded
markLoaded( $name, array $credits)
Mark a thing as loaded.
Definition: ExtensionRegistry.php:395
MediaWikiServices
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 MediaWikiServices
Definition: injection.txt:23
ExtensionRegistry\$instance
static ExtensionRegistry $instance
Definition: ExtensionRegistry.php:75
array
the array() calling protocol came about after MediaWiki 1.4rc1.