MediaWiki REL1_27
ExtensionRegistry.php
Go to the documentation of this file.
1<?php
2
13
17 const MEDIAWIKI_CORE = 'MediaWiki';
18
23
28
32 const CACHE_VERSION = 4;
33
39 const MERGE_STRATEGY = '_merge_strategy';
40
46 private $loaded = [];
47
53 protected $queued = [];
54
61 protected $attributes = [];
62
66 private static $instance;
67
71 public static function getInstance() {
72 if ( self::$instance === null ) {
73 self::$instance = new self();
74 }
75
76 return self::$instance;
77 }
78
82 public function queue( $path ) {
84
85 $mtime = $wgExtensionInfoMTime;
86 if ( $mtime === false ) {
87 if ( file_exists( $path ) ) {
88 $mtime = filemtime( $path );
89 } else {
90 throw new Exception( "$path does not exist!" );
91 }
92
93 if ( $mtime === false ) {
94 $err = error_get_last();
95 throw new Exception( "Couldn't stat $path: {$err['message']}" );
96 }
97 }
98 $this->queued[$path] = $mtime;
99 }
100
101 public function loadFromQueue() {
103 if ( !$this->queued ) {
104 return;
105 }
106
107 // A few more things to vary the cache on
108 $versions = [
109 'registration' => self::CACHE_VERSION,
110 'mediawiki' => $wgVersion
111 ];
112
113 // We use a try/catch because we don't want to fail here
114 // if $wgObjectCaches is not configured properly for APC setup
115 try {
117 } catch ( MWException $e ) {
118 $cache = new EmptyBagOStuff();
119 }
120 // See if this queue is in APC
121 $key = wfMemcKey(
122 'registration',
123 md5( json_encode( $this->queued + $versions ) )
124 );
125 $data = $cache->get( $key );
126 if ( $data ) {
127 $this->exportExtractedData( $data );
128 } else {
129 $data = $this->readFromQueue( $this->queued );
130 $this->exportExtractedData( $data );
131 // Do this late since we don't want to extract it since we already
132 // did that, but it should be cached
133 $data['globals']['wgAutoloadClasses'] += $data['autoload'];
134 unset( $data['autoload'] );
135 $cache->set( $key, $data, 60 * 60 * 24 );
136 }
137 $this->queued = [];
138 }
139
146 public function getQueue() {
147 return $this->queued;
148 }
149
154 public function clearQueue() {
155 $this->queued = [];
156 }
157
165 public function readFromQueue( array $queue ) {
167 $autoloadClasses = [];
168 $autoloaderPaths = [];
169 $processor = new ExtensionProcessor();
170 $incompatible = [];
171 $coreVersionParser = new CoreVersionChecker( $wgVersion );
172 foreach ( $queue as $path => $mtime ) {
173 $json = file_get_contents( $path );
174 if ( $json === false ) {
175 throw new Exception( "Unable to read $path, does it exist?" );
176 }
177 $info = json_decode( $json, /* $assoc = */ true );
178 if ( !is_array( $info ) ) {
179 throw new Exception( "$path is not a valid JSON file." );
180 }
181 if ( !isset( $info['manifest_version'] ) ) {
182 // For backwards-compatability, assume a version of 1
183 $info['manifest_version'] = 1;
184 }
185 $version = $info['manifest_version'];
186 if ( $version < self::OLDEST_MANIFEST_VERSION || $version > self::MANIFEST_VERSION ) {
187 throw new Exception( "$path: unsupported manifest_version: {$version}" );
188 }
189 $autoload = $this->processAutoLoader( dirname( $path ), $info );
190 // Set up the autoloader now so custom processors will work
191 $GLOBALS['wgAutoloadClasses'] += $autoload;
192 $autoloadClasses += $autoload;
193 // Check any constraints against MediaWiki core
194 $requires = $processor->getRequirements( $info );
195 if ( isset( $requires[self::MEDIAWIKI_CORE] )
196 && !$coreVersionParser->check( $requires[self::MEDIAWIKI_CORE] )
197 ) {
198 // Doesn't match, mark it as incompatible.
199 $incompatible[] = "{$info['name']} is not compatible with the current "
200 . "MediaWiki core (version {$wgVersion}), it requires: " . $requires[self::MEDIAWIKI_CORE]
201 . '.';
202 continue;
203 }
204 // Get extra paths for later inclusion
205 $autoloaderPaths = array_merge( $autoloaderPaths,
206 $processor->getExtraAutoloaderPaths( dirname( $path ), $info ) );
207 // Compatible, read and extract info
208 $processor->extractInfo( $path, $info, $version );
209 }
210 if ( $incompatible ) {
211 if ( count( $incompatible ) === 1 ) {
212 throw new Exception( $incompatible[0] );
213 } else {
214 throw new Exception( implode( "\n", $incompatible ) );
215 }
216 }
217 $data = $processor->getExtractedInfo();
218 // Need to set this so we can += to it later
219 $data['globals']['wgAutoloadClasses'] = [];
220 $data['autoload'] = $autoloadClasses;
221 $data['autoloaderPaths'] = $autoloaderPaths;
222 return $data;
223 }
224
225 protected function exportExtractedData( array $info ) {
226 foreach ( $info['globals'] as $key => $val ) {
227 // If a merge strategy is set, read it and remove it from the value
228 // so it doesn't accidentally end up getting set.
229 if ( is_array( $val ) && isset( $val[self::MERGE_STRATEGY] ) ) {
230 $mergeStrategy = $val[self::MERGE_STRATEGY];
231 unset( $val[self::MERGE_STRATEGY] );
232 } else {
233 $mergeStrategy = 'array_merge';
234 }
235
236 // Optimistic: If the global is not set, or is an empty array, replace it entirely.
237 // Will be O(1) performance.
238 if ( !isset( $GLOBALS[$key] ) || ( is_array( $GLOBALS[$key] ) && !$GLOBALS[$key] ) ) {
239 $GLOBALS[$key] = $val;
240 continue;
241 }
242
243 if ( !is_array( $GLOBALS[$key] ) || !is_array( $val ) ) {
244 // config setting that has already been overridden, don't set it
245 continue;
246 }
247
248 switch ( $mergeStrategy ) {
249 case 'array_merge_recursive':
250 $GLOBALS[$key] = array_merge_recursive( $GLOBALS[$key], $val );
251 break;
252 case 'array_plus_2d':
253 $GLOBALS[$key] = wfArrayPlus2d( $GLOBALS[$key], $val );
254 break;
255 case 'array_plus':
256 $GLOBALS[$key] += $val;
257 break;
258 case 'array_merge':
259 $GLOBALS[$key] = array_merge( $val, $GLOBALS[$key] );
260 break;
261 default:
262 throw new UnexpectedValueException( "Unknown merge strategy '$mergeStrategy'" );
263 }
264 }
265
266 foreach ( $info['defines'] as $name => $val ) {
267 define( $name, $val );
268 }
269 foreach ( $info['autoloaderPaths'] as $path ) {
270 require_once $path;
271 }
272
273 $this->loaded += $info['credits'];
274 if ( $info['attributes'] ) {
275 if ( !$this->attributes ) {
276 $this->attributes = $info['attributes'];
277 } else {
278 $this->attributes = array_merge_recursive( $this->attributes, $info['attributes'] );
279 }
280 }
281
282 foreach ( $info['callbacks'] as $name => $cb ) {
283 call_user_func( $cb, $info['credits'][$name] );
284 }
285 }
286
295 public function load( $path ) {
296 $this->loadFromQueue(); // First clear the queue
297 $this->queue( $path );
298 $this->loadFromQueue();
299 }
300
306 public function isLoaded( $name ) {
307 return isset( $this->loaded[$name] );
308 }
309
314 public function getAttribute( $name ) {
315 if ( isset( $this->attributes[$name] ) ) {
316 return $this->attributes[$name];
317 } else {
318 return [];
319 }
320 }
321
327 public function getAllThings() {
328 return $this->loaded;
329 }
330
337 protected function markLoaded( $name, array $credits ) {
338 $this->loaded[$name] = $credits;
339 }
340
348 protected function processAutoLoader( $dir, array $info ) {
349 if ( isset( $info['AutoloadClasses'] ) ) {
350 // Make paths absolute, relative to the JSON file
351 return array_map( function( $file ) use ( $dir ) {
352 return "$dir/$file";
353 }, $info['AutoloadClasses'] );
354 } else {
355 return [];
356 }
357 }
358}
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).
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.
static getLocalServerInstance( $fallback=CACHE_NONE)
Factory function for CACHE_ACCEL (referenced from DefaultSettings.php)
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:314
returning false will NOT prevent logging $e
Definition hooks.txt:1940
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
$cache
Definition mcc.php:33
$version
! html< table >< tr >< td > broken</td ></tr ></table > !end !test Table cell attributes