registration: Fix regression in ExtensionMessagesFiles handling
[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 if ( isset( $info['ResourceModules'] ) ) {
189 foreach ( $info['ResourceModules'] as $name => $data ) {
190 if ( isset( $data['localBasePath'] ) ) {
191 $data['localBasePath'] = "$dir/{$data['localBasePath']}";
192 }
193 $this->globals['wgResourceModules'][$name] = $data;
194 }
195 }
196 }
197
198 protected function extractExtensionMessagesFiles( $dir, array $info ) {
199 if ( isset( $info['ExtensionMessagesFiles'] ) ) {
200 $this->globals["wgExtensionMessagesFiles"] += array_map( function( $file ) use ( $dir ) {
201 return "$dir/$file";
202 }, $info['ExtensionMessagesFiles'] );
203 $this->processed[] = 'ExtensionMessagesFiles';
204 }
205 }
206
207 /**
208 * Set message-related settings, which need to be expanded to use
209 * absolute paths
210 *
211 * @param string $dir
212 * @param array $info
213 */
214 protected function extractMessagesDirs( $dir, array $info ) {
215 if ( isset( $info['MessagesDirs'] ) ) {
216 foreach ( $info['MessagesDirs'] as $name => $files ) {
217 foreach ( (array)$files as $file ) {
218 $this->globals["wgMessagesDirs"][$name][] = "$dir/$file";
219 }
220 }
221 $this->processed[] = 'MessagesDirs';
222 }
223 }
224
225 protected function extractCredits( $path, array $info ) {
226 $credits = array(
227 'path' => $path,
228 'type' => isset( $info['type'] ) ? $info['type'] : 'other',
229 );
230 $this->processed[] = 'type';
231 foreach ( self::$creditsAttributes as $attr ) {
232 if ( isset( $info[$attr] ) ) {
233 $credits[$attr] = $info[$attr];
234 $this->processed[] = $attr;
235 }
236 }
237
238 $this->credits[$credits['name']] = $credits;
239 }
240
241 /**
242 * Set configuration settings
243 * @todo In the future, this should be done via Config interfaces
244 *
245 * @param array $info
246 */
247 protected function extractConfig( array $info ) {
248 if ( isset( $info['config'] ) ) {
249 foreach ( $info['config'] as $key => $val ) {
250 if ( $key[0] !== '@' ) {
251 $this->globals["wg$key"] = $val;
252 }
253 }
254 $this->processed[] = 'config';
255 }
256 }
257
258 /**
259 * @param string $name
260 * @param mixed $value
261 * @param array &$array
262 */
263 protected function storeToArray( $name, $value, &$array ) {
264 if ( isset( $array[$name] ) ) {
265 $array[$name] = array_merge_recursive( $array[$name], $value );
266 } else {
267 $array[$name] = $value;
268 }
269 }
270 }