Wiki source for ActionInfo

Show raw source

====How actions work and how to write your own====

>>==See also==
[[UsingActions | Using actions in Wikka pages]]
[[ | Coding Guidelines for Wikka contributors]]
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//?

a) 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.
a) for code that **does something to** a page (like cloning, deleting, editing...), you would probably use a handler

==Two types of actions==
{{since display="inline" version="1.3.6"}} There are two types of actions: Those that act on the wiki page prior to the markup being processed ("traditional" actions, designated by ""{{...}}""), and the new **post-formatter actions** (designated by ""{{{...}}}""). The main difference between the two is that the new post-formatter action can access the contents of the wiki page it's invoked in after the markup has been processed into HTML. For instance, [[TOCActionInfo | the new {{{toc}}}]] post-formatter action will generate a table of contents for all headings on a wiki page, and uses the id generated for each heading by the Wikka formatter. Since this can't happen prior to the page being rendered by the formatter, ""{{{toc}}}"" must be implemented as a post-formatter action. The formatted text is made available to the action using ""<tt>$this->config['text']</tt>"".

If this made no sense to you, don't worry! Almost always you will want to create a "traditional" action using the ""{{...}}"" syntax.

==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 [[WikkaFeatures | 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())

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) {
$value = $this->htmlspecialchars_ent($value) # Sanitize your input!
// 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 you use a define statement, make it conditional using the syntax ##if (!defined(...))##
- if you define a function, make it conditional using the syntax ##if (!function_exists(...))##
%%(php)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()

//to be continued//
CategoryEN - CategoryReview
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki