Manual:Extension data

Extension data (ExtensionData) are small pieces of arbitrary (meta)data that an extension can add to, and later retrieve from, the ParserOutput . For instance, you can use it to register metadata around the use of your parser function on a page. Then when a hook is called for special pre-processing of the page output, it allows you to check for any metadata that might be relevant.

Each data item consists of a key and a value. The value must be either a value or integer and it must be suitable for re-use as an array key, which imposes some restrictions on the characters you can use.

Methods edit

  • ParserOutput::appendExternalData() (since MW 1.38) - lets you use keys to attach extension data to the ParserOutput and lets you merge multiple pieces of data by reusing the method under the same key. It is no longer recommended to use ::setExternalData.
  • ParserOutput::getExternalData() - lets you retrieve data by key. The result returned, if not null, is an array: the array key contains the stored value and the array value is an auto-incremental integer.

Caching and storage edit

Extension data are cached along with the ParserOutput object. Unlike page properties, they are not recorded in the database.

Example edit

Say you have written a parser function and need to use a MediaWiki hook to do some processing on the parser only if the parser function is used any number of times. In the code for your parser function, you could have something like:

$parserOutput = $parser->getOutput();
$parserOutput->appendExtensionData( "my-extension-data", "my-parser-function" );

Then when the hook is called, you can create a condition:

$myExtData = $parser->getOutput()->getExtensionData( "my-extension-data" );
if ( $myExtData !== null && array_key_exists( "my-parser-function", $myExtData ) ) {
   $counter = $myExtData["my-parser-function"];
   // do stuff