MediaWiki  1.28.0
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  'ServiceWiringFiles',
114  'ParserTestFiles',
115  'AutoloadClasses',
116  'manifest_version',
117  'load_composer_autoloader',
118  ];
119 
127  protected $globals = [
128  'wgExtensionMessagesFiles' => [],
129  'wgMessagesDirs' => [],
130  ];
131 
137  protected $defines = [];
138 
144  protected $callbacks = [];
145 
149  protected $credits = [];
150 
157  protected $attributes = [];
158 
165  public function extractInfo( $path, array $info, $version ) {
166  $this->extractConfig( $info );
167  $this->extractHooks( $info );
168  $dir = dirname( $path );
169  $this->extractExtensionMessagesFiles( $dir, $info );
170  $this->extractMessagesDirs( $dir, $info );
171  $this->extractNamespaces( $info );
172  $this->extractResourceLoaderModules( $dir, $info );
173  $this->extractServiceWiringFiles( $dir, $info );
174  $this->extractParserTestFiles( $dir, $info );
175  if ( isset( $info['callback'] ) ) {
176  $this->callbacks[] = $info['callback'];
177  }
178 
179  $this->extractCredits( $path, $info );
180  foreach ( $info as $key => $val ) {
181  if ( in_array( $key, self::$globalSettings ) ) {
182  $this->storeToArray( $path, "wg$key", $val, $this->globals );
183  // Ignore anything that starts with a @
184  } elseif ( $key[0] !== '@' && !in_array( $key, self::$notAttributes )
185  && !in_array( $key, self::$creditsAttributes )
186  ) {
187  $this->storeToArray( $path, $key, $val, $this->attributes );
188  }
189  }
190  }
191 
192  public function getExtractedInfo() {
193  // Make sure the merge strategies are set
194  foreach ( $this->globals as $key => $val ) {
195  if ( isset( self::$mergeStrategies[$key] ) ) {
196  $this->globals[$key][ExtensionRegistry::MERGE_STRATEGY] = self::$mergeStrategies[$key];
197  }
198  }
199 
200  return [
201  'globals' => $this->globals,
202  'defines' => $this->defines,
203  'callbacks' => $this->callbacks,
204  'credits' => $this->credits,
205  'attributes' => $this->attributes,
206  ];
207  }
208 
209  public function getRequirements( array $info ) {
210  $requirements = [];
212  if ( isset( $info['requires'][$key] ) ) {
213  $requirements[$key] = $info['requires'][$key];
214  }
215 
216  return $requirements;
217  }
218 
219  protected function extractHooks( array $info ) {
220  if ( isset( $info['Hooks'] ) ) {
221  foreach ( $info['Hooks'] as $name => $value ) {
222  if ( is_array( $value ) ) {
223  foreach ( $value as $callback ) {
224  $this->globals['wgHooks'][$name][] = $callback;
225  }
226  } else {
227  $this->globals['wgHooks'][$name][] = $value;
228  }
229  }
230  }
231  }
232 
238  protected function extractNamespaces( array $info ) {
239  if ( isset( $info['namespaces'] ) ) {
240  foreach ( $info['namespaces'] as $ns ) {
241  $id = $ns['id'];
242  $this->defines[$ns['constant']] = $id;
243  if ( !( isset( $ns['conditional'] ) && $ns['conditional'] ) ) {
244  // If it is not conditional, register it
245  $this->attributes['ExtensionNamespaces'][$id] = $ns['name'];
246  }
247  if ( isset( $ns['gender'] ) ) {
248  $this->globals['wgExtraGenderNamespaces'][$id] = $ns['gender'];
249  }
250  if ( isset( $ns['subpages'] ) && $ns['subpages'] ) {
251  $this->globals['wgNamespacesWithSubpages'][$id] = true;
252  }
253  if ( isset( $ns['content'] ) && $ns['content'] ) {
254  $this->globals['wgContentNamespaces'][] = $id;
255  }
256  if ( isset( $ns['defaultcontentmodel'] ) ) {
257  $this->globals['wgNamespaceContentModels'][$id] = $ns['defaultcontentmodel'];
258  }
259  if ( isset( $ns['protection'] ) ) {
260  $this->globals['wgNamespaceProtection'][$id] = $ns['protection'];
261  }
262  if ( isset( $ns['capitallinkoverride'] ) ) {
263  $this->globals['wgCapitalLinkOverrides'][$id] = $ns['capitallinkoverride'];
264  }
265  }
266  }
267  }
268 
269  protected function extractResourceLoaderModules( $dir, array $info ) {
270  $defaultPaths = isset( $info['ResourceFileModulePaths'] )
271  ? $info['ResourceFileModulePaths']
272  : false;
273  if ( isset( $defaultPaths['localBasePath'] ) ) {
274  if ( $defaultPaths['localBasePath'] === '' ) {
275  // Avoid double slashes (e.g. /extensions/Example//path)
276  $defaultPaths['localBasePath'] = $dir;
277  } else {
278  $defaultPaths['localBasePath'] = "$dir/{$defaultPaths['localBasePath']}";
279  }
280  }
281 
282  foreach ( [ 'ResourceModules', 'ResourceModuleSkinStyles' ] as $setting ) {
283  if ( isset( $info[$setting] ) ) {
284  foreach ( $info[$setting] as $name => $data ) {
285  if ( isset( $data['localBasePath'] ) ) {
286  if ( $data['localBasePath'] === '' ) {
287  // Avoid double slashes (e.g. /extensions/Example//path)
288  $data['localBasePath'] = $dir;
289  } else {
290  $data['localBasePath'] = "$dir/{$data['localBasePath']}";
291  }
292  }
293  if ( $defaultPaths ) {
294  $data += $defaultPaths;
295  }
296  $this->globals["wg$setting"][$name] = $data;
297  }
298  }
299  }
300  }
301 
302  protected function extractExtensionMessagesFiles( $dir, array $info ) {
303  if ( isset( $info['ExtensionMessagesFiles'] ) ) {
304  $this->globals["wgExtensionMessagesFiles"] += array_map( function( $file ) use ( $dir ) {
305  return "$dir/$file";
306  }, $info['ExtensionMessagesFiles'] );
307  }
308  }
309 
317  protected function extractMessagesDirs( $dir, array $info ) {
318  if ( isset( $info['MessagesDirs'] ) ) {
319  foreach ( $info['MessagesDirs'] as $name => $files ) {
320  foreach ( (array)$files as $file ) {
321  $this->globals["wgMessagesDirs"][$name][] = "$dir/$file";
322  }
323  }
324  }
325  }
326 
332  protected function extractCredits( $path, array $info ) {
333  $credits = [
334  'path' => $path,
335  'type' => isset( $info['type'] ) ? $info['type'] : 'other',
336  ];
337  foreach ( self::$creditsAttributes as $attr ) {
338  if ( isset( $info[$attr] ) ) {
339  $credits[$attr] = $info[$attr];
340  }
341  }
342 
343  $name = $credits['name'];
344 
345  // If someone is loading the same thing twice, throw
346  // a nice error (T121493)
347  if ( isset( $this->credits[$name] ) ) {
348  $firstPath = $this->credits[$name]['path'];
349  $secondPath = $credits['path'];
350  throw new Exception( "It was attempted to load $name twice, from $firstPath and $secondPath." );
351  }
352 
353  $this->credits[$name] = $credits;
354  $this->globals['wgExtensionCredits'][$credits['type']][] = $credits;
355  }
356 
363  protected function extractConfig( array $info ) {
364  if ( isset( $info['config'] ) ) {
365  if ( isset( $info['config']['_prefix'] ) ) {
366  $prefix = $info['config']['_prefix'];
367  unset( $info['config']['_prefix'] );
368  } else {
369  $prefix = 'wg';
370  }
371  foreach ( $info['config'] as $key => $val ) {
372  if ( $key[0] !== '@' ) {
373  $this->globals["$prefix$key"] = $val;
374  }
375  }
376  }
377  }
378 
379  protected function extractServiceWiringFiles( $dir, array $info ) {
380  if ( isset( $info['ServiceWiringFiles'] ) ) {
381  foreach ( $info['ServiceWiringFiles'] as $path ) {
382  $this->globals['wgServiceWiringFiles'][] = "$dir/$path";
383  }
384  }
385  }
386 
387  protected function extractParserTestFiles( $dir, array $info ) {
388  if ( isset( $info['ParserTestFiles'] ) ) {
389  foreach ( $info['ParserTestFiles'] as $path ) {
390  $this->globals['wgParserTestFiles'][] = "$dir/$path";
391  }
392  }
393  }
394 
402  protected function storeToArray( $path, $name, $value, &$array ) {
403  if ( !is_array( $value ) ) {
404  throw new InvalidArgumentException( "The value for '$name' should be an array (from $path)" );
405  }
406  if ( isset( $array[$name] ) ) {
407  $array[$name] = array_merge_recursive( $array[$name], $value );
408  } else {
409  $array[$name] = $value;
410  }
411  }
412 
413  public function getExtraAutoloaderPaths( $dir, array $info ) {
414  $paths = [];
415  if ( isset( $info['load_composer_autoloader'] ) && $info['load_composer_autoloader'] === true ) {
416  $path = "$dir/vendor/autoload.php";
417  if ( file_exists( $path ) ) {
418  $paths[] = $path;
419  }
420  }
421  return $paths;
422  }
423 }
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.
extractConfig(array $info)
Set configuration settings.
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.
extractServiceWiringFiles($dir, array $info)
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.
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
Processors read associated arrays and register whatever is required.
Definition: Processor.php:9
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:300