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 $this->cache
= ObjectCache
::newAccelerator( array(), CACHE_NONE
);
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;
76 $mtime = filemtime( $path );
78 $this->queued
[$path] = $mtime;
81 public function loadFromQueue() {
82 if ( !$this->queued
) {
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 );
92 $this->exportExtractedData( $data );
94 $data = $this->readFromQueue( $this->queued
);
95 $this->exportExtractedData( $data );
96 // Do this late since we don't want to extract it since we already
97 // did that, but it should be cached
98 $data['globals']['wgAutoloadClasses'] +
= $data['autoload'];
99 unset( $data['autoload'] );
100 $this->cache
->set( $key, $data );
102 $this->queued
= array();
106 * Process a queue of extensions and return their extracted data
108 * @param array $queue keys are filenames, values are ignored
109 * @return array extracted info
112 public function readFromQueue( array $queue ) {
113 $data = array( 'globals' => array( 'wgAutoloadClasses' => array() ) );
114 $autoloadClasses = array();
115 foreach ( $queue as $path => $mtime ) {
116 $json = file_get_contents( $path );
117 if ( $json === false ) {
118 throw new Exception( "Unable to read $path, does it exist?" );
120 $info = json_decode( $json, /* $assoc = */ true );
121 if ( !is_array( $info ) ) {
122 throw new Exception( "$path is not a valid JSON file." );
124 $autoload = $this->processAutoLoader( dirname( $path ), $info );
125 // Set up the autoloader now so custom processors will work
126 $GLOBALS['wgAutoloadClasses'] +
= $autoload;
127 $autoloadClasses +
= $autoload;
128 if ( isset( $info['processor'] ) ) {
129 $processor = $this->getProcessor( $info['processor'] );
131 $processor = $this->getProcessor( 'default' );
133 $processor->extractInfo( $path, $info );
135 foreach ( $this->processors
as $processor ) {
136 $data = array_merge_recursive( $data, $processor->getExtractedInfo() );
138 foreach ( $data['credits'] as $credit ) {
139 $data['globals']['wgExtensionCredits'][$credit['type']][] = $credit;
141 $this->processors
= array(); // Reset
142 $data['autoload'] = $autoloadClasses;
146 protected function getProcessor( $type ) {
147 if ( !isset( $this->processors
[$type] ) ) {
148 $processor = $type === 'default' ?
new ExtensionProcessor() : new $type();
149 if ( !$processor instanceof Processor
) {
150 throw new Exception( "$type is not a Processor" );
152 $this->processors
[$type] = $processor;
155 return $this->processors
[$type];
158 protected function exportExtractedData( array $info ) {
159 foreach ( $info['globals'] as $key => $val ) {
160 if ( !isset( $GLOBALS[$key] ) ||
!$GLOBALS[$key] ) {
161 $GLOBALS[$key] = $val;
162 } elseif ( $key === 'wgHooks' ||
$key === 'wgExtensionCredits' ) {
163 // Special case $wgHooks and $wgExtensionCredits, which require a recursive merge.
164 // Ideally it would have been taken care of in the first if block though.
165 $GLOBALS[$key] = array_merge_recursive( $GLOBALS[$key], $val );
166 } elseif ( $key === 'wgGroupPermissions' ) {
167 // First merge individual groups
168 foreach ( $GLOBALS[$key] as $name => &$groupVal ) {
169 if ( isset( $val[$name] ) ) {
170 $groupVal +
= $val[$name];
173 // Now merge groups that didn't exist yet
174 $GLOBALS[$key] +
= $val;
175 } elseif ( is_array( $GLOBALS[$key] ) && is_array( $val ) ) {
176 $GLOBALS[$key] = array_merge( $val, $GLOBALS[$key] );
177 } // else case is a config setting where it has already been overriden, so don't set it
179 foreach ( $info['defines'] as $name => $val ) {
180 define( $name, $val );
182 foreach ( $info['callbacks'] as $cb ) {
183 call_user_func( $cb );
186 $this->loaded +
= $info['credits'];
188 if ( $info['attributes'] ) {
189 if ( !$this->attributes
) {
190 $this->attributes
= $info['attributes'];
192 $this->attributes
= array_merge_recursive( $this->attributes
, $info['attributes'] );
198 * Loads and processes the given JSON file without delay
200 * If some extensions are already queued, this will load
203 * @param string $path Absolute path to the JSON file
205 public function load( $path ) {
206 $this->loadFromQueue(); // First clear the queue
207 $this->queue( $path );
208 $this->loadFromQueue();
212 * Whether a thing has been loaded
213 * @param string $name
216 public function isLoaded( $name ) {
217 return isset( $this->loaded
[$name] );
221 * @param string $name
224 public function getAttribute( $name ) {
225 if ( isset( $this->attributes
[$name] ) ) {
226 return $this->attributes
[$name];
233 * Get information about all things
237 public function getAllThings() {
238 return $this->loaded
;
242 * Mark a thing as loaded
244 * @param string $name
245 * @param array $credits
247 protected function markLoaded( $name, array $credits ) {
248 $this->loaded
[$name] = $credits;
252 * Register classes with the autoloader
258 protected function processAutoLoader( $dir, array $info ) {
259 if ( isset( $info['AutoloadClasses'] ) ) {
260 // Make paths absolute, relative to the JSON file
261 return array_map( function( $file ) use ( $dir ) {
263 }, $info['AutoloadClasses'] );