registration: Support ResourceModuleSkinStyles
[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 'ParserTestFiles',
28 'RecentChangesFlags',
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 '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 if ( isset( $info['callback'] ) ) {
125 $this->callbacks[] = $info['callback'];
126 $this->processed[] = 'callback';
127 }
128
129 $this->extractCredits( $path, $info );
130 foreach ( $info as $key => $val ) {
131 if ( in_array( $key, self::$globalSettings ) ) {
132 $this->storeToArray( "wg$key", $val, $this->globals );
133 // Ignore anything that starts with a @
134 } elseif ( $key[0] !== '@' && !in_array( $key, $this->processed ) ) {
135 $this->storeToArray( $key, $val, $this->attributes );
136 }
137 }
138
139 }
140
141 public function getExtractedInfo() {
142 return array(
143 'globals' => $this->globals,
144 'defines' => $this->defines,
145 'callbacks' => $this->callbacks,
146 'credits' => $this->credits,
147 'attributes' => $this->attributes,
148 );
149 }
150
151 protected function extractHooks( array $info ) {
152 if ( isset( $info['Hooks'] ) ) {
153 foreach ( $info['Hooks'] as $name => $callable ) {
154 $this->globals['wgHooks'][$name][] = $callable;
155 }
156 $this->processed[] = 'Hooks';
157 }
158 }
159
160 /**
161 * Register namespaces with the appropriate global settings
162 *
163 * @param array $info
164 */
165 protected function extractNamespaces( array $info ) {
166 if ( isset( $info['namespaces'] ) ) {
167 foreach ( $info['namespaces'] as $ns ) {
168 $id = $ns['id'];
169 $this->defines[$ns['constant']] = $id;
170 $this->globals['wgExtraNamespaces'][$id] = $ns['name'];
171 if ( isset( $ns['gender'] ) ) {
172 $this->globals['wgExtraGenderNamespaces'][$id] = $ns['gender'];
173 }
174 if ( isset( $ns['subpages'] ) && $ns['subpages'] ) {
175 $this->globals['wgNamespacesWithSubpages'][$id] = true;
176 }
177 if ( isset( $ns['content'] ) && $ns['content'] ) {
178 $this->globals['wgContentNamespaces'][] = $id;
179 }
180 if ( isset( $ns['defaultcontentmodel'] ) ) {
181 $this->globals['wgNamespaceContentModels'][$id] = $ns['defaultcontentmodel'];
182 }
183 }
184 $this->processed[] = 'namespaces';
185 }
186 }
187
188 protected function extractResourceLoaderModules( $dir, array $info ) {
189 $defaultPaths = isset( $info['ResourceFileModulePaths'] )
190 ? $info['ResourceFileModulePaths']
191 : false;
192 if ( isset( $defaultPaths['localBasePath'] ) ) {
193 $defaultPaths['localBasePath'] = "$dir/{$defaultPaths['localBasePath']}";
194 }
195
196 foreach ( array( 'ResourceModules', 'ResourceModuleSkinStyles' ) as $setting ) {
197 if ( isset( $info[$setting] ) ) {
198 foreach ( $info[$setting] as $name => $data ) {
199 if ( isset( $data['localBasePath'] ) ) {
200 $data['localBasePath'] = "$dir/{$data['localBasePath']}";
201 }
202 if ( $defaultPaths ) {
203 $data += $defaultPaths;
204 }
205 $this->globals["wg$setting"][$name] = $data;
206 }
207 }
208 }
209 }
210
211 protected function extractExtensionMessagesFiles( $dir, array $info ) {
212 if ( isset( $info['ExtensionMessagesFiles'] ) ) {
213 $this->globals["wgExtensionMessagesFiles"] += array_map( function( $file ) use ( $dir ) {
214 return "$dir/$file";
215 }, $info['ExtensionMessagesFiles'] );
216 $this->processed[] = 'ExtensionMessagesFiles';
217 }
218 }
219
220 /**
221 * Set message-related settings, which need to be expanded to use
222 * absolute paths
223 *
224 * @param string $dir
225 * @param array $info
226 */
227 protected function extractMessagesDirs( $dir, array $info ) {
228 if ( isset( $info['MessagesDirs'] ) ) {
229 foreach ( $info['MessagesDirs'] as $name => $files ) {
230 foreach ( (array)$files as $file ) {
231 $this->globals["wgMessagesDirs"][$name][] = "$dir/$file";
232 }
233 }
234 $this->processed[] = 'MessagesDirs';
235 }
236 }
237
238 protected function extractCredits( $path, array $info ) {
239 $credits = array(
240 'path' => $path,
241 'type' => isset( $info['type'] ) ? $info['type'] : 'other',
242 );
243 $this->processed[] = 'type';
244 foreach ( self::$creditsAttributes as $attr ) {
245 if ( isset( $info[$attr] ) ) {
246 $credits[$attr] = $info[$attr];
247 $this->processed[] = $attr;
248 }
249 }
250
251 $this->credits[$credits['name']] = $credits;
252 }
253
254 /**
255 * Set configuration settings
256 * @todo In the future, this should be done via Config interfaces
257 *
258 * @param array $info
259 */
260 protected function extractConfig( array $info ) {
261 if ( isset( $info['config'] ) ) {
262 foreach ( $info['config'] as $key => $val ) {
263 if ( $key[0] !== '@' ) {
264 $this->globals["wg$key"] = $val;
265 }
266 }
267 $this->processed[] = 'config';
268 }
269 }
270
271 /**
272 * @param string $name
273 * @param mixed $value
274 * @param array &$array
275 */
276 protected function storeToArray( $name, $value, &$array ) {
277 if ( isset( $array[$name] ) ) {
278 $array[$name] = array_merge_recursive( $array[$name], $value );
279 } else {
280 $array[$name] = $value;
281 }
282 }
283 }