Action Plugins

Core code

inc/events.php

<?php
/**
 * DokuWiki Events
 *
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Christopher Smith <chris@jalakai.co.uk>
 */
 
if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
require_once(DOKU_INC.'inc/pluginutils.php');
 
class event {
  var $name = '';
  var $data = NULL;
  var $action = NULL;
 
  var $_default = true;
  var $_continue = true;
  var $_result = NULL
 
  function event($name, &$data, $fn=NULL) {
    global $EVENT_HANDLER;
 
    $this->name = $name;
    $this->data =& $data;
    $this->action = $fn;
 
    if (!isset($EVENT_HANDLER)) $EVENT_HANDLER = new event_handler();
  }
 
  function advise() {
    global $EVENT_HANDLER;
 
    return $EVENT_HANDLER->process_event($this,'');
  }
 
  function trigger() {
    global $EVENT_HANDLER;
 
    if ($EVENT_HANDLER->process_event($this,'before')) {
      if (!is_null($this->fn)) {
        $result = call_user_fn($this->fn, $this->data);
 
        $EVENT_HANDLER->process_event($this,'after');
      }
    }
 
  }
 
  function stopPropagation() { $this->_continue = false;  }  
  function preventDefault() { $this->_default = false;  }
}
 
class event_handler {
 
  var $hooks = array();
 
  function event_handler() {
 
    // load action plugins
    $plugin = NULL;
    $pluginlist = plugin_list('action');
 
    foreach ($pluginlist as $plugin_name) {
      $plugin =& plugin_load('action',$plugin_name);
 
      if ($plugin !== NULL) $plugin->register($this);
    }
  }
 
  function register_hook($event, $plugin, $method, $param) {
    $this->hooks[$event][] = array($plugin, $method, $param);
  }
 
  function process_event(&$event,$advice) {
 
    $evt_name = $event->name . ($advice ? '_'.$advice : '');
 
    if (!empty($this->hooks[$evt_name])) {
      $hook = reset($this->hooks[$evt_name]);
      do {
        list($plugin, $method, $param) = $hook;
        $plugin->$method($param, $event);
 
      } while ($event->_continue && $hook = next($this->hooks[$evt_name]));
    }
 
    return $event->_default;
  }
}

Sample Action Plugin

lib/plugins/test/action.php

<?php
/**
 * Action Plugin: 
 * 
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Christopher Smith <chris@jalakai.co.uk>  
 */
 
if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'action.php');
 
/**
 * All DokuWiki plugins to extend the parser/rendering mechanism
 * need to inherit from this class
 */
class action_plugin_test extends DokuWiki_Action_Plugin {
 
 
    /**
     * return some info
     */
    function getInfo(){
      return array(
        'author' => 'Christopher Smith',
        'email'  => 'chris@jalakai.co.uk',
        'date'   => '2006-04-13',
        'name'   => 'Test Plugin',
        'desc'   => 'some action',
        'url'    => 'http://wiki.jalakai.co.uk/tutorials:action_plugins',
      );
    }
 
    /*
     * plugin should use this method to register its handlers with the dokuwiki's event controller
     */
    function register(&$controller) {
      $controller->register_hook('BEFORE_ACTION', $this, 'handle_action', NULL);
      $controller->register_hook('BEFORE_RENDER', $this, 'handle_render', NULL);
    }
 
    function handle_action($param, &$event) {
      if ($event->data == 'recent') $event->data = 'revisions';
    }
 
    function handle_render($param, &$event) {
      print "hello";
      $event->preventDefault();
 
    }
 
 
}
 
//Setup VIM: ex: et ts=4 enc=utf-8 :

 
tutorials/action_plugins.txt · Last modified: 2006/04/13 18:07 by chris
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki