GraphvizFormatter : Adds support of graphviz inline syntax


Author : Tam Kien Duong - nicky@deveha.com - development's page

Graphviz is a very nice tool to produce and generate graphs. It's maye be usefull to let user create directly graph to illustrate the content (or import figures from documents). This extension aims to provide a support of graphviz graph language description.


Requirements



Installation



If you're using URL Rewriting, don't forget to add a rule in your .htaccess file.

RewriteCond %{REQUEST_FILENAME} !uploads/.*$


How to use it


%%(graphviz)
digraph G {Hello->World}
%%


To Do



Live Test


Some live tests are available on the development's page.

The Code


graphviz.php (line 1)
  1. <?php
  2. #
  3. # WikkaWiki Formatter to display graphviz-graphs
  4. #
  5. # $Id: $
  6. #
  7. # Copyright (C) 2007, Tam Kien Duong http://cendres.net
  8. #
  9. # This file is Free Software.
  10. #
  11. # It is licensed under the following three licenses as alternatives:
  12. #   1. GNU Lesser General Public License (LGPL) V2.1 or any newer version
  13. #   2. GNU General Public License (GPL) V2 or any newer version
  14. #   3. Apache License, V2.0 or any newer version
  15. #
  16. # You may not use this file except in compliance with at least one of
  17. # the above three licenses.
  18. #
  19. # This file is based on Sebastian Dietzold Graphviz extension for WackoWiki
  20. # https://wiki.imise.uni-leipzig.de/Projects/WackoTuning?v=xii#h702-8
  21.  
  22. # if set to "" we test is the file in PATH
  23. $GraphVizSettings['bindir'] = "";
  24. $GraphVizSettings['filedir'] = $this->config["upload_path"];
  25.  
  26. # check, if there should be another tool than dot used
  27. if (isset($options['neato'])) $bin = "neato";
  28. elseif (isset($options['circo'])) $bin = "circo";
  29. elseif (isset($options['twopi'])) $bin = "twopi";
  30. elseif (isset($options['circo'])) $bin = "circo";
  31. elseif (isset($options['fdp'])) $bin = "fdp";
  32. else $bin = "dot";
  33. $GraphVizSettings['bin'] = $GraphVizSettings['bindir'] . $bin;
  34.  
  35. # write graphviz code to temporary text-file
  36. $tmpname = tempnam ($GraphVizSettings['filedir'], "temp");
  37. $tmpfile = fopen($tmpname, "w");
  38. fwrite($tmpfile, $text);
  39. fclose($tmpfile);
  40.  
  41. ### here starts the big code block ###
  42.  
  43.     ## step 1: convert the source-code to generated source code
  44.     $cmd = $GraphVizSettings['bin']." -Tdot -o $tmpname.dot $tmpname 2>&1";
  45.     $cmdOut = `{$cmd}`;
  46.     if ($cmdOut != "")
  47.     {
  48.         echo "<i>$cmdOut</i><br/>";
  49.         unlink($tmpname);
  50.     }
  51.     else
  52.     {
  53.         # extract the name of the graph (second string in first line)
  54.         $gname = file ("$tmpname.dot"); $gname = split(" ", $gname[0]); $gname = $gname[1];
  55.  
  56.         # the basename is generated in the same way like the upload-handler do this
  57.         $fname = $GraphVizSettings['filedir'] .'/'.$this->page['tag'].'/'.$gname;
  58.  
  59.         if(!file_exists($GraphVizSettings['filedir'] .'/'.$this->page['tag'])) mkdir($GraphVizSettings['filedir'] .'/'.$this->page['tag']);
  60.  
  61.         ## step 2: prepare and run the image command and put the image in the upload dir
  62.         $imagecmd = $GraphVizSettings['bin']." -Tpng -o $fname.png $tmpname.dot";
  63.         $cmdOut = `{$imagecmd}`;
  64.  
  65.         $imgurl = $this->config["base_url"].$GraphVizSettings['filedir'] .'/'.$this->page['tag'].'/'.$gname.".png";
  66.         $imagesize = getimagesize ($fname.".png");
  67.  
  68.         ## step 3: prepare and run the map command
  69.         $mapcmd = $GraphVizSettings['bin']." -Tcmap $tmpname.dot";
  70.         $map = `{$mapcmd}`;
  71.         if ($map == "")
  72.         {
  73.             # output without no image map
  74.             echo "<img border=\"0\" src=\"$imgurl\">\n";
  75.         }
  76.         else
  77.         {
  78.             # output with image map
  79.             echo "<map name=\"$gname\">$map</map>";
  80.             echo "<img border=\"0\" usemap=\"#$gname\" src=\"$imgurl\">\n";
  81.         }
  82.  
  83.         # clear the directory and vars
  84.         unset($cmdOut); # maybe this can cause trouble if un-unsetted
  85.         unlink($tmpname);
  86.         unlink($tmpname.".dot");
  87.     }
  88. ?>



CategoryDevelopmentFormatters
Comments
Comment by YaVerOt
2008-03-25 20:02:09
Does this work with 1.1.6.4? Because it is not working with my private wiki.
Do I need to put specific permissions on graphiz.php?
!! Oh the wrong name is on the page, should be graphviz.php. One problem solved by trying to report it. I'll try to edit that on the page.
rename, reload page...

Warning: mkdir() [function.mkdir]: Permission denied in /var/www/localhost/htdocs/wikka/formatters/graphviz.php on line 59

So maybe permissions do need to be changed. Or should I just create the directory manually and not worry about it?
Comment by NilsLindenberg
2008-03-26 07:31:54
Yaverot, seems like you need to make your uploads directory writable
Comment by YaVerOt
2008-03-28 20:34:21
Looks like my last comment was lost.
I don't know "who" to make the directory writable to. Changing owner/group from root/root to (whatever the rest of the wikka subdir is) didn't help.

Looks like I need the directions on UploadsDirectorySetup. :) Since it also applies to the files action, I think.
Comment by YaVerOt
2008-04-22 21:18:58
Got it working now, here's all the problems I went through (so someone else might learn from all of my mistakes here - I think I made some good ones to learn from):
1. "uploads" writeable by root:root only. changed owner to apache
2. while having the 'delete temps' lines commented out, found out my graphviz wasn't compiled with PNG support. Since I wanted PNGs, I recompiled, but it'd probably be easier to use GIF instead if you don't care. As best as I could tell this error message is completely suppressed by the above code, it was blatant when I tried it on the command line though.
3. Instead of reading g.png from "uploads/PageName" attempted to load to infinite depth "uploads/PageName/images/PageName/images/PageName/images/PageName/images/.../g.png" obviously this is mod_rewrite's fault. This was fixed by adding a new .htaccess file in "uploads" that didn't rewrite names of existing files/directories and 403'd non-existent, which then I edited again to redirect the 403 to HomePage. I'm certain there's a better way to to this, but it worked. (I'm somewhat currious where 'images' came from in the above path.)

When I tried to set this up; I was using the 1.1.6.4-rc1. When I got it all straighten out today, I started with upgrading to 1.1.6.4, that helped so far as I had to remake the uploads directory and reset it's owner first.
Comment by YaVerOt
2008-05-03 01:17:52
I've found something I'd prefer not to be dot-rendered, but neato rendered.
The above code makes it look like that is possible, but since I don't know PHP, I don't know how to get the above
(isset($options['neato']))
line to be true. How would the codeblock go? Where do I stick neato in the codeblock?
Comment by YaVerOt
2008-10-17 00:56:03
Gave up trying to set neato, I simply copied the graphviz.php to neato.php and edited the default. then making codeblocks:
%%(neato)
...
%%

instead of
%%(graphviz)
...
%%
as necessary.
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki