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