MediaWiki REL1_28
ExtensionRegistry.php
Go to the documentation of this file.
1<?php
2
4
15
19 const MEDIAWIKI_CORE = 'MediaWiki';
20
25
30
34 const CACHE_VERSION = 4;
35
41 const MERGE_STRATEGY = '_merge_strategy';
42
46 protected $cache;
47
53 private $loaded = [];
54
60 protected $queued = [];
61
68 protected $attributes = [];
69
73 private static $instance;
74
78 public static function getInstance() {
79 if ( self::$instance === null ) {
80 self::$instance = new self();
81 }
82
83 return self::$instance;
84 }
85
86 public function __construct() {
87 // We use a try/catch because we don't want to fail here
88 // if $wgObjectCaches is not configured properly for APC setup
89 try {
90 $this->cache = MediaWikiServices::getInstance()->getLocalServerObjectCache();
91 } catch ( MWException $e ) {
92 $this->cache = new EmptyBagOStuff();
93 }
94 }
95
99 public function queue( $path ) {
101
102 $mtime = $wgExtensionInfoMTime;
103 if ( $mtime === false ) {
104 if ( file_exists( $path ) ) {
105 $mtime = filemtime( $path );
106 } else {
107 throw new Exception( "$path does not exist!" );
108 }
109 if ( !$mtime ) {
110 $err = error_get_last();
111 throw new Exception( "Couldn't stat $path: {$err['message']}" );
112 }
113 }
114 $this->queued[$path] = $mtime;
115 }
116
117 public function loadFromQueue() {
119 if ( !$this->queued ) {
120 return;
121 }
122
123 // A few more things to vary the cache on
124 $versions = [
125 'registration' => self::CACHE_VERSION,
126 'mediawiki' => $wgVersion
127 ];
128
129 // See if this queue is in APC
130 $key = wfMemcKey(
131 'registration',
132 md5( json_encode( $this->queued + $versions ) )
133 );
134 $data = $this->cache->get( $key );
135 if ( $data ) {
136 $this->exportExtractedData( $data );
137 } else {
138 $data = $this->readFromQueue( $this->queued );
139 $this->exportExtractedData( $data );
140 // Do this late since we don't want to extract it since we already
141 // did that, but it should be cached
142 $data['globals']['wgAutoloadClasses'] += $data['autoload'];
143 unset( $data['autoload'] );
144 $this->cache->set( $key, $data, 60 * 60 * 24 );
145 }
146 $this->queued = [];
147 }
148
155 public function getQueue() {
156 return $this->queued;
157 }
158
163 public function clearQueue() {
164 $this->queued = [];
165 }
166
174 public function readFromQueue( array $queue ) {
176 $autoloadClasses = [];
177 $autoloaderPaths = [];
178 $processor = new ExtensionProcessor();
179 $incompatible = [];
180 $coreVersionParser = new CoreVersionChecker( $wgVersion );
181 foreach ( $queue as $path => $mtime ) {
182 $json = file_get_contents( $path );
183 if ( $json === false ) {
184 throw new Exception( "Unable to read $path, does it exist?" );
185 }
186 $info = json_decode( $json, /* $assoc = */ true );
187 if ( !is_array( $info ) ) {
188 throw new Exception( "$path is not a valid JSON file." );
189 }
190 if ( !isset( $info['manifest_version'] ) ) {
191 // For backwards-compatability, assume a version of 1
192 $info['manifest_version'] = 1;
193 }
194 $version = $info['manifest_version'];
195 if ( $version < self::OLDEST_MANIFEST_VERSION || $version > self::MANIFEST_VERSION ) {
196 throw new Exception( "$path: unsupported manifest_version: {$version}" );
197 }
198 $autoload = $this->processAutoLoader( dirname( $path ), $info );
199 // Set up the autoloader now so custom processors will work
200 $GLOBALS['wgAutoloadClasses'] += $autoload;
201 $autoloadClasses += $autoload;
202 // Check any constraints against MediaWiki core
203 $requires = $processor->getRequirements( $info );
204 if ( isset( $requires[self::MEDIAWIKI_CORE] )
205 && !$coreVersionParser->check( $requires[self::MEDIAWIKI_CORE] )
206 ) {
207 // Doesn't match, mark it as incompatible.
208 $incompatible[] = "{$info['name']} is not compatible with the current "
209 . "MediaWiki core (version {$wgVersion}), it requires: " . $requires[self::MEDIAWIKI_CORE]
210 . '.';
211 continue;
212 }
213 // Get extra paths for later inclusion
214 $autoloaderPaths = array_merge( $autoloaderPaths,
215 $processor->getExtraAutoloaderPaths( dirname( $path ), $info ) );
216 // Compatible, read and extract info
217 $processor->extractInfo( $path, $info, $version );
218 }
219 if ( $incompatible ) {
220 if ( count( $incompatible ) === 1 ) {
221 throw new Exception( $incompatible[0] );
222 } else {
223 throw new Exception( implode( "\n", $incompatible ) );
224 }
225 }
226 $data = $processor->getExtractedInfo();
227 // Need to set this so we can += to it later
228 $data['globals']['wgAutoloadClasses'] = [];
229 $data['autoload'] = $autoloadClasses;
230 $data['autoloaderPaths'] = $autoloaderPaths;
231 return $data;
232 }
233
234 protected function exportExtractedData( array $info ) {
235 foreach ( $info['globals'] as $key => $val ) {
236 // If a merge strategy is set, read it and remove it from the value
237 // so it doesn't accidentally end up getting set.
238 if ( is_array( $val ) && isset( $val[self::MERGE_STRATEGY] ) ) {
239 $mergeStrategy = $val[self::MERGE_STRATEGY];
240 unset( $val[self::MERGE_STRATEGY] );
241 } else {
242 $mergeStrategy = 'array_merge';
243 }
244
245 // Optimistic: If the global is not set, or is an empty array, replace it entirely.
246 // Will be O(1) performance.
247 if ( !isset( $GLOBALS[$key] ) || ( is_array( $GLOBALS[$key] ) && !$GLOBALS[$key] ) ) {
248 $GLOBALS[$key] = $val;
249 continue;
250 }
251
252 if ( !is_array( $GLOBALS[$key] ) || !is_array( $val ) ) {
253 // config setting that has already been overridden, don't set it
254 continue;
255 }
256
257 switch ( $mergeStrategy ) {
258 case 'array_merge_recursive':
259 $GLOBALS[$key] = array_merge_recursive( $GLOBALS[$key], $val );
260 break;
261 case 'array_replace_recursive':
262 $GLOBALS[$key] = array_replace_recursive( $GLOBALS[$key], $val );
263 break;
264 case 'array_plus_2d':
265 $GLOBALS[$key] = wfArrayPlus2d( $GLOBALS[$key], $val );
266 break;
267 case 'array_plus':
268 $GLOBALS[$key] += $val;
269 break;
270 case 'array_merge':
271 $GLOBALS[$key] = array_merge( $val, $GLOBALS[$key] );
272 break;
273 default:
274 throw new UnexpectedValueException( "Unknown merge strategy '$mergeStrategy'" );
275 }
276 }
277
278 foreach ( $info['defines'] as $name => $val ) {
279 define( $name, $val );
280 }
281 foreach ( $info['autoloaderPaths'] as $path ) {
282 require_once $path;
283 }
284
285 $this->loaded += $info['credits'];
286 if ( $info['attributes'] ) {
287 if ( !$this->attributes ) {
288 $this->attributes = $info['attributes'];
289 } else {
290 $this->attributes = array_merge_recursive( $this->attributes, $info['attributes'] );
291 }
292 }
293
294 foreach ( $info['callbacks'] as $name => $cb ) {
295 call_user_func( $cb, $info['credits'][$name] );
296 }
297 }
298
307 public function load( $path ) {
308 $this->loadFromQueue(); // First clear the queue
309 $this->queue( $path );
310 $this->loadFromQueue();
311 }
312
318 public function isLoaded( $name ) {
319 return isset( $this->loaded[$name] );
320 }
321
326 public function getAttribute( $name ) {
327 if ( isset( $this->attributes[$name] ) ) {
328 return $this->attributes[$name];
329 } else {
330 return [];
331 }
332 }
333
339 public function getAllThings() {
340 return $this->loaded;
341 }
342
349 protected function markLoaded( $name, array $credits ) {
350 $this->loaded[$name] = $credits;
351 }
352
360 protected function processAutoLoader( $dir, array $info ) {
361 if ( isset( $info['AutoloadClasses'] ) ) {
362 // Make paths absolute, relative to the JSON file
363 return array_map( function( $file ) use ( $dir ) {
364 return "$dir/$file";
365 }, $info['AutoloadClasses'] );
366 } else {
367 return [];
368 }
369 }
370}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
$GLOBALS['IP']
int bool $wgExtensionInfoMTime
When loading extensions through the extension registration system, this can be used to invalidate the...
$wgVersion
MediaWiki version number.
wfMemcKey()
Make a cache key for the local wiki.
wfArrayPlus2d(array $baseArray, array $newValues)
Merges two (possibly) 2 dimensional arrays into the target array ($baseArray).
interface is intended to be more or less compatible with the PHP memcached client.
Definition BagOStuff.php:47
A BagOStuff object with no objects in it.
ExtensionRegistry class.
isLoaded( $name)
Whether a thing has been loaded.
array $queued
List of paths that should be loaded.
const MERGE_STRATEGY
Special key that defines the merge strategy.
getQueue()
Get the current load queue.
const MANIFEST_VERSION
Version of the highest supported manifest version.
processAutoLoader( $dir, array $info)
Register classes with the autoloader.
const OLDEST_MANIFEST_VERSION
Version of the oldest supported manifest version.
array $loaded
Array of loaded things, keyed by name, values are credits information.
const CACHE_VERSION
Bump whenever the registration cache needs resetting.
clearQueue()
Clear the current load queue.
static ExtensionRegistry $instance
load( $path)
Loads and processes the given JSON file without delay.
const MEDIAWIKI_CORE
"requires" key that applies to MediaWiki core/$wgVersion
readFromQueue(array $queue)
Process a queue of extensions and return their extracted data.
exportExtractedData(array $info)
getAllThings()
Get information about all things.
array $attributes
Items in the JSON file that aren't being set as globals.
markLoaded( $name, array $credits)
Mark a thing as loaded.
MediaWiki exception.
MediaWikiServices is the service locator for the application scope of MediaWiki.
when a variable name is used in a it is silently declared as a new local masking the global
Definition design.txt:95
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
the array() calling protocol came about after MediaWiki 1.4rc1.
Allows to change the fields on the form that will be generated $name
Definition hooks.txt:304
returning false will NOT prevent logging $e
Definition hooks.txt:2110
if(count( $args)==0) $dir
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:37
you have access to all of the normal MediaWiki so you can get a DB use the cache