MediaWiki: How to update a link status programmatically

97 views Asked by At

My extension renders additional links on a page (that is adds some <a href='...'>...</a> to the page text (in HtmlPageLinkRendererEnd hook)).

See small arrows in https://withoutvowels.org/wiki/Tanakh:Genesis_1:1 for an example. The arrows are automatically added by my extension (sorry, at the time of writing this the source code is not yet released).

The problem is that red/blue ("new") status is not updated for links which I add.

Please explain how to make Wikipedia to update color of my links as appropriate together with regular [[...]] MediaWiki links.

My current workaround is to run php maintenance/update.php. It is a very bad workaround. How to do it better?

2

There are 2 answers

0
porton On BEST ANSWER

With valuable help of Wikitech-l mailing list, I found a solution.

The solution is to use ParserAfterTidy hook.

public static function onParserAfterTidy( &$parser, &$text ) {
            # ...

            $parserOutput = $parser->getOutput();

            foreach($parserOutput->getLinks() as ...) {
                    # ...

                    $parserOutput->addLink( Title::newFromDBkey(...) );
            }
}
8
Tgr On

Normally you'd use LinkRenderer to create the links and LinkBatch to make the page existence check efficient (you don't want a separate SQL query for each link). You can't really do that in HtmlPageLinkRendererEnd since you only learn about the links one by one.

The way the parser deals with this is that it replaces links with a placeholder and collects them in a list, then after parsing is mostly done it looks them all up at once and then switches the placeholders with the rendered links. You can probably hook into somthing that happens between the two (e.g. ParserAfterParse), get the list of links from the parser and use them to build a list of your own links.