API:Calling internally

Sometimes other PHP code may wish to use the data access and aggregation functionality of the action API. Rather than making an HTTP network request to the same server, you can make a call within PHP.

The steps are:

1) If you are executing in the context of an existing request from a user, prepare request parameters using the  class. The second parameter to its constructor is an array of API parameters that is the same as if making the request over the web. The third parameter to its constructor is whether to treat the data as a POST, specify   if the API module you're invoking requires POST requests.

Query request
This issues the 'allpages' list query starting at the letter 'M'. This is a simple query, not requiring a user or POST.

If you need to provide an edit token as an API parameter when making edits or other changes, you can get the edit token like so:

2) Create and execute  instance. Because the parameter is an instance of a   object,   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 pass  as a second parameter when creating the  :

3) Get the resulting data array.

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

FauxRequest
The example above creates a  RequestContext. This "inherits" some of the original request, such as IP and request headers that are set when MediaWiki is doing an action on behalf of a user, typically when handling a web request. If there is no user request context, for example when invoking the action API from a system process, or if you want to make a completely separate internal request, then you can use  instead. Using  for write operations without passing request context causes bug T36838.