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