48362757b282fe359b3da467cd4cb470f9dc0c4d
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 // See if this queue is in APC
94 $key = wfMemcKey( 'registration', md5( json_encode( $this->queued
) ) );
95 $data = $this->cache
->get( $key );
97 $this->exportExtractedData( $data );
99 $data = $this->readFromQueue( $this->queued
);
100 $this->exportExtractedData( $data );
101 // Do this late since we don't want to extract it since we already
102 // did that, but it should be cached
103 $data['globals']['wgAutoloadClasses'] +
= $data['autoload'];
104 unset( $data['autoload'] );
105 $this->cache
->set( $key, $data );
107 $this->queued
= array();
111 * Process a queue of extensions and return their extracted data
113 * @param array $queue keys are filenames, values are ignored
114 * @return array extracted info
117 public function readFromQueue( array $queue ) {
118 $data = array( 'globals' => array( 'wgAutoloadClasses' => array() ) );
119 $autoloadClasses = array();
120 foreach ( $queue as $path => $mtime ) {
121 $json = file_get_contents( $path );
122 if ( $json === false ) {
123 throw new Exception( "Unable to read $path, does it exist?" );
125 $info = json_decode( $json, /* $assoc = */ true );
126 if ( !is_array( $info ) ) {
127 throw new Exception( "$path is not a valid JSON file." );
129 $autoload = $this->processAutoLoader( dirname( $path ), $info );
130 // Set up the autoloader now so custom processors will work
131 $GLOBALS['wgAutoloadClasses'] +
= $autoload;
132 $autoloadClasses +
= $autoload;
133 if ( isset( $info['processor'] ) ) {
134 $processor = $this->getProcessor( $info['processor'] );
136 $processor = $this->getProcessor( 'default' );
138 $processor->extractInfo( $path, $info );
140 foreach ( $this->processors
as $processor ) {
141 $data = array_merge_recursive( $data, $processor->getExtractedInfo() );
143 foreach ( $data['credits'] as $credit ) {
144 $data['globals']['wgExtensionCredits'][$credit['type']][] = $credit;
146 $this->processors
= array(); // Reset
147 $data['autoload'] = $autoloadClasses;
151 protected function getProcessor( $type ) {
152 if ( !isset( $this->processors
[$type] ) ) {
153 $processor = $type === 'default' ?
new ExtensionProcessor() : new $type();
154 if ( !$processor instanceof Processor
) {
155 throw new Exception( "$type is not a Processor" );
157 $this->processors
[$type] = $processor;
160 return $this->processors
[$type];
163 protected function exportExtractedData( array $info ) {
164 foreach ( $info['globals'] as $key => $val ) {
165 if ( !isset( $GLOBALS[$key] ) ||
!$GLOBALS[$key] ) {
166 $GLOBALS[$key] = $val;
167 } elseif ( $key === 'wgHooks' ||
$key === 'wgExtensionCredits' ) {
168 // Special case $wgHooks and $wgExtensionCredits, which require a recursive merge.
169 // Ideally it would have been taken care of in the first if block though.
170 $GLOBALS[$key] = array_merge_recursive( $GLOBALS[$key], $val );
171 } elseif ( $key === 'wgGroupPermissions' ) {
172 // First merge individual groups
173 foreach ( $GLOBALS[$key] as $name => &$groupVal ) {
174 if ( isset( $val[$name] ) ) {
175 $groupVal +
= $val[$name];
178 // Now merge groups that didn't exist yet
179 $GLOBALS[$key] +
= $val;
180 } elseif ( is_array( $GLOBALS[$key] ) && is_array( $val ) ) {
181 $GLOBALS[$key] = array_merge( $val, $GLOBALS[$key] );
182 } // else case is a config setting where it has already been overriden, so don't set it
184 foreach ( $info['defines'] as $name => $val ) {
185 define( $name, $val );
187 foreach ( $info['callbacks'] as $cb ) {
188 call_user_func( $cb );
191 $this->loaded +
= $info['credits'];
193 if ( $info['attributes'] ) {
194 if ( !$this->attributes
) {
195 $this->attributes
= $info['attributes'];
197 $this->attributes
= array_merge_recursive( $this->attributes
, $info['attributes'] );
203 * Loads and processes the given JSON file without delay
205 * If some extensions are already queued, this will load
208 * @param string $path Absolute path to the JSON file
210 public function load( $path ) {
211 $this->loadFromQueue(); // First clear the queue
212 $this->queue( $path );
213 $this->loadFromQueue();
217 * Whether a thing has been loaded
218 * @param string $name
221 public function isLoaded( $name ) {
222 return isset( $this->loaded
[$name] );
226 * @param string $name
229 public function getAttribute( $name ) {
230 if ( isset( $this->attributes
[$name] ) ) {
231 return $this->attributes
[$name];
238 * Get information about all things
242 public function getAllThings() {
243 return $this->loaded
;
247 * Mark a thing as loaded
249 * @param string $name
250 * @param array $credits
252 protected function markLoaded( $name, array $credits ) {
253 $this->loaded
[$name] = $credits;
257 * Register classes with the autoloader
263 protected function processAutoLoader( $dir, array $info ) {
264 if ( isset( $info['AutoloadClasses'] ) ) {
265 // Make paths absolute, relative to the JSON file
266 return array_map( function( $file ) use ( $dir ) {
268 }, $info['AutoloadClasses'] );