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