Creating a MediaWiki API Instance Outside Installation Directory
I’m in the process of writing mediawiki2wordpress—a plugin to allow WordPress to access and display content from a MediaWiki installation. I spent a bit of the evening figuring out how to get MediaWiki to let me trick it into thinking I was making a standard request.
The method I explain here gives you access to the MediaWiki API class as opposed to the standard MediaWiki rendering class. Most of this code came from MediaWiki’s api.php which you can find in MediaWiki 1.13.
Here’s what the API output will look like for the Foobar page on Wikipedia (in XML).
I spent quite a bit of time poking around in the MediaWiki /includes directory and trying different hack-ish methods before this page that had exactly what I needed! It did need updating, but I was happy to oblige. I even had this whole post written up using a sloppier approach before finding the FauxRequest class that I explain below.
The code
Let’s start with the sample code, then go over the details.
$mediawiki_root = '/Users/eyeRmonkey/www/mediawiki-test';
putenv("MW_INSTALL_PATH=$mediawiki_root");
// Initialise common code
require ($mediawiki_root . '/includes/WebStart.php');
class MediaWikiAPIWrapper {
public function make_fake_request($params) {
$request = new FauxRequest($params, true);
$api = new ApiMain($request);
// Process data & use an output buffer to capture the resutls
$api->execute();
$result = $api->getResult();
$data = &$result->getData();
return $data;
}
}
Initializing
The first matter of business is lines 1 through 4. These need to be called outside of any function or class. I spent some time trying to figure out why none of MediaWiki’s global variables were showing up in print_r($GLOBALS) before thinking to require_once() it outside of any functions. Duh. Make sure you get the path to your MediaWiki installation root correct (no trailing slash, obviously).
Most of the work get’s done on line 4. This get’s all the code ready for a standard API call from the web. This whole approach is meant to be used by an extension, so we need to play catchup by initializing the core MediaWiki code.
Faking the Request
The good folks at MediaWiki were are kind enough to provide us with a class specifically meant for faking an API request! Joy! It’s called FauxRequest and it extends the WebRequest object that is used by default for API calls. The FauxRequest class takes an array of keys and values that represent what would’ve been passed in the URL.
The API manual has more information on formats and different queries you can make to the API.
Here is what your $params array might look like to grab the Main Page:
$data = array(
'action' => 'parse',
'page' => 'Main_Page'
);
Running the API
Lines 10 through 15 create an instance of the API and pass in our FauxRequest object to get things rolling!
The $data array will hold all of the information that you would get from a format=php API call. As always, you could use print_r() to display the Array for debugging purposes.
That’s it!
mod_rewrite Issues
On my wiki, the .htaccess mod_rewrite rules that I have seem to be preventing the API from functioning totally correctly. A workaround I discovered is to use the following API call: /api.php?action=parse&title=Main_Page&text={{:Main_Page}}. This works by asking the API to translate the text parameter into rendered text. The {{:Main_Page}} says that the text is actually a transclusion-ed page. It’s definitely a hack, but it works on servers with weird rewrite rules for pretty URLs.
Further reading
- API: Query Properties
- API: Expanding templates and rendering – Relevant to the transclusion workaround.












Thanks, this helped me a lot in a modification I am trying to do to Mediawiki.
hi, your code is exactly what i was looking for, but i get an error:
PHP Notice: in file /home/aton/public_html/wiki/includes/WebStart.php on line 69: require_once(./StartProfiler.php) [function.require-once]: failed to open stream: No such file or directory
WebStart.php calls StartProfiler.php using a relative path without a path variable :/
that pretty much makes it impossible to include the file from outside, doesnt it?
okay, i found a different approach now, using an xmlhttprequest from javascript:
output this (together with some html tags) after the user presses login. works perfectly for me
hm seems i cannot post code, shame
http://nopaste.info/664b72579a.html posted it here
Before the require() function on MediaWiki 1.16 you must define the $wgCommandLineMode variable to “true” to make internal API calls from CLI work.
Here it is:
$mediawiki_root = ‘/Users/eyeRmonkey/www/mediawiki-test’;
putenv(“MW_INSTALL_PATH=$mediawiki_root”);
$wgCommandLineMode = true;
// Initialise common code
require ($mediawiki_root . ‘/includes/WebStart.php’);
Hello Jeremy,
I’m making a portal page that aggregates recent changes from a phpBB, a WordPress and a MediaWiki and so this page is very useful for me. As of now, I didn’t have any clue for getting the data from the MW. Some kind folk at MW Users forum directed me to the API and I found this page…
So I’ll let you know if I get to something working !
@+
Benoît ‘Mutos’ ROBIN
Hoshikaze 2250 Project
Sci-Fi universe
Hi Jeremy,
Two question on your code. The first is, I just can’t get past the following line :
I made sure the file is OK by displaying it, there’s no error or warning message however, so I just have no clue at all to search on…
The second question, once I’ll have solved the first, is why did you create a class instead of simply executing the code ? I think it has something to do with the script belonging to an extension. I think I’ll not use a class because I simply wanr to include this into a simple script to display some query results.
Thanks again for the code !
@+
Benoît ‘Mutos’ ROBIN
Hoshikaze 2250 Project
Sci-Fi universe
Hi Jeremy,
Progressed but first issue still not solved. Seems it’s while loading StartProfiler.php that there’s something wrong. Never returns from the require call in WebStart.php.
require_once( “$IP/StartProfiler.php” );
When I include echoe() in a test version of StartProfiler.php, nothing happens, even if the echo() are just first line… Don’t understand…
@+
Benoît ‘Mutos’ ROBIN
Hoshikaze 2250 Project
Sci-Fi universe