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 * @var ExtensionRegistry
44 private static $instance;
47 * @return ExtensionRegistry
49 public static function getInstance() {
50 if ( self
::$instance === null ) {
51 self
::$instance = new self();
54 return self
::$instance;
57 public function __construct() {
58 // We use a try/catch instead of the $fallback parameter because
59 // we don't want to fail here if $wgObjectCaches is not configured
60 // properly for APC setup
62 $this->cache
= ObjectCache
::newAccelerator( array() );
63 } catch ( MWException
$e ) {
64 $this->cache
= new EmptyBagOStuff();
69 * @param string $path Absolute path to the JSON file
71 public function queue( $path ) {
72 global $wgExtensionInfoMTime;
74 $mtime = $wgExtensionInfoMTime;
75 if ( $mtime === false ) {
76 if ( file_exists( $path ) ) {
77 $mtime = filemtime( $path );
79 throw new Exception( "$path does not exist!" );
82 $err = error_get_last();
83 throw new Exception( "Couldn't stat $path: {$err['message']}" );
86 $this->queued
[$path] = $mtime;
89 public function loadFromQueue() {
90 if ( !$this->queued
) {
94 // See if this queue is in APC
95 $key = wfMemcKey( 'registration', md5( json_encode( $this->queued
) ) );
96 $data = $this->cache
->get( $key );
98 $this->exportExtractedData( $data );
100 $data = $this->readFromQueue( $this->queued
);
101 $this->exportExtractedData( $data );
102 // Do this late since we don't want to extract it since we already
103 // did that, but it should be cached
104 $data['globals']['wgAutoloadClasses'] +
= $data['autoload'];
105 unset( $data['autoload'] );
106 $this->cache
->set( $key, $data, 60 * 60 * 24 );
108 $this->queued
= array();
112 * Process a queue of extensions and return their extracted data
114 * @param array $queue keys are filenames, values are ignored
115 * @return array extracted info
118 public function readFromQueue( array $queue ) {
119 $data = array( 'globals' => array( 'wgAutoloadClasses' => array() ) );
120 $autoloadClasses = array();
121 $processor = new ExtensionProcessor();
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 $processor->extractInfo( $path, $info );
137 $data = $processor->getExtractedInfo();
138 // Need to set this so we can += to it later
139 $data['globals']['wgAutoloadClasses'] = array();
140 foreach ( $data['credits'] as $credit ) {
141 $data['globals']['wgExtensionCredits'][$credit['type']][] = $credit;
143 $data['autoload'] = $autoloadClasses;
147 protected function exportExtractedData( array $info ) {
148 foreach ( $info['globals'] as $key => $val ) {
149 if ( !isset( $GLOBALS[$key] ) ||
!$GLOBALS[$key] ) {
150 $GLOBALS[$key] = $val;
151 } elseif ( $key === 'wgHooks' ||
$key === 'wgExtensionCredits' ) {
152 // Special case $wgHooks and $wgExtensionCredits, which require a recursive merge.
153 // Ideally it would have been taken care of in the first if block though.
154 $GLOBALS[$key] = array_merge_recursive( $GLOBALS[$key], $val );
155 } elseif ( $key === 'wgGroupPermissions' ) {
156 // First merge individual groups
157 foreach ( $GLOBALS[$key] as $name => &$groupVal ) {
158 if ( isset( $val[$name] ) ) {
159 $groupVal +
= $val[$name];
162 // Now merge groups that didn't exist yet
163 $GLOBALS[$key] +
= $val;
164 } elseif ( is_array( $GLOBALS[$key] ) && is_array( $val ) ) {
165 $GLOBALS[$key] = array_merge( $val, $GLOBALS[$key] );
166 } // else case is a config setting where it has already been overriden, so don't set it
168 foreach ( $info['defines'] as $name => $val ) {
169 define( $name, $val );
171 foreach ( $info['callbacks'] as $cb ) {
172 call_user_func( $cb );
175 $this->loaded +
= $info['credits'];
177 if ( $info['attributes'] ) {
178 if ( !$this->attributes
) {
179 $this->attributes
= $info['attributes'];
181 $this->attributes
= array_merge_recursive( $this->attributes
, $info['attributes'] );
187 * Loads and processes the given JSON file without delay
189 * If some extensions are already queued, this will load
192 * @param string $path Absolute path to the JSON file
194 public function load( $path ) {
195 $this->loadFromQueue(); // First clear the queue
196 $this->queue( $path );
197 $this->loadFromQueue();
201 * Whether a thing has been loaded
202 * @param string $name
205 public function isLoaded( $name ) {
206 return isset( $this->loaded
[$name] );
210 * @param string $name
213 public function getAttribute( $name ) {
214 if ( isset( $this->attributes
[$name] ) ) {
215 return $this->attributes
[$name];
222 * Get information about all things
226 public function getAllThings() {
227 return $this->loaded
;
231 * Mark a thing as loaded
233 * @param string $name
234 * @param array $credits
236 protected function markLoaded( $name, array $credits ) {
237 $this->loaded
[$name] = $credits;
241 * Register classes with the autoloader
247 protected function processAutoLoader( $dir, array $info ) {
248 if ( isset( $info['AutoloadClasses'] ) ) {
249 // Make paths absolute, relative to the JSON file
250 return array_map( function( $file ) use ( $dir ) {
252 }, $info['AutoloadClasses'] );