DokuWiki

It's better when it's simple

User Tools

Site Tools


Sidebar

Translations of this page?:

Learn about DokuWiki

Advanced Use

Corporate Use

Our Community


Follow us on Facebook, Twitter and other social networks.

devel:action_plugins

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>(Doku_Event $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>, <parameter to be passed to event handler>,<sequence number>);
}

The $controller->register_hook() function is used to register the event. The parameters are:

  1. <EVENT NAME>: Name of the event. All available events can be found at the Event Reference List.
  2. <EVENT ADVISE>: can be either BEFORE or AFTER. This determines when you want to invoke the given event.
  3. $this: An object reference to your action class containing the <event handler function>, usually $this.
  4. <event handler function>: Name of the function to handle the event as string.
  5. <parameter>: (optional) parameter will passed directly and unchanged to your <event handler function>.
  6. <sequence number>: (optional) used to affect the order in which hooks are executed. Defaults to 0. It is recommended to use ranges of sequence numbers and avoid +/- PHP_INT_MAX:
-3999 - -3000 for “very early”
-2999 - -2000 for “earlier”
-1999 - -1000 for “early”
-999 - -1 for “earlier than default”
0 default
1 - 999 for “later than default”
1000 - 1999 for “late”
2000 - 2999 for “later”
3000 - 3999 for “very late”

<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:

  1. $event: The event object. Further information on the passed event object can be found on the Event page.
  2. $param: Data passed to the register_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:

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(Doku_Event $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(Doku_Event $event, $param) {
        $event->data[] = array (
            'type' => 'format',
            'title' => $this->getLang('qb_abutton'),
            'icon' => '../../plugins/actionexample/abutton.png',
            'open' => '<abutton>',
            'close' => '</abutton>',
        );
    }
 
}

Sample: handle ajax requests

1)
defined in lib/plugins/action.php
devel/action_plugins.txt · Last modified: 2015-05-27 13:43 by 130.112.1.3