Merge "Use correct fields for LinkBatch on Special:NewPages"
[mediawiki.git] / includes / registration / ExtensionProcessor.php
blob273e9ef0bacb9ed73072e99dfdb5995693866a52
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 * Keys that are part of the extension credits
52 * @var array
54 protected static $creditsAttributes = array(
55 'name',
56 'namemsg',
57 'author',
58 'version',
59 'url',
60 'description',
61 'descriptionmsg',
62 'license-name',
65 /**
66 * Things that are not 'attributes', but are not in
67 * $globalSettings or $creditsAttributes.
69 * @var array
71 protected static $notAttributes = array(
72 'callback',
73 'Hooks',
74 'namespaces',
75 'ResourceFileModulePaths',
76 'ResourceModules',
77 'ResourceModuleSkinStyles',
78 'ExtensionMessagesFiles',
79 'MessagesDirs',
80 'type',
81 'config',
82 'ParserTestFiles',
83 'AutoloadClasses',
84 'manifest_version',
87 /**
88 * Stuff that is going to be set to $GLOBALS
90 * Some keys are pre-set to arrays so we can += to them
92 * @var array
94 protected $globals = array(
95 'wgExtensionMessagesFiles' => array(),
96 'wgMessagesDirs' => array(),
99 /**
100 * Things that should be define()'d
102 * @var array
104 protected $defines = array();
107 * Things to be called once registration of these extensions are done
109 * @var callable[]
111 protected $callbacks = array();
114 * @var array
116 protected $credits = array();
119 * Any thing else in the $info that hasn't
120 * already been processed
122 * @var array
124 protected $attributes = array();
127 * @param string $path
128 * @param array $info
129 * @param int $version manifest_version for info
130 * @return array
132 public function extractInfo( $path, array $info, $version ) {
133 $this->extractConfig( $info );
134 $this->extractHooks( $info );
135 $dir = dirname( $path );
136 $this->extractExtensionMessagesFiles( $dir, $info );
137 $this->extractMessagesDirs( $dir, $info );
138 $this->extractNamespaces( $info );
139 $this->extractResourceLoaderModules( $dir, $info );
140 $this->extractParserTestFiles( $dir, $info );
141 if ( isset( $info['callback'] ) ) {
142 $this->callbacks[] = $info['callback'];
145 $this->extractCredits( $path, $info );
146 foreach ( $info as $key => $val ) {
147 if ( in_array( $key, self::$globalSettings ) ) {
148 $this->storeToArray( "wg$key", $val, $this->globals );
149 // Ignore anything that starts with a @
150 } elseif ( $key[0] !== '@' && !in_array( $key, self::$notAttributes )
151 && !in_array( $key, self::$creditsAttributes )
153 $this->storeToArray( $key, $val, $this->attributes );
158 public function getExtractedInfo() {
159 return array(
160 'globals' => $this->globals,
161 'defines' => $this->defines,
162 'callbacks' => $this->callbacks,
163 'credits' => $this->credits,
164 'attributes' => $this->attributes,
168 protected function extractHooks( array $info ) {
169 if ( isset( $info['Hooks'] ) ) {
170 foreach ( $info['Hooks'] as $name => $value ) {
171 foreach ( (array)$value as $callback ) {
172 $this->globals['wgHooks'][$name][] = $callback;
179 * Register namespaces with the appropriate global settings
181 * @param array $info
183 protected function extractNamespaces( array $info ) {
184 if ( isset( $info['namespaces'] ) ) {
185 foreach ( $info['namespaces'] as $ns ) {
186 $id = $ns['id'];
187 $this->defines[$ns['constant']] = $id;
188 $this->globals['wgExtraNamespaces'][$id] = $ns['name'];
189 if ( isset( $ns['gender'] ) ) {
190 $this->globals['wgExtraGenderNamespaces'][$id] = $ns['gender'];
192 if ( isset( $ns['subpages'] ) && $ns['subpages'] ) {
193 $this->globals['wgNamespacesWithSubpages'][$id] = true;
195 if ( isset( $ns['content'] ) && $ns['content'] ) {
196 $this->globals['wgContentNamespaces'][] = $id;
198 if ( isset( $ns['defaultcontentmodel'] ) ) {
199 $this->globals['wgNamespaceContentModels'][$id] = $ns['defaultcontentmodel'];
205 protected function extractResourceLoaderModules( $dir, array $info ) {
206 $defaultPaths = isset( $info['ResourceFileModulePaths'] )
207 ? $info['ResourceFileModulePaths']
208 : false;
209 if ( isset( $defaultPaths['localBasePath'] ) ) {
210 $defaultPaths['localBasePath'] = "$dir/{$defaultPaths['localBasePath']}";
213 foreach ( array( 'ResourceModules', 'ResourceModuleSkinStyles' ) as $setting ) {
214 if ( isset( $info[$setting] ) ) {
215 foreach ( $info[$setting] as $name => $data ) {
216 if ( isset( $data['localBasePath'] ) ) {
217 $data['localBasePath'] = "$dir/{$data['localBasePath']}";
219 if ( $defaultPaths ) {
220 $data += $defaultPaths;
222 $this->globals["wg$setting"][$name] = $data;
228 protected function extractExtensionMessagesFiles( $dir, array $info ) {
229 if ( isset( $info['ExtensionMessagesFiles'] ) ) {
230 $this->globals["wgExtensionMessagesFiles"] += array_map( function( $file ) use ( $dir ) {
231 return "$dir/$file";
232 }, $info['ExtensionMessagesFiles'] );
237 * Set message-related settings, which need to be expanded to use
238 * absolute paths
240 * @param string $dir
241 * @param array $info
243 protected function extractMessagesDirs( $dir, array $info ) {
244 if ( isset( $info['MessagesDirs'] ) ) {
245 foreach ( $info['MessagesDirs'] as $name => $files ) {
246 foreach ( (array)$files as $file ) {
247 $this->globals["wgMessagesDirs"][$name][] = "$dir/$file";
253 protected function extractCredits( $path, array $info ) {
254 $credits = array(
255 'path' => $path,
256 'type' => isset( $info['type'] ) ? $info['type'] : 'other',
258 foreach ( self::$creditsAttributes as $attr ) {
259 if ( isset( $info[$attr] ) ) {
260 $credits[$attr] = $info[$attr];
264 $this->credits[$credits['name']] = $credits;
268 * Set configuration settings
269 * @todo In the future, this should be done via Config interfaces
271 * @param array $info
273 protected function extractConfig( array $info ) {
274 if ( isset( $info['config'] ) ) {
275 foreach ( $info['config'] as $key => $val ) {
276 if ( $key[0] !== '@' ) {
277 $this->globals["wg$key"] = $val;
283 protected function extractParserTestFiles( $dir, array $info ) {
284 if ( isset( $info['ParserTestFiles'] ) ) {
285 foreach ( $info['ParserTestFiles'] as $path ) {
286 $this->globals['wgParserTestFiles'][] = "$dir/$path";
292 * @param string $name
293 * @param array $value
294 * @param array &$array
295 * @throws InvalidArgumentException
297 protected function storeToArray( $name, $value, &$array ) {
298 if ( !is_array( $value ) ) {
299 throw new InvalidArgumentException( "The value for '$name' should be an array" );
301 if ( isset( $array[$name] ) ) {
302 $array[$name] = array_merge_recursive( $array[$name], $value );
303 } else {
304 $array[$name] = $value;