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