Revision [1858]

This is an old revision of ActionInfo made by DarTar on 2009-01-31 02:04:48.

 

How actions work and how to write your own



Technically, "actions" are nothing else than PHP | files, which are included and run by the Wikka engine.

As you know from UsingActions this tutorial, to use an action in a page you simply put the name of an action (and parameters if necessary) between double braces ({{action_name [parameter1="A"][ parameter2="B"]}}). The Wikka formatter then sends the text between the two brackets to the Action() core method . Everything after the first word is treated as a parameter of the action. If the corresponding action-file is found in the action directory, parameters are passed to it in the form of an array.

You can easily extend Wikka's functionality by writing your own actions and saving them in the actions directory. This page will give you some examples and hints on custom action creation.

1. Planning

The first step before writing an action is to understand what the goal of the action is and whether writing an action is the best choice for this goal.

Action vs. Handler
Often you will have to decide whether your code must be written as an action or a handler. The simplest way to decide between these two option is the following question:
Do you wish to add something to a page, or to do something with the page?

  1. for code that adds content or a service to a page (e.g. a google search form, a weather forecast, a table etc.), an action is probably your best choice.
  1. for code that does something to a page (like cloning, deleting, editing...), you would probably use a handler

Don't reinvent the wheel
There are many PHP developers out there, so chances are someone has already written the code you need. You are probably familiar with the official feature list but the (unofficial) code contributions directory and the User Contributions category could be a good place to start from. Many existing PHP scripts can easily be adapted to be plugged into Wikka as actions.

Find someone to help
When you start working on a new action, announce this at PluginsInDevelopment, someone else may be willing to help. The community mailing list and IRC channel are also a good place to find help and support from other Wikka users and contributors.

2. Security

You should always keep in mind that any user with [ACLInfo write-access]] to your wiki will be able to use an action by default. This means if you want your action to be limited to administrators or registered users, you have to take care of it in the action itself.

# Put this as the very first line of code inside your action
# The action won't work for non-administrator users.
if (!$this->IsAdmin())
{
    return;
}


Parameters
As said above, parameters are passed as an array to the action. A special name, $vars, is reserved for this array. You can use the following code to get the values of an action's parameters:

# action called as {{example parameter="thevalue"}} will create a variable $vars
# defined like this: $vars = array('parameter' => 'thevalue');
if (is_array($vars))
{
    foreach ($vars as $parm => $value)
    {
        // your code here.
    }
}


A string representation of all specified parameters is also available through the special variable, $vars['wikka_vars']. It is supported for backward compatibility, and is documented here to help you understand at a glance some actions like {{rss}}, but it's not recommended for use it in your action scripts.

Global methods and members
Using $this as a reference in your actions you can invoke any of Wikka's core methods. Every methods and members defined in the Wakka.class.php can be called by your actions.

Defining a function inside an action
Because an action can be called more than once, all the code that is inside it can be executed twice or more. The following is the general rule to be followed to avoid errors due to redeclarations:
if (!defined('D_ACTION_MYACTION_LOGSIZE')) define ('D_ACTION_MYACTION_LOGSIZE', 60);
if (!function_exists('f_action_myfunction_get_default_logsize'))
{
    function f_action_myfunction_get_default_logsize()
    {
        return D_ACTION_MYACTION_LOGSIZE;
    }
}


to be continued

CategoryEN - CategoryReview
There are no comments on this page.
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki