Manual:Hooks/UnknownAction
This feature was removed from MediaWiki core in version 1.32.0 (after being deprecated in 1.19.0). Please see $wgActions for an alternative way to use this feature. |
UnknownAction | |
---|---|
Available from version 1.4.0 Removed in version 1.32.0 (Gerrit change 465791) Used to add new query-string actions | |
Define function: | public static function onUnknownAction( $action, Article $article ) { ... }
|
Attach hook: | In extension.json:
{
"Hooks": {
"UnknownAction": "MediaWiki\\Extension\\MyExtension\\Hooks::onUnknownAction"
}
}
|
Called from: | File(s): MediaWiki.php |
Interface: | UnknownActionHook.php |
For more information about attaching hooks, see Manual:Hooks .
For examples of extensions using this hook, see Category:UnknownAction extensions.
Details
editActions 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
edit- $action: action name
- $article: article "acted on" - this is a Page object, not the article name.
Example
edit// 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
editTo 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
edit$wgActions
- MediaWikiPerformAction hook - used before default actions are called, used for overriding normal processes (ie: alternate action=edit, action=watch)