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  'ActionFilteredLogs',
12  'Actions',
13  'AddGroups',
14  'APIFormatModules',
15  'APIListModules',
16  'APIMetaModules',
17  'APIModules',
18  'APIPropModules',
19  'AuthManagerAutoConfig',
20  'AvailableRights',
21  'CentralIdLookupProviders',
22  'ChangeCredentialsBlacklist',
23  'ConfigRegistry',
24  'ContentHandlers',
25  'DefaultUserOptions',
26  'ExtensionEntryPointListFiles',
27  'ExtensionFunctions',
28  'FeedClasses',
29  'FileExtensions',
30  'FilterLogTypes',
31  'GrantPermissionGroups',
32  'GrantPermissions',
33  'GroupPermissions',
34  'GroupsAddToSelf',
35  'GroupsRemoveFromSelf',
36  'HiddenPrefs',
37  'ImplicitGroups',
38  'JobClasses',
39  'LogActions',
40  'LogActionsHandlers',
41  'LogHeaders',
42  'LogNames',
43  'LogRestrictions',
44  'LogTypes',
45  'MediaHandlers',
46  'PasswordPolicy',
47  'RateLimits',
48  'RawHtmlMessages',
49  'ReauthenticateTime',
50  'RecentChangesFlags',
51  'RemoveCredentialsBlacklist',
52  'RemoveGroups',
53  'ResourceLoaderSources',
54  'RevokePermissions',
55  'SessionProviders',
56  'SpecialPages',
57  'ValidSkinNames',
58  ];
59 
65  protected static $coreAttributes = [
66  'SkinOOUIThemes',
67  'TrackingCategories',
68  ];
69 
77  protected static $mergeStrategies = [
78  'wgAuthManagerAutoConfig' => 'array_plus_2d',
79  'wgCapitalLinkOverrides' => 'array_plus',
80  'wgExtensionCredits' => 'array_merge_recursive',
81  'wgExtraGenderNamespaces' => 'array_plus',
82  'wgGrantPermissions' => 'array_plus_2d',
83  'wgGroupPermissions' => 'array_plus_2d',
84  'wgHooks' => 'array_merge_recursive',
85  'wgNamespaceContentModels' => 'array_plus',
86  'wgNamespaceProtection' => 'array_plus',
87  'wgNamespacesWithSubpages' => 'array_plus',
88  'wgPasswordPolicy' => 'array_merge_recursive',
89  'wgRateLimits' => 'array_plus_2d',
90  'wgRevokePermissions' => 'array_plus_2d',
91  ];
92 
98  protected static $creditsAttributes = [
99  'name',
100  'namemsg',
101  'author',
102  'version',
103  'url',
104  'description',
105  'descriptionmsg',
106  'license-name',
107  ];
108 
115  protected static $notAttributes = [
116  'callback',
117  'Hooks',
118  'namespaces',
119  'ResourceFileModulePaths',
120  'ResourceModules',
121  'ResourceModuleSkinStyles',
122  'QUnitTestModule',
123  'ExtensionMessagesFiles',
124  'MessagesDirs',
125  'type',
126  'config',
127  'config_prefix',
128  'ServiceWiringFiles',
129  'ParserTestFiles',
130  'AutoloadClasses',
131  'manifest_version',
132  'load_composer_autoloader',
133  ];
134 
142  protected $globals = [
143  'wgExtensionMessagesFiles' => [],
144  'wgMessagesDirs' => [],
145  ];
146 
152  protected $defines = [];
153 
160  protected $callbacks = [];
161 
165  protected $credits = [];
166 
170  protected $config = [];
171 
178  protected $attributes = [];
179 
186  protected $extAttributes = [];
187 
193  public function extractInfo( $path, array $info, $version ) {
194  $dir = dirname( $path );
195  $this->extractHooks( $info );
196  $this->extractExtensionMessagesFiles( $dir, $info );
197  $this->extractMessagesDirs( $dir, $info );
198  $this->extractNamespaces( $info );
199  $this->extractResourceLoaderModules( $dir, $info );
200  if ( isset( $info['ServiceWiringFiles'] ) ) {
201  $this->extractPathBasedGlobal(
202  'wgServiceWiringFiles',
203  $dir,
204  $info['ServiceWiringFiles']
205  );
206  }
207  if ( isset( $info['ParserTestFiles'] ) ) {
208  $this->extractPathBasedGlobal(
209  'wgParserTestFiles',
210  $dir,
211  $info['ParserTestFiles']
212  );
213  }
214  $name = $this->extractCredits( $path, $info );
215  if ( isset( $info['callback'] ) ) {
216  $this->callbacks[$name] = $info['callback'];
217  }
218 
219  // config should be after all core globals are extracted,
220  // so duplicate setting detection will work fully
221  if ( $version === 2 ) {
222  $this->extractConfig2( $info, $dir );
223  } else {
224  // $version === 1
225  $this->extractConfig1( $info );
226  }
227 
228  if ( $version === 2 ) {
229  $this->extractAttributes( $path, $info );
230  }
231 
232  foreach ( $info as $key => $val ) {
233  // If it's a global setting,
234  if ( in_array( $key, self::$globalSettings ) ) {
235  $this->storeToArray( $path, "wg$key", $val, $this->globals );
236  continue;
237  }
238  // Ignore anything that starts with a @
239  if ( $key[0] === '@' ) {
240  continue;
241  }
242 
243  if ( $version === 2 ) {
244  // Only whitelisted attributes are set
245  if ( in_array( $key, self::$coreAttributes ) ) {
246  $this->storeToArray( $path, $key, $val, $this->attributes );
247  }
248  } else {
249  // version === 1
250  if ( !in_array( $key, self::$notAttributes )
251  && !in_array( $key, self::$creditsAttributes )
252  ) {
253  // If it's not blacklisted, it's an attribute
254  $this->storeToArray( $path, $key, $val, $this->attributes );
255  }
256  }
257 
258  }
259  }
260 
265  protected function extractAttributes( $path, array $info ) {
266  if ( isset( $info['attributes'] ) ) {
267  foreach ( $info['attributes'] as $extName => $value ) {
268  $this->storeToArray( $path, $extName, $value, $this->extAttributes );
269  }
270  }
271  }
272 
273  public function getExtractedInfo() {
274  // Make sure the merge strategies are set
275  foreach ( $this->globals as $key => $val ) {
276  if ( isset( self::$mergeStrategies[$key] ) ) {
277  $this->globals[$key][ExtensionRegistry::MERGE_STRATEGY] = self::$mergeStrategies[$key];
278  }
279  }
280 
281  // Merge $this->extAttributes into $this->attributes depending on what is loaded
282  foreach ( $this->extAttributes as $extName => $value ) {
283  // Only set the attribute if $extName is loaded (and hence present in credits)
284  if ( isset( $this->credits[$extName] ) ) {
285  foreach ( $value as $attrName => $attrValue ) {
286  $this->storeToArray(
287  '', // Don't provide a path since it's impossible to generate an error here
288  $extName . $attrName,
289  $attrValue,
290  $this->attributes
291  );
292  }
293  unset( $this->extAttributes[$extName] );
294  }
295  }
296 
297  return [
298  'globals' => $this->globals,
299  'config' => $this->config,
300  'defines' => $this->defines,
301  'callbacks' => $this->callbacks,
302  'credits' => $this->credits,
303  'attributes' => $this->attributes,
304  ];
305  }
306 
307  public function getRequirements( array $info ) {
308  return $info['requires'] ?? [];
309  }
310 
311  protected function extractHooks( array $info ) {
312  if ( isset( $info['Hooks'] ) ) {
313  foreach ( $info['Hooks'] as $name => $value ) {
314  if ( is_array( $value ) ) {
315  foreach ( $value as $callback ) {
316  $this->globals['wgHooks'][$name][] = $callback;
317  }
318  } else {
319  $this->globals['wgHooks'][$name][] = $value;
320  }
321  }
322  }
323  }
324 
330  protected function extractNamespaces( array $info ) {
331  if ( isset( $info['namespaces'] ) ) {
332  foreach ( $info['namespaces'] as $ns ) {
333  if ( defined( $ns['constant'] ) ) {
334  // If the namespace constant is already defined, use it.
335  // This allows namespace IDs to be overwritten locally.
336  $id = constant( $ns['constant'] );
337  } else {
338  $id = $ns['id'];
339  $this->defines[ $ns['constant'] ] = $id;
340  }
341 
342  if ( !( isset( $ns['conditional'] ) && $ns['conditional'] ) ) {
343  // If it is not conditional, register it
344  $this->attributes['ExtensionNamespaces'][$id] = $ns['name'];
345  }
346  if ( isset( $ns['gender'] ) ) {
347  $this->globals['wgExtraGenderNamespaces'][$id] = $ns['gender'];
348  }
349  if ( isset( $ns['subpages'] ) && $ns['subpages'] ) {
350  $this->globals['wgNamespacesWithSubpages'][$id] = true;
351  }
352  if ( isset( $ns['content'] ) && $ns['content'] ) {
353  $this->globals['wgContentNamespaces'][] = $id;
354  }
355  if ( isset( $ns['defaultcontentmodel'] ) ) {
356  $this->globals['wgNamespaceContentModels'][$id] = $ns['defaultcontentmodel'];
357  }
358  if ( isset( $ns['protection'] ) ) {
359  $this->globals['wgNamespaceProtection'][$id] = $ns['protection'];
360  }
361  if ( isset( $ns['capitallinkoverride'] ) ) {
362  $this->globals['wgCapitalLinkOverrides'][$id] = $ns['capitallinkoverride'];
363  }
364  }
365  }
366  }
367 
368  protected function extractResourceLoaderModules( $dir, array $info ) {
369  $defaultPaths = $info['ResourceFileModulePaths'] ?? false;
370  if ( isset( $defaultPaths['localBasePath'] ) ) {
371  if ( $defaultPaths['localBasePath'] === '' ) {
372  // Avoid double slashes (e.g. /extensions/Example//path)
373  $defaultPaths['localBasePath'] = $dir;
374  } else {
375  $defaultPaths['localBasePath'] = "$dir/{$defaultPaths['localBasePath']}";
376  }
377  }
378 
379  foreach ( [ 'ResourceModules', 'ResourceModuleSkinStyles' ] as $setting ) {
380  if ( isset( $info[$setting] ) ) {
381  foreach ( $info[$setting] as $name => $data ) {
382  if ( isset( $data['localBasePath'] ) ) {
383  if ( $data['localBasePath'] === '' ) {
384  // Avoid double slashes (e.g. /extensions/Example//path)
385  $data['localBasePath'] = $dir;
386  } else {
387  $data['localBasePath'] = "$dir/{$data['localBasePath']}";
388  }
389  }
390  if ( $defaultPaths ) {
391  $data += $defaultPaths;
392  }
393  $this->globals["wg$setting"][$name] = $data;
394  }
395  }
396  }
397 
398  if ( isset( $info['QUnitTestModule'] ) ) {
399  $data = $info['QUnitTestModule'];
400  if ( isset( $data['localBasePath'] ) ) {
401  if ( $data['localBasePath'] === '' ) {
402  // Avoid double slashes (e.g. /extensions/Example//path)
403  $data['localBasePath'] = $dir;
404  } else {
405  $data['localBasePath'] = "$dir/{$data['localBasePath']}";
406  }
407  }
408  $this->attributes['QUnitTestModules']["test.{$info['name']}"] = $data;
409  }
410  }
411 
412  protected function extractExtensionMessagesFiles( $dir, array $info ) {
413  if ( isset( $info['ExtensionMessagesFiles'] ) ) {
414  foreach ( $info['ExtensionMessagesFiles'] as &$file ) {
415  $file = "$dir/$file";
416  }
417  $this->globals["wgExtensionMessagesFiles"] += $info['ExtensionMessagesFiles'];
418  }
419  }
420 
428  protected function extractMessagesDirs( $dir, array $info ) {
429  if ( isset( $info['MessagesDirs'] ) ) {
430  foreach ( $info['MessagesDirs'] as $name => $files ) {
431  foreach ( (array)$files as $file ) {
432  $this->globals["wgMessagesDirs"][$name][] = "$dir/$file";
433  }
434  }
435  }
436  }
437 
444  protected function extractCredits( $path, array $info ) {
445  $credits = [
446  'path' => $path,
447  'type' => $info['type'] ?? 'other',
448  ];
449  foreach ( self::$creditsAttributes as $attr ) {
450  if ( isset( $info[$attr] ) ) {
451  $credits[$attr] = $info[$attr];
452  }
453  }
454 
455  $name = $credits['name'];
456 
457  // If someone is loading the same thing twice, throw
458  // a nice error (T121493)
459  if ( isset( $this->credits[$name] ) ) {
460  $firstPath = $this->credits[$name]['path'];
461  $secondPath = $credits['path'];
462  throw new Exception( "It was attempted to load $name twice, from $firstPath and $secondPath." );
463  }
464 
465  $this->credits[$name] = $credits;
466  $this->globals['wgExtensionCredits'][$credits['type']][] = $credits;
467 
468  return $name;
469  }
470 
477  protected function extractConfig1( array $info ) {
478  if ( isset( $info['config'] ) ) {
479  if ( isset( $info['config']['_prefix'] ) ) {
480  $prefix = $info['config']['_prefix'];
481  unset( $info['config']['_prefix'] );
482  } else {
483  $prefix = 'wg';
484  }
485  foreach ( $info['config'] as $key => $val ) {
486  if ( $key[0] !== '@' ) {
487  $this->addConfigGlobal( "$prefix$key", $val, $info['name'] );
488  }
489  }
490  }
491  }
492 
500  protected function extractConfig2( array $info, $dir ) {
501  $prefix = $info['config_prefix'] ?? 'wg';
502  if ( isset( $info['config'] ) ) {
503  foreach ( $info['config'] as $key => $data ) {
504  $value = $data['value'];
505  if ( isset( $data['merge_strategy'] ) ) {
506  $value[ExtensionRegistry::MERGE_STRATEGY] = $data['merge_strategy'];
507  }
508  if ( isset( $data['path'] ) && $data['path'] ) {
509  $value = "$dir/$value";
510  }
511  $this->addConfigGlobal( "$prefix$key", $value, $info['name'] );
512  $data['providedby'] = $info['name'];
513  if ( isset( $info['ConfigRegistry'][0] ) ) {
514  $data['configregistry'] = array_keys( $info['ConfigRegistry'] )[0];
515  }
516  $this->config[$key] = $data;
517  }
518  }
519  }
520 
528  private function addConfigGlobal( $key, $value, $extName ) {
529  if ( array_key_exists( $key, $this->globals ) ) {
530  throw new RuntimeException(
531  "The configuration setting '$key' was already set by MediaWiki core or"
532  . " another extension, and cannot be set again by $extName." );
533  }
534  $this->globals[$key] = $value;
535  }
536 
537  protected function extractPathBasedGlobal( $global, $dir, $paths ) {
538  foreach ( $paths as $path ) {
539  $this->globals[$global][] = "$dir/$path";
540  }
541  }
542 
550  protected function storeToArray( $path, $name, $value, &$array ) {
551  if ( !is_array( $value ) ) {
552  throw new InvalidArgumentException( "The value for '$name' should be an array (from $path)" );
553  }
554  if ( isset( $array[$name] ) ) {
555  $array[$name] = array_merge_recursive( $array[$name], $value );
556  } else {
557  $array[$name] = $value;
558  }
559  }
560 
561  public function getExtraAutoloaderPaths( $dir, array $info ) {
562  $paths = [];
563  if ( isset( $info['load_composer_autoloader'] ) && $info['load_composer_autoloader'] === true ) {
564  $paths[] = "$dir/vendor/autoload.php";
565  }
566  return $paths;
567  }
568 }
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
extractCredits( $path, array $info)
extractInfo( $path, array $info, $version)
array $globals
Stuff that is going to be set to $GLOBALS.
static array $mergeStrategies
Mapping of global settings to their specific merge strategies.
$data
Utility to generate mapping file used in mw.Title (phpCharToUpper.json)
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Definition: router.php:42
extractMessagesDirs( $dir, array $info)
Set message-related settings, which need to be expanded to use absolute paths.
$value
const MERGE_STRATEGY
Special key that defines the merge strategy.
extractAttributes( $path, array $info)
static array $notAttributes
Things that are not &#39;attributes&#39;, and are not in $globalSettings or $creditsAttributes.
callable [] $callbacks
Things to be called once registration of these extensions are done keyed by the name of the extension...
addConfigGlobal( $key, $value, $extName)
Helper function to set a value to a specific global, if it isn&#39;t set already.
static array $globalSettings
Keys that should be set to $GLOBALS.
array $attributes
Any thing else in the $info that hasn&#39;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.
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
extractResourceLoaderModules( $dir, array $info)
extractExtensionMessagesFiles( $dir, array $info)
array array $defines
Things that should be define()&#39;d.
storeToArray( $path, $name, $value, &$array)
getExtraAutoloaderPaths( $dir, array $info)
Get the path for additional autoloaders, e.g.
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
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:271
extractConfig1(array $info)
Set configuration settings for manifest_version == 1.
Processors read associated arrays and register whatever is required.
Definition: Processor.php:9
array $extAttributes
Extension attributes, keyed by name => settings.
extractConfig2(array $info, $dir)
Set configuration settings for manifest_version == 2.
extractPathBasedGlobal( $global, $dir, $paths)
static string [] $coreAttributes
Top-level attributes that come from MW core.
extractNamespaces(array $info)
Register namespaces with the appropriate global settings.