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