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