Developing extensions/Access to in-process data

From semantic-mediawiki.org
Smw-user-input-storage-process.png

This article gives a short introduction on how to gain access to in-process data when using an extension, through a registered hook, or an additional parser function.

Outline[edit]

The general outline of the process is this :

  • When the SemanticData object is in an intermediary state, i.e. when it is being processed in hooks or through the parser, it is attached to MediaWiki's ParserOutput to allow interdependent data exchange during the request. We will call the data at this stage in-process data.
  • To modify in-process data, extensions must do so before the LinksUpdateComplete (previously, LinksUpdateConstructed) hook is called. By this time, the ParserOutput must contain the most up-to-date SemanticData object.
  • When the hook is called, it will trigger the Store::updateData together with the accumulated data.

Use ParserData[edit]

To avoid loss of data or inconsistencies when accessing the SemanticData object, it is recommended to use a ParserData instance as gateway.

    // Create in-memory ParserOutput transfer object
    $parserData = ApplicationFactory::getInstance()->newParserData(
        $parser->getTitle(),
        $parser->getOutput()
    );
    // Access the SemanticData object
    $semanticData = $parserData->getSemanticData();

    // Do something

An individual extension is required to finalize its update process by invoking copyToParserOutput(), or else the extended SemanticData instance will not be transferred to the ParserOutput object :

    // Ensures that objects are pushed to the ParserOutput
    $parserData->copyToParserOutput();

This ensures that the ParserOutput contains the most up-to-date SemanticData object at the time when the LinksUpdateComplete hook is called. When that hook is called, it will trigger the Store::updateData together with the accumulated data.

Notes[edit]

ParserData::updateStore is not for public use therefore an extension is not expected to call this function.

Examples[edit]

Here are some examples of extensions with parser functions that create SemanticData objects :

Extensions that add and consolidate SemanticData when using hooks :

  • SemanticBreadcrumbLinks, using the ParserAfterTidy hook: see HookRegistry::addCallbackHandlers and SubpageParentAnnotator::addAnnotation
  • SemanticInterlanguageLinks: similarly using the ParserAfterTidy hook: see HookRegistry::addCallbackHandlers, etc.
  • WSSlots: see WSSlotsHooks::onBeforeDataUpdateComplete, which uses the BeforeDataUpdateComplete hook, for a way in which the SemanticData of multiple MCR slots can be combined.

See also[edit]

  • #1202 SemanticData access from an extension (or how to add property values). And see also this.