What is the most practical way of calling back into an extension from a link in the MediaWiki UI?
I have an extension that will add a link (e.g. "Report") in the history and diff pages, that needs run code in my extension.
Is it possible to have a "generic" callback URL (e.g. index.php/PageName?action=report&edit=123
) or will I need to create a full special page (index.php/Special:Report?edit=123`) to handle this an execute my own code?
There won't be any more UI after this beyond reporting success or failure.
Both of those are possible.
To define a custom action like
action=report
, define a subclass of the Action class and add it to$wgActions
. (There's also an UnknownAction hook that can be used for the same purpose, but it's considered deprecated.)To define a new special page, you need to create a subclass of SpecialPage and add it to
$wgSpecialPages
. There's a lot more information at Manual:Special pages on mediawiki.org.There's also a third option you might want to consider:
$wgAPIModules
. Such API modules can then be invoked using AJAX code injected into the page by your extension (e.g. using ResourceLoader), and they can report the success of failure of the action back using JSON.Calling a custom API module via AJAX could offer a better user experience than just a plain link to a success / error page. For accessibility, though, you may want to provide both methods, so that your extension remains usable even for users without JavaScript support, or with JavaScript disabled. This is generally best done by writing your SpecialPage / Action and ApiBase subclasses as thin wrappers around a shared backend.