Rename WikiPage::isParserCacheUsed to WikiPage::shouldCheckParserCache
[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 '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 'ValidSkinNames',
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 'namemsg',
57 'author',
58 'version',
59 'url',
60 'description',
61 'descriptionmsg',
62 'license-name',
63 );
64
65 /**
66 * Things that are not 'attributes', but are not in
67 * $globalSettings or $creditsAttributes.
68 *
69 * @var array
70 */
71 protected static $notAttributes = array(
72 'callback',
73 'Hooks',
74 'namespaces',
75 'ResourceFileModulePaths',
76 'ResourceModules',
77 'ResourceModuleSkinStyles',
78 'ExtensionMessagesFiles',
79 'MessagesDirs',
80 'type',
81 'config',
82 'ParserTestFiles',
83 'AutoloadClasses',
84 'manifest_version',
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 * @param int $version manifest_version for info
130 * @return array
131 */
132 public function extractInfo( $path, array $info, $version ) {
133 $this->extractConfig( $info );
134 $this->extractHooks( $info );
135 $dir = dirname( $path );
136 $this->extractExtensionMessagesFiles( $dir, $info );
137 $this->extractMessagesDirs( $dir, $info );
138 $this->extractNamespaces( $info );
139 $this->extractResourceLoaderModules( $dir, $info );
140 $this->extractParserTestFiles( $dir, $info );
141 if ( isset( $info['callback'] ) ) {
142 $this->callbacks[] = $info['callback'];
143 }
144
145 $this->extractCredits( $path, $info );
146 foreach ( $info as $key => $val ) {
147 if ( in_array( $key, self::$globalSettings ) ) {
148 $this->storeToArray( "wg$key", $val, $this->globals );
149 // Ignore anything that starts with a @
150 } elseif ( $key[0] !== '@' && !in_array( $key, self::$notAttributes )
151 && !in_array( $key, self::$creditsAttributes )
152 ) {
153 $this->storeToArray( $key, $val, $this->attributes );
154 }
155 }
156 }
157
158 public function getExtractedInfo() {
159 return array(
160 'globals' => $this->globals,
161 'defines' => $this->defines,
162 'callbacks' => $this->callbacks,
163 'credits' => $this->credits,
164 'attributes' => $this->attributes,
165 );
166 }
167
168 protected function extractHooks( array $info ) {
169 if ( isset( $info['Hooks'] ) ) {
170 foreach ( $info['Hooks'] as $name => $value ) {
171 foreach ( (array)$value as $callback ) {
172 $this->globals['wgHooks'][$name][] = $callback;
173 }
174 }
175 }
176 }
177
178 /**
179 * Register namespaces with the appropriate global settings
180 *
181 * @param array $info
182 */
183 protected function extractNamespaces( array $info ) {
184 if ( isset( $info['namespaces'] ) ) {
185 foreach ( $info['namespaces'] as $ns ) {
186 $id = $ns['id'];
187 $this->defines[$ns['constant']] = $id;
188 $this->globals['wgExtraNamespaces'][$id] = $ns['name'];
189 if ( isset( $ns['gender'] ) ) {
190 $this->globals['wgExtraGenderNamespaces'][$id] = $ns['gender'];
191 }
192 if ( isset( $ns['subpages'] ) && $ns['subpages'] ) {
193 $this->globals['wgNamespacesWithSubpages'][$id] = true;
194 }
195 if ( isset( $ns['content'] ) && $ns['content'] ) {
196 $this->globals['wgContentNamespaces'][] = $id;
197 }
198 if ( isset( $ns['defaultcontentmodel'] ) ) {
199 $this->globals['wgNamespaceContentModels'][$id] = $ns['defaultcontentmodel'];
200 }
201 }
202 }
203 }
204
205 protected function extractResourceLoaderModules( $dir, array $info ) {
206 $defaultPaths = isset( $info['ResourceFileModulePaths'] )
207 ? $info['ResourceFileModulePaths']
208 : false;
209 if ( isset( $defaultPaths['localBasePath'] ) ) {
210 $defaultPaths['localBasePath'] = "$dir/{$defaultPaths['localBasePath']}";
211 }
212
213 foreach ( array( 'ResourceModules', 'ResourceModuleSkinStyles' ) as $setting ) {
214 if ( isset( $info[$setting] ) ) {
215 foreach ( $info[$setting] as $name => $data ) {
216 if ( isset( $data['localBasePath'] ) ) {
217 $data['localBasePath'] = "$dir/{$data['localBasePath']}";
218 }
219 if ( $defaultPaths ) {
220 $data += $defaultPaths;
221 }
222 $this->globals["wg$setting"][$name] = $data;
223 }
224 }
225 }
226 }
227
228 protected function extractExtensionMessagesFiles( $dir, array $info ) {
229 if ( isset( $info['ExtensionMessagesFiles'] ) ) {
230 $this->globals["wgExtensionMessagesFiles"] += array_map( function( $file ) use ( $dir ) {
231 return "$dir/$file";
232 }, $info['ExtensionMessagesFiles'] );
233 }
234 }
235
236 /**
237 * Set message-related settings, which need to be expanded to use
238 * absolute paths
239 *
240 * @param string $dir
241 * @param array $info
242 */
243 protected function extractMessagesDirs( $dir, array $info ) {
244 if ( isset( $info['MessagesDirs'] ) ) {
245 foreach ( $info['MessagesDirs'] as $name => $files ) {
246 foreach ( (array)$files as $file ) {
247 $this->globals["wgMessagesDirs"][$name][] = "$dir/$file";
248 }
249 }
250 }
251 }
252
253 protected function extractCredits( $path, array $info ) {
254 $credits = array(
255 'path' => $path,
256 'type' => isset( $info['type'] ) ? $info['type'] : 'other',
257 );
258 foreach ( self::$creditsAttributes as $attr ) {
259 if ( isset( $info[$attr] ) ) {
260 $credits[$attr] = $info[$attr];
261 }
262 }
263
264 $this->credits[$credits['name']] = $credits;
265 }
266
267 /**
268 * Set configuration settings
269 * @todo In the future, this should be done via Config interfaces
270 *
271 * @param array $info
272 */
273 protected function extractConfig( array $info ) {
274 if ( isset( $info['config'] ) ) {
275 foreach ( $info['config'] as $key => $val ) {
276 if ( $key[0] !== '@' ) {
277 $this->globals["wg$key"] = $val;
278 }
279 }
280 }
281 }
282
283 protected function extractParserTestFiles( $dir, array $info ) {
284 if ( isset( $info['ParserTestFiles'] ) ) {
285 foreach ( $info['ParserTestFiles'] as $path ) {
286 $this->globals['wgParserTestFiles'][] = "$dir/$path";
287 }
288 }
289 }
290
291 /**
292 * @param string $name
293 * @param array $value
294 * @param array &$array
295 * @throws InvalidArgumentException
296 */
297 protected function storeToArray( $name, $value, &$array ) {
298 if ( !is_array( $value ) ) {
299 throw new InvalidArgumentException( "The value for '$name' should be an array" );
300 }
301 if ( isset( $array[$name] ) ) {
302 $array[$name] = array_merge_recursive( $array[$name], $value );
303 } else {
304 $array[$name] = $value;
305 }
306 }
307 }