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