Revision [641]

This is an old revision of ActionInfo made by DarTar on 2008-02-08 10:03:40.



How actions work and how to write your own

Technically, actions are nothing else then normal php-files, which are included through the wikka-engine:

As you know from Using actions in Wikka pages, you simply put the name of an action (and parameters if necessary) between {{ and }}. The wikka-formatter then sends the text between the two brackets to the function Action($action, $forceLinkTracking = 0) in the wikka.php . If you take a closer look at it, you will see that everything after the first word is treated as a parameter of the action. If the action-file is found, the parameters are given to it in form of an array.

You can easily extend wikka's functionality by writing your own actions and saving them under a name in the actions directory. This page intends to give you some help with it.

1. Planning

At first, you should think about what you want to do and if an action is the best choice for it.

Action vs. Handler
Mostly you will have to choose between action and handler. You can easily seperate them with the following question:
Do you add something to a page, or do you want to do something with the page?

  1. for an addition to a page (the google-form, a weather-forecast, a table), an action should be your choice.
  1. for doing something to a page (like cloning, deleting, editing...), use a handler

Let others do your work... ;-)
Since there are many people working with php, it is highly possible that someone has already written the code you need. You surely have looked at the (offical) WikkaFeatures and the Development-Category, but have you also seen the (unoffical) CodeContributions and the UserContributions-category?

It may take you some time to search all this places, but it is better than doing the same work twice.

... or at least help you
When you have started working on your action, announce this at PluginsInDevelopment, perhaps someone else wants to help you working on it.

2. Security

You should always keep in mind that every user, who has write-access to your pages, can use an action by default. That means if you want your action being limited to administrators or registered users, you have to take care of it in the action itself, because Wikka won't!

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

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

# 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 the parameters given 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 to use it in your action scripts.

Global methods and members
On the example above, you've seen the syntax $this. It refers to the main Wakka class, just as if your action were a method of this class. Thus, you have every methods and members defined for this class available for you.

define and 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 general rule you must take care of is:
if (!function_exists('f_action_myfunction_get_default_logsize'))
    function f_action_myfunction_get_default_logsize()

to be continued

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