Jump to content

Topic on Project:Support desk

Replace old extension code that parses wikitext

5
Summary by Jonathan3

Used API.

Jonathan3 (talkcontribs)

I need to update some extension code to work with MW1.39. I believe the problem is because it relies on Manual:$wgParser.

The code is from https://stackoverflow.com/questions/18379992/how-to-use-the-mediawiki-parser-to-get-html-from-wikitext

$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;

I think the replacement for the $parser = line is $parser = \MediaWiki\MediaWikiServices::getInstance()->getParser();

But now $parserOptions = new ParserOptions needs a UserIdentity object and I'm stuck there.

Any pointers would be gratefully received.

Bawolff (talkcontribs)

What is proper to do here depends on the context of where this code is running from. Where is this code running? What is it trying to do?


The code you posted doesn't even use $wgParser.

That said, ParserOptions::newFromAnon() may be what you are looking for.

Jonathan3 (talkcontribs)

It's an extension I updated years ago based on the old "Bulletfeed" extension. When used with "action=bullet_feed" it turns HTML bullet points into an RSS feed. So basically the best way of getting the HTML content of the page is what I need. The little I knew is now very rusty, as you were able to tell already... The extension isn't exactly the same as the code above (e.g. it doesn't just echo $html at the end).

Jonathan3 (talkcontribs)

Basically I can get the page wikitext all right, but would like to parse it to HTML. The reason is that the wikitext calls a template. If I had unlimited time I would probably rewrite it from scratch. But if I could find out how to convert wikitext to HTML, including expanding templates, then that sticking plaster would be good enough for now!

Jonathan3 (talkcontribs)

In the end I used the API, based on the PHP code here: API:Parsing_wikitext#PHP. For some reason, "http_build_query" didn't work, so I just created $url by hand.