Merge "Add tests for WikiMap and WikiReference"
[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 * Mapping of global settings to their specific merge strategies.
51 *
52 * @see ExtensionRegistry::exportExtractedData
53 * @see getExtractedInfo
54 * @var array
55 */
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 '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 );
106
107 /**
108 * Stuff that is going to be set to $GLOBALS
109 *
110 * Some keys are pre-set to arrays so we can += to them
111 *
112 * @var array
113 */
114 protected $globals = array(
115 'wgExtensionMessagesFiles' => array(),
116 'wgMessagesDirs' => array(),
117 );
118
119 /**
120 * Things that should be define()'d
121 *
122 * @var array
123 */
124 protected $defines = array();
125
126 /**
127 * Things to be called once registration of these extensions are done
128 *
129 * @var callable[]
130 */
131 protected $callbacks = array();
132
133 /**
134 * @var array
135 */
136 protected $credits = array();
137
138 /**
139 * Any thing else in the $info that hasn't
140 * already been processed
141 *
142 * @var array
143 */
144 protected $attributes = array();
145
146 /**
147 * @param string $path
148 * @param array $info
149 * @param int $version manifest_version for info
150 * @return array
151 */
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'];
163 }
164
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 )
172 ) {
173 $this->storeToArray( $key, $val, $this->attributes );
174 }
175 }
176 }
177
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];
183 }
184 }
185
186 return array(
187 'globals' => $this->globals,
188 'defines' => $this->defines,
189 'callbacks' => $this->callbacks,
190 'credits' => $this->credits,
191 'attributes' => $this->attributes,
192 );
193 }
194
195 protected function extractHooks( array $info ) {
196 if ( isset( $info['Hooks'] ) ) {
197 foreach ( $info['Hooks'] as $name => $value ) {
198 foreach ( (array)$value as $callback ) {
199 $this->globals['wgHooks'][$name][] = $callback;
200 }
201 }
202 }
203 }
204
205 /**
206 * Register namespaces with the appropriate global settings
207 *
208 * @param array $info
209 */
210 protected function extractNamespaces( array $info ) {
211 if ( isset( $info['namespaces'] ) ) {
212 foreach ( $info['namespaces'] as $ns ) {
213 $id = $ns['id'];
214 $this->defines[$ns['constant']] = $id;
215 $this->attributes['ExtensionNamespaces'][$id] = $ns['name'];
216 if ( isset( $ns['gender'] ) ) {
217 $this->globals['wgExtraGenderNamespaces'][$id] = $ns['gender'];
218 }
219 if ( isset( $ns['subpages'] ) && $ns['subpages'] ) {
220 $this->globals['wgNamespacesWithSubpages'][$id] = true;
221 }
222 if ( isset( $ns['content'] ) && $ns['content'] ) {
223 $this->globals['wgContentNamespaces'][] = $id;
224 }
225 if ( isset( $ns['defaultcontentmodel'] ) ) {
226 $this->globals['wgNamespaceContentModels'][$id] = $ns['defaultcontentmodel'];
227 }
228 if ( isset( $ns['protection'] ) ) {
229 $this->globals['wgNamespaceProtection'][$id] = $ns['protection'];
230 }
231 if ( isset( $ns['capitallinkoverride'] ) ) {
232 $this->globals['wgCapitalLinkOverrides'][$id] = $ns['capitallinkoverride'];
233 }
234 }
235 }
236 }
237
238 protected function extractResourceLoaderModules( $dir, array $info ) {
239 $defaultPaths = isset( $info['ResourceFileModulePaths'] )
240 ? $info['ResourceFileModulePaths']
241 : false;
242 if ( isset( $defaultPaths['localBasePath'] ) ) {
243 $defaultPaths['localBasePath'] = "$dir/{$defaultPaths['localBasePath']}";
244 }
245
246 foreach ( array( 'ResourceModules', 'ResourceModuleSkinStyles' ) as $setting ) {
247 if ( isset( $info[$setting] ) ) {
248 foreach ( $info[$setting] as $name => $data ) {
249 if ( isset( $data['localBasePath'] ) ) {
250 $data['localBasePath'] = "$dir/{$data['localBasePath']}";
251 }
252 if ( $defaultPaths ) {
253 $data += $defaultPaths;
254 }
255 $this->globals["wg$setting"][$name] = $data;
256 }
257 }
258 }
259 }
260
261 protected function extractExtensionMessagesFiles( $dir, array $info ) {
262 if ( isset( $info['ExtensionMessagesFiles'] ) ) {
263 $this->globals["wgExtensionMessagesFiles"] += array_map( function( $file ) use ( $dir ) {
264 return "$dir/$file";
265 }, $info['ExtensionMessagesFiles'] );
266 }
267 }
268
269 /**
270 * Set message-related settings, which need to be expanded to use
271 * absolute paths
272 *
273 * @param string $dir
274 * @param array $info
275 */
276 protected function extractMessagesDirs( $dir, array $info ) {
277 if ( isset( $info['MessagesDirs'] ) ) {
278 foreach ( $info['MessagesDirs'] as $name => $files ) {
279 foreach ( (array)$files as $file ) {
280 $this->globals["wgMessagesDirs"][$name][] = "$dir/$file";
281 }
282 }
283 }
284 }
285
286 protected function extractCredits( $path, array $info ) {
287 $credits = array(
288 'path' => $path,
289 'type' => isset( $info['type'] ) ? $info['type'] : 'other',
290 );
291 foreach ( self::$creditsAttributes as $attr ) {
292 if ( isset( $info[$attr] ) ) {
293 $credits[$attr] = $info[$attr];
294 }
295 }
296
297 $this->credits[$credits['name']] = $credits;
298 }
299
300 /**
301 * Set configuration settings
302 * @todo In the future, this should be done via Config interfaces
303 *
304 * @param array $info
305 */
306 protected function extractConfig( array $info ) {
307 if ( isset( $info['config'] ) ) {
308 if ( isset( $info['config']['_prefix'] ) ) {
309 $prefix = $info['config']['_prefix'];
310 unset( $info['config']['_prefix'] );
311 } else {
312 $prefix = 'wg';
313 }
314 foreach ( $info['config'] as $key => $val ) {
315 if ( $key[0] !== '@' ) {
316 $this->globals["$prefix$key"] = $val;
317 }
318 }
319 }
320 }
321
322 protected function extractParserTestFiles( $dir, array $info ) {
323 if ( isset( $info['ParserTestFiles'] ) ) {
324 foreach ( $info['ParserTestFiles'] as $path ) {
325 $this->globals['wgParserTestFiles'][] = "$dir/$path";
326 }
327 }
328 }
329
330 /**
331 * @param string $name
332 * @param array $value
333 * @param array &$array
334 * @throws InvalidArgumentException
335 */
336 protected function storeToArray( $name, $value, &$array ) {
337 if ( !is_array( $value ) ) {
338 throw new InvalidArgumentException( "The value for '$name' should be an array" );
339 }
340 if ( isset( $array[$name] ) ) {
341 $array[$name] = array_merge_recursive( $array[$name], $value );
342 } else {
343 $array[$name] = $value;
344 }
345 }
346 }