Merge "Remove un-needed cast for array key in WatchedItemStore"
[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 foreach ( (array)$value as $callback ) {
213 $this->globals['wgHooks'][$name][] = $callback;
214 }
215 }
216 }
217 }
218
219 /**
220 * Register namespaces with the appropriate global settings
221 *
222 * @param array $info
223 */
224 protected function extractNamespaces( array $info ) {
225 if ( isset( $info['namespaces'] ) ) {
226 foreach ( $info['namespaces'] as $ns ) {
227 $id = $ns['id'];
228 $this->defines[$ns['constant']] = $id;
229 $this->attributes['ExtensionNamespaces'][$id] = $ns['name'];
230 if ( isset( $ns['gender'] ) ) {
231 $this->globals['wgExtraGenderNamespaces'][$id] = $ns['gender'];
232 }
233 if ( isset( $ns['subpages'] ) && $ns['subpages'] ) {
234 $this->globals['wgNamespacesWithSubpages'][$id] = true;
235 }
236 if ( isset( $ns['content'] ) && $ns['content'] ) {
237 $this->globals['wgContentNamespaces'][] = $id;
238 }
239 if ( isset( $ns['defaultcontentmodel'] ) ) {
240 $this->globals['wgNamespaceContentModels'][$id] = $ns['defaultcontentmodel'];
241 }
242 if ( isset( $ns['protection'] ) ) {
243 $this->globals['wgNamespaceProtection'][$id] = $ns['protection'];
244 }
245 if ( isset( $ns['capitallinkoverride'] ) ) {
246 $this->globals['wgCapitalLinkOverrides'][$id] = $ns['capitallinkoverride'];
247 }
248 }
249 }
250 }
251
252 protected function extractResourceLoaderModules( $dir, array $info ) {
253 $defaultPaths = isset( $info['ResourceFileModulePaths'] )
254 ? $info['ResourceFileModulePaths']
255 : false;
256 if ( isset( $defaultPaths['localBasePath'] ) ) {
257 if ( $defaultPaths['localBasePath'] === '' ) {
258 // Avoid double slashes (e.g. /extensions/Example//path)
259 $defaultPaths['localBasePath'] = $dir;
260 } else {
261 $defaultPaths['localBasePath'] = "$dir/{$defaultPaths['localBasePath']}";
262 }
263 }
264
265 foreach ( [ 'ResourceModules', 'ResourceModuleSkinStyles' ] as $setting ) {
266 if ( isset( $info[$setting] ) ) {
267 foreach ( $info[$setting] as $name => $data ) {
268 if ( isset( $data['localBasePath'] ) ) {
269 if ( $data['localBasePath'] === '' ) {
270 // Avoid double slashes (e.g. /extensions/Example//path)
271 $data['localBasePath'] = $dir;
272 } else {
273 $data['localBasePath'] = "$dir/{$data['localBasePath']}";
274 }
275 }
276 if ( $defaultPaths ) {
277 $data += $defaultPaths;
278 }
279 $this->globals["wg$setting"][$name] = $data;
280 }
281 }
282 }
283 }
284
285 protected function extractExtensionMessagesFiles( $dir, array $info ) {
286 if ( isset( $info['ExtensionMessagesFiles'] ) ) {
287 $this->globals["wgExtensionMessagesFiles"] += array_map( function( $file ) use ( $dir ) {
288 return "$dir/$file";
289 }, $info['ExtensionMessagesFiles'] );
290 }
291 }
292
293 /**
294 * Set message-related settings, which need to be expanded to use
295 * absolute paths
296 *
297 * @param string $dir
298 * @param array $info
299 */
300 protected function extractMessagesDirs( $dir, array $info ) {
301 if ( isset( $info['MessagesDirs'] ) ) {
302 foreach ( $info['MessagesDirs'] as $name => $files ) {
303 foreach ( (array)$files as $file ) {
304 $this->globals["wgMessagesDirs"][$name][] = "$dir/$file";
305 }
306 }
307 }
308 }
309
310 /**
311 * @param string $path
312 * @param array $info
313 * @throws Exception
314 */
315 protected function extractCredits( $path, array $info ) {
316 $credits = [
317 'path' => $path,
318 'type' => isset( $info['type'] ) ? $info['type'] : 'other',
319 ];
320 foreach ( self::$creditsAttributes as $attr ) {
321 if ( isset( $info[$attr] ) ) {
322 $credits[$attr] = $info[$attr];
323 }
324 }
325
326 $name = $credits['name'];
327
328 // If someone is loading the same thing twice, throw
329 // a nice error (T121493)
330 if ( isset( $this->credits[$name] ) ) {
331 $firstPath = $this->credits[$name]['path'];
332 $secondPath = $credits['path'];
333 throw new Exception( "It was attempted to load $name twice, from $firstPath and $secondPath." );
334 }
335
336 $this->credits[$name] = $credits;
337 $this->globals['wgExtensionCredits'][$credits['type']][] = $credits;
338 }
339
340 /**
341 * Set configuration settings
342 * @todo In the future, this should be done via Config interfaces
343 *
344 * @param array $info
345 */
346 protected function extractConfig( array $info ) {
347 if ( isset( $info['config'] ) ) {
348 if ( isset( $info['config']['_prefix'] ) ) {
349 $prefix = $info['config']['_prefix'];
350 unset( $info['config']['_prefix'] );
351 } else {
352 $prefix = 'wg';
353 }
354 foreach ( $info['config'] as $key => $val ) {
355 if ( $key[0] !== '@' ) {
356 $this->globals["$prefix$key"] = $val;
357 }
358 }
359 }
360 }
361
362 protected function extractParserTestFiles( $dir, array $info ) {
363 if ( isset( $info['ParserTestFiles'] ) ) {
364 foreach ( $info['ParserTestFiles'] as $path ) {
365 $this->globals['wgParserTestFiles'][] = "$dir/$path";
366 }
367 }
368 }
369
370 /**
371 * @param string $path
372 * @param string $name
373 * @param array $value
374 * @param array &$array
375 * @throws InvalidArgumentException
376 */
377 protected function storeToArray( $path, $name, $value, &$array ) {
378 if ( !is_array( $value ) ) {
379 throw new InvalidArgumentException( "The value for '$name' should be an array (from $path)" );
380 }
381 if ( isset( $array[$name] ) ) {
382 $array[$name] = array_merge_recursive( $array[$name], $value );
383 } else {
384 $array[$name] = $value;
385 }
386 }
387
388 public function getExtraAutoloaderPaths( $dir, array $info ) {
389 $paths = [];
390 if ( isset( $info['load_composer_autoloader'] ) && $info['load_composer_autoloader'] === true ) {
391 $path = "$dir/vendor/autoload.php";
392 if ( file_exists( $path ) ) {
393 $paths[] = $path;
394 }
395 }
396 return $paths;
397 }
398 }