Implement extension registration from an extension.json file
[mediawiki.git] / includes / registration / ExtensionRegistry.php
blob44855d8ef4747d2f293058787d2a6b1a7c2e6577
1 <?php
3 /**
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.
10 * @since 1.25
12 class ExtensionRegistry {
14 /**
15 * @var BagOStuff
17 protected $cache;
19 /**
20 * Array of loaded things, keyed by name, values are credits information
22 * @var array
24 private $loaded = array();
26 /**
27 * List of paths that should be loaded
29 * @var array
31 protected $queued = array();
33 /**
34 * Items in the JSON file that aren't being
35 * set as globals
37 * @var array
39 protected $attributes = array();
41 /**
42 * Processors, 'default' should be set by subclasses in the constructor
44 * @var Processor[]
46 protected $processors = array();
48 /**
49 * @var ExtensionRegistry
51 private static $instance;
53 /**
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 $this->cache = ObjectCache::newAccelerator( array(), CACHE_NONE );
68 /**
69 * @param string $path Absolute path to the JSON file
71 public function queue( $path ) {
72 global $wgExtensionInfoMTime;
73 if ( $wgExtensionInfoMTime !== false ) {
74 $mtime = $wgExtensionInfoMTime;
75 } else {
76 $mtime = filemtime( $path );
78 $this->queued[$path] = $mtime;
81 public function loadFromQueue() {
82 if ( !$this->queued ) {
83 return;
86 $this->queued = array_unique( $this->queued );
88 // See if this queue is in APC
89 $key = wfMemcKey( 'registration', md5( json_encode( $this->queued ) ) );
90 $data = $this->cache->get( $key );
91 if ( $data ) {
92 $this->exportExtractedData( $data );
93 } else {
94 $data = array( 'globals' => array( 'wgAutoloadClasses' => array() ) );
95 $autoloadClasses = array();
96 foreach ( $this->queued as $path => $mtime ) {
97 $json = file_get_contents( $path );
98 $info = json_decode( $json, /* $assoc = */ true );
99 $autoload = $this->processAutoLoader( dirname( $path ), $info );
100 // Set up the autoloader now so custom processors will work
101 $GLOBALS['wgAutoloadClasses'] += $autoload;
102 $autoloadClasses += $autoload;
103 if ( isset( $info['processor'] ) ) {
104 $processor = $this->getProcessor( $info['processor'] );
105 } else {
106 $processor = $this->getProcessor( 'default' );
108 $processor->extractInfo( $path, $info );
110 foreach ( $this->processors as $processor ) {
111 $data = array_merge_recursive( $data, $processor->getExtractedInfo() );
113 foreach ( $data['credits'] as $credit ) {
114 $data['globals']['wgExtensionCredits'][$credit['type']][] = $credit;
116 $this->processors = array(); // Reset
117 $this->exportExtractedData( $data );
118 // Do this late since we don't want to extract it since we already
119 // did that, but it should be cached
120 $data['globals']['wgAutoloadClasses'] += $autoloadClasses;
121 $this->cache->set( $key, $data );
123 $this->queued = array();
126 protected function getProcessor( $type ) {
127 if ( !isset( $this->processors[$type] ) ) {
128 $processor = $type === 'default' ? new ExtensionProcessor() : new $type();
129 if ( !$processor instanceof Processor ) {
130 throw new Exception( "$type is not a Processor" );
132 $this->processors[$type] = $processor;
135 return $this->processors[$type];
138 protected function exportExtractedData( array $info ) {
139 foreach ( $info['globals'] as $key => $val ) {
140 if ( !isset( $GLOBALS[$key] ) || !$GLOBALS[$key] ) {
141 $GLOBALS[$key] = $val;
142 } elseif ( is_array( $GLOBALS[$key] ) && is_array( $val ) ) {
143 $GLOBALS[$key] = array_merge_recursive( $GLOBALS[$key], $val );
144 } // else case is a config setting where it has already been overriden, so don't set it
146 foreach ( $info['defines'] as $name => $val ) {
147 define( $name, $val );
149 foreach ( $info['callbacks'] as $cb ) {
150 call_user_func( $cb );
153 $this->loaded += $info['credits'];
155 if ( $info['attributes'] ) {
156 if ( !$this->attributes ) {
157 $this->attributes = $info['attributes'];
158 } else {
159 $this->attributes = array_merge_recursive( $this->attributes, $info['attributes'] );
165 * Loads and processes the given JSON file without delay
167 * If some extensions are already queued, this will load
168 * those as well.
170 * @param string $path Absolute path to the JSON file
172 public function load( $path ) {
173 $this->loadFromQueue(); // First clear the queue
174 $this->queue( $path );
175 $this->loadFromQueue();
179 * Whether a thing has been loaded
180 * @param string $name
181 * @return bool
183 public function isLoaded( $name ) {
184 return isset( $this->loaded[$name] );
188 * @param string $name
189 * @return array
191 public function getAttribute( $name ) {
192 if ( isset( $this->attributes[$name] ) ) {
193 return $this->attributes[$name];
194 } else {
195 return array();
200 * Get information about all things
202 * @return array
204 public function getAllThings() {
205 return $this->loaded;
209 * Mark a thing as loaded
211 * @param string $name
212 * @param array $credits
214 protected function markLoaded( $name, array $credits ) {
215 $this->loaded[$name] = $credits;
219 * Register classes with the autoloader
221 * @param string $dir
222 * @param array $info
223 * @return array
225 protected function processAutoLoader( $dir, array $info ) {
226 if ( isset( $info['AutoloadClasses'] ) ) {
227 // Make paths absolute, relative to the JSON file
228 return array_map( function( $file ) use ( $dir ) {
229 return "$dir/$file";
230 }, $info['AutoloadClasses'] );
231 } else {
232 return array();
237 * @param string $filename absolute path to the JSON file
238 * @param int $mtime modified time of the file
239 * @return array
241 protected function loadInfoFromFile( $filename, $mtime ) {
242 $key = wfMemcKey( 'registry', md5( $filename ) );
243 $cached = $this->cache->get( $key );
244 if ( isset( $cached['mtime'] ) && $cached['mtime'] === $mtime ) {
245 return $cached['info'];
248 $contents = file_get_contents( $filename );
249 $json = json_decode( $contents, /* $assoc = */ true );
250 if ( is_array( $json ) ) {
251 $this->cache->set( $key, array( 'mtime' => $mtime, 'info' => $json ) );
252 } else {
253 // Don't throw an error here, but don't cache it either.
254 // @todo log somewhere?
255 $json = array();
258 return $json;