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
$EVENT_HANDLER
. Scripts that wish to receive an event need to register their interest with this object. When an event is signalled it will cycle through all the registered hooks (or event handlers) passing them the event in turn.Detailed information describing existing events and when they occur is provided in the events list.
Class name dokuwiki\Extension\Event
An event object consists of:
name
, (readonly) hooks must use this to register to process a particular eventdata
, (read/write) data pertaining to the event, hooks have an opportunity to inspect and modify thisresult
, (read/write) available after the default action has taken place to hooks that have registered for the after
advise.canPreventDefault
, (readonly) informs a hook whether or not the default action can be preventedrunDefault
(boolean, initial value true
), whether or not the default action associated with the event should be carried out. Interact with this property via the preventDefault()
method.mayContinue
(boolean, initial value true
), whether or not to continue sending the event to registered hooks that have yet to receive it. Interact with this property via the stopPropagation()
method.trigger()
- automated signalling of events. This method accepts two optional parameters, the default action (callback), and whether or not it may be prevented (bool) and returns the results of the event. It looks after the whole event process, signalling the “BEFORE” advise, triggering the default action and signalling the “AFTER” advise.stopPropagation()
- stop any further processing of the event by event handlers this function does not prevent the default action taking placemayPropagate()
- may the event propagate to the next handler?preventDefault()
- prevent the default action taking placemayRunDefault()
- should the default action be executed?advise_*()
methods - for use when the signalling script wishes to handle the complete event signalling process (perhaps when functionalising a default action is not appropriate).advise_before()
- accepts one parameter, a boolean indicating whether the default action can be prevented, issues the “BEFORE” signal.advise_after()
- issues the “AFTER” signal.
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:
$event
string, name used by the event$advise
string, BEFORE
or AFTER
, the advise the hook wished to receive$obj
object, object in whose scope method is to be executed. If null
the method is assumed to be a globally available function$method
function, event handler function. More info at the Event handlers page.$param
mixed (optional), the data to be passed to the event handler. Default null.$seq
int (optional), sequence number used to control the order in which hooks are executed. Hooks are executed in ascending $seq order. If two or more hooks have the same $seq value, their order (relative to each other) is undefined. Hooks can use -PHP_INT_MAX
or PHP_INT_MAX
, in an attempt to be first or last, but that is not recommended. Be aware that these values provide no guarantee of being first/last as more than one plugin can use them.An event can be signalled in three ways.
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.result
attribute of the Event
object, where the Event is available in handlers. Event::createAndTrigger(<EVENT_NAME>, <event data>, <action callback>, <can prevent default>)
$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);
(These are examples only and may not exist in DokuWiki.)
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.
// 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.
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