Table of Contents

Event System

The event system allows custom handling in addition to or instead of the standard processing for any part of DokuWiki which signals its activity via the event system. The custom handlers, or hooks, can be included in any plugin or template script (or even in DokuWiki itself). Action Plugins are specifically designed to take advantage of the events system. They are guaranteed to be loaded at the start of processing and to have an opportunity to register for events before any events have taken place. Other parts of DokuWiki may not be executed immediately or at all for any given page and execution pathway.

It is also possible for custom DokuWiki content to create and signal events of their own.

The event system consists of three parts

Detailed information describing existing events and when they occur is provided in the events list.

Event Object

Class name dokuwiki\Extension\Event

An event object consists of:

Registering to Receive an Event

To register a hook to receive an event, call the register_hook() method of the $EVENT_HANDLER. Action plugins can do this using the $controller parameter from within their own register() method. Other parts of DokuWiki should ensure they are either in global scope or declare $EVENT_HANDLER as a global. e.g.

global $EVENT_HANDLER;
$EVENT_HANDLER->register_hook( ... )

For up-to-date details of the register_hook() function and its parameters refer to its declaration.

Use register_hook($event, $advise, $obj, $method, $param=null, $seq=0) with the arguments:

Signalling an Event

An event can be signalled in three ways.

  1. The simplest is to use the function wrapper Event::createAndTrigger(). This function takes all the parameters necessary to create an event object and trigger it.
    Use Event::createAndTrigger($name, &$data, $action=null, $canPreventDefault=true) with the arguments:
    • $name string, name for the event
    • $data mixed, event data
    • $action callback (optional), default action given as php callback function. Default null.
    • $canPreventDefault boolean (optional), can hooks prevent the default action. Default true.
    • return mixed, the event result value after all event processing is complete. By default this is the return value of the default action. However it can be set or modified by event handlers hooks as it is stored in result attribute of the Event object, where the Event is available in handlers.
      Event::createAndTrigger(<EVENT_NAME>, <event data>, 
                    <action callback>, <can prevent default>) 
  2. using the trigger() method. This isn't recommended as it is better to use the Event::createAndTrigger() function wrapper described above.
    $event = new dokuwiki\Extension\Event(<EVENT_NAME>,<event_data>);
    $event->trigger(<default action>,<can prevent default>);
    unset($event);
  3. managing the whole event signalling process with advise_before($enablePreventDefault = true) and advise_after() on the Event object. Use this method when there is a default action but it not possible to package it as a PHP callback function.
    $event = new dokuwiki\Extension\Event(<EVENT_NAME>, <event data>);
    if ($event->advise_before(<can prevent default>)) {
      // default action code block
    }
    $event->advise_after();
    unset($event);

Examples

(These are examples only and may not exist in DokuWiki.)

On Wiki page save

use dokuwiki\Extension\Event;
 
// event:  'IO_WIKIPAGE_SAVE'
// data:   array(file name, the raw wiki page)
// action: save the raw wiki page to file name
// return: bool, page saved
$data = [$id, $wikitext];
$ok = Event::createAndTrigger('SAVE_WIKIPAGE', $data, io_savewikipage);

Possible handlers, indexers, translators.

Additional/Replacement ?do=... actions

// events:  'ACTION_ACT_PREPROCESS' & 'TPL_ACT_UNKNOWN'
// data:    $ACT  (value of the ''do'' query string variable)
// action:  none, handled by signalling script
 
// in ''inc/actions.php act_dispatch()''
$event = new dokuwiki\Extension\Event('ACTION_ACT_PREPROCESS', $ACT);  
if ($event->advise_before()) { 
    /* process $ACT normally */ 
}
$event->advise_after();
unset($event);
 
// in ''inc/template.php tpl_content()''
// default: unrecognised $ACT value
$event = new dokuwiki\Extension\Event('TPL_ACT_UNKNOWN', $ACT);
if ($event->advise_before()) { 
    print "unknown action"; 
}
$event->advise_after();
unset($event);

Possible handlers, customer form processing, additional do commands from template UI.

On handler instruction list completion

use dokuwiki\Extension\Event;
 
// event:  ''PARSER_HANDLER_DONE''
// data:   the handler, including the completed instruction list
// action: none
 
// in ''inc/parser/handler.php  _finalize()
Event::createAndTrigger('PARSER_HANDLER_DONE', $this);

possible handlers, footnote replacement plugins, enhanced TOC handlers

See also