Manual:Hooks/APIQueryAfterExecute

From MediaWiki.org
Jump to: navigation, search
APIQueryAfterExecute
Available from version 1.14.0
Use this hook to extend core query modules
Define function:
public static function onAPIQueryAfterExecute( &$module ) { ... }
Attach hook:

In extension.json:

{
	"Hooks": {
		"APIQueryAfterExecute": [
			"MyExtensionHooks::onAPIQueryAfterExecute"
		]
	}
}

For MediaWiki ≤1.25:

$wgHooks['APIQueryAfterExecute'][] = 'MyExtensionHooks::onAPIQueryAfterExecute';
Called from: File(s): ApiQuery.php

For more information about attaching hooks, see Manual:Hooks.
For examples of extensions using this hook, see Category:APIQueryAfterExecute extensions.

Details[edit]

  • $module: Module object

Notes[edit]

Called after calling the execute() method of an API query module.

Extending[edit]

To extend from one particular API module as opposed to all of them, run an instanceof check against the particular class in the ./includes/API directory. So that, to extend the parse module you would do a check against the class ApiParse, returning true even if it isn't the module you intend to extend.

Sample code:

function apiExtensionFunction( &$module ) {
	if( !( $module instanceof ApiParse ) ) {
		return true;
	}
	// ... extension code here ...
}

Without adding this, all modules of the API will be affected by the extension.

To add additional output to the feed, the result must be pulled from the module. Original output may be modified using the same method.

Sample code:

$result = $module->getResult();
$data = $result->getData();

$status = $result->addValue(
	array( 'foo', 'bar' ),
	'etc',
	$addition_value
);

In effect, what the above sample will do is append a new value onto the data structure foo->bar so that at foo->bar->etc the value will be whatever is contained within $addition_value. Where in the hierarchy exactly things may go is up to the extension developer however each API feed does have a somewhat unique data hierarchy which it should placed into.