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 = 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 if ( !is_array( $info ) ) {
100 throw new Exception( "$path is not a valid JSON file." );
102 $autoload = $this->processAutoLoader( dirname( $path ), $info );
103 // Set up the autoloader now so custom processors will work
104 $GLOBALS['wgAutoloadClasses'] +
= $autoload;
105 $autoloadClasses +
= $autoload;
106 if ( isset( $info['processor'] ) ) {
107 $processor = $this->getProcessor( $info['processor'] );
109 $processor = $this->getProcessor( 'default' );
111 $processor->extractInfo( $path, $info );
113 foreach ( $this->processors
as $processor ) {
114 $data = array_merge_recursive( $data, $processor->getExtractedInfo() );
116 foreach ( $data['credits'] as $credit ) {
117 $data['globals']['wgExtensionCredits'][$credit['type']][] = $credit;
119 $this->processors
= array(); // Reset
120 $this->exportExtractedData( $data );
121 // Do this late since we don't want to extract it since we already
122 // did that, but it should be cached
123 $data['globals']['wgAutoloadClasses'] +
= $autoloadClasses;
124 $this->cache
->set( $key, $data );
126 $this->queued
= array();
129 protected function getProcessor( $type ) {
130 if ( !isset( $this->processors
[$type] ) ) {
131 $processor = $type === 'default' ?
new ExtensionProcessor() : new $type();
132 if ( !$processor instanceof Processor
) {
133 throw new Exception( "$type is not a Processor" );
135 $this->processors
[$type] = $processor;
138 return $this->processors
[$type];
141 protected function exportExtractedData( array $info ) {
142 foreach ( $info['globals'] as $key => $val ) {
143 if ( !isset( $GLOBALS[$key] ) ||
!$GLOBALS[$key] ) {
144 $GLOBALS[$key] = $val;
145 } elseif ( $key === 'wgHooks' ) {
146 // Special case $wgHooks, which requires a recursive merge.
147 // Ideally it would have been taken care of in the first if block though.
148 $GLOBALS[$key] = array_merge_recursive( $GLOBALS[$key], $val );
149 } elseif ( $key === 'wgGroupPermissions' ) {
150 // First merge individual groups
151 foreach ( $GLOBALS[$key] as $name => &$groupVal ) {
152 if ( isset( $val[$name] ) ) {
153 $groupVal +
= $val[$name];
156 // Now merge groups that didn't exist yet
157 $GLOBALS[$key] +
= $val;
158 } elseif ( is_array( $GLOBALS[$key] ) && is_array( $val ) ) {
159 $GLOBALS[$key] +
= $val;
160 } // else case is a config setting where it has already been overriden, so don't set it
162 foreach ( $info['defines'] as $name => $val ) {
163 define( $name, $val );
165 foreach ( $info['callbacks'] as $cb ) {
166 call_user_func( $cb );
169 $this->loaded +
= $info['credits'];
171 if ( $info['attributes'] ) {
172 if ( !$this->attributes
) {
173 $this->attributes
= $info['attributes'];
175 $this->attributes
= array_merge_recursive( $this->attributes
, $info['attributes'] );
181 * Loads and processes the given JSON file without delay
183 * If some extensions are already queued, this will load
186 * @param string $path Absolute path to the JSON file
188 public function load( $path ) {
189 $this->loadFromQueue(); // First clear the queue
190 $this->queue( $path );
191 $this->loadFromQueue();
195 * Whether a thing has been loaded
196 * @param string $name
199 public function isLoaded( $name ) {
200 return isset( $this->loaded
[$name] );
204 * @param string $name
207 public function getAttribute( $name ) {
208 if ( isset( $this->attributes
[$name] ) ) {
209 return $this->attributes
[$name];
216 * Get information about all things
220 public function getAllThings() {
221 return $this->loaded
;
225 * Mark a thing as loaded
227 * @param string $name
228 * @param array $credits
230 protected function markLoaded( $name, array $credits ) {
231 $this->loaded
[$name] = $credits;
235 * Register classes with the autoloader
241 protected function processAutoLoader( $dir, array $info ) {
242 if ( isset( $info['AutoloadClasses'] ) ) {
243 // Make paths absolute, relative to the JSON file
244 return array_map( function( $file ) use ( $dir ) {
246 }, $info['AutoloadClasses'] );