Import: Handle uploads with sha1 starting with 0 properly
[mediawiki.git] / includes / registration / ExtensionProcessor.php
blob13c450a4157532b02f50682e1154514ec301d77b
1 <?php
3 class ExtensionProcessor implements Processor {
5 /**
6 * Keys that should be set to $GLOBALS
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 'RateLimits',
27 'RecentChangesFlags',
28 'MediaHandlers',
29 'ExtensionFunctions',
30 'ExtensionEntryPointListFiles',
31 'SpecialPages',
32 'JobClasses',
33 'LogTypes',
34 'LogRestrictions',
35 'FilterLogTypes',
36 'LogNames',
37 'LogHeaders',
38 'LogActions',
39 'LogActionsHandlers',
40 'Actions',
41 'APIModules',
42 'APIFormatModules',
43 'APIMetaModules',
44 'APIPropModules',
45 'APIListModules',
46 'ValidSkinNames',
49 /**
50 * Mapping of global settings to their specific merge strategies.
52 * @see ExtensionRegistry::exportExtractedData
53 * @see getExtractedInfo
54 * @var array
56 protected static $mergeStrategies = array(
57 'wgGroupPermissions' => 'array_plus_2d',
58 'wgRevokePermissions' => 'array_plus_2d',
59 'wgHooks' => 'array_merge_recursive',
60 'wgExtensionCredits' => 'array_merge_recursive',
61 'wgExtraGenderNamespaces' => 'array_plus',
62 'wgNamespacesWithSubpages' => 'array_plus',
63 'wgNamespaceContentModels' => 'array_plus',
64 'wgNamespaceProtection' => 'array_plus',
65 'wgCapitalLinkOverrides' => 'array_plus',
68 /**
69 * Keys that are part of the extension credits
71 * @var array
73 protected static $creditsAttributes = array(
74 'name',
75 'namemsg',
76 'author',
77 'version',
78 'url',
79 'description',
80 'descriptionmsg',
81 'license-name',
84 /**
85 * Things that are not 'attributes', but are not in
86 * $globalSettings or $creditsAttributes.
88 * @var array
90 protected static $notAttributes = array(
91 'callback',
92 'Hooks',
93 'namespaces',
94 'ResourceFileModulePaths',
95 'ResourceModules',
96 'ResourceModuleSkinStyles',
97 'ExtensionMessagesFiles',
98 'MessagesDirs',
99 'type',
100 'config',
101 'ParserTestFiles',
102 'AutoloadClasses',
103 'manifest_version',
104 'load_composer_autoloader',
108 * Stuff that is going to be set to $GLOBALS
110 * Some keys are pre-set to arrays so we can += to them
112 * @var array
114 protected $globals = array(
115 'wgExtensionMessagesFiles' => array(),
116 'wgMessagesDirs' => array(),
120 * Things that should be define()'d
122 * @var array
124 protected $defines = array();
127 * Things to be called once registration of these extensions are done
129 * @var callable[]
131 protected $callbacks = array();
134 * @var array
136 protected $credits = array();
139 * Any thing else in the $info that hasn't
140 * already been processed
142 * @var array
144 protected $attributes = array();
147 * @param string $path
148 * @param array $info
149 * @param int $version manifest_version for info
150 * @return array
152 public function extractInfo( $path, array $info, $version ) {
153 $this->extractConfig( $info );
154 $this->extractHooks( $info );
155 $dir = dirname( $path );
156 $this->extractExtensionMessagesFiles( $dir, $info );
157 $this->extractMessagesDirs( $dir, $info );
158 $this->extractNamespaces( $info );
159 $this->extractResourceLoaderModules( $dir, $info );
160 $this->extractParserTestFiles( $dir, $info );
161 if ( isset( $info['callback'] ) ) {
162 $this->callbacks[] = $info['callback'];
165 $this->extractCredits( $path, $info );
166 foreach ( $info as $key => $val ) {
167 if ( in_array( $key, self::$globalSettings ) ) {
168 $this->storeToArray( "wg$key", $val, $this->globals );
169 // Ignore anything that starts with a @
170 } elseif ( $key[0] !== '@' && !in_array( $key, self::$notAttributes )
171 && !in_array( $key, self::$creditsAttributes )
173 $this->storeToArray( $key, $val, $this->attributes );
178 public function getExtractedInfo() {
179 // Make sure the merge strategies are set
180 foreach ( $this->globals as $key => $val ) {
181 if ( isset( self::$mergeStrategies[$key] ) ) {
182 $this->globals[$key][ExtensionRegistry::MERGE_STRATEGY] = self::$mergeStrategies[$key];
186 return array(
187 'globals' => $this->globals,
188 'defines' => $this->defines,
189 'callbacks' => $this->callbacks,
190 'credits' => $this->credits,
191 'attributes' => $this->attributes,
195 public function getRequirements( array $info ) {
196 $requirements = array();
197 $key = ExtensionRegistry::MEDIAWIKI_CORE;
198 if ( isset( $info['requires'][$key] ) ) {
199 $requirements[$key] = $info['requires'][$key];
202 return $requirements;
205 protected function extractHooks( array $info ) {
206 if ( isset( $info['Hooks'] ) ) {
207 foreach ( $info['Hooks'] as $name => $value ) {
208 foreach ( (array)$value as $callback ) {
209 $this->globals['wgHooks'][$name][] = $callback;
216 * Register namespaces with the appropriate global settings
218 * @param array $info
220 protected function extractNamespaces( array $info ) {
221 if ( isset( $info['namespaces'] ) ) {
222 foreach ( $info['namespaces'] as $ns ) {
223 $id = $ns['id'];
224 $this->defines[$ns['constant']] = $id;
225 $this->attributes['ExtensionNamespaces'][$id] = $ns['name'];
226 if ( isset( $ns['gender'] ) ) {
227 $this->globals['wgExtraGenderNamespaces'][$id] = $ns['gender'];
229 if ( isset( $ns['subpages'] ) && $ns['subpages'] ) {
230 $this->globals['wgNamespacesWithSubpages'][$id] = true;
232 if ( isset( $ns['content'] ) && $ns['content'] ) {
233 $this->globals['wgContentNamespaces'][] = $id;
235 if ( isset( $ns['defaultcontentmodel'] ) ) {
236 $this->globals['wgNamespaceContentModels'][$id] = $ns['defaultcontentmodel'];
238 if ( isset( $ns['protection'] ) ) {
239 $this->globals['wgNamespaceProtection'][$id] = $ns['protection'];
241 if ( isset( $ns['capitallinkoverride'] ) ) {
242 $this->globals['wgCapitalLinkOverrides'][$id] = $ns['capitallinkoverride'];
248 protected function extractResourceLoaderModules( $dir, array $info ) {
249 $defaultPaths = isset( $info['ResourceFileModulePaths'] )
250 ? $info['ResourceFileModulePaths']
251 : false;
252 if ( isset( $defaultPaths['localBasePath'] ) ) {
253 $defaultPaths['localBasePath'] = "$dir/{$defaultPaths['localBasePath']}";
256 foreach ( array( 'ResourceModules', 'ResourceModuleSkinStyles' ) as $setting ) {
257 if ( isset( $info[$setting] ) ) {
258 foreach ( $info[$setting] as $name => $data ) {
259 if ( isset( $data['localBasePath'] ) ) {
260 $data['localBasePath'] = "$dir/{$data['localBasePath']}";
262 if ( $defaultPaths ) {
263 $data += $defaultPaths;
265 $this->globals["wg$setting"][$name] = $data;
271 protected function extractExtensionMessagesFiles( $dir, array $info ) {
272 if ( isset( $info['ExtensionMessagesFiles'] ) ) {
273 $this->globals["wgExtensionMessagesFiles"] += array_map( function( $file ) use ( $dir ) {
274 return "$dir/$file";
275 }, $info['ExtensionMessagesFiles'] );
280 * Set message-related settings, which need to be expanded to use
281 * absolute paths
283 * @param string $dir
284 * @param array $info
286 protected function extractMessagesDirs( $dir, array $info ) {
287 if ( isset( $info['MessagesDirs'] ) ) {
288 foreach ( $info['MessagesDirs'] as $name => $files ) {
289 foreach ( (array)$files as $file ) {
290 $this->globals["wgMessagesDirs"][$name][] = "$dir/$file";
297 * @param string $path
298 * @param array $info
299 * @throws Exception
301 protected function extractCredits( $path, array $info ) {
302 $credits = array(
303 'path' => $path,
304 'type' => isset( $info['type'] ) ? $info['type'] : 'other',
306 foreach ( self::$creditsAttributes as $attr ) {
307 if ( isset( $info[$attr] ) ) {
308 $credits[$attr] = $info[$attr];
312 $name = $credits['name'];
314 // If someone is loading the same thing twice, throw
315 // a nice error (T121493)
316 if ( isset( $this->credits[$name] ) ) {
317 $firstPath = $this->credits[$name]['path'];
318 $secondPath = $credits['path'];
319 throw new Exception( "It was attempted to load $name twice, from $firstPath and $secondPath." );
322 $this->credits[$name] = $credits;
323 $this->globals['wgExtensionCredits'][$credits['type']][] = $credits;
327 * Set configuration settings
328 * @todo In the future, this should be done via Config interfaces
330 * @param array $info
332 protected function extractConfig( array $info ) {
333 if ( isset( $info['config'] ) ) {
334 if ( isset( $info['config']['_prefix'] ) ) {
335 $prefix = $info['config']['_prefix'];
336 unset( $info['config']['_prefix'] );
337 } else {
338 $prefix = 'wg';
340 foreach ( $info['config'] as $key => $val ) {
341 if ( $key[0] !== '@' ) {
342 $this->globals["$prefix$key"] = $val;
348 protected function extractParserTestFiles( $dir, array $info ) {
349 if ( isset( $info['ParserTestFiles'] ) ) {
350 foreach ( $info['ParserTestFiles'] as $path ) {
351 $this->globals['wgParserTestFiles'][] = "$dir/$path";
357 * @param string $name
358 * @param array $value
359 * @param array &$array
360 * @throws InvalidArgumentException
362 protected function storeToArray( $name, $value, &$array ) {
363 if ( !is_array( $value ) ) {
364 throw new InvalidArgumentException( "The value for '$name' should be an array" );
366 if ( isset( $array[$name] ) ) {
367 $array[$name] = array_merge_recursive( $array[$name], $value );
368 } else {
369 $array[$name] = $value;
373 public function getExtraAutoloaderPaths( $dir, array $info ) {
374 $paths = array();
375 if ( isset( $info['load_composer_autoloader'] ) && $info['load_composer_autoloader'] === true ) {
376 $path = "$dir/vendor/autoload.php";
377 if ( file_exists( $path ) ) {
378 $paths[] = $path;
381 return $paths;