3 class ExtensionProcessor
implements Processor
{
6 * Keys that should be set to $GLOBALS
10 protected static $globalSettings = array(
11 'ResourceLoaderSources',
12 'ResourceLoaderLESSVars',
13 'ResourceLoaderLESSImportPaths',
20 'GroupsRemoveFromSelf',
30 'ExtensionEntryPointListFiles',
50 * Mapping of global settings to their specific merge strategies.
52 * @see ExtensionRegistry::exportExtractedData
53 * @see getExtractedInfo
56 protected static $mergeStrategies = array(
57 'wgGroupPermissions' => 'array_plus_2d',
58 'wgRevokePermissions' => 'array_plus_2d',
59 'wgHooks' => 'array_merge_recursive',
60 'wgExtensionCredits' => 'array_merge_recursive',
61 'wgExtraNamespaces' => 'array_plus',
62 'wgExtraGenderNamespaces' => 'array_plus',
63 'wgNamespacesWithSubpages' => 'array_plus',
64 'wgNamespaceContentModels' => 'array_plus',
65 'wgNamespaceProtection' => 'array_plus',
69 * Keys that are part of the extension credits
73 protected static $creditsAttributes = array(
85 * Things that are not 'attributes', but are not in
86 * $globalSettings or $creditsAttributes.
90 protected static $notAttributes = array(
94 'ResourceFileModulePaths',
96 'ResourceModuleSkinStyles',
97 'ExtensionMessagesFiles',
107 * Stuff that is going to be set to $GLOBALS
109 * Some keys are pre-set to arrays so we can += to them
113 protected $globals = array(
114 'wgExtensionMessagesFiles' => array(),
115 'wgMessagesDirs' => array(),
119 * Things that should be define()'d
123 protected $defines = array();
126 * Things to be called once registration of these extensions are done
130 protected $callbacks = array();
135 protected $credits = array();
138 * Any thing else in the $info that hasn't
139 * already been processed
143 protected $attributes = array();
146 * @param string $path
148 * @param int $version manifest_version for info
151 public function extractInfo( $path, array $info, $version ) {
152 $this->extractConfig( $info );
153 $this->extractHooks( $info );
154 $dir = dirname( $path );
155 $this->extractExtensionMessagesFiles( $dir, $info );
156 $this->extractMessagesDirs( $dir, $info );
157 $this->extractNamespaces( $info );
158 $this->extractResourceLoaderModules( $dir, $info );
159 $this->extractParserTestFiles( $dir, $info );
160 if ( isset( $info['callback'] ) ) {
161 $this->callbacks
[] = $info['callback'];
164 $this->extractCredits( $path, $info );
165 foreach ( $info as $key => $val ) {
166 if ( in_array( $key, self
::$globalSettings ) ) {
167 $this->storeToArray( "wg$key", $val, $this->globals
);
168 // Ignore anything that starts with a @
169 } elseif ( $key[0] !== '@' && !in_array( $key, self
::$notAttributes )
170 && !in_array( $key, self
::$creditsAttributes )
172 $this->storeToArray( $key, $val, $this->attributes
);
177 public function getExtractedInfo() {
178 // Make sure the merge strategies are set
179 foreach ( $this->globals
as $key => $val ) {
180 if ( isset( self
::$mergeStrategies[$key] ) ) {
181 $this->globals
[$key][ExtensionRegistry
::MERGE_STRATEGY
] = self
::$mergeStrategies[$key];
186 'globals' => $this->globals
,
187 'defines' => $this->defines
,
188 'callbacks' => $this->callbacks
,
189 'credits' => $this->credits
,
190 'attributes' => $this->attributes
,
194 protected function extractHooks( array $info ) {
195 if ( isset( $info['Hooks'] ) ) {
196 foreach ( $info['Hooks'] as $name => $value ) {
197 foreach ( (array)$value as $callback ) {
198 $this->globals
['wgHooks'][$name][] = $callback;
205 * Register namespaces with the appropriate global settings
209 protected function extractNamespaces( array $info ) {
210 if ( isset( $info['namespaces'] ) ) {
211 foreach ( $info['namespaces'] as $ns ) {
213 $this->defines
[$ns['constant']] = $id;
214 $this->globals
['wgExtraNamespaces'][$id] = $ns['name'];
215 if ( isset( $ns['gender'] ) ) {
216 $this->globals
['wgExtraGenderNamespaces'][$id] = $ns['gender'];
218 if ( isset( $ns['subpages'] ) && $ns['subpages'] ) {
219 $this->globals
['wgNamespacesWithSubpages'][$id] = true;
221 if ( isset( $ns['content'] ) && $ns['content'] ) {
222 $this->globals
['wgContentNamespaces'][] = $id;
224 if ( isset( $ns['defaultcontentmodel'] ) ) {
225 $this->globals
['wgNamespaceContentModels'][$id] = $ns['defaultcontentmodel'];
227 if ( isset( $ns['protection'] ) ) {
228 $this->globals
['wgNamespaceProtection'][$id] = $ns['protection'];
234 protected function extractResourceLoaderModules( $dir, array $info ) {
235 $defaultPaths = isset( $info['ResourceFileModulePaths'] )
236 ?
$info['ResourceFileModulePaths']
238 if ( isset( $defaultPaths['localBasePath'] ) ) {
239 $defaultPaths['localBasePath'] = "$dir/{$defaultPaths['localBasePath']}";
242 foreach ( array( 'ResourceModules', 'ResourceModuleSkinStyles' ) as $setting ) {
243 if ( isset( $info[$setting] ) ) {
244 foreach ( $info[$setting] as $name => $data ) {
245 if ( isset( $data['localBasePath'] ) ) {
246 $data['localBasePath'] = "$dir/{$data['localBasePath']}";
248 if ( $defaultPaths ) {
249 $data +
= $defaultPaths;
251 $this->globals
["wg$setting"][$name] = $data;
257 protected function extractExtensionMessagesFiles( $dir, array $info ) {
258 if ( isset( $info['ExtensionMessagesFiles'] ) ) {
259 $this->globals
["wgExtensionMessagesFiles"] +
= array_map( function( $file ) use ( $dir ) {
261 }, $info['ExtensionMessagesFiles'] );
266 * Set message-related settings, which need to be expanded to use
272 protected function extractMessagesDirs( $dir, array $info ) {
273 if ( isset( $info['MessagesDirs'] ) ) {
274 foreach ( $info['MessagesDirs'] as $name => $files ) {
275 foreach ( (array)$files as $file ) {
276 $this->globals
["wgMessagesDirs"][$name][] = "$dir/$file";
282 protected function extractCredits( $path, array $info ) {
285 'type' => isset( $info['type'] ) ?
$info['type'] : 'other',
287 foreach ( self
::$creditsAttributes as $attr ) {
288 if ( isset( $info[$attr] ) ) {
289 $credits[$attr] = $info[$attr];
293 $this->credits
[$credits['name']] = $credits;
297 * Set configuration settings
298 * @todo In the future, this should be done via Config interfaces
302 protected function extractConfig( array $info ) {
303 if ( isset( $info['config'] ) ) {
304 foreach ( $info['config'] as $key => $val ) {
305 if ( $key[0] !== '@' ) {
306 $this->globals
["wg$key"] = $val;
312 protected function extractParserTestFiles( $dir, array $info ) {
313 if ( isset( $info['ParserTestFiles'] ) ) {
314 foreach ( $info['ParserTestFiles'] as $path ) {
315 $this->globals
['wgParserTestFiles'][] = "$dir/$path";
321 * @param string $name
322 * @param array $value
323 * @param array &$array
324 * @throws InvalidArgumentException
326 protected function storeToArray( $name, $value, &$array ) {
327 if ( !is_array( $value ) ) {
328 throw new InvalidArgumentException( "The value for '$name' should be an array" );
330 if ( isset( $array[$name] ) ) {
331 $array[$name] = array_merge_recursive( $array[$name], $value );
333 $array[$name] = $value;