Manual:SpecialPage AJAX API

This article illustrates how to use AJAX on a Special Page in the MediaWiki framework. Creation of such a page involves several different API’s including API Extension, HTMLForm, ResourceLoader, in addition to the normal steps involved in a Special Page extension.

Configure Basic Extension pieces
The developing extensions manual gives a step-by-step guide to this setup. In this example, the extension is named “HelloWorldAJAX,” stored in a directory named “HelloWorldAJAX,” uses core extension setup file “HelloWorldAJAX.php,” and uses the newer style JSON I18N configuration in directory i18n (not using the deprecated single message file setup). Then register the extension in LocalSettings.php with:

At this stage, HelloWorldAJAX.php contains:

API Extension
The next step is to configure our message generator API extension. In the HelloWorldAJAX directory, create a new file HelloWorldAPI.php containing a class named HelloWorldAPI which extends APIBase.

For this example, we will provide the minimum functionality necessary by implementing the execute method.

We next need to register the extension in the extension setup page, HelloWorldAJAX.php:

At this point, we can test our API Extension using the API page:  (server could be localhost or whatever development server you are using and the api path can be customized). The result in XML format should look like:

Special Page
The next step is to setup the framework for the special page which will be calling this API.

In extension directory HelloWorldAJAX, create a new file “SpecialHelloWorldAJAX.php” which contains a class SpecialHelloWorldAJAX extending SpecialPage. The constructor must be implemented and the execute method will hold the logic creating the HTMLForm. Of special note, is the ID attribute on the button (hwldButton) and text field(hwldTextBox). These will be essential in the JavaScript code to find the elements we wish to customize.

The basic elements of registering a special page needs to added to our extension setup file, HelloWorldAJAX.php.

There is also some internationalization settings in the JSON bundle. In the en.json file, strings should be added for form elements:

At this point, the page should be available in the special pages list as well as directly at the URL.

JavaScript
In extension directory HelloWorldAJAX, create a new directory “js”. In that directory, create file ext.HelloWorldAJAX.core.js. Before we get involved with writing JavaScript, register the file with the ResourceLoader in our extension setup page, HelloWorldAJAX.php.

In this javascript file, the first order of business is to convert the submit button to not actually submit, but instead to invoke another method. In the real world, you would probably want a submit button in addition to another button that invokes an AJAX method. Many pages may not use a button to trigger the AJAX call, but a different event, like a drop down change event or similar. In this example, we first disable form submission and then set an event handler. This uses the JQuery syntax supported by recent versions of Mediawiki. Switching to raw javascript would involve the addEventHandler API.

This implementation of the click event handler function starts with a JQuery style interface to event handling and then uses the ResourceLoader api call to the server to invoke the API. This is an asynchronous call. The  method executes when the AJAX call returns, so there could be some delay due to network latency or slow server response, but the browser will return control to the user. Indeed, several calls could be invoked in parallel and return out of order. The  value extracts the   argument which was set by the API class using , in turn referring back to the main extension file, which registered the API using. A real world example could use much more complicated pathing and, thus, require more sophisticated JSON extraction logic.

Now the Special Page file needs to reference this javascript resource it relies on. Following the  line in the execute method, add a reference to the module defined for the ResourceLoader.

At this point, the special page should be fully functional and display the "Hello World" string in the text box when the button is clicked.