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