Graphviz-plugin

graphviz plugin by Carl-Christian Salvesen
Graph Visualization (from text with links between objects to image)

Last updated on 2005-05-25. Provides Syntax.
No compatibility info given!

Similar to ditaa, format, gnuplot, graphgear.

Tagged with diagram, graphviz, images, media.

    For more information refer to http://andkorn.org/wiki/doku.php?id=howtos:computers:dokuwiki:graphviz_plugin

    Read more about graphviz at http://www.graphviz.org/Documentation.php.

    This plugin download page seems to have been removed - does anyone have a copy of the code?

    Try something like this, slightly modified for my needs:

    <?php
    /**
     * graphviz-Plugin: Parses graphviz-blocks
     * 
     * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
     * @author     Carl-Christian Salvesen <calle@ioslo.net>
     * @version    0.1.20050525
     */
     
    if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
    require_once(DOKU_INC.'inc/init.php');
    if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
    require_once(DOKU_PLUGIN.'syntax.php');
     
     
    /**
     * All DokuWiki plugins to extend the parser/rendering mechanism
     * need to inherit from this class
     */
    class syntax_plugin_graphviz extends DokuWiki_Syntax_Plugin {
     
     
      function getInfo(){
        return array(
    		 'author' => 'Carl-Christian Salvesen',
    		 'email'  => 'calle@ioslo.net',
    		 'date'   => '2007-02-11',
    		 'name'   => 'graphviz Plugin',
    		 'desc'   => 'Parses graphviz-blocks',
    		 'url'    => 'http://wiki.ioslo.net/dokuwiki/graphviz',
    		 );
      }
     
      /**
       * What kind of syntax are we?
       */
      function getType(){
        return 'protected';
      }
     
      /**
       * Where to sort in?
       */ 
      function getSort(){
        return 100;
      }
     
     
      /**
       * Connect pattern to lexer
       */
      function connectTo($mode) {
        $this->Lexer->addEntryPattern('<graphviz(?=.*\x3C/graphviz\x3E)',$mode,'plugin_graphviz');
      }
     
      function postConnect() {
        $this->Lexer->addExitPattern('</graphviz>','plugin_graphviz');
      }
     
      /**
       * Handle the match
       */
     
     
      function handle($match, $state, $pos) {
        if ( $state == DOKU_LEXER_UNMATCHED ) {
          $matches = preg_split('/>/u',$match,2);
          $matches[0] = trim($matches[0]);
          if ( trim($matches[0]) == '' ) {
    	$matches[0] = NULL;
          }
          return array($matches[1],$matches[0]);
        }
        return TRUE;
      }
      /**
       * Create output
       */
      function render($mode, &$renderer, $data) {
        global $conf;
        if($mode == 'xhtml' && strlen($data[0]) > 1) {
          if ( !is_dir($conf['mediadir'] . '/graphviz') ) 
    	io_mkdir_p($conf['mediadir'] . '/graphviz'); //Using dokuwiki framework
          $hash = md5(serialize($data));
          $filename = $conf['mediadir'] . '/graphviz/'.$hash.'.png';
          $url = ml('graphviz:'.$hash.'.png'); //Using dokuwiki framework
     
          if ( is_readable($filename) ) {
    	// cached.
    	$renderer->doc .= '<img src="'.$url.'" class="media" title="Graph" alt="Graph" />';
    	//						$renderer->doc .= $renderer->internalmedialink('graphviz:'.$hash.'.png');
    	return true;
          }
     
          if (!$this->createImage($filename, $data[0], $data[1])) {
    	$renderer->doc .= '<img src="'.$url.'" class="media" title="Graph" alt="Graph" />';
    	//					$renderer->doc .= $renderer->internalmedialink('graphviz:'.$hash.'.png');
          } else {
    	$renderer->doc .= '**ERROR RENDERING GRAPHVIZ**';
          }
          return true;
        }
        if($mode == 'latex' && strlen($data[0]) > 1) { //Latex mode for dokuTeXit
          global $TeXitImage_glob;
          global $_dokutexit_conf;
          $hash = md5(serialize($data));
          if (isset($_dokutexit_conf) && $_dokutexit_conf['mode'] == 'pdflatex') {
    	$filename = $conf['mediadir'] . '/graphviz/'.$hash.'.png';
          } else {
    	$filename = $conf['mediadir'] . '/graphviz/'.$hash.'.ps';
          }
          //Saving filename for zipfile
          $TeXitImage_glob['plugin_list'][$hash] = $filename; 
          if (is_readable($filename) ) {
    	// cached.
    	$renderer->doc .= "\\begin{figure}[h]\n";
    	$renderer->doc .= "\t\\begin{center}\n";
    	$renderer->doc .= "\t\t\\includegraphics{";
    	$renderer->doc .= $filename;
    	$renderer->doc .= "}\n";
    	$renderer->doc .= "\t\\end{center}\n";
    	$renderer->doc .= "\\end{figure}\n";
    	return true;
          }
          if (!$this->createImageLatex($filename, $data[0], $data[1])) {
    	$renderer->doc .= "\\begin{figure}[h]\n";
    	$renderer->doc .= "\t\\begin{center}\n";
    	$renderer->doc .= "\t\t\\includegraphics{";
    	$renderer->doc .= $filename;
    	$renderer->doc .= "}\n";
    	$renderer->doc .= "\t\\end{center}\n";
    	$renderer->doc .= "\\end{figure}\n";
          } else {
    	$renderer->putent('**ERROR RENDERING GRAPHVIZ**');
          }
          return true;
        }
        return false;
      }
     
      function createImageLatex($filename, &$data, $graphcmd='dot') { //Latex mode have better rendering with ps
        if (isset($_dokutexit_conf) && $_dokutexit_conf['mode'] == 'pdflatex') {
          return $this->createImage($filename, $data, $graphcmd);
        }
        $cmds = array('dot','neato','twopi','circo','fdp');
        if ( !in_array($graphcmd, $cmds) ) $graphcmd = 'dot';
     
        $tmpfname = tempnam("/tmp", "dokuwiki.graphviz");
        io_saveFile($tmpfname, $data);
        $retval = exec('/usr/bin/'.$graphcmd.' -Gsize="5,4" -Tps ' .
    		   $tmpfname.' -o '. $filename);
        unlink($tmpfname);
        return $retval;
      }
     
      function createImage($filename, &$data, $graphcmd='dot') {
     
        $cmds = array('dot','neato','twopi','circo','fdp');
        if ( !in_array($graphcmd, $cmds) ) $graphcmd = 'dot';
     
        $tmpfname = tempnam("/tmp", "dokuwiki.graphviz");
        io_saveFile($tmpfname, $data); //Using dokuwiki framework
        //    file_put_contents($tmpfname, $data); 
        // I don't use Imagemagik, becouse i have problem with russian letter in this case
        //$retval = exec('/usr/bin/'.$graphcmd.' -Tps '.$tmpfname.'|/usr/bin/convert ps:- png:'.$filename);
        // Comment out the line over this and uncomment the line below to NOT use ImageMagick for antialiazing.
     
     
         $retval = exec('/usr/bin/'.$graphcmd.' -Tpng -o '.$filename .' '.$tmpfname);
        /* WINDOWS VERSION */
        // change     $tmpfname = tempnam("C:\temp", "dokuwiki.graphviz");
        //change $retval = exec('C:\grapviz\bin\'.$graphcmd.' -Tpng -o '.$filename .' '.$tmpfname);
        unlink($tmpfname);
        return $retval;
      }
     
    }
     
    ?>

    Discussion

    Security enhancement

    If you use open_basedir and/or safe mode add this patch to fetch from php.ini the tmp value

    --- lib/plugins/graphviz/syntax.php	2008-08-13 12:53:37.000000000 +0200
    +++ lib/plugins/graphviz/syntax.save.php	2008-08-13 12:53:21.000000000 +0200
    @@ -159,7 +159,7 @@ class syntax_plugin_graphviz extends Dok
         $cmds = array('dot','neato','twopi','circo','fdp');
         if ( !in_array($graphcmd, $cmds) ) $graphcmd = 'dot';
     
    -    $tmpfname = tempnam(ini_get('upload_tmp_dir'), "dokuwiki.graphviz");
    +    $tmpfname = tempnam("/tmp", "dokuwiki.graphviz");
         io_saveFile($tmpfname, $data); //Using dokuwiki framework
         //    file_put_contents($tmpfname, $data); 
         $retval = exec('/usr/local/bin/'.$graphcmd.' -Tps '.$tmpfname.'|/usr/local/bin/convert ps:- png:'.$filename);
    

    Nicolas GORALSKI

    Imagemap enhancement

    Here's the patch to update plugin to render image maps. All code is “borrowed” from http://www.wickle.com/wiki/index.php/Graphviz_extension (src). Diff is generated against the source posted above, which is modified against the original version of the plugin.

    --- syntax-orig.php	Tue Dec 23 14:39:09 2008
    +++ syntax.php	Tue Dec 23 14:24:41 2008
    @@ -8,11 +8,9 @@
      */
      
     if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
    -require_once(DOKU_INC.'inc/init.php');
     if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
     require_once(DOKU_PLUGIN.'syntax.php');
      
    - 
     /**
      * All DokuWiki plugins to extend the parser/rendering mechanism
      * need to inherit from this class
    @@ -24,7 +22,7 @@
         return array(
     		 'author' => 'Carl-Christian Salvesen',
     		 'email'  => 'calle@ioslo.net',
    -		 'date'   => '2007-02-11',
    +			'date'   => '2005-05-25',
     		 'name'   => 'graphviz Plugin',
     		 'desc'   => 'Parses graphviz-blocks',
     		 'url'    => 'http://wiki.ioslo.net/dokuwiki/graphviz',
    @@ -79,97 +77,58 @@
       function render($mode, &$renderer, $data) {
         global $conf;
         if($mode == 'xhtml' && strlen($data[0]) > 1) {
    -      if ( !is_dir($conf['mediadir'] . '/graphviz') ) 
    -	io_mkdir_p($conf['mediadir'] . '/graphviz'); //Using dokuwiki framework
    +				if ( !is_dir($conf['mediadir'] . '/graphviz') ) mkdir($conf['mediadir'] . '/graphviz', $conf['dmask'], true);
           $hash = md5(serialize($data));
           $filename = $conf['mediadir'] . '/graphviz/'.$hash.'.png';
    -      $url = ml('graphviz:'.$hash.'.png'); //Using dokuwiki framework
    +				$url = DOKU_BASE.'/lib/exe/fetch.php?cache='.$cache.'&amp;media='.urlencode('graphviz:'.$hash.'.png');
      
           if ( is_readable($filename) ) {
     	// cached.
    -	$renderer->doc .= '<img src="'.$url.'" class="media" title="Graph" alt="Graph" />';
    +					$renderer->doc .= "<map name=\"$hash\">{$map}</map>".
    +							  "<img usemap=\"#{$hash}\" src=\"$url\" class=\"media\" title=\"Graph\" alt=\"Graph\" />";
     	//						$renderer->doc .= $renderer->internalmedialink('graphviz:'.$hash.'.png');
     	return true;
           }
      
           if (!$this->createImage($filename, $data[0], $data[1])) {
    -	$renderer->doc .= '<img src="'.$url.'" class="media" title="Graph" alt="Graph" />';
    -	//					$renderer->doc .= $renderer->internalmedialink('graphviz:'.$hash.'.png');
    +
    +					@$map = file_get_contents( $filename.".map" );
    +
    +					$map=preg_replace("#<ma(.*)>#"," ",$map);
    +					$map=str_replace("</map>","",$map);
    +				        
    +					$renderer->doc  .= "<map name=\"$hash\">{$map}</map>".
    +							  "<img usemap=\"#{$hash}\" src=\"$url\" class=\"media\" title=\"Graph\" alt=\"Graph\" />";
    +														
    +					//$renderer->doc .= '<img src="'.$url.'" class="media" title="Graph" alt="Graph" />';
    +					////$renderer->doc .= $renderer->internalmedialink('graphviz:'.$hash.'.png');
    +					
           } else {
     	$renderer->doc .= '**ERROR RENDERING GRAPHVIZ**';
           }
           return true;
         }
    -    if($mode == 'latex' && strlen($data[0]) > 1) { //Latex mode for dokuTeXit
    -      global $TeXitImage_glob;
    -      global $_dokutexit_conf;
    -      $hash = md5(serialize($data));
    -      if (isset($_dokutexit_conf) && $_dokutexit_conf['mode'] == 'pdflatex') {
    -	$filename = $conf['mediadir'] . '/graphviz/'.$hash.'.png';
    -      } else {
    -	$filename = $conf['mediadir'] . '/graphviz/'.$hash.'.ps';
    -      }
    -      //Saving filename for zipfile
    -      $TeXitImage_glob['plugin_list'][$hash] = $filename; 
    -      if (is_readable($filename) ) {
    -	// cached.
    -	$renderer->doc .= "\\begin{figure}[h]\n";
    -	$renderer->doc .= "\t\\begin{center}\n";
    -	$renderer->doc .= "\t\t\\includegraphics{";
    -	$renderer->doc .= $filename;
    -	$renderer->doc .= "}\n";
    -	$renderer->doc .= "\t\\end{center}\n";
    -	$renderer->doc .= "\\end{figure}\n";
    -	return true;
    -      }
    -      if (!$this->createImageLatex($filename, $data[0], $data[1])) {
    -	$renderer->doc .= "\\begin{figure}[h]\n";
    -	$renderer->doc .= "\t\\begin{center}\n";
    -	$renderer->doc .= "\t\t\\includegraphics{";
    -	$renderer->doc .= $filename;
    -	$renderer->doc .= "}\n";
    -	$renderer->doc .= "\t\\end{center}\n";
    -	$renderer->doc .= "\\end{figure}\n";
    -      } else {
    -	$renderer->putent('**ERROR RENDERING GRAPHVIZ**');
    -      }
    -      return true;
    -    }
         return false;
       }
      
    -  function createImageLatex($filename, &$data, $graphcmd='dot') { //Latex mode have better rendering with ps
    -    if (isset($_dokutexit_conf) && $_dokutexit_conf['mode'] == 'pdflatex') {
    -      return $this->createImage($filename, $data, $graphcmd);
    -    }
    -    $cmds = array('dot','neato','twopi','circo','fdp');
    -    if ( !in_array($graphcmd, $cmds) ) $graphcmd = 'dot';
    - 
    -    $tmpfname = tempnam("/tmp", "dokuwiki.graphviz");
    -    io_saveFile($tmpfname, $data);
    -    $retval = exec('/usr/bin/'.$graphcmd.' -Gsize="5,4" -Tps ' .
    -		   $tmpfname.' -o '. $filename);
    -    unlink($tmpfname);
    -    return $retval;
    -  }
    - 
       function createImage($filename, &$data, $graphcmd='dot') {
      
         $cmds = array('dot','neato','twopi','circo','fdp');
         if ( !in_array($graphcmd, $cmds) ) $graphcmd = 'dot';
      
         $tmpfname = tempnam("/tmp", "dokuwiki.graphviz");
    -    io_saveFile($tmpfname, $data); //Using dokuwiki framework
    -    //    file_put_contents($tmpfname, $data); 
    -    // I don't use Imagemagik, becouse i have problem with russian letter in this case
    -    //$retval = exec('/usr/bin/'.$graphcmd.' -Tps '.$tmpfname.'|/usr/bin/convert ps:- png:'.$filename);
    -    // Comment out the line over this and uncomment the line below to NOT use ImageMagick for antialiazing.
    +			file_put_contents($tmpfname, $data);
    +			//$retval = exec('c:/apps/graphviz/graphviz/bin/'.$graphcmd.' -Tsvg '.$tmpfname.'|c:/apps/imagemagick/convert svg:- png:'.$filename);
    +			//$retval = exec('c:/apps/graphviz/graphviz/bin/'.$graphcmd.' -Tpng '.$tmpfname.' -o '.$filename);
    +			$retval = exec('c:/apps/graphviz/2.21/bin/'.$graphcmd.' -Tpng '.$tmpfname.' -o '.$filename);
    +			
    +			//generate map
    +			$retval = exec('c:/apps/graphviz/2.21/bin/'.$graphcmd.' -Tcmapx '.$tmpfname.' -o '.$filename.'.map');
      
      
    -     $retval = exec('/usr/bin/'.$graphcmd.' -Tpng -o '.$filename .' '.$tmpfname);
    -    /* WINDOWS VERSION */
    -    // change     $tmpfname = tempnam("C:\temp", "dokuwiki.graphviz");
    -    //change $retval = exec('C:\grapviz\bin\'.$graphcmd.' -Tpng -o '.$filename .' '.$tmpfname);
    +
    +			// Comment out the line over this and uncomment the line below to NOT use ImageMagick for antialiazing.
    +			// $retval = exec('/usr/bin/'.$graphcmd.' -Tpng -o '.$filename .' '.$tmpfname);
         unlink($tmpfname);
         return $retval;
       }
    
    

    You may also need to update your Graphviz installation.

    Alex POPOV

    ODT Export

    If you wish to export graphviz graphs (as PNG inclusions) in ODT documents with odt plugin simply apply this patch in syntax.php :

    --- syntaxweb.php       2009-08-28 11:05:38.000000000 +0200
    +++ syntax.php  2009-08-28 11:08:54.000000000 +0200
    @@ -93,14 +93,42 @@
           }
     
           if (!$this->createImage($filename, $data[0], $data[1])) {
    -       $renderer->doc .= '<img src="'.$url.'" class="media" title="Graph" alt="Graph" />';
    +       $renderer->doc .= '<img src="'.$url.'" class="media" title="Graph" alt="Graph" /> ';
            //                                      $renderer->doc .= $renderer->internalmedialink('graphviz:'.$hash.'.png');
           } else {
            $renderer->doc .= '**ERROR RENDERING GRAPHVIZ**';
           }
           return true;
         }
    -    if($mode == 'latex' && strlen($data[0]) > 1) { //Latex mode for dokuTeXit
    +    elseif($mode == 'odt' && strlen($data[0])>1){
    +       list($state, $datae) = $data;   
    +
    +        $hash = md5(serialize($data));
    +       $imfilename=$conf['mediadir'] . '/graphviz/'.$hash.'.png';
    +       
    +       if (is_readable($filename)){
    +                    // Content
    +                     $renderer->p_close();
    +                     $renderer->doc .= '<text:p text:style-name="Table_20_Contents">';
    +                     $renderer->_odtAddImage($imfilename);
    +                     $renderer->doc .= '</text:p>';
    +                     $renderer->p_open();
    +               }
    +               elseif (!$this->createImage($imfilename, $data[0], $data[1])) {
    +                       // Content
    +                     $renderer->p_close();
    +
    +                     $renderer->doc .= '<text:p text:style-name="Table_20_Contents">';
    +                     $renderer->_odtAddImage($imfilename);
    +                     $renderer->doc .= '</text:p>';
    +                     $renderer->p_open();
    +               }
    +               else{
    +                       $renderer->doc .= "UNABLE TO ADD GRAPHVIZ GRAPH";
    +               }
    +       return true;
    +    }
    +    elseif($mode == 'latex' && strlen($data[0]) > 1) { //Latex mode for dokuTeXit
           global $TeXitImage_glob;
           global $_dokutexit_conf;
           $hash = md5(serialize($data));


    Jean-Christophe Arnu

    Disk Usage

    I realize this is a very old plugin, but its power and usefulness are amazing when coupled with DokuWiki, and I'd really like to fix one issue I've run into.

    The graphviz plugin saves generated images to the /media/graphviz directory by default, but my problem is for every minute page update a new image is created with a new name (and the old one remains). Is this a bug, or is this normal? Since the page revisions are saved by DokuWiki shouldn't there be no need to save a copy of every image revision?

    If you have graphviz set up to output PNG (the prettiest), the filesize can easily go over 1MB, and after just a few changes the graphviz folder balloons into tens of MBs. I've just been deleting the folder manually, but is there an easy way to make graphviz simply overwrite the same file?

    Any help would be greatly appreciated!


    Daniel Dupriest

     
    plugin/graphviz.txt · Last modified: 2010/02/05 19:47 by 74.93.99.97
     
    Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
    Imprint Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki
    WikiForumIRCBugsGitXRefTranslate