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