Merge "registration: Have cache entries expire after 24 hours"
[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 'RecentChangesFlags',
28 'MediaHandlers',
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 'ValidSkinNames',
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 'namemsg',
58 'author',
59 'version',
60 'url',
61 'description',
62 'descriptionmsg',
63 'license-name',
64 );
65
66 /**
67 * Things that are not 'attributes', but are not in
68 * $globalSettings or $creditsAttributes.
69 *
70 * @var array
71 */
72 protected static $notAttributes = array(
73 'callback',
74 'Hooks',
75 'namespaces',
76 'ResourceFileModulePaths',
77 'ResourceModules',
78 'ResourceModuleSkinStyles',
79 'ExtensionMessagesFiles',
80 'MessagesDirs',
81 'type',
82 'config',
83 'ParserTestFiles',
84 'AutoloadClasses',
85 );
86
87 /**
88 * Stuff that is going to be set to $GLOBALS
89 *
90 * Some keys are pre-set to arrays so we can += to them
91 *
92 * @var array
93 */
94 protected $globals = array(
95 'wgExtensionMessagesFiles' => array(),
96 'wgMessagesDirs' => array(),
97 );
98
99 /**
100 * Things that should be define()'d
101 *
102 * @var array
103 */
104 protected $defines = array();
105
106 /**
107 * Things to be called once registration of these extensions are done
108 *
109 * @var callable[]
110 */
111 protected $callbacks = array();
112
113 /**
114 * @var array
115 */
116 protected $credits = array();
117
118 /**
119 * Any thing else in the $info that hasn't
120 * already been processed
121 *
122 * @var array
123 */
124 protected $attributes = array();
125
126 /**
127 * @param string $path
128 * @param array $info
129 * @return array
130 */
131 public function extractInfo( $path, array $info ) {
132 $this->extractConfig( $info );
133 $this->extractHooks( $info );
134 $dir = dirname( $path );
135 $this->extractExtensionMessagesFiles( $dir, $info );
136 $this->extractMessagesDirs( $dir, $info );
137 $this->extractNamespaces( $info );
138 $this->extractResourceLoaderModules( $dir, $info );
139 $this->extractParserTestFiles( $dir, $info );
140 if ( isset( $info['callback'] ) ) {
141 $this->callbacks[] = $info['callback'];
142 }
143
144 $this->extractCredits( $path, $info );
145 foreach ( $info as $key => $val ) {
146 if ( in_array( $key, self::$globalSettings ) ) {
147 $this->storeToArray( "wg$key", $val, $this->globals );
148 // Ignore anything that starts with a @
149 } elseif ( $key[0] !== '@' && !in_array( $key, self::$notAttributes )
150 && !in_array( $key, self::$creditsAttributes )
151 ) {
152 $this->storeToArray( $key, $val, $this->attributes );
153 }
154 }
155 }
156
157 public function getExtractedInfo() {
158 return array(
159 'globals' => $this->globals,
160 'defines' => $this->defines,
161 'callbacks' => $this->callbacks,
162 'credits' => $this->credits,
163 'attributes' => $this->attributes,
164 );
165 }
166
167 protected function extractHooks( array $info ) {
168 if ( isset( $info['Hooks'] ) ) {
169 foreach ( $info['Hooks'] as $name => $value ) {
170 foreach ( (array)$value as $callback ) {
171 $this->globals['wgHooks'][$name][] = $callback;
172 }
173 }
174 }
175 }
176
177 /**
178 * Register namespaces with the appropriate global settings
179 *
180 * @param array $info
181 */
182 protected function extractNamespaces( array $info ) {
183 if ( isset( $info['namespaces'] ) ) {
184 foreach ( $info['namespaces'] as $ns ) {
185 $id = $ns['id'];
186 $this->defines[$ns['constant']] = $id;
187 $this->globals['wgExtraNamespaces'][$id] = $ns['name'];
188 if ( isset( $ns['gender'] ) ) {
189 $this->globals['wgExtraGenderNamespaces'][$id] = $ns['gender'];
190 }
191 if ( isset( $ns['subpages'] ) && $ns['subpages'] ) {
192 $this->globals['wgNamespacesWithSubpages'][$id] = true;
193 }
194 if ( isset( $ns['content'] ) && $ns['content'] ) {
195 $this->globals['wgContentNamespaces'][] = $id;
196 }
197 if ( isset( $ns['defaultcontentmodel'] ) ) {
198 $this->globals['wgNamespaceContentModels'][$id] = $ns['defaultcontentmodel'];
199 }
200 }
201 }
202 }
203
204 protected function extractResourceLoaderModules( $dir, array $info ) {
205 $defaultPaths = isset( $info['ResourceFileModulePaths'] )
206 ? $info['ResourceFileModulePaths']
207 : false;
208 if ( isset( $defaultPaths['localBasePath'] ) ) {
209 $defaultPaths['localBasePath'] = "$dir/{$defaultPaths['localBasePath']}";
210 }
211
212 foreach ( array( 'ResourceModules', 'ResourceModuleSkinStyles' ) as $setting ) {
213 if ( isset( $info[$setting] ) ) {
214 foreach ( $info[$setting] as $name => $data ) {
215 if ( isset( $data['localBasePath'] ) ) {
216 $data['localBasePath'] = "$dir/{$data['localBasePath']}";
217 }
218 if ( $defaultPaths ) {
219 $data += $defaultPaths;
220 }
221 $this->globals["wg$setting"][$name] = $data;
222 }
223 }
224 }
225 }
226
227 protected function extractExtensionMessagesFiles( $dir, array $info ) {
228 if ( isset( $info['ExtensionMessagesFiles'] ) ) {
229 $this->globals["wgExtensionMessagesFiles"] += array_map( function( $file ) use ( $dir ) {
230 return "$dir/$file";
231 }, $info['ExtensionMessagesFiles'] );
232 }
233 }
234
235 /**
236 * Set message-related settings, which need to be expanded to use
237 * absolute paths
238 *
239 * @param string $dir
240 * @param array $info
241 */
242 protected function extractMessagesDirs( $dir, array $info ) {
243 if ( isset( $info['MessagesDirs'] ) ) {
244 foreach ( $info['MessagesDirs'] as $name => $files ) {
245 foreach ( (array)$files as $file ) {
246 $this->globals["wgMessagesDirs"][$name][] = "$dir/$file";
247 }
248 }
249 }
250 }
251
252 protected function extractCredits( $path, array $info ) {
253 $credits = array(
254 'path' => $path,
255 'type' => isset( $info['type'] ) ? $info['type'] : 'other',
256 );
257 foreach ( self::$creditsAttributes as $attr ) {
258 if ( isset( $info[$attr] ) ) {
259 $credits[$attr] = $info[$attr];
260 }
261 }
262
263 $this->credits[$credits['name']] = $credits;
264 }
265
266 /**
267 * Set configuration settings
268 * @todo In the future, this should be done via Config interfaces
269 *
270 * @param array $info
271 */
272 protected function extractConfig( array $info ) {
273 if ( isset( $info['config'] ) ) {
274 foreach ( $info['config'] as $key => $val ) {
275 if ( $key[0] !== '@' ) {
276 $this->globals["wg$key"] = $val;
277 }
278 }
279 }
280 }
281
282 protected function extractParserTestFiles( $dir, array $info ) {
283 if ( isset( $info['ParserTestFiles'] ) ) {
284 foreach ( $info['ParserTestFiles'] as $path ) {
285 $this->globals['wgParserTestFiles'][] = "$dir/$path";
286 }
287 }
288 }
289
290 /**
291 * @param string $name
292 * @param mixed $value
293 * @param array &$array
294 */
295 protected function storeToArray( $name, $value, &$array ) {
296 if ( isset( $array[$name] ) ) {
297 $array[$name] = array_merge_recursive( $array[$name], $value );
298 } else {
299 $array[$name] = $value;
300 }
301 }
302 }