Merge "Import BatchRowUpdate classes from Echo"
[mediawiki.git] / includes / registration / ExtensionProcessor.php
blob68e5a17796f268ebcfdf6e7ddf2d14ec6b2395a7
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 // credits are handled in the ExtensionRegistry
61 //'wgExtensionCredits' => 'array_merge_recursive',
62 'wgExtraNamespaces' => 'array_plus',
63 'wgExtraGenderNamespaces' => 'array_plus',
64 'wgNamespacesWithSubpages' => 'array_plus',
65 'wgNamespaceContentModels' => 'array_plus',
66 'wgNamespaceProtection' => 'array_plus',
67 'wgCapitalLinkOverrides' => 'array_plus',
70 /**
71 * Keys that are part of the extension credits
73 * @var array
75 protected static $creditsAttributes = array(
76 'name',
77 'namemsg',
78 'author',
79 'version',
80 'url',
81 'description',
82 'descriptionmsg',
83 'license-name',
86 /**
87 * Things that are not 'attributes', but are not in
88 * $globalSettings or $creditsAttributes.
90 * @var array
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',
109 * Stuff that is going to be set to $GLOBALS
111 * Some keys are pre-set to arrays so we can += to them
113 * @var array
115 protected $globals = array(
116 'wgExtensionMessagesFiles' => array(),
117 'wgMessagesDirs' => array(),
121 * Things that should be define()'d
123 * @var array
125 protected $defines = array();
128 * Things to be called once registration of these extensions are done
130 * @var callable[]
132 protected $callbacks = array();
135 * @var array
137 protected $credits = array();
140 * Any thing else in the $info that hasn't
141 * already been processed
143 * @var array
145 protected $attributes = array();
148 * @param string $path
149 * @param array $info
150 * @param int $version manifest_version for info
151 * @return array
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'];
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 )
174 $this->storeToArray( $key, $val, $this->attributes );
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];
187 return array(
188 'globals' => $this->globals,
189 'defines' => $this->defines,
190 'callbacks' => $this->callbacks,
191 'credits' => $this->credits,
192 'attributes' => $this->attributes,
196 protected function extractHooks( array $info ) {
197 if ( isset( $info['Hooks'] ) ) {
198 foreach ( $info['Hooks'] as $name => $value ) {
199 foreach ( (array)$value as $callback ) {
200 $this->globals['wgHooks'][$name][] = $callback;
207 * Register namespaces with the appropriate global settings
209 * @param array $info
211 protected function extractNamespaces( array $info ) {
212 if ( isset( $info['namespaces'] ) ) {
213 foreach ( $info['namespaces'] as $ns ) {
214 $id = $ns['id'];
215 $this->defines[$ns['constant']] = $id;
216 $this->globals['wgExtraNamespaces'][$id] = $ns['name'];
217 if ( isset( $ns['gender'] ) ) {
218 $this->globals['wgExtraGenderNamespaces'][$id] = $ns['gender'];
220 if ( isset( $ns['subpages'] ) && $ns['subpages'] ) {
221 $this->globals['wgNamespacesWithSubpages'][$id] = true;
223 if ( isset( $ns['content'] ) && $ns['content'] ) {
224 $this->globals['wgContentNamespaces'][] = $id;
226 if ( isset( $ns['defaultcontentmodel'] ) ) {
227 $this->globals['wgNamespaceContentModels'][$id] = $ns['defaultcontentmodel'];
229 if ( isset( $ns['protection'] ) ) {
230 $this->globals['wgNamespaceProtection'][$id] = $ns['protection'];
232 if ( isset( $ns['capitallinkoverride'] ) ) {
233 $this->globals['wgCapitalLinkOverrides'][$id] = $ns['capitallinkoverride'];
239 protected function extractResourceLoaderModules( $dir, array $info ) {
240 $defaultPaths = isset( $info['ResourceFileModulePaths'] )
241 ? $info['ResourceFileModulePaths']
242 : false;
243 if ( isset( $defaultPaths['localBasePath'] ) ) {
244 $defaultPaths['localBasePath'] = "$dir/{$defaultPaths['localBasePath']}";
247 foreach ( array( 'ResourceModules', 'ResourceModuleSkinStyles' ) as $setting ) {
248 if ( isset( $info[$setting] ) ) {
249 foreach ( $info[$setting] as $name => $data ) {
250 if ( isset( $data['localBasePath'] ) ) {
251 $data['localBasePath'] = "$dir/{$data['localBasePath']}";
253 if ( $defaultPaths ) {
254 $data += $defaultPaths;
256 $this->globals["wg$setting"][$name] = $data;
262 protected function extractExtensionMessagesFiles( $dir, array $info ) {
263 if ( isset( $info['ExtensionMessagesFiles'] ) ) {
264 $this->globals["wgExtensionMessagesFiles"] += array_map( function( $file ) use ( $dir ) {
265 return "$dir/$file";
266 }, $info['ExtensionMessagesFiles'] );
271 * Set message-related settings, which need to be expanded to use
272 * absolute paths
274 * @param string $dir
275 * @param array $info
277 protected function extractMessagesDirs( $dir, array $info ) {
278 if ( isset( $info['MessagesDirs'] ) ) {
279 foreach ( $info['MessagesDirs'] as $name => $files ) {
280 foreach ( (array)$files as $file ) {
281 $this->globals["wgMessagesDirs"][$name][] = "$dir/$file";
287 protected function extractCredits( $path, array $info ) {
288 $credits = array(
289 'path' => $path,
290 'type' => isset( $info['type'] ) ? $info['type'] : 'other',
292 foreach ( self::$creditsAttributes as $attr ) {
293 if ( isset( $info[$attr] ) ) {
294 $credits[$attr] = $info[$attr];
298 $this->credits[$credits['name']] = $credits;
302 * Set configuration settings
303 * @todo In the future, this should be done via Config interfaces
305 * @param array $info
307 protected function extractConfig( array $info ) {
308 if ( isset( $info['config'] ) ) {
309 foreach ( $info['config'] as $key => $val ) {
310 if ( $key[0] !== '@' ) {
311 $this->globals["wg$key"] = $val;
317 protected function extractParserTestFiles( $dir, array $info ) {
318 if ( isset( $info['ParserTestFiles'] ) ) {
319 foreach ( $info['ParserTestFiles'] as $path ) {
320 $this->globals['wgParserTestFiles'][] = "$dir/$path";
326 * @param string $name
327 * @param array $value
328 * @param array &$array
329 * @throws InvalidArgumentException
331 protected function storeToArray( $name, $value, &$array ) {
332 if ( !is_array( $value ) ) {
333 throw new InvalidArgumentException( "The value for '$name' should be an array" );
335 if ( isset( $array[$name] ) ) {
336 $array[$name] = array_merge_recursive( $array[$name], $value );
337 } else {
338 $array[$name] = $value;