如何使用MediaWiki解析器从wikitext中获取HTML


How to use the MediaWiki parser to get HTML from wikitext

我正在尝试使用维基百科的MediaWiki解析器将维基百科标记文本解析为HTML。我在这里浏览了手册-https://www.mediawiki.org/wiki/Manual:Parser.php然而,由于我对PHP完全陌生,我无法编写测试脚本

以下是我想要解析并转换为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.

以下是解析wikitext的最小代码(在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;

再见!

您甚至不必使用PHP。您可以使用维基百科的API(或者在您自己的MediaWiki安装上使用API)。有关详细信息,请参见分析wikitext。

您可以使用JWPLhttp://code.google.com/p/jwpl/,它将与wiki的本地副本一起使用。加载转储,转换为Datamaschine,导入数据库,用它做你想做的事。

   //<myname></myname>    
    public static function onParserFirstCallInit( Parser $parser ){
        $parser->setHook('myname', 'MyClass::getOutputHtml');
    }
    public static function getOutputHtml(){
        $localParser = new Parser();
        $input = OtherClass::myOutput();
        $context = new RequestContext();
        $title = $context->getTitle();
        $parserOptions = new ParserOptions;
        $output = $localParser->parse($input, $title, $parserOptions);
        return $output->getText();
    }