summaryrefslogtreecommitdiff
path: root/www/wiki/includes/registration/ExtensionRegistry.php
blob: aae5fc28241549f822533e4cad5aa00ba6416ba7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
<?php

use MediaWiki\MediaWikiServices;

/**
 * ExtensionRegistry class
 *
 * The Registry loads JSON files, and uses a Processor
 * to extract information from them. It also registers
 * classes with the autoloader.
 *
 * @since 1.25
 */
class ExtensionRegistry {

	/**
	 * "requires" key that applies to MediaWiki core/$wgVersion
	 */
	const MEDIAWIKI_CORE = 'MediaWiki';

	/**
	 * Version of the highest supported manifest version
	 * Note: Update MANIFEST_VERSION_MW_VERSION when changing this
	 */
	const MANIFEST_VERSION = 2;

	/**
	 * MediaWiki version constraint representing what the current
	 * highest MANIFEST_VERSION is supported in
	 */
	const MANIFEST_VERSION_MW_VERSION = '>= 1.29.0';

	/**
	 * Version of the oldest supported manifest version
	 */
	const OLDEST_MANIFEST_VERSION = 1;

	/**
	 * Bump whenever the registration cache needs resetting
	 */
	const CACHE_VERSION = 6;

	/**
	 * Special key that defines the merge strategy
	 *
	 * @since 1.26
	 */
	const MERGE_STRATEGY = '_merge_strategy';

	/**
	 * Array of loaded things, keyed by name, values are credits information
	 *
	 * @var array
	 */
	private $loaded = [];

	/**
	 * List of paths that should be loaded
	 *
	 * @var array
	 */
	protected $queued = [];

	/**
	 * Whether we are done loading things
	 *
	 * @var bool
	 */
	private $finished = false;

	/**
	 * Items in the JSON file that aren't being
	 * set as globals
	 *
	 * @var array
	 */
	protected $attributes = [];

	/**
	 * @var ExtensionRegistry
	 */
	private static $instance;

	/**
	 * @codeCoverageIgnore
	 * @return ExtensionRegistry
	 */
	public static function getInstance() {
		if ( self::$instance === null ) {
			self::$instance = new self();
		}

		return self::$instance;
	}

	/**
	 * @param string $path Absolute path to the JSON file
	 */
	public function queue( $path ) {
		global $wgExtensionInfoMTime;

		$mtime = $wgExtensionInfoMTime;
		if ( $mtime === false ) {
			if ( file_exists( $path ) ) {
				$mtime = filemtime( $path );
			} else {
				throw new Exception( "$path does not exist!" );
			}
			// @codeCoverageIgnoreStart
			if ( $mtime === false ) {
				$err = error_get_last();
				throw new Exception( "Couldn't stat $path: {$err['message']}" );
				// @codeCoverageIgnoreEnd
			}
		}
		$this->queued[$path] = $mtime;
	}

	/**
	 * @throws MWException If the queue is already marked as finished (no further things should
	 *  be loaded then).
	 */
	public function loadFromQueue() {
		global $wgVersion, $wgDevelopmentWarnings;
		if ( !$this->queued ) {
			return;
		}

		if ( $this->finished ) {
			throw new MWException(
				"The following paths tried to load late: "
				. implode( ', ', array_keys( $this->queued ) )
			);
		}

		// A few more things to vary the cache on
		$versions = [
			'registration' => self::CACHE_VERSION,
			'mediawiki' => $wgVersion
		];

		// We use a try/catch because we don't want to fail here
		// if $wgObjectCaches is not configured properly for APC setup
		try {
			$cache = MediaWikiServices::getInstance()->getLocalServerObjectCache();
		} catch ( MWException $e ) {
			$cache = new EmptyBagOStuff();
		}
		// See if this queue is in APC
		$key = $cache->makeKey(
			'registration',
			md5( json_encode( $this->queued + $versions ) )
		);
		$data = $cache->get( $key );
		if ( $data ) {
			$this->exportExtractedData( $data );
		} else {
			$data = $this->readFromQueue( $this->queued );
			$this->exportExtractedData( $data );
			// Do this late since we don't want to extract it since we already
			// did that, but it should be cached
			$data['globals']['wgAutoloadClasses'] += $data['autoload'];
			unset( $data['autoload'] );
			if ( !( $data['warnings'] && $wgDevelopmentWarnings ) ) {
				// If there were no warnings that were shown, cache it
				$cache->set( $key, $data, 60 * 60 * 24 );
			}
		}
		$this->queued = [];
	}

	/**
	 * Get the current load queue. Not intended to be used
	 * outside of the installer.
	 *
	 * @return array
	 */
	public function getQueue() {
		return $this->queued;
	}

	/**
	 * Clear the current load queue. Not intended to be used
	 * outside of the installer.
	 */
	public function clearQueue() {
		$this->queued = [];
	}

	/**
	 * After this is called, no more extensions can be loaded
	 *
	 * @since 1.29
	 */
	public function finish() {
		$this->finished = true;
	}

	/**
	 * Process a queue of extensions and return their extracted data
	 *
	 * @param array $queue keys are filenames, values are ignored
	 * @return array extracted info
	 * @throws Exception
	 * @throws ExtensionDependencyError
	 */
	public function readFromQueue( array $queue ) {
		global $wgVersion;
		$autoloadClasses = [];
		$autoloadNamespaces = [];
		$autoloaderPaths = [];
		$processor = new ExtensionProcessor();
		$versionChecker = new VersionChecker( $wgVersion );
		$extDependencies = [];
		$incompatible = [];
		$warnings = false;
		foreach ( $queue as $path => $mtime ) {
			$json = file_get_contents( $path );
			if ( $json === false ) {
				throw new Exception( "Unable to read $path, does it exist?" );
			}
			$info = json_decode( $json, /* $assoc = */ true );
			if ( !is_array( $info ) ) {
				throw new Exception( "$path is not a valid JSON file." );
			}

			if ( !isset( $info['manifest_version'] ) ) {
				wfDeprecated(
					"{$info['name']}'s extension.json or skin.json does not have manifest_version",
					'1.29'
				);
				$warnings = true;
				// For backwards-compatability, assume a version of 1
				$info['manifest_version'] = 1;
			}
			$version = $info['manifest_version'];
			if ( $version < self::OLDEST_MANIFEST_VERSION || $version > self::MANIFEST_VERSION ) {
				$incompatible[] = "$path: unsupported manifest_version: {$version}";
			}

			$dir = dirname( $path );
			if ( isset( $info['AutoloadClasses'] ) ) {
				$autoload = $this->processAutoLoader( $dir, $info['AutoloadClasses'] );
				$GLOBALS['wgAutoloadClasses'] += $autoload;
				$autoloadClasses += $autoload;
			}
			if ( isset( $info['AutoloadNamespaces'] ) ) {
				$autoloadNamespaces += $this->processAutoLoader( $dir, $info['AutoloadNamespaces'] );
				AutoLoader::$psr4Namespaces += $autoloadNamespaces;
			}

			// get all requirements/dependencies for this extension
			$requires = $processor->getRequirements( $info );

			// validate the information needed and add the requirements
			if ( is_array( $requires ) && $requires && isset( $info['name'] ) ) {
				$extDependencies[$info['name']] = $requires;
			}

			// Get extra paths for later inclusion
			$autoloaderPaths = array_merge( $autoloaderPaths,
				$processor->getExtraAutoloaderPaths( $dir, $info ) );
			// Compatible, read and extract info
			$processor->extractInfo( $path, $info, $version );
		}
		$data = $processor->getExtractedInfo();
		$data['warnings'] = $warnings;

		// check for incompatible extensions
		$incompatible = array_merge(
			$incompatible,
			$versionChecker
				->setLoadedExtensionsAndSkins( $data['credits'] )
				->checkArray( $extDependencies )
		);

		if ( $incompatible ) {
			throw new ExtensionDependencyError( $incompatible );
		}

		// Need to set this so we can += to it later
		$data['globals']['wgAutoloadClasses'] = [];
		$data['autoload'] = $autoloadClasses;
		$data['autoloaderPaths'] = $autoloaderPaths;
		$data['autoloaderNS'] = $autoloadNamespaces;
		return $data;
	}

	protected function exportExtractedData( array $info ) {
		foreach ( $info['globals'] as $key => $val ) {
			// If a merge strategy is set, read it and remove it from the value
			// so it doesn't accidentally end up getting set.
			if ( is_array( $val ) && isset( $val[self::MERGE_STRATEGY] ) ) {
				$mergeStrategy = $val[self::MERGE_STRATEGY];
				unset( $val[self::MERGE_STRATEGY] );
			} else {
				$mergeStrategy = 'array_merge';
			}

			// Optimistic: If the global is not set, or is an empty array, replace it entirely.
			// Will be O(1) performance.
			if ( !array_key_exists( $key, $GLOBALS ) || ( is_array( $GLOBALS[$key] ) && !$GLOBALS[$key] ) ) {
				$GLOBALS[$key] = $val;
				continue;
			}

			if ( !is_array( $GLOBALS[$key] ) || !is_array( $val ) ) {
				// config setting that has already been overridden, don't set it
				continue;
			}

			switch ( $mergeStrategy ) {
				case 'array_merge_recursive':
					$GLOBALS[$key] = array_merge_recursive( $GLOBALS[$key], $val );
					break;
				case 'array_replace_recursive':
					$GLOBALS[$key] = array_replace_recursive( $GLOBALS[$key], $val );
					break;
				case 'array_plus_2d':
					$GLOBALS[$key] = wfArrayPlus2d( $GLOBALS[$key], $val );
					break;
				case 'array_plus':
					$GLOBALS[$key] += $val;
					break;
				case 'array_merge':
					$GLOBALS[$key] = array_merge( $val, $GLOBALS[$key] );
					break;
				default:
					throw new UnexpectedValueException( "Unknown merge strategy '$mergeStrategy'" );
			}
		}

		if ( isset( $info['autoloaderNS'] ) ) {
			AutoLoader::$psr4Namespaces += $info['autoloaderNS'];
		}

		foreach ( $info['defines'] as $name => $val ) {
			define( $name, $val );
		}
		foreach ( $info['autoloaderPaths'] as $path ) {
			if ( file_exists( $path ) ) {
				require_once $path;
			}
		}

		$this->loaded += $info['credits'];
		if ( $info['attributes'] ) {
			if ( !$this->attributes ) {
				$this->attributes = $info['attributes'];
			} else {
				$this->attributes = array_merge_recursive( $this->attributes, $info['attributes'] );
			}
		}

		foreach ( $info['callbacks'] as $name => $cb ) {
			if ( !is_callable( $cb ) ) {
				if ( is_array( $cb ) ) {
					$cb = '[ ' . implode( ', ', $cb ) . ' ]';
				}
				throw new UnexpectedValueException( "callback '$cb' is not callable" );
			}
			call_user_func( $cb, $info['credits'][$name] );
		}
	}

	/**
	 * Loads and processes the given JSON file without delay
	 *
	 * If some extensions are already queued, this will load
	 * those as well.
	 *
	 * @param string $path Absolute path to the JSON file
	 */
	public function load( $path ) {
		$this->loadFromQueue(); // First clear the queue
		$this->queue( $path );
		$this->loadFromQueue();
	}

	/**
	 * Whether a thing has been loaded
	 * @param string $name
	 * @return bool
	 */
	public function isLoaded( $name ) {
		return isset( $this->loaded[$name] );
	}

	/**
	 * @param string $name
	 * @return array
	 */
	public function getAttribute( $name ) {
		if ( isset( $this->attributes[$name] ) ) {
			return $this->attributes[$name];
		} else {
			return [];
		}
	}

	/**
	 * Get information about all things
	 *
	 * @return array
	 */
	public function getAllThings() {
		return $this->loaded;
	}

	/**
	 * Fully expand autoloader paths
	 *
	 * @param string $dir
	 * @param array $files
	 * @return array
	 */
	protected function processAutoLoader( $dir, array $files ) {
		// Make paths absolute, relative to the JSON file
		foreach ( $files as &$file ) {
			$file = "$dir/$file";
		}
		return $files;
	}
}