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