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
{
20 * Array of loaded things, keyed by name, values are credits information
24 private $loaded = array();
27 * List of paths that should be loaded
31 protected $queued = array();
34 * Items in the JSON file that aren't being
39 protected $attributes = array();
42 * Processors, 'default' should be set by subclasses in the constructor
46 protected $processors = array();
49 * @var ExtensionRegistry
51 private static $instance;
54 * @return ExtensionRegistry
56 public static function getInstance() {
57 if ( self
::$instance === null ) {
58 self
::$instance = new self();
61 return self
::$instance;
64 public function __construct() {
65 // We use a try/catch instead of the $fallback parameter because
66 // we don't want to fail here if $wgObjectCaches is not configured
67 // properly for APC setup
69 $this->cache
= ObjectCache
::newAccelerator( array() );
70 } catch ( MWException
$e ) {
71 $this->cache
= new EmptyBagOStuff();
76 * @param string $path Absolute path to the JSON file
78 public function queue( $path ) {
79 global $wgExtensionInfoMTime;
80 if ( $wgExtensionInfoMTime !== false ) {
81 $mtime = $wgExtensionInfoMTime;
83 $mtime = filemtime( $path );
85 $this->queued
[$path] = $mtime;
88 public function loadFromQueue() {
89 if ( !$this->queued
) {
93 $this->queued
= array_unique( $this->queued
);
95 // See if this queue is in APC
96 $key = wfMemcKey( 'registration', md5( json_encode( $this->queued
) ) );
97 $data = $this->cache
->get( $key );
99 $this->exportExtractedData( $data );
101 $data = $this->readFromQueue( $this->queued
);
102 $this->exportExtractedData( $data );
103 // Do this late since we don't want to extract it since we already
104 // did that, but it should be cached
105 $data['globals']['wgAutoloadClasses'] +
= $data['autoload'];
106 unset( $data['autoload'] );
107 $this->cache
->set( $key, $data );
109 $this->queued
= array();
113 * Process a queue of extensions and return their extracted data
115 * @param array $queue keys are filenames, values are ignored
116 * @return array extracted info
119 public function readFromQueue( array $queue ) {
120 $data = array( 'globals' => array( 'wgAutoloadClasses' => array() ) );
121 $autoloadClasses = array();
122 foreach ( $queue as $path => $mtime ) {
123 $json = file_get_contents( $path );
124 if ( $json === false ) {
125 throw new Exception( "Unable to read $path, does it exist?" );
127 $info = json_decode( $json, /* $assoc = */ true );
128 if ( !is_array( $info ) ) {
129 throw new Exception( "$path is not a valid JSON file." );
131 $autoload = $this->processAutoLoader( dirname( $path ), $info );
132 // Set up the autoloader now so custom processors will work
133 $GLOBALS['wgAutoloadClasses'] +
= $autoload;
134 $autoloadClasses +
= $autoload;
135 if ( isset( $info['processor'] ) ) {
136 $processor = $this->getProcessor( $info['processor'] );
138 $processor = $this->getProcessor( 'default' );
140 $processor->extractInfo( $path, $info );
142 foreach ( $this->processors
as $processor ) {
143 $data = array_merge_recursive( $data, $processor->getExtractedInfo() );
145 foreach ( $data['credits'] as $credit ) {
146 $data['globals']['wgExtensionCredits'][$credit['type']][] = $credit;
148 $this->processors
= array(); // Reset
149 $data['autoload'] = $autoloadClasses;
153 protected function getProcessor( $type ) {
154 if ( !isset( $this->processors
[$type] ) ) {
155 $processor = $type === 'default' ?
new ExtensionProcessor() : new $type();
156 if ( !$processor instanceof Processor
) {
157 throw new Exception( "$type is not a Processor" );
159 $this->processors
[$type] = $processor;
162 return $this->processors
[$type];
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'] );