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