[dokuwiki] Re: Plugins that "run once"...
- From: Ben Coburn <btcoburn@xxxxxxxxxxxxx>
- To: dokuwiki@xxxxxxxxxxxxx
- Date: Thu, 6 Jul 2006 19:22:03 -0700
On Jul 6, 2006, at 11:58 AM, Terence J. Grant wrote:
Referrer and analytics could "run once" on page
load, or (ideally for analytics) during the meta headers function...
So is it common practice to run the function //during// the "register"
function? Or what's the proper way to do this?
Doing work in the 'register' method will produce unpredictable results
because other plugins that modify the behavior of DokuWiki may not have
been registered yet. Action plugins that want to run once per page
request should register for the 'DOKUWIKI_STARTED' or 'DOKUWIKI_DONE'
events.
Also, the multiple wiki pages could act as an "exported function",
though again I'm not sure what the best way to handle this would be...
any ideas?
If you want to add a function to the global scope, just define it (in
the plugin file) outside the scope of the plugin class. The function
will be included along with the plugin class when the plugin is loaded.
Remember to make function name unique so that it will not collide with
other functions. (Using the plugin name in the function name is
probably a good idea.)
If you want to call a method on the plugin object, it becomes a little
more complicated. This will give the method access to the plugin
object's members through the $this keyword. To call a plugin method you
could do the following:
$ret = false;
$obj =& plugin_load('action', 'example'); // plugin type, plugin name
if ($obj!==NULL) { $ret = $obj->method_name($arg1, $arg2); }
Adding a method to 'inc/pluginutils.php' would simplify calling plugin
methods. I think something like this would cover all possibilities:
function plugin_call_method($type, $name, $method, $args, &$ret) {
$ret = NULL;
$obj =& plugin_load($type, $name);
if ($obj!==NULL && method_exists($obj, $method)) {
$ret =& call_user_func_array(array($obj, $method), $args);
return true;
} else {
return false;
}
}
// example usage in template....
if (plugin_call_method('action', 'example', 'generate_content',
array('arg1', 2), $ret)) { echo($ret); }
Regards, Ben Coburn
-------------------
silicodon.net
-------------------
--
DokuWiki mailing list - more info at
http://wiki.splitbrain.org/wiki:mailinglist
Other related posts: