GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||
GeezerVison is showing www.mediawiki.org — API:Extensions - MediaWiki
This document covers creating an API module in an extension for use with MediaWiki 1.30 and later. Module creation and registrationAll API modules are subclasses of ApiBase , but some types of modules use a derived base class. The method of registration also depends on the module type.
In all cases, the value for the registration key is an object with the module name (i.e. the value for the parameter) as the key and the class name as the value. Modules may also be registered conditionally using the ApiMain::moduleManager (for action and format modules) and ApiQuery::moduleManager (for query submodules) hooks. ImplementationPrefixIn the constructor of your API module, when you call ParametersMost modules require parameters.
These are defined by implementing getAllowedParams().
The return value is an associative array where keys are the (unprefixed) parameter names and values are either the scalar default value for the parameter or an array defining the properties of the parameter using the The example illustrates the syntax and some of the more common protected function getAllowedParams() { return [ // An optional parameter with a default value 'simple' => 'value', // A required parameter 'required' => [ ApiBase::PARAM_TYPE => 'string', ApiBase::PARAM_REQUIRED => true, ], // A parameter accepting multiple values from a list 'variable' => [ // The default set of values ApiBase::PARAM_DFLT => 'foo|bar|baz', // All possible values ApiBase::PARAM_TYPE => [ 'foo', 'bar', 'baz', 'quux', 'fred', 'blah' ], // Indicate that multiple values are accepted ApiBase::PARAM_ISMULTI => true, // Use standard "per value" documentation messages ApiBase::PARAM_HELP_MSG_PER_VALUE => [], ], // A standard "limit" parameter. It's generally best not to vary from this standard. 'limit' => [ ApiBase::PARAM_DFLT => 10, ApiBase::PARAM_TYPE => 'limit', ApiBase::PARAM_MIN => 1, ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1, ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2, ], ]; } Parameters are documented using MediaWiki's i18n mechanism. See #Documentation for details. Execution and outputThe code actually implementing the module goes in the execute() method. This code will generally use $this→extractRequestParams() to get the input parameters, and will use $this→getResult() to get the ApiResult object to add any output to. Query prop submodules should use $this→getPageSet() to access the set of pages to operate on. Query submodules that can be used as generators will also need to implement executeGenerator() which is passed an ApiPageSet that should be filled with the generated pages.
In this case, the CachingBy default API responses are marked as not cacheable ('Cache-Control: private')!
For action modules, you can allow caching by calling $this→getMain()→setCacheMode().
This still requires clients pass the For query modules, do not call those methods. You can allow caching by instead implementing getCacheMode(). In either case, be sure that private data is not exposed.
Token handlingIf your action module changes the wiki in any way, it should require a token of some kind.
To have this handled automatically, implement the If you don't want to use a token that is part of core, but rather a custom token with your own permission checks, use ApiQueryTokensRegisterTypes hook to register your token. Primary database accessIf your module accesses the primary database, it should implement the Returning errorsApiBase includes several methods for performing various checks, for example,
But you will often run into cases where you need to raise an error of your own.
The usual way to do that is to call $this→dieWithError(), although if you have a If you need to issue a warning rather than an error, use $this→addWarning() or $this→addDeprecation() if it's a deprecation warning. DocumentationThe API is documented using MediaWiki's i18n mechanism. Needed messages generally have default names based on the module's "path". For action and format modules, the path is the same as the module's name used during registration. For query submodules, it's the name prefixed with query+. Every module will need a More details on API documentation are available at API:Localisation . Extensions may also document their extra API modules on mediawiki.org.
This should be located on the extension's main page or, if more space is required, on pages named Extending core modulesSince MediaWiki 1.14, it's possible to extend core modules' functionality using the following hooks:
List of extensions with API functionalitySee Category:API extensions for examples of extensions that add to or extend the API. Testing your extension
| ||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |