1<?php
2/**
3 * plot-Plugin: Parses gnuplot-blocks
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Wizardry and Steamworks <wizardry.steamworks@outlook.com>,
7 *             Alexander 'E-Razor' Krause <alexander.krause@erazor-zone.de>
8 *
9 * [WaS] The following plugin is a revision of the GNUplot Plugin by
10 * Alexander 'E-Razor' Krause from 2005 that uses the original gnuplot binary
11 * in order to render graphs that has been patched up to work on modern
12 * DokuWki versions.
13 *
14 */
15
16if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
17if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
18require_once(DOKU_PLUGIN.'syntax.php');
19
20/**
21 * All DokuWiki plugins to extend the parser/rendering mechanism
22 * need to inherit from this class
23 */
24class syntax_plugin_plotwas extends DokuWiki_Syntax_Plugin {
25    /**
26    * What kind of syntax are we?
27    */
28    //function accepts($m) { return true; }
29    function getType() { return 'protected'; }
30    function getPType() { return 'normal'; }
31    function getSort() { return 200; }
32
33    /**
34     * Connect pattern to lexer
35     */
36    function connectTo($mode) {
37        $this->Lexer->addEntryPattern('<plot(?=.*\x3C/plot\x3E)', $mode, 'plugin_plotwas');
38    }
39
40    function postConnect() {
41        $this->Lexer->addExitPattern('</plot>', 'plugin_plotwas');
42    }
43
44    /**
45     * Handle the match
46     */
47    function handle($match, $state, $pos, Doku_Handler $handler) {
48      if ( $state == DOKU_LEXER_UNMATCHED ) {
49        $matches = preg_split('/>/u', $match, 2);
50        $matches[0] = trim($matches[0]);
51        if ( trim($matches[0]) == '' ) {
52          $matches[0] = NULL;
53        }
54        return array($matches[1],$matches[0]);
55      }
56      return array($state, '', '');
57    }
58    /**
59     * Create output
60     */
61    function render($mode, Doku_Renderer $renderer, $data) {
62      global $conf;
63      if($mode == 'xhtml' && strlen($data[0]) > 1) {
64        if ( !is_dir($conf['mediadir'] . '/plot') ) {
65            mkdir($conf['mediadir'] . '/plot', 0777 - $conf['dmask']);
66        }
67
68        $hash = md5(serialize($data[0]));
69        $filename = $conf['mediadir'] . '/plot/'.$hash.'.png';
70
71        $size_str=$data[1];
72
73        if ( is_readable($filename) ) {
74          $renderer->doc .=  $this->plugin_render('{{'.'plot:'.$hash.'.png'."$size_str}}");
75          return true;
76        }
77
78        if (!$this->createImage($filename, $data[0])) {
79          $renderer->doc .=  $this->plugin_render('{{'.'plot:'.$hash.'.png'."$size_str}}");
80       } else {
81          $renderer->doc .= '**ERROR RENDERING GNUPLOT**';
82        }
83        return true;
84      }
85
86      return false;
87    }
88
89    function createImage($filename, &$data) {
90      $tmpfname = tempnam("/tmp", "dokuwiki.plot");
91
92      $data = 'set terminal png transparent nocrop enhanced font arial 8 size 420,320'."\n".
93               "set output '".$filename."'\n" . $data;
94
95      file_put_contents($tmpfname, $data);
96
97      $retval = exec('/usr/bin/gnuplot '.$tmpfname);
98
99      unlink($tmpfname);
100      return 0;
101    }
102
103    // output text string through the parser, allows dokuwiki markup to be used
104    // very ineffecient for small pieces of data - try not to use
105    function plugin_render($text, $format='xhtml') {
106      return p_render($format, p_get_instructions($text),$info);
107    }
108}
109
110