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