API:Calling internally
| This page is part of the MediaWiki API documentation. |
| Language: | English • 日本語 |
|---|
Quick overview:
- Quick start guide
- FAQ
- Formats
- Error reporting
- Restricting usage
- Authentication
- Queries
- Search suggestions
- Expanding templates and rendering
- Purging pages' caches
- Parameter information
- Changing wiki content
- Watchlist feed
- Extensions
- Using the API in MediaWiki and extensions
- Miscellaneous
- Implementation
- Client 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();
Important: If you want to create or edit pages, you have to send another parameter = true, when creating the ApiMain object. Like so:
$enableWrite = true; // This is set to false by default, in the ApiMain constructor $api = new ApiMain( $params, $enableWrite ); $api->execute();
You may also need to send an edit token along as the last parameter when making edits or changes. You can get the edit token like so:
global $wgUser; $token = $wgUser->editToken();
3) Get the resulting data array.
$data = & $api->getResultData();
Note: A description of this process for use outside of extensions is available.