Manual:Extension registration

Extension registration is the mechanism that MediaWiki uses to load 1>Special:MyLanguage/Manual:Extensions|extensions and 2>Special:MyLanguage/Manual:Skins|skins.

You put configuration data in a file named   or   in the root directory of your extension or skin, and MediaWiki uses this to register extensions and skins.

Migration for site administrators
Before MediaWiki 1.25, configuration for extensions and skins was done in a PHP file using the extension's or skin's name, for example   or  .

This can be converted to:

If you keep your extensions in a location different from  , you need to override .

If your skins are not in   you need to override the poorly named <tvar|2></>.

This must be done before you load any extensions or skins.

Migration for extension developers
Since MW 1.30, namespace IDs defined in <tvar|1> </> can be overwritten locally, by defining the respective constant in <tvar|2> </> before loading the extension.

Consider for instance the following namespace declaration in a <tvar|1> </> file:

This would per default cause the constant <tvar|1> </> to be defined to have the value 1212.

However, this can be overwritten by defining the respective constant in <tvar|1> </>:

This would cause the "<tvar|1>Foo</>" namespace to be registered with the ID 6688 instead of 1212.

When overriding namespace IDs, don't forget that all talk namespaces must have odd IDs, and the ID of the talk namespace must always be the subject namespace's ID plus one.


 *  See also the [<tvar|url>https://tools.wmflabs.org/extreg-wos/</> extension registration wall of sadness (now superpowers)]. 

The script <tvar|1> </> helps you migrating from PHP entry points to a JSON metadata file.

If your extension supports older versions of MediaWiki, you should keep your PHP entry point <tvar|1> </> until you drop support for those older versions.

Sample command lines:

You may need to uninstall your extension from <tvar|1> </> if you receive errors that constants or functions cannot be redefined.

You should replace your PHP entry point file (<tvar|1> </>) with something like the following happens to not break wikis during the upgrade process.

Or skins

Retaining documentation
PHP entry points usually have some documentation of configuration settings that is useful and shouldn't be lost. Unfortunately JSON doesn't support comments. It is recommended that you transfer configuration documentation to a <tvar|1> </> file in the extension's repository. You should also document configuration on-wiki in your <tvar|1>Extension:MyExtension</> page. It is possible to include some documentation directly in the <tvar|1> </> file as well. Extension registration ignores any key in <tvar|1> </> starting with '<tvar|2> </>' in the top-level structure, so you can put comments in those parts of the JSON file. For example:

Version 1 of the <tvar|1> </> format also allowed <tvar|2> </> in <tvar|3> </> section, but this is no longer recommended or supported in version 2. <tvar|4> </> field of the config variable should be used instead.

This should only be used for brief notes and comments.

Features
If you are loading a large number of extensions, extension registration will provide a performance boost as long as you have 1>Special:MyLanguage/Manual:Performance tuning</>|APC (or APCu) installed. Extensions that are loaded together with <tvar|2> </> (with plural <tvar|3>-s</>) will be cached together.

Attributes
A recurring problem is how to register something with another extension.

Usually this meant that you had to load one extension before another.

For example, VisualEditor has a <tvar|1> </> which allows extensions to add their modules.

However, in VisualEditor's entry point it has:

This means that if any extension appends to the array before VisualEditor is loaded, VE will wipe out its entry in this array.

Some extensions depended upon a specific load order, others hacked around this with <tvar|1></>.

Extension registration solves this problem with attributes.

In the Math extension, its <tvar|1> </> would have something like:

Starting with manifest version 2, the attributes need to be defined in the separate section <tvar|1> </>.

The <tvar|1> </> node needs to be an object with the extension name as key and an object of attribute/value pairs as the value. Be aware that the key in the subobject must not contain the extension name!

When VisualEditor wants to access this attribute it uses:

Requirements (dependencies)
Extension registration has a <tvar|1> </> section, which acts similar to 2>Special:MyLanguage/Composer|Composer's <tvar|3> </> 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 <tvar|1> </>:

The key of the <tvar|1> </> object is the name of the dependency (prior to MediaWiki 1.29.0 only <tvar|2> </> was supported), the value is a valid version constraint (the format has to match the one [<tvar|url>https://getcomposer.org/doc/articles/versions.md</> 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 <tvar|1> </> 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 <tvar|1></> 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 <tvar|1> </> 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.

Configs (Your extension/skins settings)
By default, <tvar|1> </> assumes that your config settings start with a "<tvar|2> </>" prefix.

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

That would use a prefix of "<tvar|1> </>", and set the global variable <tvar|2> </> 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 much more detailed. Instead of having a single key -> value store for your configuration options, you can also add the following information.

The general structure of the <tvar|1> </> 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 <tvar|1> </> 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 <tvar|1> </> and the <tvar|2> </> is true, the actual value will be <tvar|3> </>.

Description
The <tvar|1> </> 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.

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!

There's also the possibility to add a message key of MediaWiki's internal localisation system as a description (<tvar|1> </>), 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 false, which means, that the configuration option and the value is marked as <tvar|1>"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 (<tvar|1> </> and <tvar|2> </>) and the indication, if the configuration option should be exposed or not (<tvar|3> </>) is needed.

Unit tests auto-discovery
MediaWiki allows any extension to 1>Special:MyLanguage/Manual:PHP unit testing/Writing unit tests for extensions</>|register phpunit tests.

Without extension registration, you would need to register a hook handler for the <tvar|1></> 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 <tvar|1> </> 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.

Customizing registration
Sometimes extensions need to do non-standard things during registration, or are just very complex.

You can specify a '<tvar|1> </>' key in your <tvar|2> </> if you need to execute some PHP code.

The value should be set to a valid [<tvar|url>https://php.net/language.types.callable</> PHP callable], for example: <tvar|1> </>.

<tvar|1> </> will execute this callback after it processes the <tvar|2> </> file.

The callback isn't a normal 1>Special:MyLanguage/Manual:Hooks</>|hook function, it runs in early initialization.

See <tvar|1></> for what sort of things may require custom registration.

See <tvar|1></> and the <tvar|2></>, <tvar|3></> and <tvar|4></> hooks for other ways of programmatically interacting with configuration.

Also composer.json
If an extension or skin has library dependencies, it may have a <tvar|1> </> file as well, see <tvar|2></>. Use the <tvar|3> </> field to make MediaWiki use Composer's autoloading when appropriate.

Some metadata fields overlap between <tvar|1> </> and <tvar|2> </> (discussed in <tvar|7>T89456</>), including :
 * <tvar|3> </> and <tvar|4> </>
 * <tvar|5> </> and <tvar|6> </>