Manual:Hooks/UnknownAction
File:OOjs UI icon alert-destructive.svg | <translate> This feature was removed from MediaWiki core in version <tvar name=ver>1.32.0</tvar> (after being deprecated in <tvar name=4>1.19.0</tvar>).</translate> <translate> Please see <tvar name=page>$wgActions</tvar> for an alternative way to use this feature.</translate> |
UnknownAction | |
---|---|
<translate> Available from <tvar name=1><translate> version <tvar </tvar></translate> <translate> Removed in <tvar name=1><translate> version <tvar (Gerrit change 465791)</tvar></translate> Used to add new query-string actions | |
<translate> Define function:</translate> | public static function onUnknownAction( $action, Article $article ) { ... }
|
<translate> Attach hook:</translate> | <translate> In <tvar name=1>extension.json</tvar>:</translate>
{
"Hooks": {
"UnknownAction": "MediaWiki\\Extension\\MyExtension\\Hooks::onUnknownAction"
}
}
|
<translate> Called from:</translate> | <translate> File(s):</translate> MediaWiki.php |
<translate> Interface:</translate> | UnknownActionHook.php |
<translate> For more information about attaching hooks, see <tvar name=1>Manual:Hooks </tvar>.</translate>
<translate> For examples of extensions using this hook, see <tvar name=cat>Category:UnknownAction extensions</tvar>.</translate>
Details
Actions for a page are passed to MediaWiki via the action parameter in the URL. For example, to edit the page 'Foo' the action edit
would be appended to the main article URL, giving http://www.mywiki.org/wiki/index.php?title=Foo&action=edit
.
This hook allows you to add custom actions to MediaWiki. If an action is requested that the software doesn't handle natively, this hook will be called.
Returning false will allow processing to continue, but it will not output any content for the page. You will need to use $wgOut->addWikiText(...);
and similar functions to create the appropriate page content for this action, and $wgOut->setPageTitle(...);
to set the title for display at the top of the page.
Returning true will cause the standard 'no such action' message that you get when entering &action=nonexistant_action.
$wgActions
.Arguments
- $action: action name
- $article: article "acted on" - this is a Page object, not the article name.
Example
// Register callback function for the UnknownAction hook.
$wgHooks['UnknownAction'][] = 'MyExtension_UnknownAction';
function MyExtension_UnknownAction( $action, $article ) {
global $wgOut, $wgRequest;
// If the requested action is not 'example_action' then don't do anything.
// We return true, indicating that MediaWiki should carry on as normal.
if ( $action != "example_action" )
return true;
// The view is the same for the main page and the talk page, so if we're on the
// talk page then we need to change $Title to point to the subject page instead.
$title = $article->getTitle();
if ( $title->isTalkPage() ) {
$title = $title->getSubjectPage();
}
// Set page title.
$wgOut->setPageTitle( 'Example Page Title' );
// Get some parameters from the URL.
$param = $wgRequest->getIntOrNull( 'example_param' );
// Do some internal stuff to generate the content (placed in $output).
// Output the results.
$wgOut->addHTML( $output );
// or
$wgOut->addWikiText( $output );
// Return false to tell MediaWiki that we have successfully generated the page
// contents and to skip processing of other hooks.
return false;
}
Migrating to $wgActions
To upgrade older code to work using the modern method of handling custom actions (required as-of MW 1.32 and recommended for MW >= 1.19) you would rewrite the above example as follows:
// Register the action in the array of available actions.
// Key is the action name (as supplied in the URL) and value is the name of a class that extends Action.
// If you only need to support more recent versions of MediaWiki, register the action using
// extension.json, instead.
$wgActions['example_action'] = 'ExampleAction';
class ExampleAction extends Action {
// The action is called 'example_action'. This class will only be used when the specified
// action is requested, so we no longer need to make an explicit check for the action
// type when processing the page code.
public function getName() {
// This should be the same name as used when registering the action in $wgActions.
return 'example_action';
}
// This is the function that is called whenever a page is being requested using this action.
// You should not use globals $wgOut, $wgRequest, etc. Instead, use the methods provided
// by the Action class (e.g. $this->getOutput()), instead.
public function show() {
// Create local instances of the context variables we need, to simplify later code.
$out = $this->getOutput();
$request = $this->getRequest();
// The view is the same for the main page and the talk page, so if we're on the
// talk page then we need to change $Title to point to the subject page instead.
$title = $this->page->getTitle();
if ( $title->isTalkPage() ) {
$title = $title->getSubjectPage();
}
// Set page title.
$out->setPageTitle( 'Example Page Title' );
// Get some parameters from the URL.
$param = $request->getIntOrNull( 'example_param' );
// Do some internal stuff to generate the content (placed in $output).
// Output the results.
$out->addHTML( $output );
// or
$out->addWikiText( $output );
}
}
See also
$wgActions
- MediaWikiPerformAction hook - used before default actions are called, used for overriding normal processes (ie: alternate action=edit, action=watch)