4 * ExtensionRegistry class
6 * The Registry loads JSON files, and uses a Processor
7 * to extract information from them. It also registers
8 * classes with the autoloader.
12 class ExtensionRegistry
{
15 * Version of the highest supported manifest version
17 const MANIFEST_VERSION
= 1;
20 * Version of the oldest supported manifest version
22 const OLDEST_MANIFEST_VERSION
= 1;
30 * Array of loaded things, keyed by name, values are credits information
34 private $loaded = array();
37 * List of paths that should be loaded
41 protected $queued = array();
44 * Items in the JSON file that aren't being
49 protected $attributes = array();
52 * @var ExtensionRegistry
54 private static $instance;
57 * @return ExtensionRegistry
59 public static function getInstance() {
60 if ( self
::$instance === null ) {
61 self
::$instance = new self();
64 return self
::$instance;
67 public function __construct() {
68 // We use a try/catch instead of the $fallback parameter because
69 // we don't want to fail here if $wgObjectCaches is not configured
70 // properly for APC setup
72 $this->cache
= ObjectCache
::newAccelerator( array() );
73 } catch ( MWException
$e ) {
74 $this->cache
= new EmptyBagOStuff();
79 * @param string $path Absolute path to the JSON file
81 public function queue( $path ) {
82 global $wgExtensionInfoMTime;
84 $mtime = $wgExtensionInfoMTime;
85 if ( $mtime === false ) {
86 if ( file_exists( $path ) ) {
87 $mtime = filemtime( $path );
89 throw new Exception( "$path does not exist!" );
92 $err = error_get_last();
93 throw new Exception( "Couldn't stat $path: {$err['message']}" );
96 $this->queued
[$path] = $mtime;
99 public function loadFromQueue() {
100 if ( !$this->queued
) {
104 // See if this queue is in APC
105 $key = wfMemcKey( 'registration', md5( json_encode( $this->queued
) ) );
106 $data = $this->cache
->get( $key );
108 $this->exportExtractedData( $data );
110 $data = $this->readFromQueue( $this->queued
);
111 $this->exportExtractedData( $data );
112 // Do this late since we don't want to extract it since we already
113 // did that, but it should be cached
114 $data['globals']['wgAutoloadClasses'] +
= $data['autoload'];
115 unset( $data['autoload'] );
116 $this->cache
->set( $key, $data, 60 * 60 * 24 );
118 $this->queued
= array();
122 * Process a queue of extensions and return their extracted data
124 * @param array $queue keys are filenames, values are ignored
125 * @return array extracted info
128 public function readFromQueue( array $queue ) {
129 $data = array( 'globals' => array( 'wgAutoloadClasses' => array() ) );
130 $autoloadClasses = array();
131 $processor = new ExtensionProcessor();
132 foreach ( $queue as $path => $mtime ) {
133 $json = file_get_contents( $path );
134 if ( $json === false ) {
135 throw new Exception( "Unable to read $path, does it exist?" );
137 $info = json_decode( $json, /* $assoc = */ true );
138 if ( !is_array( $info ) ) {
139 throw new Exception( "$path is not a valid JSON file." );
141 if ( !isset( $info['manifest_version' ] ) ) {
142 // For backwards-compatability, assume a version of 1
143 $info['manifest_version'] = 1;
145 $version = $info['manifest_version'];
146 if ( $version < self
::OLDEST_MANIFEST_VERSION ||
$version > self
::MANIFEST_VERSION
) {
147 throw new Exception( "$path: unsupported manifest_version: {$version}" );
149 $autoload = $this->processAutoLoader( dirname( $path ), $info );
150 // Set up the autoloader now so custom processors will work
151 $GLOBALS['wgAutoloadClasses'] +
= $autoload;
152 $autoloadClasses +
= $autoload;
153 $processor->extractInfo( $path, $info, $version );
155 $data = $processor->getExtractedInfo();
156 // Need to set this so we can += to it later
157 $data['globals']['wgAutoloadClasses'] = array();
158 foreach ( $data['credits'] as $credit ) {
159 $data['globals']['wgExtensionCredits'][$credit['type']][] = $credit;
161 $data['autoload'] = $autoloadClasses;
165 protected function exportExtractedData( array $info ) {
166 foreach ( $info['globals'] as $key => $val ) {
167 if ( !isset( $GLOBALS[$key] ) ||
!$GLOBALS[$key] ) {
168 $GLOBALS[$key] = $val;
169 } elseif ( $key === 'wgHooks' ||
$key === 'wgExtensionCredits' ) {
170 // Special case $wgHooks and $wgExtensionCredits, which require a recursive merge.
171 // Ideally it would have been taken care of in the first if block though.
172 $GLOBALS[$key] = array_merge_recursive( $GLOBALS[$key], $val );
173 } elseif ( $key === 'wgGroupPermissions' ) {
174 // First merge individual groups
175 foreach ( $GLOBALS[$key] as $name => &$groupVal ) {
176 if ( isset( $val[$name] ) ) {
177 $groupVal +
= $val[$name];
180 // Now merge groups that didn't exist yet
181 $GLOBALS[$key] +
= $val;
182 } elseif ( is_array( $GLOBALS[$key] ) && is_array( $val ) ) {
183 $GLOBALS[$key] = array_merge( $val, $GLOBALS[$key] );
184 } // else case is a config setting where it has already been overriden, so don't set it
186 foreach ( $info['defines'] as $name => $val ) {
187 define( $name, $val );
189 foreach ( $info['callbacks'] as $cb ) {
190 call_user_func( $cb );
193 $this->loaded +
= $info['credits'];
195 if ( $info['attributes'] ) {
196 if ( !$this->attributes
) {
197 $this->attributes
= $info['attributes'];
199 $this->attributes
= array_merge_recursive( $this->attributes
, $info['attributes'] );
205 * Loads and processes the given JSON file without delay
207 * If some extensions are already queued, this will load
210 * @param string $path Absolute path to the JSON file
212 public function load( $path ) {
213 $this->loadFromQueue(); // First clear the queue
214 $this->queue( $path );
215 $this->loadFromQueue();
219 * Whether a thing has been loaded
220 * @param string $name
223 public function isLoaded( $name ) {
224 return isset( $this->loaded
[$name] );
228 * @param string $name
231 public function getAttribute( $name ) {
232 if ( isset( $this->attributes
[$name] ) ) {
233 return $this->attributes
[$name];
240 * Get information about all things
244 public function getAllThings() {
245 return $this->loaded
;
249 * Mark a thing as loaded
251 * @param string $name
252 * @param array $credits
254 protected function markLoaded( $name, array $credits ) {
255 $this->loaded
[$name] = $credits;
259 * Register classes with the autoloader
265 protected function processAutoLoader( $dir, array $info ) {
266 if ( isset( $info['AutoloadClasses'] ) ) {
267 // Make paths absolute, relative to the JSON file
268 return array_map( function( $file ) use ( $dir ) {
270 }, $info['AutoloadClasses'] );