Movatterモバイル変換


[0]ホーム

URL:


Jump to content
MediaWiki
Search

Manual:Configuration for developers

From mediawiki.org
Translate this page
Languages:
For documentation about configuring MediaWiki, seeManual:Configuration.

This is a guide for core and extension developers about creating and accessing configuration settings.

For core

[edit]

To access a configuration variable such as$wgFoo:

$config=$this->getConfig();// this is a Config object$foo=$config->get('Foo');

If you don't have access to anyContextSources, you can get a Config object with

useMediaWiki\MediaWikiServices;$config=MediaWikiServices::getInstance()->getMainConfig();// same as: $config = MediaWikiServices::getInstance()->getConfigFactory()->makeConfig( 'main' );

You can useMainConfigNames, for instance:

$scriptPath=MediaWikiServices::getInstance()->getMainConfig()->get(MainConfigNames::ScriptPath);

Exceptions

[edit]

This should not be used to fetch global variable objects such as$wgUser or$wgRequest, nor can it be used to fetch variables such as$IP.

For extensions

[edit]

Set configuration options usingextension.json (recommended)

[edit]
Main page:Manual:Extension registration#Configs (Your extension/skins settings)

Extensions that have anextension.json file should set up configuration variables as described in this section.

If your extension is calledYourExtension, inextension.json you'd write:

{"config":{"YourExtensionSomeConfigKey":{"value":"SomeValue","description":"The description for the configuration",}},"ConfigRegistry":{"yourextension":"GlobalVarConfig::newInstance"},"manifest_version":2}

Naming conventions:

  • It is highly recommended to start the name of the configuration key with the name of your extension (as in the example), to ensure the config key is unique among all keys of all applications. Not doing this is a bad idea, and will probably break the use ofattributes.
  • It is customary to make the name for the ConfigRegistry, here "yourextension", lowercase and without spaces.

Custom prefixes

[edit]

If the prefix for your configuration keys is not the default "wg", you can specify it with theconfig_prefix or_prefix key, depending on the schema version (see docs).You should make sure it doesn't collide with any existing extension.

Set configuration options using globals

[edit]

If you can,use theextension.json file for configuration (see above).If you can't, use this snippet (only works withwg prefixed variables):

$wgConfigRegistry['yourextension']='GlobalVarConfig::newInstance';// Now, whenever you want your config object$config=ConfigFactory::getDefaultInstance()->makeConfig('yourextension');

Custom prefixes

[edit]

In the past, some extensions used "eg" instead of "wg".We want to move away from prefixes, but you can still continue to use them:

// In your extension's setup file (ExtName.php)functionwfExtNameConfigBuilder(){returnnewGlobalVarConfig('eg');// replace "eg" with whatever your custom prefix is}$wgConfigRegistry['ext-name']='wfExtNameConfigBuilder';

If you use extension registration, there is aprefix orconfig_prefix (depending on the schema version) field you can use instead.


Access configuration options

[edit]

Accessing configuration values from an extension works the same way as accessing such values from MediaWiki core:

  • Access the config. In some contexts such as when extendingSpecialPage orApiBase, you can simply call$this->getConfig(). Again, if you don't have access to anyContextSource, you can call the main config withMediaWikiServices::getInstance()->getMainConfig().
  • Useget() with the configuration name but without the prefix.

For instance:

useMediaWiki\MediaWikiServices;$config=MediaWikiServices::getInstance()->getMainConfig();// or if you can: $config = $this->getConfig();$myConfigOption=$config->get('YourExtensionSomeConfigKey');// without prefix!

As a last resort, you can call it as a global:

global$wgYourExtensionSomeConfigKey;

Testing

[edit]

When debugging, you use the following to test that you are accessing the right Config instance.You should do this in place of the $wgConfigRegistry shown in thefor extensions section above.

$wgConfigRegistry['ext-name']=function(){returnnewHashConfig(array(// Array of config variables and values'Foo'=>'baz'));};

If you are accessing the wrong Config instance, aConfigException will be produced.

For modifying configuration variables in PhpUnit tests in extensions using manifest version 1 (or in MediaWiki core), you can do the following in test cases that extendMediaWikiIntegrationTestCase:

$this->setMwGlobals(['wgFoo'=>'baz']);

Programmatically modifying configuration values

[edit]

The only Config implementation that supportsmodification of values isHashConfig, which is mostly used in tests.

One way to modify values from the MainConfig service is via theMediaWikiServices hook, but this is discouraged.Instead, a hook should be used to allow more controlled and explicit modification of the relevant values.

Upgrading from before MediaWiki 1.23

[edit]
MediaWiki version:
1.23

InMediaWiki 1.23 a newConfig interface was introduced to access configuration options.This allowed us to abstract the backends in which we store configuration options.

Pre-1.23 code would look like:

<?phpclassApiMyModuleextendsApiBase{publicfunctionexecute(){global$wgFoo;if($wgFoo){// do stuff}}}
Tracked inPhabricator
Task T71084

1.23+ code should look like this:

<?phpuseMediaWiki\Api\ApiBase;classApiMyModuleextendsApiBase{publicfunctionexecute(){$config=$this->getConfig();// this is a Config objectif($config->get('Foo')){// do stuff}}}

You'll notice a few changes here:

  • We use$this->getConfig() to get the defaultConfig object. Most contexts implementgetConfig().
  • Rather than checking for "wgFoo", you ask the Config object for "Foo", without anywg prefix.


See also

[edit]
Retrieved from "https://www.mediawiki.org/w/index.php?title=Manual:Configuration_for_developers&oldid=7644242"
Category:

[8]ページ先頭

©2009-2025 Movatter.jp