Do not allow setting deprecated $wgSpecialPageGroups over extension.json
[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 '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',
47 );
48
49 /**
50 * Keys that are part of the extension credits
51 *
52 * @var array
53 */
54 protected static $creditsAttributes = array(
55 'name',
56 'namemsg',
57 'author',
58 'version',
59 'url',
60 'description',
61 'descriptionmsg',
62 'license-name',
63 );
64
65 /**
66 * Stuff that is going to be set to $GLOBALS
67 *
68 * Some keys are pre-set to arrays so we can += to them
69 *
70 * @var array
71 */
72 protected $globals = array(
73 'wgExtensionMessagesFiles' => array(),
74 'wgMessagesDirs' => array(),
75 );
76
77 /**
78 * Things that should be define()'d
79 *
80 * @var array
81 */
82 protected $defines = array();
83
84 /**
85 * Things to be called once registration of these extensions are done
86 *
87 * @var callable[]
88 */
89 protected $callbacks = array();
90
91 /**
92 * @var array
93 */
94 protected $credits = array();
95
96 /**
97 * Any thing else in the $info that hasn't
98 * already been processed
99 *
100 * @var array
101 */
102 protected $attributes = array();
103
104 /**
105 * List of keys that have already been processed
106 *
107 * @var array
108 */
109 protected $processed = array();
110
111 /**
112 * @param string $path
113 * @param array $info
114 * @return array
115 */
116 public function extractInfo( $path, array $info ) {
117 $this->extractConfig( $info );
118 $this->extractHooks( $info );
119 $dir = dirname( $path );
120 $this->extractExtensionMessagesFiles( $dir, $info );
121 $this->extractMessagesDirs( $dir, $info );
122 $this->extractNamespaces( $info );
123 $this->extractResourceLoaderModules( $dir, $info );
124 $this->extractParserTestFiles( $dir, $info );
125 if ( isset( $info['callback'] ) ) {
126 $this->callbacks[] = $info['callback'];
127 $this->processed[] = 'callback';
128 }
129
130 $this->extractCredits( $path, $info );
131 foreach ( $info as $key => $val ) {
132 if ( in_array( $key, self::$globalSettings ) ) {
133 $this->storeToArray( "wg$key", $val, $this->globals );
134 // Ignore anything that starts with a @
135 } elseif ( $key[0] !== '@' && !in_array( $key, $this->processed ) ) {
136 $this->storeToArray( $key, $val, $this->attributes );
137 }
138 }
139
140 }
141
142 public function getExtractedInfo() {
143 return array(
144 'globals' => $this->globals,
145 'defines' => $this->defines,
146 'callbacks' => $this->callbacks,
147 'credits' => $this->credits,
148 'attributes' => $this->attributes,
149 );
150 }
151
152 protected function extractHooks( array $info ) {
153 if ( isset( $info['Hooks'] ) ) {
154 foreach ( $info['Hooks'] as $name => $value ) {
155 foreach ( (array)$value as $callback ) {
156 $this->globals['wgHooks'][$name][] = $callback;
157 }
158 }
159 $this->processed[] = 'Hooks';
160 }
161 }
162
163 /**
164 * Register namespaces with the appropriate global settings
165 *
166 * @param array $info
167 */
168 protected function extractNamespaces( array $info ) {
169 if ( isset( $info['namespaces'] ) ) {
170 foreach ( $info['namespaces'] as $ns ) {
171 $id = $ns['id'];
172 $this->defines[$ns['constant']] = $id;
173 $this->globals['wgExtraNamespaces'][$id] = $ns['name'];
174 if ( isset( $ns['gender'] ) ) {
175 $this->globals['wgExtraGenderNamespaces'][$id] = $ns['gender'];
176 }
177 if ( isset( $ns['subpages'] ) && $ns['subpages'] ) {
178 $this->globals['wgNamespacesWithSubpages'][$id] = true;
179 }
180 if ( isset( $ns['content'] ) && $ns['content'] ) {
181 $this->globals['wgContentNamespaces'][] = $id;
182 }
183 if ( isset( $ns['defaultcontentmodel'] ) ) {
184 $this->globals['wgNamespaceContentModels'][$id] = $ns['defaultcontentmodel'];
185 }
186 }
187 $this->processed[] = 'namespaces';
188 }
189 }
190
191 protected function extractResourceLoaderModules( $dir, array $info ) {
192 $defaultPaths = isset( $info['ResourceFileModulePaths'] )
193 ? $info['ResourceFileModulePaths']
194 : false;
195 if ( isset( $defaultPaths['localBasePath'] ) ) {
196 $defaultPaths['localBasePath'] = "$dir/{$defaultPaths['localBasePath']}";
197 }
198
199 foreach ( array( 'ResourceModules', 'ResourceModuleSkinStyles' ) as $setting ) {
200 if ( isset( $info[$setting] ) ) {
201 foreach ( $info[$setting] as $name => $data ) {
202 if ( isset( $data['localBasePath'] ) ) {
203 $data['localBasePath'] = "$dir/{$data['localBasePath']}";
204 }
205 if ( $defaultPaths ) {
206 $data += $defaultPaths;
207 }
208 $this->globals["wg$setting"][$name] = $data;
209 }
210 }
211 }
212 }
213
214 protected function extractExtensionMessagesFiles( $dir, array $info ) {
215 if ( isset( $info['ExtensionMessagesFiles'] ) ) {
216 $this->globals["wgExtensionMessagesFiles"] += array_map( function( $file ) use ( $dir ) {
217 return "$dir/$file";
218 }, $info['ExtensionMessagesFiles'] );
219 $this->processed[] = 'ExtensionMessagesFiles';
220 }
221 }
222
223 /**
224 * Set message-related settings, which need to be expanded to use
225 * absolute paths
226 *
227 * @param string $dir
228 * @param array $info
229 */
230 protected function extractMessagesDirs( $dir, array $info ) {
231 if ( isset( $info['MessagesDirs'] ) ) {
232 foreach ( $info['MessagesDirs'] as $name => $files ) {
233 foreach ( (array)$files as $file ) {
234 $this->globals["wgMessagesDirs"][$name][] = "$dir/$file";
235 }
236 }
237 $this->processed[] = 'MessagesDirs';
238 }
239 }
240
241 protected function extractCredits( $path, array $info ) {
242 $credits = array(
243 'path' => $path,
244 'type' => isset( $info['type'] ) ? $info['type'] : 'other',
245 );
246 $this->processed[] = 'type';
247 foreach ( self::$creditsAttributes as $attr ) {
248 if ( isset( $info[$attr] ) ) {
249 $credits[$attr] = $info[$attr];
250 $this->processed[] = $attr;
251 }
252 }
253
254 $this->credits[$credits['name']] = $credits;
255 }
256
257 /**
258 * Set configuration settings
259 * @todo In the future, this should be done via Config interfaces
260 *
261 * @param array $info
262 */
263 protected function extractConfig( array $info ) {
264 if ( isset( $info['config'] ) ) {
265 foreach ( $info['config'] as $key => $val ) {
266 if ( $key[0] !== '@' ) {
267 $this->globals["wg$key"] = $val;
268 }
269 }
270 $this->processed[] = 'config';
271 }
272 }
273
274 protected function extractParserTestFiles( $dir, array $info ) {
275 if ( isset( $info['ParserTestFiles'] ) ) {
276 foreach ( $info['ParserTestFiles'] as $path ) {
277 $this->globals['wgParserTestFiles'][] = "$dir/$path";
278 }
279 $this->processed[] = 'ParserTestFiles';
280 }
281 }
282
283 /**
284 * @param string $name
285 * @param mixed $value
286 * @param array &$array
287 */
288 protected function storeToArray( $name, $value, &$array ) {
289 if ( isset( $array[$name] ) ) {
290 $array[$name] = array_merge_recursive( $array[$name], $value );
291 } else {
292 $array[$name] = $value;
293 }
294 }
295 }