MediaWiki, l'API que tout le monde oublie…

Certains développeurs se prennent bien la tête pour intégrer du contenu MediaWiki, en externe dans un site… D’ailleurs, la majorité opte pour faire adapter le thème mediawiki et souvent, le thème du site alors qu’il existe une solution vraiment tout simple et existante : l’API !

L’API permet, entre autres, de récupérer le contenu de pages / catégories / namespaces / modèles tout prêt au format XML ou JSON (et YAML, WDDX etc…) avec, à dans ce flux, un beau node contenu du HTML tout chaud, à jour et déjà “parsé” avec le parser, ses extensions et bien entendu les modèles !

Allons direct à un exemple :

Page à récupérer : http://www.burningsea.com/wiki/index.php/Treasure_Aisle
bah, pas plus compliqué que ça : http://www.burningsea.com/wiki/api.php?action=parse&page=Treasure_Aisle&format=json

Décorticons l’url (et ça se passe commentaire tellement c’est simple) :

  1. burningsea.com/wiki/
  2. api.php
  3. action=parse
  4. page=Treasure_Aisle
  5. format=json

Sachant que Action peut prendre l’un des paramètres suivants : login, logout, query, expandtemplates, parse, opensearch, feedwatchlist, help, paraminfo, purge, rollback, delete, undelete, protect, block, unblock, move, edit, emailuser, watch, patrol, import

En somme les possibilités sont très grandes !

Prenons un exemple concret, en php (même s’il est possible de faire du Python, restons accessible ^^) :

<?php
// Get the Content
$xml_content = @simplexml_load_file(“http://www.burningsea.com/wiki/api.php?action=parse&page=Treasure_Aisle&format=xml”);
// Access to the right node
$parse = $xml_content->parse;
$xml_text = $parse->text;
// Fix layout and links
$xml_text = str_replace(‘<a href=”/’, ‘<a targer=”_blank” href=”/wiki/’, $xml_text);
$xml_text = str_replace(‘src=”/wiki/’, ‘src=”http://domain.tld/wiki/’, $xml_text);
$xml_text = str_replace(‘&lt;/dt&gt;’, ”, $xml_text);
$xml_text = str_replace(‘&lt;/dd&gt;’, ”, $xml_text);
// Print my text
print (string)$xml_text;
// Here you are!
?>

Pour ma part, quand il faut le faire en php, l’appel est simple :
mwContent::printPage(‘Non de mA pagE’);

Enjoy the simplicity. FFS!

2 thoughts on “MediaWiki, l'API que tout le monde oublie…

  1. Magnificent items from you, man. I’ve be aware your stuff prior to and you are simply too wonderful. I actually like what you’ve received here, certainly like what you are stating and the way in which in which you are saying it. You’re making it enjoyable and you still take care of to keep it wise. I cant wait to learn much more from you. That is really a terrific website.

  2. I actually wanted to write a small remark to thank you for all the unique strategies you are sharing at this website. My incredibly long internet investigation has at the end been compensated with incredibly good concept to talk about with my colleagues. I ‘d suppose that we website visitors are undeniably fortunate to exist in a decent website with many special people with very beneficial tactics. I feel very much happy to have seen your entire web page and look forward to some more cool minutes reading here. Thank you once more for all the details.

Leave a Reply to Ohio Cancel reply

Your email address will not be published. Required fields are marked *