Merge "Decolonize 'viewsourcetext' and 'viewyourtext' messages"
[lhc/web/wiklou.git] / includes / registration / ExtensionRegistry.php
1 <?php
2
3 /**
4 * ExtensionRegistry class
5 *
6 * The Registry loads JSON files, and uses a Processor
7 * to extract information from them. It also registers
8 * classes with the autoloader.
9 *
10 * @since 1.25
11 */
12 class ExtensionRegistry {
13
14 /**
15 * Version of the highest supported manifest version
16 */
17 const MANIFEST_VERSION = 1;
18
19 /**
20 * Version of the oldest supported manifest version
21 */
22 const OLDEST_MANIFEST_VERSION = 1;
23
24 /**
25 * @var BagOStuff
26 */
27 protected $cache;
28
29 /**
30 * Array of loaded things, keyed by name, values are credits information
31 *
32 * @var array
33 */
34 private $loaded = array();
35
36 /**
37 * List of paths that should be loaded
38 *
39 * @var array
40 */
41 protected $queued = array();
42
43 /**
44 * Items in the JSON file that aren't being
45 * set as globals
46 *
47 * @var array
48 */
49 protected $attributes = array();
50
51 /**
52 * @var ExtensionRegistry
53 */
54 private static $instance;
55
56 /**
57 * @return ExtensionRegistry
58 */
59 public static function getInstance() {
60 if ( self::$instance === null ) {
61 self::$instance = new self();
62 }
63
64 return self::$instance;
65 }
66
67 public function __construct() {
68 // We use a try/catch instead of the $fallback parameter because
69 // we don't want to fail here if $wgObjectCaches is not configured
70 // properly for APC setup
71 try {
72 $this->cache = ObjectCache::newAccelerator( array() );
73 } catch ( MWException $e ) {
74 $this->cache = new EmptyBagOStuff();
75 }
76 }
77
78 /**
79 * @param string $path Absolute path to the JSON file
80 */
81 public function queue( $path ) {
82 global $wgExtensionInfoMTime;
83
84 $mtime = $wgExtensionInfoMTime;
85 if ( $mtime === false ) {
86 if ( file_exists( $path ) ) {
87 $mtime = filemtime( $path );
88 } else {
89 throw new Exception( "$path does not exist!" );
90 }
91 if ( !$mtime ) {
92 $err = error_get_last();
93 throw new Exception( "Couldn't stat $path: {$err['message']}" );
94 }
95 }
96 $this->queued[$path] = $mtime;
97 }
98
99 public function loadFromQueue() {
100 if ( !$this->queued ) {
101 return;
102 }
103
104 // See if this queue is in APC
105 $key = wfMemcKey( 'registration', md5( json_encode( $this->queued ) ) );
106 $data = $this->cache->get( $key );
107 if ( $data ) {
108 $this->exportExtractedData( $data );
109 } else {
110 $data = $this->readFromQueue( $this->queued );
111 $this->exportExtractedData( $data );
112 // Do this late since we don't want to extract it since we already
113 // did that, but it should be cached
114 $data['globals']['wgAutoloadClasses'] += $data['autoload'];
115 unset( $data['autoload'] );
116 $this->cache->set( $key, $data, 60 * 60 * 24 );
117 }
118 $this->queued = array();
119 }
120
121 /**
122 * Get the current load queue. Not intended to be used
123 * outside of the installer.
124 *
125 * @return array
126 */
127 public function getQueue() {
128 return $this->queued;
129 }
130
131 /**
132 * Clear the current load queue. Not intended to be used
133 * outside of the installer.
134 */
135 public function clearQueue() {
136 $this->queued = array();
137 }
138
139 /**
140 * Process a queue of extensions and return their extracted data
141 *
142 * @param array $queue keys are filenames, values are ignored
143 * @return array extracted info
144 * @throws Exception
145 */
146 public function readFromQueue( array $queue ) {
147 $autoloadClasses = array();
148 $processor = new ExtensionProcessor();
149 foreach ( $queue as $path => $mtime ) {
150 $json = file_get_contents( $path );
151 if ( $json === false ) {
152 throw new Exception( "Unable to read $path, does it exist?" );
153 }
154 $info = json_decode( $json, /* $assoc = */ true );
155 if ( !is_array( $info ) ) {
156 throw new Exception( "$path is not a valid JSON file." );
157 }
158 if ( !isset( $info['manifest_version'] ) ) {
159 // For backwards-compatability, assume a version of 1
160 $info['manifest_version'] = 1;
161 }
162 $version = $info['manifest_version'];
163 if ( $version < self::OLDEST_MANIFEST_VERSION || $version > self::MANIFEST_VERSION ) {
164 throw new Exception( "$path: unsupported manifest_version: {$version}" );
165 }
166 $autoload = $this->processAutoLoader( dirname( $path ), $info );
167 // Set up the autoloader now so custom processors will work
168 $GLOBALS['wgAutoloadClasses'] += $autoload;
169 $autoloadClasses += $autoload;
170 $processor->extractInfo( $path, $info, $version );
171 }
172 $data = $processor->getExtractedInfo();
173 // Need to set this so we can += to it later
174 $data['globals']['wgAutoloadClasses'] = array();
175 foreach ( $data['credits'] as $credit ) {
176 $data['globals']['wgExtensionCredits'][$credit['type']][] = $credit;
177 }
178 $data['autoload'] = $autoloadClasses;
179 return $data;
180 }
181
182 protected function exportExtractedData( array $info ) {
183 foreach ( $info['globals'] as $key => $val ) {
184 if ( !isset( $GLOBALS[$key] ) || ( is_array( $GLOBALS[$key] ) && !$GLOBALS[$key] ) ) {
185 $GLOBALS[$key] = $val;
186 } elseif ( $key === 'wgHooks' || $key === 'wgExtensionCredits' ) {
187 // Special case $wgHooks and $wgExtensionCredits, which require a recursive merge.
188 // Ideally it would have been taken care of in the first if block though.
189 $GLOBALS[$key] = array_merge_recursive( $GLOBALS[$key], $val );
190 } elseif ( $key === 'wgGroupPermissions' ) {
191 // First merge individual groups
192 foreach ( $GLOBALS[$key] as $name => &$groupVal ) {
193 if ( isset( $val[$name] ) ) {
194 $groupVal += $val[$name];
195 }
196 }
197 // Now merge groups that didn't exist yet
198 $GLOBALS[$key] += $val;
199 } elseif ( is_array( $GLOBALS[$key] ) && is_array( $val ) ) {
200 $GLOBALS[$key] = array_merge( $val, $GLOBALS[$key] );
201 } // else case is a config setting where it has already been overriden, so don't set it
202 }
203 foreach ( $info['defines'] as $name => $val ) {
204 define( $name, $val );
205 }
206 foreach ( $info['callbacks'] as $cb ) {
207 call_user_func( $cb );
208 }
209
210 $this->loaded += $info['credits'];
211
212 if ( $info['attributes'] ) {
213 if ( !$this->attributes ) {
214 $this->attributes = $info['attributes'];
215 } else {
216 $this->attributes = array_merge_recursive( $this->attributes, $info['attributes'] );
217 }
218 }
219 }
220
221 /**
222 * Loads and processes the given JSON file without delay
223 *
224 * If some extensions are already queued, this will load
225 * those as well.
226 *
227 * @param string $path Absolute path to the JSON file
228 */
229 public function load( $path ) {
230 $this->loadFromQueue(); // First clear the queue
231 $this->queue( $path );
232 $this->loadFromQueue();
233 }
234
235 /**
236 * Whether a thing has been loaded
237 * @param string $name
238 * @return bool
239 */
240 public function isLoaded( $name ) {
241 return isset( $this->loaded[$name] );
242 }
243
244 /**
245 * @param string $name
246 * @return array
247 */
248 public function getAttribute( $name ) {
249 if ( isset( $this->attributes[$name] ) ) {
250 return $this->attributes[$name];
251 } else {
252 return array();
253 }
254 }
255
256 /**
257 * Get information about all things
258 *
259 * @return array
260 */
261 public function getAllThings() {
262 return $this->loaded;
263 }
264
265 /**
266 * Mark a thing as loaded
267 *
268 * @param string $name
269 * @param array $credits
270 */
271 protected function markLoaded( $name, array $credits ) {
272 $this->loaded[$name] = $credits;
273 }
274
275 /**
276 * Register classes with the autoloader
277 *
278 * @param string $dir
279 * @param array $info
280 * @return array
281 */
282 protected function processAutoLoader( $dir, array $info ) {
283 if ( isset( $info['AutoloadClasses'] ) ) {
284 // Make paths absolute, relative to the JSON file
285 return array_map( function( $file ) use ( $dir ) {
286 return "$dir/$file";
287 }, $info['AutoloadClasses'] );
288 } else {
289 return array();
290 }
291 }
292 }