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