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