Completed
Branch master (5998bb)
by
unknown
29:17
created

ExtensionProcessor::extractConfig()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 7
nop 1
dl 0
loc 15
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
class ExtensionProcessor implements Processor {
4
5
	/**
6
	 * Keys that should be set to $GLOBALS
7
	 *
8
	 * @var array
9
	 */
10
	protected static $globalSettings = [
11
		'ResourceLoaderSources',
12
		'ResourceLoaderLESSVars',
13
		'DefaultUserOptions',
14
		'HiddenPrefs',
15
		'GroupPermissions',
16
		'RevokePermissions',
17
		'GrantPermissions',
18
		'GrantPermissionGroups',
19
		'ImplicitGroups',
20
		'GroupsAddToSelf',
21
		'GroupsRemoveFromSelf',
22
		'AddGroups',
23
		'RemoveGroups',
24
		'AvailableRights',
25
		'ContentHandlers',
26
		'ConfigRegistry',
27
		'SessionProviders',
28
		'AuthManagerAutoConfig',
29
		'CentralIdLookupProviders',
30
		'ChangeCredentialsBlacklist',
31
		'RemoveCredentialsBlacklist',
32
		'RateLimits',
33
		'RecentChangesFlags',
34
		'MediaHandlers',
35
		'ExtensionFunctions',
36
		'ExtensionEntryPointListFiles',
37
		'SpecialPages',
38
		'JobClasses',
39
		'LogTypes',
40
		'LogRestrictions',
41
		'FilterLogTypes',
42
		'ActionFilteredLogs',
43
		'LogNames',
44
		'LogHeaders',
45
		'LogActions',
46
		'LogActionsHandlers',
47
		'Actions',
48
		'APIModules',
49
		'APIFormatModules',
50
		'APIMetaModules',
51
		'APIPropModules',
52
		'APIListModules',
53
		'ValidSkinNames',
54
		'FeedClasses',
55
	];
56
57
	/**
58
	 * Mapping of global settings to their specific merge strategies.
59
	 *
60
	 * @see ExtensionRegistry::exportExtractedData
61
	 * @see getExtractedInfo
62
	 * @var array
63
	 */
64
	protected static $mergeStrategies = [
65
		'wgGroupPermissions' => 'array_plus_2d',
66
		'wgRevokePermissions' => 'array_plus_2d',
67
		'wgGrantPermissions' => 'array_plus_2d',
68
		'wgHooks' => 'array_merge_recursive',
69
		'wgExtensionCredits' => 'array_merge_recursive',
70
		'wgExtraGenderNamespaces' => 'array_plus',
71
		'wgNamespacesWithSubpages' => 'array_plus',
72
		'wgNamespaceContentModels' => 'array_plus',
73
		'wgNamespaceProtection' => 'array_plus',
74
		'wgCapitalLinkOverrides' => 'array_plus',
75
		'wgRateLimits' => 'array_plus_2d',
76
		'wgAuthManagerAutoConfig' => 'array_plus_2d',
77
	];
78
79
	/**
80
	 * Keys that are part of the extension credits
81
	 *
82
	 * @var array
83
	 */
84
	protected static $creditsAttributes = [
85
		'name',
86
		'namemsg',
87
		'author',
88
		'version',
89
		'url',
90
		'description',
91
		'descriptionmsg',
92
		'license-name',
93
	];
94
95
	/**
96
	 * Things that are not 'attributes', but are not in
97
	 * $globalSettings or $creditsAttributes.
98
	 *
99
	 * @var array
100
	 */
101
	protected static $notAttributes = [
102
		'callback',
103
		'Hooks',
104
		'namespaces',
105
		'ResourceFileModulePaths',
106
		'ResourceModules',
107
		'ResourceModuleSkinStyles',
108
		'ExtensionMessagesFiles',
109
		'MessagesDirs',
110
		'type',
111
		'config',
112
		'config_prefix',
113
		'ParserTestFiles',
114
		'AutoloadClasses',
115
		'manifest_version',
116
		'load_composer_autoloader',
117
	];
118
119
	/**
120
	 * Stuff that is going to be set to $GLOBALS
121
	 *
122
	 * Some keys are pre-set to arrays so we can += to them
123
	 *
124
	 * @var array
125
	 */
126
	protected $globals = [
127
		'wgExtensionMessagesFiles' => [],
128
		'wgMessagesDirs' => [],
129
	];
130
131
	/**
132
	 * Things that should be define()'d
133
	 *
134
	 * @var array
135
	 */
136
	protected $defines = [];
137
138
	/**
139
	 * Things to be called once registration of these extensions are done
140
	 *
141
	 * @var callable[]
142
	 */
143
	protected $callbacks = [];
144
145
	/**
146
	 * @var array
147
	 */
148
	protected $credits = [];
149
150
	/**
151
	 * Any thing else in the $info that hasn't
152
	 * already been processed
153
	 *
154
	 * @var array
155
	 */
156
	protected $attributes = [];
157
158
	/**
159
	 * @param string $path
160
	 * @param array $info
161
	 * @param int $version manifest_version for info
162
	 * @return array
163
	 */
164
	public function extractInfo( $path, array $info, $version ) {
165
		if ( $version === 2 ) {
166
			$this->extractConfig2( $info );
167
		} else {
168
			// $version === 1
169
			$this->extractConfig1( $info );
170
		}
171
		$this->extractHooks( $info );
172
		$dir = dirname( $path );
173
		$this->extractExtensionMessagesFiles( $dir, $info );
174
		$this->extractMessagesDirs( $dir, $info );
175
		$this->extractNamespaces( $info );
176
		$this->extractResourceLoaderModules( $dir, $info );
177
		$this->extractParserTestFiles( $dir, $info );
178
		if ( isset( $info['callback'] ) ) {
179
			$this->callbacks[] = $info['callback'];
180
		}
181
182
		$this->extractCredits( $path, $info );
183
		foreach ( $info as $key => $val ) {
184
			if ( in_array( $key, self::$globalSettings ) ) {
185
				$this->storeToArray( $path, "wg$key", $val, $this->globals );
186
			// Ignore anything that starts with a @
187
			} elseif ( $key[0] !== '@' && !in_array( $key, self::$notAttributes )
188
				&& !in_array( $key, self::$creditsAttributes )
189
			) {
190
				$this->storeToArray( $path, $key, $val, $this->attributes );
191
			}
192
		}
193
	}
194
195
	public function getExtractedInfo() {
196
		// Make sure the merge strategies are set
197
		foreach ( $this->globals as $key => $val ) {
198
			if ( isset( self::$mergeStrategies[$key] ) ) {
199
				$this->globals[$key][ExtensionRegistry::MERGE_STRATEGY] = self::$mergeStrategies[$key];
200
			}
201
		}
202
203
		return [
204
			'globals' => $this->globals,
205
			'defines' => $this->defines,
206
			'callbacks' => $this->callbacks,
207
			'credits' => $this->credits,
208
			'attributes' => $this->attributes,
209
		];
210
	}
211
212
	public function getRequirements( array $info ) {
213
		$requirements = [];
214
		$key = ExtensionRegistry::MEDIAWIKI_CORE;
215
		if ( isset( $info['requires'][$key] ) ) {
216
			$requirements[$key] = $info['requires'][$key];
217
		}
218
219
		return $requirements;
220
	}
221
222
	protected function extractHooks( array $info ) {
223
		if ( isset( $info['Hooks'] ) ) {
224
			foreach ( $info['Hooks'] as $name => $value ) {
225
				if ( is_array( $value ) ) {
226
					foreach ( $value as $callback ) {
227
						$this->globals['wgHooks'][$name][] = $callback;
228
					}
229
				} else {
230
					$this->globals['wgHooks'][$name][] = $value;
231
				}
232
			}
233
		}
234
	}
235
236
	/**
237
	 * Register namespaces with the appropriate global settings
238
	 *
239
	 * @param array $info
240
	 */
241
	protected function extractNamespaces( array $info ) {
242
		if ( isset( $info['namespaces'] ) ) {
243
			foreach ( $info['namespaces'] as $ns ) {
244
				$id = $ns['id'];
245
				$this->defines[$ns['constant']] = $id;
246
				$this->attributes['ExtensionNamespaces'][$id] = $ns['name'];
247
				if ( isset( $ns['gender'] ) ) {
248
					$this->globals['wgExtraGenderNamespaces'][$id] = $ns['gender'];
249
				}
250
				if ( isset( $ns['subpages'] ) && $ns['subpages'] ) {
251
					$this->globals['wgNamespacesWithSubpages'][$id] = true;
252
				}
253
				if ( isset( $ns['content'] ) && $ns['content'] ) {
254
					$this->globals['wgContentNamespaces'][] = $id;
255
				}
256
				if ( isset( $ns['defaultcontentmodel'] ) ) {
257
					$this->globals['wgNamespaceContentModels'][$id] = $ns['defaultcontentmodel'];
258
				}
259
				if ( isset( $ns['protection'] ) ) {
260
					$this->globals['wgNamespaceProtection'][$id] = $ns['protection'];
261
				}
262
				if ( isset( $ns['capitallinkoverride'] ) ) {
263
					$this->globals['wgCapitalLinkOverrides'][$id] = $ns['capitallinkoverride'];
264
				}
265
			}
266
		}
267
	}
268
269
	protected function extractResourceLoaderModules( $dir, array $info ) {
270
		$defaultPaths = isset( $info['ResourceFileModulePaths'] )
271
			? $info['ResourceFileModulePaths']
272
			: false;
273 View Code Duplication
		if ( isset( $defaultPaths['localBasePath'] ) ) {
274
			if ( $defaultPaths['localBasePath'] === '' ) {
275
				// Avoid double slashes (e.g. /extensions/Example//path)
276
				$defaultPaths['localBasePath'] = $dir;
277
			} else {
278
				$defaultPaths['localBasePath'] = "$dir/{$defaultPaths['localBasePath']}";
279
			}
280
		}
281
282
		foreach ( [ 'ResourceModules', 'ResourceModuleSkinStyles' ] as $setting ) {
283
			if ( isset( $info[$setting] ) ) {
284
				foreach ( $info[$setting] as $name => $data ) {
285 View Code Duplication
					if ( isset( $data['localBasePath'] ) ) {
286
						if ( $data['localBasePath'] === '' ) {
287
							// Avoid double slashes (e.g. /extensions/Example//path)
288
							$data['localBasePath'] = $dir;
289
						} else {
290
							$data['localBasePath'] = "$dir/{$data['localBasePath']}";
291
						}
292
					}
293
					if ( $defaultPaths ) {
294
						$data += $defaultPaths;
295
					}
296
					$this->globals["wg$setting"][$name] = $data;
297
				}
298
			}
299
		}
300
	}
301
302
	protected function extractExtensionMessagesFiles( $dir, array $info ) {
303
		if ( isset( $info['ExtensionMessagesFiles'] ) ) {
304
			$this->globals["wgExtensionMessagesFiles"] += array_map( function( $file ) use ( $dir ) {
305
				return "$dir/$file";
306
			}, $info['ExtensionMessagesFiles'] );
307
		}
308
	}
309
310
	/**
311
	 * Set message-related settings, which need to be expanded to use
312
	 * absolute paths
313
	 *
314
	 * @param string $dir
315
	 * @param array $info
316
	 */
317
	protected function extractMessagesDirs( $dir, array $info ) {
318
		if ( isset( $info['MessagesDirs'] ) ) {
319
			foreach ( $info['MessagesDirs'] as $name => $files ) {
320
				foreach ( (array)$files as $file ) {
321
					$this->globals["wgMessagesDirs"][$name][] = "$dir/$file";
322
				}
323
			}
324
		}
325
	}
326
327
	/**
328
	 * @param string $path
329
	 * @param array $info
330
	 * @throws Exception
331
	 */
332
	protected function extractCredits( $path, array $info ) {
333
		$credits = [
334
			'path' => $path,
335
			'type' => isset( $info['type'] ) ? $info['type'] : 'other',
336
		];
337
		foreach ( self::$creditsAttributes as $attr ) {
338
			if ( isset( $info[$attr] ) ) {
339
				$credits[$attr] = $info[$attr];
340
			}
341
		}
342
343
		$name = $credits['name'];
344
345
		// If someone is loading the same thing twice, throw
346
		// a nice error (T121493)
347
		if ( isset( $this->credits[$name] ) ) {
348
			$firstPath = $this->credits[$name]['path'];
349
			$secondPath = $credits['path'];
350
			throw new Exception( "It was attempted to load $name twice, from $firstPath and $secondPath." );
351
		}
352
353
		$this->credits[$name] = $credits;
354
		$this->globals['wgExtensionCredits'][$credits['type']][] = $credits;
355
	}
356
357
	/**
358
	 * Set configuration settings for manifest_version == 1
359
	 * @todo In the future, this should be done via Config interfaces
360
	 *
361
	 * @param array $info
362
	 */
363
	protected function extractConfig1( array $info ) {
364
		if ( isset( $info['config'] ) ) {
365
			if ( isset( $info['config']['_prefix'] ) ) {
366
				$prefix = $info['config']['_prefix'];
367
				unset( $info['config']['_prefix'] );
368
			} else {
369
				$prefix = 'wg';
370
			}
371
			foreach ( $info['config'] as $key => $val ) {
372
				if ( $key[0] !== '@' ) {
373
					$this->globals["$prefix$key"] = $val;
374
				}
375
			}
376
		}
377
	}
378
379
	/**
380
	 * Set configuration settings for manifest_version == 2
381
	 * @todo In the future, this should be done via Config interfaces
382
	 *
383
	 * @param array $info
384
	 */
385
	protected function extractConfig2( array $info ) {
386
		if ( isset( $info['config_prefix'] ) ) {
387
			$prefix = $info['config_prefix'];
388
		} else {
389
			$prefix = 'wg';
390
		}
391
		if ( isset( $info['config'] ) ) {
392
			foreach ( $info['config'] as $key => $data ) {
393
				$value = $data['value'];
394
				if ( isset( $value['merge_strategy'] ) ) {
395
					$value[ExtensionRegistry::MERGE_STRATEGY] = $value['merge_strategy'];
396
				}
397
				$this->globals["$prefix$key"] = $value;
398
			}
399
		}
400
	}
401
402
	protected function extractParserTestFiles( $dir, array $info ) {
403
		if ( isset( $info['ParserTestFiles'] ) ) {
404
			foreach ( $info['ParserTestFiles'] as $path ) {
405
				$this->globals['wgParserTestFiles'][] = "$dir/$path";
406
			}
407
		}
408
	}
409
410
	/**
411
	 * @param string $path
412
	 * @param string $name
413
	 * @param array $value
414
	 * @param array &$array
415
	 * @throws InvalidArgumentException
416
	 */
417
	protected function storeToArray( $path, $name, $value, &$array ) {
418
		if ( !is_array( $value ) ) {
419
			throw new InvalidArgumentException( "The value for '$name' should be an array (from $path)" );
420
		}
421 View Code Duplication
		if ( isset( $array[$name] ) ) {
422
			$array[$name] = array_merge_recursive( $array[$name], $value );
423
		} else {
424
			$array[$name] = $value;
425
		}
426
	}
427
428
	public function getExtraAutoloaderPaths( $dir, array $info ) {
429
		$paths = [];
430
		if ( isset( $info['load_composer_autoloader'] ) && $info['load_composer_autoloader'] === true ) {
431
			$path = "$dir/vendor/autoload.php";
432
			if ( file_exists( $path ) ) {
433
				$paths[] = $path;
434
			}
435
		}
436
		return $paths;
437
	}
438
}
439