DokuWiki

It's better when it's simple

User Tools

Site Tools


plugin:function

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
plugin:function [2012-11-09 19:38]
65.206.8.1
plugin:function [2018-05-31 00:16] (current)
Klap-in [Installation]
Line 6: Line 6:
 email      : tcafferty@glocalfocal.com ​ email      : tcafferty@glocalfocal.com ​
 type       : syntax type       : syntax
-lastupdate : 2012-11-09 +lastupdate : 2013-06-08 
-compatible : Angua+compatible : Angua, Binky, Hrun
 depends ​   :  depends ​   : 
 conflicts ​ :  conflicts ​ : 
Line 22: Line 22:
  
 ===== Installation ===== ===== Installation =====
 +Search and install the plugin using the [[plugin:​extension|Extension Manager]]. Refer to [[:​Plugins]] on how to install plugins manually.
  
-:!: **External requirements:​** None 
- 
-Install the plugin using the [[plugin:​plugin|Plugin Manager]] and the download URL above, which points to latest version of the plugin. Refer to [[:​Plugins]] on how to install plugins manually. 
  
 ===== Examples/​Usage ===== ===== Examples/​Usage =====
  
-sample ​function is included called /​functions/​helloWorld.php. To use add the following plugin syntax to a wiki page. No configuration changes are needed to run the sample. +Two sample ​functions are included called ​''​/​functions/​helloWorld.php''​ and ''/​functions/​anotherhelloWorld.php''​. To use add the following plugin syntax to a wiki page. No configuration changes are needed to run the sample. 
-<​code><​function=file1></​code> ​+<​code>​ 
 +<​function=file2?​0=That'​s&​1=All&​2=Folks!&​num=3> 
 +<​function=file3>​ 
 +<​function=file2?​0=That'​s&​1=All&​2=Folks!&​num=3>​ 
 +</​code>​ 
 +The example passes 4 parameters to the invoked function, invokes a different function with no parameters, and then calls the first function again.  
 + 
 +Parameters are passed as array elements. The example shows how the invoked function typecasts the last parameter to be used as an integer. 
 + 
 +Parameters are not required. The simplest usage is to invoke a function with no parameters.
  
 +Examples can be seen at [[http://​clean.glocalfocal.com/​doku.php?​id=start|Plugin Examples Site]]
 ===== Syntax ===== ===== Syntax =====
  
Line 37: Line 45:
 <​code><​function=name></​code>​ <​code><​function=name></​code>​
     * //​name// ​   --- pick a name from the /​conf/​default.php file      * //​name// ​   --- pick a name from the /​conf/​default.php file 
 +    * ?​parameter_name=value_1&​parameter_name_x=value_x (optional parameters)
 +
 +Use of <​function=name>​ is all that is required. If your function requires parameters these can be supplied by using a "?"​ after the function name and providing the parameters as id=value. Parameters are separated by a "&"​ symbol.
  
 ===== Configuration and Settings ===== ===== Configuration and Settings =====
  
-  - Add a file containing the php function into the /functions folder. The main function must be called ​'​run'​.+  - Add a file containing the php function into the /functions folder. The main function must be called ​defined as 
 +      <​code>​$thisfunction = function($params) { 
 +                ​..... 
 +            }; 
 +       </​code>​
   - Add a configuration parameter in /​conf/​default.php that contains a name to identify the function file and set it equal to the name of the file. No path information is required.   - Add a configuration parameter in /​conf/​default.php that contains a name to identify the function file and set it equal to the name of the file. No path information is required.
   - You can add a function description in /​lang/​../​settings.php. This will show up in the admin configuration panel for the plugin.   - You can add a function description in /​lang/​../​settings.php. This will show up in the admin configuration panel for the plugin.
Line 48: Line 63:
 === Change Log === === Change Log ===
  
-{{rss>http://​github.com/feeds/​TomCafferty/commits/​plugin-function/​master date}}+{{rss>https://​github.com/​TomCafferty/​plugin-function/commits/master.atom date}}
  
 === Known Bugs and Issues === === Known Bugs and Issues ===
Line 66: Line 81:
 An example would be useful. What is this plugin for? What can you do with it? What's the advantage of calling a named php function? Is this just a different way to make a syntax plug-in, for people who know php but don't want to learn the dokuwiki syntax? An example would be useful. What is this plugin for? What can you do with it? What's the advantage of calling a named php function? Is this just a different way to make a syntax plug-in, for people who know php but don't want to learn the dokuwiki syntax?
 -- [[user>​Zioth]] -- [[user>​Zioth]]
 +
 +I often want to run php to query a database and output the result. These queries sometimes involve other functionality that would not be handled by the generic database plugins available. I would not want to write a plugin for each type of query I run. Also for security reasons I am reluctant to allow php to be executed on a wiki page. The phpinc plugin was close to providing this functionality but it appears to have some security issues and states that it is no longer supported. ​ With this function plugin I can easily and I believe securely connect a php function to run on a wiki page.  If the php functions are general in functionality they could be documented on the wiki for use by the users.
 +
 +I provided a simplified example as the function I am running involves interfacing with a database with specific tables. ​ If I create a function better suited as an example I will provide it.
 +Best regards
 +-- [[user>​tom_c]]
 +
 +I noticed a problem when I was using two different functions on a single page. The run() declaration was already defined when it hit the second function call. I was able to solve this by using variable function declarations. Here is the updated render function in syntax.php and the function declaration in the example helloworld.php ​
 +<code syntax.php>​
 +    function render($mode,​ &​$renderer,​ $indata) {
 +        global $conf;
 +        if($mode == '​xhtml'​){
 +          list($state,​ $data) = $indata;
 +
 +          switch ($state) {
 +            case DOKU_LEXER_SPECIAL :
 +              preg_match("#​^<​function=(.+)>​$#",​ $data, $matches);
 +              $func = $matches[1];​
 +              $a = explode('?',​ $func);
 +              $func = $a[0];
 +              if (!empty($a[1])) { parse_str($a[1],​ $params); }
 +              else { $params = '';​ }
 +              if(preg_match("#​^[a-z0-9\-_ \./​]+$#​i",​ $func)) {
 +                    $renderer->​info['​cache'​] = FALSE;
 +                    $filename = DOKU_PLUGIN . '​function/​functions/'​ . $this->​getConf($func);​
 +                    include ($filename);​
 +                    $renderer->​doc .= $thisfunction($params);​
 +              }
 +              else
 +                    $renderer->​doc .= $renderer->​_xmlEntities($data);​
 +              break;
 +  ​
 +          }
 +          return true;
 +        }
 +        ​
 +        // unsupported $mode
 +        return false;
 +    } 
 +</​code>​
 +<code helloworld.php>​
 +$thisfunction = function($params) {
 +    // Create output data
 +    $dataout='<​p class="​cellGreen">​Hello World!</​p>';​
 +
 +    $num=(int)$params['​num'​];​
 +    $dataout .= ' The '​.($num+1).'​ parameters were the number '.$num .' preceded by <br />';​
 +    foreach ($params AS $key=>​$val){
 +        if ($val != $num) $dataout.=$val.'<​br />';​
 +    }
 + return $dataout;
 +};
 +</​code>​
 +Using a variable to hold the function allows it to be overwritten for each function declaration. ​
 +-- [[user>​millsdude]]
 +
 +Millsdude, thanks for this update! It works real nice. I incorporated it into the release. ​
 +-- [[user>​tom_c]]
 +
 +It may be worth noting that, although DokuWiki itself [[:​requirements|requires only PHP version 5.2]], the function plugin seems to require a more recent version. ​ The plugin makes use of [[http://​php.net/​manual/​en/​functions.anonymous.php|anonymous function variable assignment]],​ a capability introduced in PHP version 5.3. -- [[user>​areeves]]
plugin/function.1352486298.txt.gz · Last modified: 2012-11-09 19:38 by 65.206.8.1