registration: Add ConfigRegistry to the extension.json schema
[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 'TrackingCategories',
15 'DefaultUserOptions',
16 'HiddenPrefs',
17 'GroupPermissions',
18 'RevokePermissions',
19 'ImplicitGroups',
20 'GroupsAddToSelf',
21 'GroupsRemoveFromSelf',
22 'AddGroups',
23 'RemoveGroups',
24 'AvailableRights',
25 'ContentHandlers',
26 'ConfigRegistry',
27 'RateLimits',
28 'ParserTestFiles',
29 'RecentChangesFlags',
30 'ExtensionFunctions',
31 'ExtensionEntryPointListFiles',
32 'SpecialPages',
33 'SpecialPageGroups',
34 'JobClasses',
35 'LogTypes',
36 'LogRestrictions',
37 'FilterLogTypes',
38 'LogNames',
39 'LogHeaders',
40 'LogActions',
41 'LogActionsHandlers',
42 'Actions',
43 'APIModules',
44 'APIFormatModules',
45 'APIMetaModules',
46 'APIPropModules',
47 'APIListModules',
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->extractMessageSettings( $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 } elseif ( !in_array( $key, $this->processed ) ) {
133 $this->storeToArray( $key, $val, $this->attributes );
134 }
135 }
136
137 }
138
139 public function getExtractedInfo() {
140 return array(
141 'globals' => $this->globals,
142 'defines' => $this->defines,
143 'callbacks' => $this->callbacks,
144 'credits' => $this->credits,
145 'attributes' => $this->attributes,
146 );
147 }
148
149 protected function extractHooks( array $info ) {
150 if ( isset( $info['Hooks'] ) ) {
151 foreach ( $info['Hooks'] as $name => $callable ) {
152 $this->globals['wgHooks'][$name][] = $callable;
153 }
154 $this->processed[] = 'Hooks';
155 }
156 }
157
158 /**
159 * Register namespaces with the appropriate global settings
160 *
161 * @param array $info
162 */
163 protected function extractNamespaces( array $info ) {
164 if ( isset( $info['namespaces'] ) ) {
165 foreach ( $info['namespaces'] as $ns ) {
166 $id = $ns['id'];
167 $this->defines[$ns['constant']] = $id;
168 $this->globals['wgExtraNamespaces'][$id] = $ns['name'];
169 if ( isset( $ns['gender'] ) ) {
170 $this->globals['wgExtraGenderNamespaces'][$id] = $ns['gender'];
171 }
172 if ( isset( $ns['subpages'] ) && $ns['subpages'] ) {
173 $this->globals['wgNamespacesWithSubpages'][$id] = true;
174 }
175 if ( isset( $ns['content'] ) && $ns['content'] ) {
176 $this->globals['wgContentNamespaces'][] = $id;
177 }
178 if ( isset( $ns['defaultcontentmodel'] ) ) {
179 $this->globals['wgNamespaceContentModels'][$id] = $ns['defaultcontentmodel'];
180 }
181 }
182 $this->processed[] = 'namespaces';
183 }
184 }
185
186 protected function extractResourceLoaderModules( $dir, array $info ) {
187 if ( isset( $info['ResourceModules'] ) ) {
188 foreach ( $info['ResourceModules'] as $name => $data ) {
189 if ( isset( $data['localBasePath'] ) ) {
190 $data['localBasePath'] = "$dir/{$data['localBasePath']}";
191 }
192 $this->globals['wgResourceModules'][$name] = $data;
193 }
194 }
195 }
196
197 /**
198 * Set message-related settings, which need to be expanded to use
199 * absolute paths
200 *
201 * @param string $dir
202 * @param array $info
203 */
204 protected function extractMessageSettings( $dir, array $info ) {
205 foreach ( array( 'ExtensionMessagesFiles', 'MessagesDirs' ) as $key ) {
206 if ( isset( $info[$key] ) ) {
207 $this->globals["wg$key"] += array_map( function( $file ) use ( $dir ) {
208 return "$dir/$file";
209 }, $info[$key] );
210 $this->processed[] = $key;
211 }
212 }
213 }
214
215 protected function extractCredits( $path, array $info ) {
216 $credits = array(
217 'path' => $path,
218 'type' => isset( $info['type'] ) ? $info['type'] : 'other',
219 );
220 $this->processed[] = 'type';
221 foreach ( self::$creditsAttributes as $attr ) {
222 if ( isset( $info[$attr] ) ) {
223 $credits[$attr] = $info[$attr];
224 $this->processed[] = $attr;
225 }
226 }
227
228 $this->credits[$credits['name']] = $credits;
229 }
230
231 /**
232 * Set configuration settings
233 * @todo In the future, this should be done via Config interfaces
234 *
235 * @param array $info
236 */
237 protected function extractConfig( array $info ) {
238 if ( isset( $info['config'] ) ) {
239 foreach ( $info['config'] as $key => $val ) {
240 $this->globals["wg$key"] = $val;
241 }
242 $this->processed[] = 'config';
243 }
244 }
245
246 /**
247 * @param string $name
248 * @param mixed $value
249 * @param array &$array
250 */
251 protected function storeToArray( $name, $value, &$array ) {
252 if ( isset( $array[$name] ) ) {
253 $array[$name] = array_merge_recursive( $array[$name], $value );
254 } else {
255 $array[$name] = $value;
256 }
257 }
258 }