Implement extension registration from an extension.json file
[mediawiki.git] / includes / registration / ExtensionProcessor.php
blobf42e9f3ee5356a38a7b265cae88e213aa0d08483
1 <?php
3 class ExtensionProcessor implements Processor {
5 /**
6 * Keys that should be set to $GLOBALS
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 'RateLimits',
27 'ParserTestFiles',
28 'RecentChangesFlags',
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',
49 /**
50 * Keys that are part of the extension credits
52 * @var array
54 protected static $creditsAttributes = array(
55 'name',
56 'author',
57 'version',
58 'url',
59 'description',
60 'descriptionmsg',
61 'license-name',
64 /**
65 * Stuff that is going to be set to $GLOBALS
67 * Some keys are pre-set to arrays so we can += to them
69 * @var array
71 protected $globals = array(
72 'wgExtensionMessagesFiles' => array(),
73 'wgMessagesDirs' => array(),
76 /**
77 * Things that should be define()'d
79 * @var array
81 protected $defines = array();
83 /**
84 * Things to be called once registration of these extensions are done
86 * @var callable[]
88 protected $callbacks = array();
90 /**
91 * @var array
93 protected $credits = array();
95 /**
96 * Any thing else in the $info that hasn't
97 * already been processed
99 * @var array
101 protected $attributes = array();
104 * List of keys that have already been processed
106 * @var array
108 protected $processed = array();
111 * @param string $path
112 * @param array $info
113 * @return array
115 public function extractInfo( $path, array $info ) {
116 $this->extractConfig( $info );
117 $this->extractHooks( $info );
118 $dir = dirname( $path );
119 $this->extractMessageSettings( $dir, $info );
120 $this->extractNamespaces( $info );
121 $this->extractResourceLoaderModules( $dir, $info );
122 if ( isset( $info['callback'] ) ) {
123 $this->callbacks[] = $info['callback'];
124 $this->processed[] = 'callback';
127 $this->extractCredits( $path, $info );
128 foreach ( $info as $key => $val ) {
129 if ( in_array( $key, self::$globalSettings ) ) {
130 $this->storeToArray( "wg$key", $val, $this->globals );
131 } elseif ( !in_array( $key, $this->processed ) ) {
132 $this->storeToArray( $key, $val, $this->attributes );
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,
150 protected function extractHooks( array $info ) {
151 if ( isset( $info['Hooks'] ) ) {
152 foreach ( $info['Hooks'] as $name => $callable ) {
153 $this->globals['wgHooks'][$name][] = $callable;
155 $this->processed[] = 'Hooks';
160 * Register namespaces with the appropriate global settings
162 * @param array $info
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'];
173 if ( isset( $ns['subpages'] ) && $ns['subpages'] ) {
174 $this->globals['wgNamespacesWithSubpages'][$id] = true;
176 if ( isset( $ns['content'] ) && $ns['content'] ) {
177 $this->globals['wgContentNamespaces'][] = $id;
179 if ( isset( $ns['defaultcontentmodel'] ) ) {
180 $this->globals['wgNamespaceContentModels'][$id] = $ns['defaultcontentmodel'];
183 $this->processed[] = 'namespaces';
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']}";
193 $this->globals['wgResourceModules'][$name] = $data;
199 * Set message-related settings, which need to be expanded to use
200 * absolute paths
202 * @param string $dir
203 * @param array $info
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;
216 protected function extractCredits( $path, array $info ) {
217 $credits = array(
218 'path' => $path,
219 'type' => isset( $info['type'] ) ? $info['type'] : 'other',
221 $this->processed[] = 'type';
222 foreach ( self::$creditsAttributes as $attr ) {
223 if ( isset( $info[$attr] ) ) {
224 $credits[$attr] = $info[$attr];
225 $this->processed[] = $attr;
229 $this->credits[$credits['name']] = $credits;
233 * Set configuration settings
234 * @todo In the future, this should be done via Config interfaces
236 * @param array $info
238 protected function extractConfig( array $info ) {
239 if ( isset( $info['config'] ) ) {
240 foreach ( $info['config'] as $key => $val ) {
241 $this->globals["wg$key"] = $val;
243 $this->processed[] = 'config';
248 * @param string $name
249 * @param mixed $value
250 * @param array &$array
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;