lib/plugins/action.php
This is an old revision of the document!
Table of Contents
Action Plugins
Action plugins are designed to work with DokuWiki events to allow for customisation/extension of any part of DokuWiki that signals its activity using events. They are a way to modify many aspects of how DokuWiki behaves in certain cases independent of a page's syntax. To be able to modify a DokuWiki internal behavior it needs to trigger an event. Your action plugin can register as a handler for such an event and then work with the given event data.
Working principle
Action plugins are loaded before any significant DokuWiki processing takes place. Immediately after loading, each plugin is called by its register()
method to give it the opportunity to register any of its event handlers. When an event is signaled all event handlers registered for that event are called in turn (and in no particular order [ Since release 2014-05-05 “Ponder Stibbons” - and in ascending order of the $seq number used to register them ]) and passed the event object by reference. The handler has the opportunity to take action based on the event data and to alter either the event data or the event's subsequent processing. For more details of how the events system works and lists of events refer to the events page.
Synopsis
An Action Plugin Example needs:
- class name
action_plugin_example
- which extends DokuWiki_Action_Plugin1).
- to be stored in a file
lib/plugins/example/action.php
.
Moreover, a plugin.info.txt file is needed. For full details of plugins and their files and how to create more action components refer to plugin file structure.
- The plugin must declare one method
register()
and some handler that is registered there. - External libs must be loaded at the time the plugin needs them or in the constructor and not at the top of the file
Required methods
An action plugin requires at least two methods:
register(Doku_Event_Handler $controller)
Use this method to register your handlers with the DokuWiki's event controller<event handler>(&$event, $param)
Your event handler(s), that perform your actions when they are triggered.
You can register multiple events in a single action plugin. When doing this you may need multiple <event handler>()
functions.
Inherited methods
- See common plugin functions for inherited function available to all plugins. e.g. localisation, configuration and introspection.
register() method
The register()
method is used to subscribe an event. The following code shows a generic version to register an event.
/** * plugin should use this method to register its handlers with the DokuWiki's event controller * * @param Doku_Event_Handler $controller DokuWiki's event controller object. Also available as global $EVENT_HANDLER * * @return not required */ public function register(Doku_Event_Handler $controller) { $controller->register_hook(<EVENT NAME>, <EVENT ADVISE>, $this, <event handler function>, <parameters to be passed to event handler>,<sequence number>); }
The $controller->register_hook()
function is used to register the event. The parameters are:
<EVENT NAME>
: Name of the event. All available events can be found at the Event Reference List.<EVENT ADVISE>
: can be eitherBEFORE
orAFTER
. This determines when you want to invoke the given event.$this
: An object reference to your action class containing the<event handler function>
, usually$this
.<event handler function>
: Name of the function to handle the event as string.<parameters>
: (optional) this will passed directly and unchanged to your<event handler function>
.<sequence number>
: (optional) used to affect the order in which hooks are executed.
<event handler>() method
Have as many as necessary, can be given any name not already in use in this plugin or its ancestor classes. This function must be public. It will be called by DokuWiki's event controller.
The passed arguments are:
$event
: The event object. Further information on the passed event object can be found on the Event page.$param
: Data passed to theregister_hook()
function, when this handler was registered.
/** * custom event handler * * @param Doku_Event $event event object by reference * @param mixed $param the parameters passed to register_hook when this * handler was registered * * @return not required */ public function <event_handler>(Doku_Event &$event, $param) { // custom script statements ... }
Further reading
Examples
Here some examples:
- Below: Sample Action Plugin – include javascript file in all pages
- Below: Sample Action Plugin – insert button in toolbar
- Examples of Event handlers code
- eventCheck Plugin – plugin to register all event and call them
- Caching – selective disabling
- Headers and Footers – add your own stuff to top and bottom of page
- Content Modification – points event that handles directly wikicontent
- Dumps of Event Objects – examples of event objects for some events
- Implementation of a custom Section Editor
Sample: add always a JavaScript file
Insert a javascript script link in all pages.
- Register the TPL_METAHEADER_OUTPUT event, with a before EVENT_ADVISE.
- Add javascript information to “script” meta headers as array type.
- action.php
<?php /** * Example Action Plugin: Example Component. * * @author Samuele Tognini <samuele@cli.di.unipi.it> */ if(!defined('DOKU_INC')) die(); class action_plugin_example extends DokuWiki_Action_Plugin { /** * Register its handlers with the DokuWiki's event controller */ public function register(Doku_Event_Handler $controller) { $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, '_hookjs'); } /** * Hook js script into page headers. * * @author Samuele Tognini <samuele@cli.di.unipi.it> */ public function _hookjs(&$event, $param) { $event->data['script'][] = array( 'type' => 'text/javascript', 'charset' => 'utf-8', '_data' => '', 'src' => DOKU_PLUGIN.'example/example.js'); } }
Sample: add toolbar button
Inserts a button into the editor toolbar:
- registers as handler for the TOOLBAR_DEFINE event with an AFTER advise
- adds a button definition to the event's
data
- addbuton.php
<?php /** * Example Action Plugin: Inserts a button into the toolbar * * @author Gina Haeussge <osd@foosel.net> */ if (!defined('DOKU_INC')) die(); class action_plugin_actionexample extends DokuWiki_Action_Plugin { /** * Register the eventhandlers */ public function register(Doku_Event_Handler $controller) { $controller->register_hook('TOOLBAR_DEFINE', 'AFTER', $this, 'insert_button', array ()); } /** * Inserts the toolbar button */ public function insert_button(& $event, $param) { $event->data[] = array ( 'type' => 'format', 'title' => $this->getLang('qb_abutton'), 'icon' => '../../plugins/actionexample/abutton.png', 'open' => '<abutton>', 'close' => '</abutton>', ); } }