Maarten van Hoof schreef:
My first question is: how do I place an API module in an extension?
Wikidata has it's own branch, so I can simply put my API module and it's formatters in the includes/api directory and modify both includes/api/ApiMain.php and includes/AutoLoader.php. But I'd much rather put my code in the Wikidata extension and not modify anything in the branch. I found an svn commit that suggests that this is possible, but I fail to see how.
Do
$wgAPIModules['myextension'] = 'MyExtensionAPI';
in your extension setup file ('myextension' being the action= parameter for the API, and 'MyExtensionAPI' being the name of the class that implements the API module). To get your class in the autoloader, use
$wgAutoloadClasses['MyExtensionAPI'] = '$IP/extensions/MyExtension/MyExtensionAPI.php';
My second question is: how can a custom printer learn about the format parameter passed in the request?
My module uses it's own formatter. I created one for several reasons, one being that I had trouble getting a big data structure into the ApiResult object. Maybe I just didn't understands the interface. That wasn't the only reason, I felt like I needed an different internal representation. Anyway, there's a nice hook for creating a custom printer: the getCustomPrinter method. But unfortunately that method doesn't get the value of the format parameter passed in. My module can format different types of output, but right now I have to solve this by adding my own format parameter (which gets prefixed with 'wd' for wikidata). Is there any way I can find out what that parameter was?
You really shouldn't want to write your own formatter: you mentioned your module wants to output XML data, and we have an XML formatter. Large data structures are no problem for ApiResult (as long as they're not dazzlingly huge, of course). The code below uses ApiResult to mimic the output of http://en.wikipedia.org/w/api.php?action=query&prop=revisions&titles...
If you have any questions, please ask. If you have trouble understanding/writing English and prefer to communicate in Dutch, drop me a private e-mail.
Roan Kattouw (Catrope)
====== Begin PHP code ======
// Get the result object (must be done in a class that inherits ApiBase) $result = $this->getResult();
// The array to return $arr = array( 'normalized' => array( array( 'from' => 'Main_page', 'to' => 'Main page' ) ), 'pages' => array( 217225 => array( 'pageid' => 217225, 'ns' => 0, 'title' => 'Main page', 'revisions' => array( array( 'user' => 'Tizio', 'comment' => 'cat' // revision content is added later ), array( 'user' => 'Philwelch', 'comment' => 'rv vandalism', // revision content is added later ) ) ) ) );
// Add revision contents ApiResult::setContent($arr['pages'][217225]['revisions'][0], "#REDIRECT [[Main Page]]\n[[Category:Protected redirects]]"); ApiResult::setContent($arr['pages'][217225]['revisions'][1], "#REDIRECT [[Main Page]]");
// Some arrays have numerical indexes. Since XML doesn't allow numerical tag names, you have to set a tag name for them // NOTE: No indexes also means numerical indexes (0, 1, 2, ...)! // If you forget to do this, you'll get an error mentioning setIndexedTagName() $result->setIndexedTagName($arr['normalized'], 'n'); $result->setIndexedTagName($arr['pages'], 'page'); $result->setIndexedTagName($arr['pages'][217225]['revisions'], 'rev');
// Finally add the array to the result under 'query' $result->addValue('query', null, $arr);
====== End PHP code ======