Merge "Don't fallback from uk to ru"
[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 *
145 * @var callable[]
146 */
147 protected $callbacks = [];
148
149 /**
150 * @var array
151 */
152 protected $credits = [];
153
154 /**
155 * Any thing else in the $info that hasn't
156 * already been processed
157 *
158 * @var array
159 */
160 protected $attributes = [];
161
162 /**
163 * @param string $path
164 * @param array $info
165 * @param int $version manifest_version for info
166 * @return array
167 */
168 public function extractInfo( $path, array $info, $version ) {
169 $dir = dirname( $path );
170 if ( $version === 2 ) {
171 $this->extractConfig2( $info, $dir );
172 } else {
173 // $version === 1
174 $this->extractConfig1( $info );
175 }
176 $this->extractHooks( $info );
177 $this->extractExtensionMessagesFiles( $dir, $info );
178 $this->extractMessagesDirs( $dir, $info );
179 $this->extractNamespaces( $info );
180 $this->extractResourceLoaderModules( $dir, $info );
181 $this->extractServiceWiringFiles( $dir, $info );
182 $this->extractParserTestFiles( $dir, $info );
183 if ( isset( $info['callback'] ) ) {
184 $this->callbacks[] = $info['callback'];
185 }
186
187 $this->extractCredits( $path, $info );
188 foreach ( $info as $key => $val ) {
189 if ( in_array( $key, self::$globalSettings ) ) {
190 $this->storeToArray( $path, "wg$key", $val, $this->globals );
191 // Ignore anything that starts with a @
192 } elseif ( $key[0] !== '@' && !in_array( $key, self::$notAttributes )
193 && !in_array( $key, self::$creditsAttributes )
194 ) {
195 $this->storeToArray( $path, $key, $val, $this->attributes );
196 }
197 }
198 }
199
200 public function getExtractedInfo() {
201 // Make sure the merge strategies are set
202 foreach ( $this->globals as $key => $val ) {
203 if ( isset( self::$mergeStrategies[$key] ) ) {
204 $this->globals[$key][ExtensionRegistry::MERGE_STRATEGY] = self::$mergeStrategies[$key];
205 }
206 }
207
208 return [
209 'globals' => $this->globals,
210 'defines' => $this->defines,
211 'callbacks' => $this->callbacks,
212 'credits' => $this->credits,
213 'attributes' => $this->attributes,
214 ];
215 }
216
217 public function getRequirements( array $info ) {
218 $requirements = [];
219 $key = ExtensionRegistry::MEDIAWIKI_CORE;
220 if ( isset( $info['requires'][$key] ) ) {
221 $requirements[$key] = $info['requires'][$key];
222 }
223
224 return $requirements;
225 }
226
227 protected function extractHooks( array $info ) {
228 if ( isset( $info['Hooks'] ) ) {
229 foreach ( $info['Hooks'] as $name => $value ) {
230 if ( is_array( $value ) ) {
231 foreach ( $value as $callback ) {
232 $this->globals['wgHooks'][$name][] = $callback;
233 }
234 } else {
235 $this->globals['wgHooks'][$name][] = $value;
236 }
237 }
238 }
239 }
240
241 /**
242 * Register namespaces with the appropriate global settings
243 *
244 * @param array $info
245 */
246 protected function extractNamespaces( array $info ) {
247 if ( isset( $info['namespaces'] ) ) {
248 foreach ( $info['namespaces'] as $ns ) {
249 $id = $ns['id'];
250 $this->defines[$ns['constant']] = $id;
251 if ( !( isset( $ns['conditional'] ) && $ns['conditional'] ) ) {
252 // If it is not conditional, register it
253 $this->attributes['ExtensionNamespaces'][$id] = $ns['name'];
254 }
255 if ( isset( $ns['gender'] ) ) {
256 $this->globals['wgExtraGenderNamespaces'][$id] = $ns['gender'];
257 }
258 if ( isset( $ns['subpages'] ) && $ns['subpages'] ) {
259 $this->globals['wgNamespacesWithSubpages'][$id] = true;
260 }
261 if ( isset( $ns['content'] ) && $ns['content'] ) {
262 $this->globals['wgContentNamespaces'][] = $id;
263 }
264 if ( isset( $ns['defaultcontentmodel'] ) ) {
265 $this->globals['wgNamespaceContentModels'][$id] = $ns['defaultcontentmodel'];
266 }
267 if ( isset( $ns['protection'] ) ) {
268 $this->globals['wgNamespaceProtection'][$id] = $ns['protection'];
269 }
270 if ( isset( $ns['capitallinkoverride'] ) ) {
271 $this->globals['wgCapitalLinkOverrides'][$id] = $ns['capitallinkoverride'];
272 }
273 }
274 }
275 }
276
277 protected function extractResourceLoaderModules( $dir, array $info ) {
278 $defaultPaths = isset( $info['ResourceFileModulePaths'] )
279 ? $info['ResourceFileModulePaths']
280 : false;
281 if ( isset( $defaultPaths['localBasePath'] ) ) {
282 if ( $defaultPaths['localBasePath'] === '' ) {
283 // Avoid double slashes (e.g. /extensions/Example//path)
284 $defaultPaths['localBasePath'] = $dir;
285 } else {
286 $defaultPaths['localBasePath'] = "$dir/{$defaultPaths['localBasePath']}";
287 }
288 }
289
290 foreach ( [ 'ResourceModules', 'ResourceModuleSkinStyles' ] as $setting ) {
291 if ( isset( $info[$setting] ) ) {
292 foreach ( $info[$setting] as $name => $data ) {
293 if ( isset( $data['localBasePath'] ) ) {
294 if ( $data['localBasePath'] === '' ) {
295 // Avoid double slashes (e.g. /extensions/Example//path)
296 $data['localBasePath'] = $dir;
297 } else {
298 $data['localBasePath'] = "$dir/{$data['localBasePath']}";
299 }
300 }
301 if ( $defaultPaths ) {
302 $data += $defaultPaths;
303 }
304 $this->globals["wg$setting"][$name] = $data;
305 }
306 }
307 }
308 }
309
310 protected function extractExtensionMessagesFiles( $dir, array $info ) {
311 if ( isset( $info['ExtensionMessagesFiles'] ) ) {
312 $this->globals["wgExtensionMessagesFiles"] += array_map( function( $file ) use ( $dir ) {
313 return "$dir/$file";
314 }, $info['ExtensionMessagesFiles'] );
315 }
316 }
317
318 /**
319 * Set message-related settings, which need to be expanded to use
320 * absolute paths
321 *
322 * @param string $dir
323 * @param array $info
324 */
325 protected function extractMessagesDirs( $dir, array $info ) {
326 if ( isset( $info['MessagesDirs'] ) ) {
327 foreach ( $info['MessagesDirs'] as $name => $files ) {
328 foreach ( (array)$files as $file ) {
329 $this->globals["wgMessagesDirs"][$name][] = "$dir/$file";
330 }
331 }
332 }
333 }
334
335 /**
336 * @param string $path
337 * @param array $info
338 * @throws Exception
339 */
340 protected function extractCredits( $path, array $info ) {
341 $credits = [
342 'path' => $path,
343 'type' => isset( $info['type'] ) ? $info['type'] : 'other',
344 ];
345 foreach ( self::$creditsAttributes as $attr ) {
346 if ( isset( $info[$attr] ) ) {
347 $credits[$attr] = $info[$attr];
348 }
349 }
350
351 $name = $credits['name'];
352
353 // If someone is loading the same thing twice, throw
354 // a nice error (T121493)
355 if ( isset( $this->credits[$name] ) ) {
356 $firstPath = $this->credits[$name]['path'];
357 $secondPath = $credits['path'];
358 throw new Exception( "It was attempted to load $name twice, from $firstPath and $secondPath." );
359 }
360
361 $this->credits[$name] = $credits;
362 $this->globals['wgExtensionCredits'][$credits['type']][] = $credits;
363 }
364
365 /**
366 * Set configuration settings for manifest_version == 1
367 * @todo In the future, this should be done via Config interfaces
368 *
369 * @param array $info
370 */
371 protected function extractConfig1( array $info ) {
372 if ( isset( $info['config'] ) ) {
373 if ( isset( $info['config']['_prefix'] ) ) {
374 $prefix = $info['config']['_prefix'];
375 unset( $info['config']['_prefix'] );
376 } else {
377 $prefix = 'wg';
378 }
379 foreach ( $info['config'] as $key => $val ) {
380 if ( $key[0] !== '@' ) {
381 $this->globals["$prefix$key"] = $val;
382 }
383 }
384 }
385 }
386
387 /**
388 * Set configuration settings for manifest_version == 2
389 * @todo In the future, this should be done via Config interfaces
390 *
391 * @param array $info
392 * @param string $dir
393 */
394 protected function extractConfig2( array $info, $dir ) {
395 if ( isset( $info['config_prefix'] ) ) {
396 $prefix = $info['config_prefix'];
397 } else {
398 $prefix = 'wg';
399 }
400 if ( isset( $info['config'] ) ) {
401 foreach ( $info['config'] as $key => $data ) {
402 $value = $data['value'];
403 if ( isset( $data['merge_strategy'] ) ) {
404 $value[ExtensionRegistry::MERGE_STRATEGY] = $data['merge_strategy'];
405 }
406 if ( isset( $data['path'] ) && $data['path'] ) {
407 $value = "$dir/$value";
408 }
409 $this->globals["$prefix$key"] = $value;
410 }
411 }
412 }
413
414 protected function extractServiceWiringFiles( $dir, array $info ) {
415 if ( isset( $info['ServiceWiringFiles'] ) ) {
416 foreach ( $info['ServiceWiringFiles'] as $path ) {
417 $this->globals['wgServiceWiringFiles'][] = "$dir/$path";
418 }
419 }
420 }
421
422 protected function extractParserTestFiles( $dir, array $info ) {
423 if ( isset( $info['ParserTestFiles'] ) ) {
424 foreach ( $info['ParserTestFiles'] as $path ) {
425 $this->globals['wgParserTestFiles'][] = "$dir/$path";
426 }
427 }
428 }
429
430 /**
431 * @param string $path
432 * @param string $name
433 * @param array $value
434 * @param array &$array
435 * @throws InvalidArgumentException
436 */
437 protected function storeToArray( $path, $name, $value, &$array ) {
438 if ( !is_array( $value ) ) {
439 throw new InvalidArgumentException( "The value for '$name' should be an array (from $path)" );
440 }
441 if ( isset( $array[$name] ) ) {
442 $array[$name] = array_merge_recursive( $array[$name], $value );
443 } else {
444 $array[$name] = $value;
445 }
446 }
447
448 public function getExtraAutoloaderPaths( $dir, array $info ) {
449 $paths = [];
450 if ( isset( $info['load_composer_autoloader'] ) && $info['load_composer_autoloader'] === true ) {
451 $path = "$dir/vendor/autoload.php";
452 if ( file_exists( $path ) ) {
453 $paths[] = $path;
454 }
455 }
456 return $paths;
457 }
458 }