Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the MediaWiki parser to get HTML from wikitext

I am trying to use Wikipedia's MediaWiki parser to parse Wikipedia mark up text to HTML. I went through the manual here - https://www.mediawiki.org/wiki/Manual:Parser.php However, since I am completely new to PHP, I am unable to write a test script,

Here is a sample input that I would want to parse and convert to HTML:

Shakespeare's sonnets
==Characters==
When analysed as characters, the subjects of the sonnets are usually referred
to as the Fair Youth, the Rival Poet, and the Dark Lady. The speaker expresses
admiration for the Fair Youth's beauty, and later has an affair with the Dark
Lady. It is not known whether the poems and their characters are fiction or
autobiographical; scholars who find the sonnets to be autobiographical, notably
[[A. L. Rowse]], have attempted to identify the characters with historical
individuals.
like image 258
Vansh Khurana Avatar asked Oct 21 '22 02:10

Vansh Khurana


2 Answers

Here is the minimal code for parsing wikitext (tested on MediaWiki 1.32):

$text = "Your [[wikitext]]";
$title = $skin->getTitle(); // Get the title object from somewhere or use $wgTitle
$parser = new Parser;
$parserOptions = new ParserOptions;
$parserOutput = $parser->parse( $text, $title, $parserOptions );
$html = $parserOutput->getText();
echo $html;

Bye!

like image 111
Sophivorus Avatar answered Oct 29 '22 06:10

Sophivorus


You don't even have to use PHP. You can use Wikipedia's API (or the API on your own MediaWiki installation). See Parsing wikitext for more information.

like image 34
Mark A. Hershberger Avatar answered Oct 29 '22 05:10

Mark A. Hershberger