MediaWiki  1.27.4
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  '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  'SessionProviders',
27  'AuthManagerAutoConfig',
28  'CentralIdLookupProviders',
29  'RateLimits',
30  'RecentChangesFlags',
31  'MediaHandlers',
32  'ExtensionFunctions',
33  'ExtensionEntryPointListFiles',
34  'SpecialPages',
35  'JobClasses',
36  'LogTypes',
37  'LogRestrictions',
38  'FilterLogTypes',
39  'ActionFilteredLogs',
40  'LogNames',
41  'LogHeaders',
42  'LogActions',
43  'LogActionsHandlers',
44  'Actions',
45  'APIModules',
46  'APIFormatModules',
47  'APIMetaModules',
48  'APIPropModules',
49  'APIListModules',
50  'ValidSkinNames',
51  'FeedClasses',
52  ];
53 
61  protected static $mergeStrategies = [
62  'wgGroupPermissions' => 'array_plus_2d',
63  'wgRevokePermissions' => 'array_plus_2d',
64  'wgHooks' => 'array_merge_recursive',
65  'wgExtensionCredits' => 'array_merge_recursive',
66  'wgExtraGenderNamespaces' => 'array_plus',
67  'wgNamespacesWithSubpages' => 'array_plus',
68  'wgNamespaceContentModels' => 'array_plus',
69  'wgNamespaceProtection' => 'array_plus',
70  'wgCapitalLinkOverrides' => 'array_plus',
71  'wgRateLimits' => 'array_plus_2d',
72  'wgAuthManagerAutoConfig' => 'array_plus_2d',
73  ];
74 
80  protected static $creditsAttributes = [
81  'name',
82  'namemsg',
83  'author',
84  'version',
85  'url',
86  'description',
87  'descriptionmsg',
88  'license-name',
89  ];
90 
97  protected static $notAttributes = [
98  'callback',
99  'Hooks',
100  'namespaces',
101  'ResourceFileModulePaths',
102  'ResourceModules',
103  'ResourceModuleSkinStyles',
104  'ExtensionMessagesFiles',
105  'MessagesDirs',
106  'type',
107  'config',
108  'ParserTestFiles',
109  'AutoloadClasses',
110  'manifest_version',
111  'load_composer_autoloader',
112  ];
113 
121  protected $globals = [
122  'wgExtensionMessagesFiles' => [],
123  'wgMessagesDirs' => [],
124  ];
125 
131  protected $defines = [];
132 
139  protected $callbacks = [];
140 
144  protected $credits = [];
145 
152  protected $attributes = [];
153 
160  public function extractInfo( $path, array $info, $version ) {
161  $this->extractConfig( $info );
162  $this->extractHooks( $info );
163  $dir = dirname( $path );
164  $this->extractExtensionMessagesFiles( $dir, $info );
165  $this->extractMessagesDirs( $dir, $info );
166  $this->extractNamespaces( $info );
167  $this->extractResourceLoaderModules( $dir, $info );
168  $this->extractParserTestFiles( $dir, $info );
169  $name = $this->extractCredits( $path, $info );
170  if ( isset( $info['callback'] ) ) {
171  $this->callbacks[$name] = $info['callback'];
172  }
173 
174  foreach ( $info as $key => $val ) {
175  if ( in_array( $key, self::$globalSettings ) ) {
176  $this->storeToArray( $path, "wg$key", $val, $this->globals );
177  // Ignore anything that starts with a @
178  } elseif ( $key[0] !== '@' && !in_array( $key, self::$notAttributes )
179  && !in_array( $key, self::$creditsAttributes )
180  ) {
181  $this->storeToArray( $path, $key, $val, $this->attributes );
182  }
183  }
184  }
185 
186  public function getExtractedInfo() {
187  // Make sure the merge strategies are set
188  foreach ( $this->globals as $key => $val ) {
189  if ( isset( self::$mergeStrategies[$key] ) ) {
190  $this->globals[$key][ExtensionRegistry::MERGE_STRATEGY] = self::$mergeStrategies[$key];
191  }
192  }
193 
194  return [
195  'globals' => $this->globals,
196  'defines' => $this->defines,
197  'callbacks' => $this->callbacks,
198  'credits' => $this->credits,
199  'attributes' => $this->attributes,
200  ];
201  }
202 
203  public function getRequirements( array $info ) {
204  $requirements = [];
206  if ( isset( $info['requires'][$key] ) ) {
207  $requirements[$key] = $info['requires'][$key];
208  }
209 
210  return $requirements;
211  }
212 
213  protected function extractHooks( array $info ) {
214  if ( isset( $info['Hooks'] ) ) {
215  foreach ( $info['Hooks'] as $name => $value ) {
216  if ( is_array( $value ) ) {
217  foreach ( $value as $callback ) {
218  $this->globals['wgHooks'][$name][] = $callback;
219  }
220  } else {
221  $this->globals['wgHooks'][$name][] = $value;
222  }
223  }
224  }
225  }
226 
232  protected function extractNamespaces( array $info ) {
233  if ( isset( $info['namespaces'] ) ) {
234  foreach ( $info['namespaces'] as $ns ) {
235  if ( defined( $ns['constant'] ) ) {
236  // If the namespace constant is already defined, use it.
237  // This allows namespace IDs to be overwritten locally.
238  $id = constant( $ns['constant'] );
239  } else {
240  $id = $ns['id'];
241  $this->defines[ $ns['constant'] ] = $id;
242  }
243 
244  if ( !( isset( $ns['conditional'] ) && $ns['conditional'] ) ) {
245  // If it is not conditional, register it
246  $this->attributes['ExtensionNamespaces'][$id] = $ns['name'];
247  }
248  if ( isset( $ns['gender'] ) ) {
249  $this->globals['wgExtraGenderNamespaces'][$id] = $ns['gender'];
250  }
251  if ( isset( $ns['subpages'] ) && $ns['subpages'] ) {
252  $this->globals['wgNamespacesWithSubpages'][$id] = true;
253  }
254  if ( isset( $ns['content'] ) && $ns['content'] ) {
255  $this->globals['wgContentNamespaces'][] = $id;
256  }
257  if ( isset( $ns['defaultcontentmodel'] ) ) {
258  $this->globals['wgNamespaceContentModels'][$id] = $ns['defaultcontentmodel'];
259  }
260  if ( isset( $ns['protection'] ) ) {
261  $this->globals['wgNamespaceProtection'][$id] = $ns['protection'];
262  }
263  if ( isset( $ns['capitallinkoverride'] ) ) {
264  $this->globals['wgCapitalLinkOverrides'][$id] = $ns['capitallinkoverride'];
265  }
266  }
267  }
268  }
269 
270  protected function extractResourceLoaderModules( $dir, array $info ) {
271  $defaultPaths = isset( $info['ResourceFileModulePaths'] )
272  ? $info['ResourceFileModulePaths']
273  : false;
274  if ( isset( $defaultPaths['localBasePath'] ) ) {
275  if ( $defaultPaths['localBasePath'] === '' ) {
276  // Avoid double slashes (e.g. /extensions/Example//path)
277  $defaultPaths['localBasePath'] = $dir;
278  } else {
279  $defaultPaths['localBasePath'] = "$dir/{$defaultPaths['localBasePath']}";
280  }
281  }
282 
283  foreach ( [ 'ResourceModules', 'ResourceModuleSkinStyles' ] as $setting ) {
284  if ( isset( $info[$setting] ) ) {
285  foreach ( $info[$setting] as $name => $data ) {
286  if ( isset( $data['localBasePath'] ) ) {
287  if ( $data['localBasePath'] === '' ) {
288  // Avoid double slashes (e.g. /extensions/Example//path)
289  $data['localBasePath'] = $dir;
290  } else {
291  $data['localBasePath'] = "$dir/{$data['localBasePath']}";
292  }
293  }
294  if ( $defaultPaths ) {
295  $data += $defaultPaths;
296  }
297  $this->globals["wg$setting"][$name] = $data;
298  }
299  }
300  }
301  }
302 
303  protected function extractExtensionMessagesFiles( $dir, array $info ) {
304  if ( isset( $info['ExtensionMessagesFiles'] ) ) {
305  $this->globals["wgExtensionMessagesFiles"] += array_map( function( $file ) use ( $dir ) {
306  return "$dir/$file";
307  }, $info['ExtensionMessagesFiles'] );
308  }
309  }
310 
318  protected function extractMessagesDirs( $dir, array $info ) {
319  if ( isset( $info['MessagesDirs'] ) ) {
320  foreach ( $info['MessagesDirs'] as $name => $files ) {
321  foreach ( (array)$files as $file ) {
322  $this->globals["wgMessagesDirs"][$name][] = "$dir/$file";
323  }
324  }
325  }
326  }
327 
334  protected function extractCredits( $path, array $info ) {
335  $credits = [
336  'path' => $path,
337  'type' => isset( $info['type'] ) ? $info['type'] : 'other',
338  ];
339  foreach ( self::$creditsAttributes as $attr ) {
340  if ( isset( $info[$attr] ) ) {
341  $credits[$attr] = $info[$attr];
342  }
343  }
344 
345  $name = $credits['name'];
346 
347  // If someone is loading the same thing twice, throw
348  // a nice error (T121493)
349  if ( isset( $this->credits[$name] ) ) {
350  $firstPath = $this->credits[$name]['path'];
351  $secondPath = $credits['path'];
352  throw new Exception( "It was attempted to load $name twice, from $firstPath and $secondPath." );
353  }
354 
355  $this->credits[$name] = $credits;
356  $this->globals['wgExtensionCredits'][$credits['type']][] = $credits;
357 
358  return $name;
359  }
360 
367  protected function extractConfig( array $info ) {
368  if ( isset( $info['config'] ) ) {
369  if ( isset( $info['config']['_prefix'] ) ) {
370  $prefix = $info['config']['_prefix'];
371  unset( $info['config']['_prefix'] );
372  } else {
373  $prefix = 'wg';
374  }
375  foreach ( $info['config'] as $key => $val ) {
376  if ( $key[0] !== '@' ) {
377  $this->globals["$prefix$key"] = $val;
378  }
379  }
380  }
381  }
382 
383  protected function extractParserTestFiles( $dir, array $info ) {
384  if ( isset( $info['ParserTestFiles'] ) ) {
385  foreach ( $info['ParserTestFiles'] as $path ) {
386  $this->globals['wgParserTestFiles'][] = "$dir/$path";
387  }
388  }
389  }
390 
398  protected function storeToArray( $path, $name, $value, &$array ) {
399  if ( !is_array( $value ) ) {
400  throw new InvalidArgumentException( "The value for '$name' should be an array (from $path)" );
401  }
402  if ( isset( $array[$name] ) ) {
403  $array[$name] = array_merge_recursive( $array[$name], $value );
404  } else {
405  $array[$name] = $value;
406  }
407  }
408 
409  public function getExtraAutoloaderPaths( $dir, array $info ) {
410  $paths = [];
411  if ( isset( $info['load_composer_autoloader'] ) && $info['load_composer_autoloader'] === true ) {
412  $path = "$dir/vendor/autoload.php";
413  if ( file_exists( $path ) ) {
414  $paths[] = $path;
415  }
416  }
417  return $paths;
418  }
419 }
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.
magic word the default is to use $key to get the and $key value or $key value text $key value html to format the value $key
Definition: hooks.txt:2325
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 keyed by the name of the extension...
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.
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.
!html< table >< tr >< td > broken</td ></tr ></table >!end!test Table cell attributes
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
$version
Definition: parserTests.php:85
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:314