API:Calling internally
From MediaWiki.org
| This page is part of the MediaWiki API documentation. |
- Formats
- Error reporting
- Restricting usage
- Authentication
- Queries
- Expanding templates and rendering
- Purging pages' caches
- Parameter information
- Editing
- Extensions which extend the API
- Using internally and in extensions
- Miscellaneous
- Implementation
- Client code
[edit] Using API internally by other code
Sometimes other parts of the code may wish to use the data access and aggregation functionality of the API. Here are the steps needed to accomplish such usage:
1) Prepare request parameters using FauxRequest class. All parameters are the same as if making the request over the web.
$params = new FauxRequest(array ( 'action' => 'query', 'list' => 'allpages', 'apnamespace' => 0, 'aplimit' => 10, 'apprefix' => $search ));
2) Create and execute ApiMain instance. Because the parameter is an instance of a FauxRequest object, ApiMain will not execute any formatting printers, nor will it handle any errors. A parameter error or any other internal error will cause an exception that may be caught in the calling code.
$api = new ApiMain($params); $api->execute();
3) Get the resulting data array.
$data = & $api->getResultData();
Note: A description of this process for use outside of extensions is available.
[edit] Creating API modules in extensions
Extensions can also add API modules. Create a new class that inherits ApiBase (for a top-level module), ApiQueryBase (for a non-generator submodule of action=query) or ApiQueryGeneratorBase (for a submodule of action=query with generator functionality) and implement the methods execute(), getAllowedParams(), getParamDescription(), getDescription() and getVersion(). Generator modules also need to implement executeGenerator().
When you've created your new class, you need to register it with the API by adding it to $wgAPIModules (top-level modules), $wgAPIPropModules (prop= modules), $wgAPIMetaModules (meta= modules) or $wgAPIListModules (list= modules), like this:
// Register the ApiMyExt class as handler for action=myext $wgAPIModules['myext'] = 'ApiMyExt';
It's possible to override core modules.
[edit] Extending core modules
Since MediaWiki 1.14, it's possible to extend core modules' functionality using the following hooks:
- APIGetAllowedParams to add or modify the module's parameter list
- APIGetParamDescription to add or modify the module's parameter descriptions
- APIAfterExecute to do something after the module has been executed (but before the result has been output)
- Use APIQueryAfterExecute for
prop=,list=andmeta=modules- If the module is run in generator mode, APIQueryGeneratorAfterExecute will be called instead
- Use APIQueryAfterExecute for