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;
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 // See if this queue is in APC
87 $key = wfMemcKey( 'registration', md5( json_encode( $this->queued
) ) );
88 $data = $this->cache
->get( $key );
90 $this->exportExtractedData( $data );
92 $data = $this->readFromQueue( $this->queued
);
93 $this->exportExtractedData( $data );
94 // Do this late since we don't want to extract it since we already
95 // did that, but it should be cached
96 $data['globals']['wgAutoloadClasses'] +
= $data['autoload'];
97 unset( $data['autoload'] );
98 $this->cache
->set( $key, $data, 60 * 60 * 24 );
100 $this->queued
= array();
104 * Process a queue of extensions and return their extracted data
106 * @param array $queue keys are filenames, values are ignored
107 * @return array extracted info
110 public function readFromQueue( array $queue ) {
111 $data = array( 'globals' => array( 'wgAutoloadClasses' => array() ) );
112 $autoloadClasses = array();
113 $processor = new ExtensionProcessor();
114 foreach ( $queue as $path => $mtime ) {
115 $json = file_get_contents( $path );
116 if ( $json === false ) {
117 throw new Exception( "Unable to read $path, does it exist?" );
119 $info = json_decode( $json, /* $assoc = */ true );
120 if ( !is_array( $info ) ) {
121 throw new Exception( "$path is not a valid JSON file." );
123 $autoload = $this->processAutoLoader( dirname( $path ), $info );
124 // Set up the autoloader now so custom processors will work
125 $GLOBALS['wgAutoloadClasses'] +
= $autoload;
126 $autoloadClasses +
= $autoload;
127 $processor->extractInfo( $path, $info );
129 $data = $processor->getExtractedInfo();
130 // Need to set this so we can += to it later
131 $data['globals']['wgAutoloadClasses'] = array();
132 foreach ( $data['credits'] as $credit ) {
133 $data['globals']['wgExtensionCredits'][$credit['type']][] = $credit;
135 $data['autoload'] = $autoloadClasses;
139 protected function exportExtractedData( array $info ) {
140 foreach ( $info['globals'] as $key => $val ) {
141 if ( !isset( $GLOBALS[$key] ) ||
!$GLOBALS[$key] ) {
142 $GLOBALS[$key] = $val;
143 } elseif ( $key === 'wgHooks' ||
$key === 'wgExtensionCredits' ) {
144 // Special case $wgHooks and $wgExtensionCredits, which require a recursive merge.
145 // Ideally it would have been taken care of in the first if block though.
146 $GLOBALS[$key] = array_merge_recursive( $GLOBALS[$key], $val );
147 } elseif ( $key === 'wgGroupPermissions' ) {
148 // First merge individual groups
149 foreach ( $GLOBALS[$key] as $name => &$groupVal ) {
150 if ( isset( $val[$name] ) ) {
151 $groupVal +
= $val[$name];
154 // Now merge groups that didn't exist yet
155 $GLOBALS[$key] +
= $val;
156 } elseif ( is_array( $GLOBALS[$key] ) && is_array( $val ) ) {
157 $GLOBALS[$key] = array_merge( $val, $GLOBALS[$key] );
158 } // else case is a config setting where it has already been overriden, so don't set it
160 foreach ( $info['defines'] as $name => $val ) {
161 define( $name, $val );
163 foreach ( $info['callbacks'] as $cb ) {
164 call_user_func( $cb );
167 $this->loaded +
= $info['credits'];
169 if ( $info['attributes'] ) {
170 if ( !$this->attributes
) {
171 $this->attributes
= $info['attributes'];
173 $this->attributes
= array_merge_recursive( $this->attributes
, $info['attributes'] );
179 * Loads and processes the given JSON file without delay
181 * If some extensions are already queued, this will load
184 * @param string $path Absolute path to the JSON file
186 public function load( $path ) {
187 $this->loadFromQueue(); // First clear the queue
188 $this->queue( $path );
189 $this->loadFromQueue();
193 * Whether a thing has been loaded
194 * @param string $name
197 public function isLoaded( $name ) {
198 return isset( $this->loaded
[$name] );
202 * @param string $name
205 public function getAttribute( $name ) {
206 if ( isset( $this->attributes
[$name] ) ) {
207 return $this->attributes
[$name];
214 * Get information about all things
218 public function getAllThings() {
219 return $this->loaded
;
223 * Mark a thing as loaded
225 * @param string $name
226 * @param array $credits
228 protected function markLoaded( $name, array $credits ) {
229 $this->loaded
[$name] = $credits;
233 * Register classes with the autoloader
239 protected function processAutoLoader( $dir, array $info ) {
240 if ( isset( $info['AutoloadClasses'] ) ) {
241 // Make paths absolute, relative to the JSON file
242 return array_map( function( $file ) use ( $dir ) {
244 }, $info['AutoloadClasses'] );