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