How to add your own 'feature' in Wikka? (by example)
The goal of this page is to give you an overview. For more detailed information please visit one of the related pages...Add the (html)code within double-double-quotes
- If you have set "double_doublequote_html" to "safe" (or "raw", less secure) in your config file, then you could include html tags within double-double-quotes. With this method you can add more complex tables, an example of an user-interface,...
""<span style="color: #FF0000">Hello World!</span>""
- Related Pages: ConfigurationOptions UsingHTML
Add the (html)code into an action
- If you want to re-use the code (JavaScript, a fixed block, ...), why not add it in a central place? This can be done by placing the code into an action. An action is a php script that is placed into the actions/ subdir and is called in a page as {{scriptname}} (without the ".php" extension)
- e.g. as html
<span style="color: #FF0000">
Hello World!</span>
- e.g. as php
<?php
print "<span style=\"color: #FF0000\">";
print "Hello World!";
print "</span>\n";
?>
print "<span style=\"color: #FF0000\">";
print "Hello World!";
print "</span>\n";
?>
- Two special actions are actions/header.php and actions/footer.php who take care of the layout of Wikka (together with the CSS files).
- Related Pages: ActionInfo UsingActions
Make two actions x_start and x_stop
- A first step in making actions more dynamic is to split them into a start and a stop part that you wrap around your block:
{{red_start}}Hello World!{{red_stop}}
- The nice thing about this solution is that you can still keep on using wiki formatting within the two actions.
- e.g. actions/red_start.php
<span style="color: #FF0000">
- e.g. actions/red_stop.php
</span>
- Related Pages: ActionInfo
Make an action and use the variable $wikka_vars
- In stead of two actions, you could combine them and add a param to do the switch...
{{red start}}Hello World!{{red stop}}
- $wikka_vars is defined as the string after the first space. (so in this example "start" in the first occurrence and "stop" in the second)
- e.g. actions/red.php
<?php
if(isset($wikka_vars) AND $wikka_vars<>""){
switch($wikka_vars){
case "start": print "<span style=\"color: #FF0000\">"; break;
case "stop": print "</span>"; break;
}
}
?>
if(isset($wikka_vars) AND $wikka_vars<>""){
switch($wikka_vars){
case "start": print "<span style=\"color: #FF0000\">"; break;
case "stop": print "</span>"; break;
}
}
?>
- So why not passing the string? Can also be done, but you are less sure in your script what your going to receive from a user...
{{red Hello World!}}
- e.g. actions/red.php
<?php
print "<span style=\"color: #FF0000\">";
print $wikka_vars;
print "</span>\n";
?>
print "<span style=\"color: #FF0000\">";
print $wikka_vars;
print "</span>\n";
?>
- Remark: If your $wikka_vars doesn't just contain text, but also wiki formating, you will need to send it to the function Format first...
So the second "print" line will become: print $this->Format( $wikka_vars );
- Related Pages: ActionInfo actions/include.php
Make an action and use the array $vars
- If you use the format key="value" this data is used to build the array $vars out of $wikka_vars (both can be used in the same action).
{{red color="#FF0000" string="Hello World!"}}
- e.g. actions/red.php
<?php
if ( isset($vars["color"]) AND $vars["color"]<>"" ){ $c = $vars["color"]; }else{ $c="#999999"; }
print "<span style=\"color: ". $c ."\">";
print $this->Format( $wikka_vars );
print "</span>\n";
?>
if ( isset($vars["color"]) AND $vars["color"]<>"" ){ $c = $vars["color"]; }else{ $c="#999999"; }
print "<span style=\"color: ". $c ."\">";
print $this->Format( $wikka_vars );
print "</span>\n";
?>
- Related Pages: ActionInfo actions/image.php
Make a formatter
- If an action is "insert code here", then a formatter would be "do something with this block". Within a formatter you have a var $text with the content of the block.
- When you call:
%%(red)First line
Second line
Last Line%%
- You will include the php script formatters/red.php and $text will be "First line\nSecond line\nLast Line".
- e.g. formatters/red.php
<?php
print "<span style=\"color: #FF0000\">";
print $this->Format( $text );
print "</span>\n";
?>
print "<span style=\"color: #FF0000\">";
print $this->Format( $text );
print "</span>\n";
?>
- A special formatter is formatters/wakka.php witch is the magic behind the $this->Format function and so translating the wiki code into html.
- Related Pages: FormatterInfo formatters/ini.php
Make an handler
- If I tell you that "edit" is the most famous handler, then you would know you have to append "/edit" to an url to get to the php script handlers/page/edit.php.
- Within Wikka, the handler name is stored in the variable $this->method.
- e.g. handlers/page/raw.php (included in Wikka 1.1.6.0)
<?php
if ($this->HasAccess("read") && $this->page)
{
// display raw page
print($this->page["body"]);
}
?>
if ($this->HasAccess("read") && $this->page)
{
// display raw page
print($this->page["body"]);
}
?>
- No $this->Format was used, so we end up with the source code of the page without header or footer...
- A special handler is "show", this is the default that would print the content of the page in html + the comments if needed...
- Related Pages: HandlerInfo
Change the code / add a new pattern
- The place to be for adding a new pattern is formatters/wakka.php, it's the logic behind the $this->Format function.
- We need to add two things: the regex (trigger) in preg_replace_callback and a new "else if" in the wakka2callback function that will return the right code. In wakka2callback you receive a string called $thing, within the "else if" you need to return the right html code when "$thing" is used in the source.
- 1. Scroll to the end of the file and find the line "$text = preg_replace_callback(", there in the regex (just above "\n".) add the line:
"rfc\d{1,4}|RFC\d{1,4}|".
- 2. Scroll a few pages up and locate the line "// mind map xml" and add just above these lines:
else if(preg_match("/(rfc(\d+))/i", $thing, $matches) )
{
return $wakka->Link("http://www.faqs.org/rfcs/rfc".$matches[2].".html","",$matches[1]);
}
{
return $wakka->Link("http://www.faqs.org/rfcs/rfc".$matches[2].".html","",$matches[1]);
}
- These two code additions will make that whenever I type rfc2616 or RFC793 Wikka will make links to the right pages of it, just as easy as typing a HomePage to make a link to a page...
- One typo or missing ";" in this critical script will make your whole site unaccessible, so be warned...
- Related Pages: formatters/wakka.php
And now something completely different... Security
- Every user with write access to a page can call all actions, formatters,... If you would like to restrict code to only registered users, admin's,... you should make the needed checks in your code!
- To not overload these examples, no input checks were done. You should never trust variables coming from a user. Exploiting bugs like this are the main goal for hackers ;-) ...
I hope this has triggered your creative brain and I look forward to reading your code on this site...
If your stuck with some code, considder posting it in ProgrammingHelp.
Category Howto | Category User Contributions | Category Development