1<?php
2/**
3 * plot-Plugin: Parses gnuplot-blocks
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Alexander 'E-Razor' Krause <alexander.krause@erazor-zone.de>
7 */
8
9if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
10if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
11require_once(DOKU_PLUGIN.'syntax.php');
12
13if (!is_callable("file_put_contents")) {
14  @require_once('/usr/lib/php/PHP/Compat/Function/file_put_contents.php');
15  if (!is_callable("file_put_contents")) {
16    echo 'Please install dev-php/PEAR-PHP_Compat or >=php-5.0.0 !';
17  }
18}
19
20/**
21 * All DokuWiki plugins to extend the parser/rendering mechanism
22 * need to inherit from this class
23 */
24class syntax_plugin_plot extends DokuWiki_Syntax_Plugin {
25     function getInfo(){
26        return array(
27            'author' => 'Alexander Krause',
28            'email'  => 'alexander.krause@erazor-zone.de',
29            'date'   => '2006-05-23',
30            'name'   => 'Plot Plugin',
31            'desc'   => 'render functions nicely',
32            'url'    => 'http://wiki.erazor-zone.de/doku.php?id=wiki:projects:php:dokuwiki:plugins:plot'
33        );
34    }
35  /**
36  * What kind of syntax are we?
37  */
38  function getType(){
39    return 'protected';
40  }
41
42  /**
43  * Where to sort in?
44  */
45  function getSort(){
46    return 100;
47  }
48
49
50    /**
51     * Connect pattern to lexer
52     */
53    function connectTo($mode) {
54        $this->Lexer->addEntryPattern('<plot(?=.*\x3C/plot\x3E)',$mode,'plugin_plot');
55    }
56
57    function postConnect() {
58      $this->Lexer->addExitPattern('</plot>','plugin_plot');
59    }
60
61    /**
62     * Handle the match
63     */
64
65
66    function handle($match, $state, $pos) {
67      if ( $state == DOKU_LEXER_UNMATCHED ) {
68        $matches = preg_split('/>/u',$match,2);
69        $matches[0] = trim($matches[0]);
70        if ( trim($matches[0]) == '' ) {
71          $matches[0] = NULL;
72        }
73        return array($matches[1],$matches[0]);
74      }
75      return TRUE;
76    }
77    /**
78     * Create output
79     */
80    function render($mode, &$renderer, $data) {
81      global $conf;
82      if($mode == 'xhtml' && strlen($data[0]) > 1) {
83        if ( !is_dir($conf['mediadir'] . '/plot') ) mkdir($conf['mediadir'] . '/plot', 0777-$conf['dmask']);
84        $hash = md5(serialize($data[0]));
85        $filename = $conf['mediadir'] . '/plot/'.$hash.'.png';
86
87        $size_str=$data[1];
88
89        if ( is_readable($filename) ) {
90          $renderer->doc .=  $this->plugin_render('{{'.'plot:'.$hash.'.png'."$size_str}}");
91          return true;
92        }
93
94        if (!$this->createImage($filename, $data[0])) {
95          $renderer->doc .=  $this->plugin_render('{{'.'plot:'.$hash.'.png'."$size_str}}");
96       } else {
97          $renderer->doc .= '**ERROR RENDERING GNUPLOT**';
98        }
99        return true;
100      }
101      return false;
102    }
103
104    function createImage($filename, &$data) {
105      global $conf;
106
107      $tmpfname = tempnam("/tmp", "dokuwiki.plot");
108
109      $data = 'set terminal png transparent nocrop enhanced font arial 8 size 420,320'."\n".
110               "set output '".$filename."'\n" . $data;
111
112      file_put_contents($tmpfname, $data);
113
114      $retval = exec('/usr/bin/gnuplot '.$tmpfname);
115
116      unlink($tmpfname);
117      return 0;
118    }
119
120    // output text string through the parser, allows dokuwiki markup to be used
121    // very ineffecient for small pieces of data - try not to use
122    function plugin_render($text, $format='xhtml') {
123      return p_render($format, p_get_instructions($text),$info);
124    }
125}
126
127?>
128