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