registration: Ignore attributes that start with @
[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 // 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 /**
199 * Set message-related settings, which need to be expanded to use
200 * absolute paths
201 *
202 * @param string $dir
203 * @param array $info
204 */
205 protected function extractMessageSettings( $dir, array $info ) {
206 foreach ( array( 'ExtensionMessagesFiles', 'MessagesDirs' ) as $key ) {
207 if ( isset( $info[$key] ) ) {
208 $this->globals["wg$key"] += array_map( function( $file ) use ( $dir ) {
209 return "$dir/$file";
210 }, $info[$key] );
211 $this->processed[] = $key;
212 }
213 }
214 }
215
216 protected function extractCredits( $path, array $info ) {
217 $credits = array(
218 'path' => $path,
219 'type' => isset( $info['type'] ) ? $info['type'] : 'other',
220 );
221 $this->processed[] = 'type';
222 foreach ( self::$creditsAttributes as $attr ) {
223 if ( isset( $info[$attr] ) ) {
224 $credits[$attr] = $info[$attr];
225 $this->processed[] = $attr;
226 }
227 }
228
229 $this->credits[$credits['name']] = $credits;
230 }
231
232 /**
233 * Set configuration settings
234 * @todo In the future, this should be done via Config interfaces
235 *
236 * @param array $info
237 */
238 protected function extractConfig( array $info ) {
239 if ( isset( $info['config'] ) ) {
240 foreach ( $info['config'] as $key => $val ) {
241 $this->globals["wg$key"] = $val;
242 }
243 $this->processed[] = 'config';
244 }
245 }
246
247 /**
248 * @param string $name
249 * @param mixed $value
250 * @param array &$array
251 */
252 protected function storeToArray( $name, $value, &$array ) {
253 if ( isset( $array[$name] ) ) {
254 $array[$name] = array_merge_recursive( $array[$name], $value );
255 } else {
256 $array[$name] = $value;
257 }
258 }
259 }