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