Table des matières
Système d'évènement
Traduction en cours
Le système d'évènement permet une gestion personnalisée en plus ou à la place du fonctionnement standard de n'importe quel partie du moteur Dokuwiki qui signale son action par ce système d'évènement.
Les gestionnaires personnalisés handlers
, aussi appelés “hooks”, peuvent être utilisés dans n'importe quel plugin ou script de template (ou même dans Dokuwiki lui-même). Les plugins d'Action sont spécifiquement prévus pour traiter les événements. Ils sont assurés d'être chargés au début de l’exécution et ont ainsi la possibilité de s'enregistrer avant qu'aucun événement ne soit généré. Other parts of DokuWiki may not be executed immediately or at all for any given page and execution pathway.
Il est également permis à du contenu personnalisé de Dokuwiki de créé et généré un signal d'évènement le concernant.
Le système d'évènement est composé de trois parties
- the main event handler or controller. This is a DokuWiki global,
$EVENT_HANDLER
. Les scripts qui souhaitent recevoir le signal d'un évènement doivent enregistrer leur intéret pour ce dernier. Lorsqu'un événement envoi son signal, les hooks (ou gestionnaires d'évènements) enregistrés sont exécutés les uns après les autres et reçoivent en paramètre l’évènement concerné. - individual event handlers or hooks. These are functions that wish to receive a particular event. The Action Plugin is a vehicle specifically for these functions, however they can also be part of templates, plugins of other types or the main DokuWiki scripts.
Detailed information describing existing events and when they occur is provided in the events list.
Objet d'évènement
Nom de la classe : Doku_Event
Un Objet d'évènement est constitué de :
- Propriétés publiques
name
, (lecture seule) hooks must use this to register to process a particular eventdata
, (lecture/écriture) data pertaining to the event, hooks have an opportunity to inspect and modify thisresult
, (lecture/écriture) available after the default action has taken place to hooks that have registered for theafter
advise.canPreventDefault
, (lecture seule) informs a hook whether or not the default action can be prevented
- Propriétés privées
_default
(booléen, initiée àtrue
), whether or not the default action associated with the event should be carried out. Interact with this property via thepreventDefault()
method._continue
(booléen, initiée àtrue
), whether or not to continue sending the event to registered hooks that have yet to receive it. Interact with this property via thestopPropagation()
method.
- Méthodes publiques
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 placepreventDefault()
- prevent the default action taking place
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.
Enregistrement pour réception du signal d'un évènement
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 in inc/events.php
.
Use register_hook($event, $advise, $obj, $method, $param=null, $seq=0)
with the arguments:
$event
string, name used by the event$advise
string,BEFORE
orAFTER
, the advise the hook wished to receive$obj
object, object in whose scope method is to be executed. Ifnull
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), [develonly] 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
orPHP_INT_MAX
, in an attempt to be first or last. Be aware that these values provide no guarantee of being first/last as more than one plugin can use them.
Signalling an Event
An event can be signalled in three ways.
- The simplest is to use the function wrapper
trigger_event()
. This function takes all the parameters necessary to create an event object and trigger it.
Usetrigger_event($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 theDoku_Event
object, where the Doku_Event is available in handlers.trigger_event(<EVENT_NAME>, <event data>, <action callback>, <can prevent default>)
- managing the whole event signalling process with advise_before($enablePreventDefault = true) and advise_after() on the Doku_Event object. Use this method when there is a default action but it not possible to package it as a PHP callback function.
$evt = new Doku_Event(<EVENT_NAME>, <event data>); if ($evt->advise_before(<can prevent default>)) { // default action code block } $evt->advise_after(); unset($evt);
Examples
(These are examples only and may not exist in DokuWiki.)
On Wiki page save
// 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 = array($id,$wikitext); $ok = trigger_event('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()'' $evt = new Doku_Event('ACTION_ACT_PREPROCESS', $ACT); if ($evt->advise_before()) { /* process $ACT normally */ } $evt->advise_after(); unset($evt); // in ''inc/template.php tpl_content()'' default: /* unrecognised $ACT value */ $evt = new Doku_Event('TPL_ACT_UNKNOWN', $ACT); if ($evt->advise_before()) { print "unknown action"; } $evt->advise_after(); unset($evt);
Possible handlers, customer form processing, additional do
commands from template UI.
On handler instruction list completion
// event: ''PARSER_HANDLER_DONE'' // data: the handler, including the completed instruction list // action: none // in ''inc/parser/handler.php _finalize() trigger_event('PARSER_HANDLER_DONE',$this);
possible handlers, footnote replacement plugins, enhanced TOC handlers
See also
- Events List with existing events.
- Use Action plugins to register handlers on events.
- More about Event handlers
- Examples of event handlers code