Manual:Extension registration/zh


 * extension.json redirects here. For a list of specifications, go to Manual:Extension.json/Schema directly.

扩展注册（Extension registration）是MediaWiki用于加载扩展和皮肤的机制. 您将配置数据放置在您的扩展或皮肤根目录中，名叫 或 的文件中，然后MediaWiki使用它以注册扩展和皮肤.

If you were looking for documentation on installing extensions instead, see this guide.

功能
If you are loading a large number of extensions, extension registration will provide a performance boost as long as you have APC (or APCu) installed. Extensions that are loaded together with  (with plural -s) will be cached together.

属性
"Attributes" let you register something, such as a module, with another extension. For instance, the VisualEditor (VE) extension offers the hook, which other extensions can use to register a module with VE. If the Math extension were to register a module with VE, it would have something like the following in its : If VisualEditor wants to access this attribute, it would use:



要求（依赖性）
Extension registration has a  section, which acts similar to Composer's   section. It allows an extension developer to specify several requirements for the extension, such as a specific MediaWiki version (or greater/less than) or another extension/skin. For example, to add a dependency on a MediaWiki version that is greater than 1.26.0, you can add the following code to :

The key of the  object is the name of the dependency (prior to MediaWiki 1.29.0 only   was supported), the value is a valid version constraint (the format has to match the one used by composer).

In MediaWiki 1.29.0 and above you can also add dependencies on skins and other extensions like so:

In MediaWiki 1.33.0(?!??) and above you can also add dependencies on PHP like so:

Check if an extension is loaded without actually requiring it
Many extensions may provide features that work only if another extension is loaded too, without really needing this feature for the core extension function to work. As an example: If extension B is loaded, extension A can provide a real WYSIWYG editor, otherwise it will use a simple textarea. Extension A can profit from extension B (if it is loaded), but doesn't require it to be loaded to work properly. For this, you generally check, if the extension is loaded, rather than adding it as a hard dependency.

To implement a standardized way of checking, if an extension is loaded or not (without the need of extra work in an extension that is a soft-dependency in another one), extension registration can be used. It implements an  method, which returns a simple boolean, if the extension is loaded or not (the extension needs to be loaded with extension registration for this to work). Example:

Since MediaWiki 1.32 it's also possible to check if an extension is loaded and satisfies a given composer version constraint:

If you would like to check if a specific version of an extension is loaded in earlier versions of MediaWiki, information like that can be extracted with the  method, which returns credit information for all loaded extensions. Example:

Alternatively, if the extension B defines a special constant meant for this purpose during loading, it is possible to check, if it is defined:

A more brittle way, that should be avoided is to check if a specific class of extension B exists or not, e.g. using this code:

This might break if the extension exists in the file system but is not loaded, e.g. if composer was used for autoloading. If the class was renamed or ceases to exist (e.g. because it is not package public) this will also break.

In general it is preferred to share code via composer components instead of extensions. If the classes of an extension only need to exist, but the extension does not need to be configured nor loaded, for what you want to do, that is a strong indicator that that code should be split off into a composer component you should depend on instead.



配置（您的扩展/皮肤设置）
By default,  assumes that your config settings start with a "wg" prefix.

If that's not the case, you can override the prefix by using a special key:

That would use a prefix of "eg", and set the global variable to true.

Starting with manifest version 2, the configuration section of extension registration provides a lot more features and allows you to describe your configuration options with much more detail. Instead of having a single key -> value store for your configuration options, you can also add the following information.

The general structure of the  changes slightly to the following, more object-oriented version:

value
The value of the configuration moved to this place. This is the only required key for a configuration object.

path
The boolean value of the  key identifies, if the value of the configuration option should be interpreted as a filesystem path, relative to the extension directory root. E.g., if the value of the configuration is  and the   is true, the actual value will be.

description
The  key for a configuration option can hold a non-localized string, which can be used to explain the configuration option to other developers or the users (system administrators) of your extension. It may also be used as tooltip text on the parameters section of the extension infobox on the MediaWiki.org extension description page. The value of the description key is usually not exposed to the frontend of the wiki, however, take a look to the outlook for more information how this feature could be used in the future!

descriptionmsg
There's also the possibility to add a message key of MediaWiki's internal localisation system as a description, which, in the future, will be used to expose the description in the frontend of the MediaWiki installation.

public / private
This option is a boolean, which defaults to, which means, that the configuration option and the value is marked as "private". This value is not used anywhere at the moment, take a look to the outlook to find out more about this option.

Outlook
The mentioned changes above are also preparation steps for an improved configuration management in MediaWiki. The above changes allow us to, e.g., expose the configuration options of extensions in the MediaWiki UI. For this, the localised description message ( and  ) and the indication, if the configuration option should be exposed or not  is needed.



单位测试自动发现
MediaWiki允许任意扩展注册phpunit测试. Without extension registration, you would need to register a hook handler for the hook, which would look something like:

(as described on the manual page). However, this code looks the same for a lot of extensions, so you could call it unnecessary code duplication. If your extension uses extension registration and your phpunit tests are located in the  subdirectory of your extension, the phpunit wrapper of MediaWiki will autodiscover the unit tests with the help of extension registration. Therefore, you don't need to register the hook anymore and you don't need to specify, that your unit tests are saved in the default directory.



定制注册

 * See Manual:Extension.json/Schema#callback.



同样在composer.json
If an extension or skin has library dependencies, it may have a  file as well, see. Use the  field to make MediaWiki use Composer's autoloading when appropriate.

Some metadata fields overlap between  and   (discussed in ), including :
 * and
 * and



代碼維護


Documentation

 * is the schema for  (and skin.json).
 * Migration guide - some recommendations on updating older extensions
 * Migration guide - some recommendations on updating older extensions
 * Migration guide - some recommendations on updating older extensions
 * Migration guide - some recommendations on updating older extensions

Feedback

 * Report bugs against the MediaWiki-Configuration project.
 * RfC about implementing extension registration