MediaWiki  master
ExtensionProcessor.php
Go to the documentation of this file.
1 <?php
2 
3 class ExtensionProcessor implements Processor {
4 
10  protected static $globalSettings = [
11  'ResourceLoaderSources',
12  'ResourceLoaderLESSVars',
13  'DefaultUserOptions',
14  'HiddenPrefs',
15  'GroupPermissions',
16  'RevokePermissions',
17  'GrantPermissions',
18  'GrantPermissionGroups',
19  'ImplicitGroups',
20  'GroupsAddToSelf',
21  'GroupsRemoveFromSelf',
22  'AddGroups',
23  'RemoveGroups',
24  'AvailableRights',
25  'ContentHandlers',
26  'ConfigRegistry',
27  'SessionProviders',
28  'AuthManagerAutoConfig',
29  'CentralIdLookupProviders',
30  'ChangeCredentialsBlacklist',
31  'RemoveCredentialsBlacklist',
32  'RateLimits',
33  'RecentChangesFlags',
34  'MediaHandlers',
35  'ExtensionFunctions',
36  'ExtensionEntryPointListFiles',
37  'SpecialPages',
38  'JobClasses',
39  'LogTypes',
40  'LogRestrictions',
41  'FilterLogTypes',
42  'ActionFilteredLogs',
43  'LogNames',
44  'LogHeaders',
45  'LogActions',
46  'LogActionsHandlers',
47  'Actions',
48  'APIModules',
49  'APIFormatModules',
50  'APIMetaModules',
51  'APIPropModules',
52  'APIListModules',
53  'ValidSkinNames',
54  'FeedClasses',
55  ];
56 
64  protected static $mergeStrategies = [
65  'wgGroupPermissions' => 'array_plus_2d',
66  'wgRevokePermissions' => 'array_plus_2d',
67  'wgGrantPermissions' => 'array_plus_2d',
68  'wgHooks' => 'array_merge_recursive',
69  'wgExtensionCredits' => 'array_merge_recursive',
70  'wgExtraGenderNamespaces' => 'array_plus',
71  'wgNamespacesWithSubpages' => 'array_plus',
72  'wgNamespaceContentModels' => 'array_plus',
73  'wgNamespaceProtection' => 'array_plus',
74  'wgCapitalLinkOverrides' => 'array_plus',
75  'wgRateLimits' => 'array_plus_2d',
76  'wgAuthManagerAutoConfig' => 'array_plus_2d',
77  ];
78 
84  protected static $creditsAttributes = [
85  'name',
86  'namemsg',
87  'author',
88  'version',
89  'url',
90  'description',
91  'descriptionmsg',
92  'license-name',
93  ];
94 
101  protected static $notAttributes = [
102  'callback',
103  'Hooks',
104  'namespaces',
105  'ResourceFileModulePaths',
106  'ResourceModules',
107  'ResourceModuleSkinStyles',
108  'ExtensionMessagesFiles',
109  'MessagesDirs',
110  'type',
111  'config',
112  'config_prefix',
113  'ParserTestFiles',
114  'AutoloadClasses',
115  'manifest_version',
116  'load_composer_autoloader',
117  ];
118 
126  protected $globals = [
127  'wgExtensionMessagesFiles' => [],
128  'wgMessagesDirs' => [],
129  ];
130 
136  protected $defines = [];
137 
143  protected $callbacks = [];
144 
148  protected $credits = [];
149 
156  protected $attributes = [];
157 
164  public function extractInfo( $path, array $info, $version ) {
165  $dir = dirname( $path );
166  if ( $version === 2 ) {
167  $this->extractConfig2( $info, $dir );
168  } else {
169  // $version === 1
170  $this->extractConfig1( $info );
171  }
172  $this->extractHooks( $info );
173  $this->extractExtensionMessagesFiles( $dir, $info );
174  $this->extractMessagesDirs( $dir, $info );
175  $this->extractNamespaces( $info );
176  $this->extractResourceLoaderModules( $dir, $info );
177  $this->extractParserTestFiles( $dir, $info );
178  if ( isset( $info['callback'] ) ) {
179  $this->callbacks[] = $info['callback'];
180  }
181 
182  $this->extractCredits( $path, $info );
183  foreach ( $info as $key => $val ) {
184  if ( in_array( $key, self::$globalSettings ) ) {
185  $this->storeToArray( $path, "wg$key", $val, $this->globals );
186  // Ignore anything that starts with a @
187  } elseif ( $key[0] !== '@' && !in_array( $key, self::$notAttributes )
188  && !in_array( $key, self::$creditsAttributes )
189  ) {
190  $this->storeToArray( $path, $key, $val, $this->attributes );
191  }
192  }
193  }
194 
195  public function getExtractedInfo() {
196  // Make sure the merge strategies are set
197  foreach ( $this->globals as $key => $val ) {
198  if ( isset( self::$mergeStrategies[$key] ) ) {
199  $this->globals[$key][ExtensionRegistry::MERGE_STRATEGY] = self::$mergeStrategies[$key];
200  }
201  }
202 
203  return [
204  'globals' => $this->globals,
205  'defines' => $this->defines,
206  'callbacks' => $this->callbacks,
207  'credits' => $this->credits,
208  'attributes' => $this->attributes,
209  ];
210  }
211 
212  public function getRequirements( array $info ) {
213  $requirements = [];
215  if ( isset( $info['requires'][$key] ) ) {
216  $requirements[$key] = $info['requires'][$key];
217  }
218 
219  return $requirements;
220  }
221 
222  protected function extractHooks( array $info ) {
223  if ( isset( $info['Hooks'] ) ) {
224  foreach ( $info['Hooks'] as $name => $value ) {
225  if ( is_array( $value ) ) {
226  foreach ( $value as $callback ) {
227  $this->globals['wgHooks'][$name][] = $callback;
228  }
229  } else {
230  $this->globals['wgHooks'][$name][] = $value;
231  }
232  }
233  }
234  }
235 
241  protected function extractNamespaces( array $info ) {
242  if ( isset( $info['namespaces'] ) ) {
243  foreach ( $info['namespaces'] as $ns ) {
244  $id = $ns['id'];
245  $this->defines[$ns['constant']] = $id;
246  if ( !( isset( $ns['conditional'] ) && $ns['conditional'] ) ) {
247  // If it is not conditional, register it
248  $this->attributes['ExtensionNamespaces'][$id] = $ns['name'];
249  }
250  if ( isset( $ns['gender'] ) ) {
251  $this->globals['wgExtraGenderNamespaces'][$id] = $ns['gender'];
252  }
253  if ( isset( $ns['subpages'] ) && $ns['subpages'] ) {
254  $this->globals['wgNamespacesWithSubpages'][$id] = true;
255  }
256  if ( isset( $ns['content'] ) && $ns['content'] ) {
257  $this->globals['wgContentNamespaces'][] = $id;
258  }
259  if ( isset( $ns['defaultcontentmodel'] ) ) {
260  $this->globals['wgNamespaceContentModels'][$id] = $ns['defaultcontentmodel'];
261  }
262  if ( isset( $ns['protection'] ) ) {
263  $this->globals['wgNamespaceProtection'][$id] = $ns['protection'];
264  }
265  if ( isset( $ns['capitallinkoverride'] ) ) {
266  $this->globals['wgCapitalLinkOverrides'][$id] = $ns['capitallinkoverride'];
267  }
268  }
269  }
270  }
271 
272  protected function extractResourceLoaderModules( $dir, array $info ) {
273  $defaultPaths = isset( $info['ResourceFileModulePaths'] )
274  ? $info['ResourceFileModulePaths']
275  : false;
276  if ( isset( $defaultPaths['localBasePath'] ) ) {
277  if ( $defaultPaths['localBasePath'] === '' ) {
278  // Avoid double slashes (e.g. /extensions/Example//path)
279  $defaultPaths['localBasePath'] = $dir;
280  } else {
281  $defaultPaths['localBasePath'] = "$dir/{$defaultPaths['localBasePath']}";
282  }
283  }
284 
285  foreach ( [ 'ResourceModules', 'ResourceModuleSkinStyles' ] as $setting ) {
286  if ( isset( $info[$setting] ) ) {
287  foreach ( $info[$setting] as $name => $data ) {
288  if ( isset( $data['localBasePath'] ) ) {
289  if ( $data['localBasePath'] === '' ) {
290  // Avoid double slashes (e.g. /extensions/Example//path)
291  $data['localBasePath'] = $dir;
292  } else {
293  $data['localBasePath'] = "$dir/{$data['localBasePath']}";
294  }
295  }
296  if ( $defaultPaths ) {
297  $data += $defaultPaths;
298  }
299  $this->globals["wg$setting"][$name] = $data;
300  }
301  }
302  }
303  }
304 
305  protected function extractExtensionMessagesFiles( $dir, array $info ) {
306  if ( isset( $info['ExtensionMessagesFiles'] ) ) {
307  $this->globals["wgExtensionMessagesFiles"] += array_map( function( $file ) use ( $dir ) {
308  return "$dir/$file";
309  }, $info['ExtensionMessagesFiles'] );
310  }
311  }
312 
320  protected function extractMessagesDirs( $dir, array $info ) {
321  if ( isset( $info['MessagesDirs'] ) ) {
322  foreach ( $info['MessagesDirs'] as $name => $files ) {
323  foreach ( (array)$files as $file ) {
324  $this->globals["wgMessagesDirs"][$name][] = "$dir/$file";
325  }
326  }
327  }
328  }
329 
335  protected function extractCredits( $path, array $info ) {
336  $credits = [
337  'path' => $path,
338  'type' => isset( $info['type'] ) ? $info['type'] : 'other',
339  ];
340  foreach ( self::$creditsAttributes as $attr ) {
341  if ( isset( $info[$attr] ) ) {
342  $credits[$attr] = $info[$attr];
343  }
344  }
345 
346  $name = $credits['name'];
347 
348  // If someone is loading the same thing twice, throw
349  // a nice error (T121493)
350  if ( isset( $this->credits[$name] ) ) {
351  $firstPath = $this->credits[$name]['path'];
352  $secondPath = $credits['path'];
353  throw new Exception( "It was attempted to load $name twice, from $firstPath and $secondPath." );
354  }
355 
356  $this->credits[$name] = $credits;
357  $this->globals['wgExtensionCredits'][$credits['type']][] = $credits;
358  }
359 
366  protected function extractConfig1( array $info ) {
367  if ( isset( $info['config'] ) ) {
368  if ( isset( $info['config']['_prefix'] ) ) {
369  $prefix = $info['config']['_prefix'];
370  unset( $info['config']['_prefix'] );
371  } else {
372  $prefix = 'wg';
373  }
374  foreach ( $info['config'] as $key => $val ) {
375  if ( $key[0] !== '@' ) {
376  $this->globals["$prefix$key"] = $val;
377  }
378  }
379  }
380  }
381 
389  protected function extractConfig2( array $info, $dir ) {
390  if ( isset( $info['config_prefix'] ) ) {
391  $prefix = $info['config_prefix'];
392  } else {
393  $prefix = 'wg';
394  }
395  if ( isset( $info['config'] ) ) {
396  foreach ( $info['config'] as $key => $data ) {
397  $value = $data['value'];
398  if ( isset( $value['merge_strategy'] ) ) {
399  $value[ExtensionRegistry::MERGE_STRATEGY] = $data['merge_strategy'];
400  }
401  if ( isset( $data['path'] ) && $data['path'] ) {
402  $value = "$dir/$value";
403  }
404  $this->globals["$prefix$key"] = $value;
405  }
406  }
407  }
408 
409  protected function extractParserTestFiles( $dir, array $info ) {
410  if ( isset( $info['ParserTestFiles'] ) ) {
411  foreach ( $info['ParserTestFiles'] as $path ) {
412  $this->globals['wgParserTestFiles'][] = "$dir/$path";
413  }
414  }
415  }
416 
424  protected function storeToArray( $path, $name, $value, &$array ) {
425  if ( !is_array( $value ) ) {
426  throw new InvalidArgumentException( "The value for '$name' should be an array (from $path)" );
427  }
428  if ( isset( $array[$name] ) ) {
429  $array[$name] = array_merge_recursive( $array[$name], $value );
430  } else {
431  $array[$name] = $value;
432  }
433  }
434 
435  public function getExtraAutoloaderPaths( $dir, array $info ) {
436  $paths = [];
437  if ( isset( $info['load_composer_autoloader'] ) && $info['load_composer_autoloader'] === true ) {
438  $path = "$dir/vendor/autoload.php";
439  if ( file_exists( $path ) ) {
440  $paths[] = $path;
441  }
442  }
443  return $paths;
444  }
445 }
array $globals
Stuff that is going to be set to $GLOBALS.
static array $mergeStrategies
Mapping of global settings to their specific merge strategies.
extractResourceLoaderModules($dir, array $info)
the array() calling protocol came about after MediaWiki 1.4rc1.
if(count($args)==0) $dir
extractParserTestFiles($dir, array $info)
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
extractCredits($path, array $info)
$value
$files
const MERGE_STRATEGY
Special key that defines the merge strategy.
static array $notAttributes
Things that are not 'attributes', but are not in $globalSettings or $creditsAttributes.
storeToArray($path, $name, $value, &$array)
callable[] $callbacks
Things to be called once registration of these extensions are done.
extractInfo($path, array $info, $version)
const MEDIAWIKI_CORE
"requires" key that applies to MediaWiki core/$wgVersion
static array $globalSettings
Keys that should be set to $GLOBALS.
getExtraAutoloaderPaths($dir, array $info)
Get the path for additional autoloaders, e.g.
array $attributes
Any thing else in the $info that hasn't already been processed.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
getRequirements(array $info)
Get the requirements for the provided info.
This directory contains media files for use with the tests in includes media directory Image credits
Definition: README:9
extractExtensionMessagesFiles($dir, array $info)
static array static array $creditsAttributes
Keys that are part of the extension credits.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
array array $defines
Things that should be define()'d.
globals will be eliminated from MediaWiki replaced by an application object which would be passed to constructors Whether that would be an convenient solution remains to be but certainly PHP makes such object oriented programming models easier than they were in previous versions For the time being MediaWiki programmers will have to work in an environment with some global context At the time of globals were initialised on startup by MediaWiki of these were configuration which are documented in DefaultSettings php There is no comprehensive documentation for the remaining globals
Definition: globals.txt:25
extractConfig1(array $info)
Set configuration settings for manifest_version == 1.
$version
Definition: parserTests.php:94
Processors read associated arrays and register whatever is required.
Definition: Processor.php:9
extractConfig2(array $info, $dir)
Set configuration settings for manifest_version == 2.
extractMessagesDirs($dir, array $info)
Set message-related settings, which need to be expanded to use absolute paths.
extractNamespaces(array $info)
Register namespaces with the appropriate global settings.
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:310