API:Calling internally

Sometimes other parts of the code may wish to use the data access and aggregation functionality of the API.

Depending on your use-case, it may be easier to use  instead of using an API request.

Here are the steps needed to accomplish such usage:

1) If you are executing in the context of existing request from the user, prepare request parameters using DerivativeRequest class. All parameters are the same as if making the request over the web.

2) Create and execute ApiMain instance. Because the parameter is an instance of a DerivativeRequest 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.

Important: If you want to create or edit pages, you have to send another parameter = true, when creating the ApiMain object. Like so:

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:

3) Get the resulting data array.

Here is a complete example taken from Extension:WikiLove (as of r112758):

If there is no user request context, you can use  instead of. Using  for write operations without passing request context causes bug 34838.