Extension talk:GraphViz

From MediaWiki.org
(Redirected from Extension talk:Graphviz)
Jump to: navigation, search
An archive box Archives 

Contents

[edit] New "improved improved" version of the GraphViz-Extension

Dear Users of the GraphViz-Extension,

I am a student assistant at the FZI [1], a German research center. We're using the Graphviz-Extension(s) for some of our own projects.

Actually the problem is that it is not that easy to use a GraphViz Extension, because there are many different versions and improvements circulating around on mediawiki.org: some for windows and others for linux, some work out of the box and others don't, some support different renderers and others have automatic clean-up . I think you know what I mean.

So I'm currently combining the various GraphViz Extensions into one functional and up to date Extension. This means that I try to get the best out of each extension and I think that the result is a very good "improved improved" GraphViz-Extension.

My question is now if you'd mind if I was refreshing the Extension-Page (Extension:GraphViz) on MediaWiki as well. I'd like to clean up the old parts and the Discussion-Page so that there is space for some instructions.

If this is a problem, I will create another "Graphviz"-Extension, but I think that this would not help to get rid of the current chaos.

You can have a look at my current code and the corresponding page here: User:Hummel-riegel/GraphViz

Of course I'll try to name all the actual and previous contributors like the FDL/GPL-Licences specify. And as this is a wiki feel free to add some stuff if you want to.

Thanks for your response in advance,
Yours sincerely

Thomas Hummel --Hummel-riegel 13:28, 1 November 2010 (UTC)


[edit] Does this work on MW 1.12+?

If anyone has gotten this working on MW 1.12+, please post. I've tried nearly everything (the original Graphviz.php, the 'updated' one, and various combinations of the fixes supplied on this page) and MW1.13.2 never processes the <graphviz> element. I also tried the newest Graphviz 2.20, and the oldest one I could find (1.14). This is on windows. For paths I have tried backslash, two backslashes, and forward slash, as well as pointing at the Graphviz in Program Files or copying it to the wiki extensions directory. I'm inclined to believe this won't work on newer wikis.


Hi, i'm running graphviz under windows with MW 1.13.4. without problems. I use the latest graphviz extension and also the latest dot. --Ozz 09:50, 10 February 2009 (UTC)

I realized this was due to a change in how plugins are 'activated' in recent MW's. The documentation says 1.12+ but I found the old way worked on 1.12.0 but not 1.13.2. Here is my final working version for Windows (based off the updated plugin):

 <?php
 /*
  * Extension to allow Graphviz to work inside MediaWiki.
  * This Version is based on CoffMan's nice Graphviz Extension.
  *
  * Licence: http://www.gnu.org/copyleft/fdl.html
  *
  * Configuration
  *
  *  These settings can be overwritten in LocalSettings.php.
  *  Configuration must be done AFTER including this extension using
  *  require("extensions/Graphviz.php");
  *
  *  $wgGraphVizSettings->dotCommand
  *    Describes where your actual dot executable remains.
  *
  *    Windows Default: C:/Programme/ATT/Graphviz/bin/dot.exe
  *    Other Platform : /usr/local/bin/dot
  *
  *  $wgGraphVizSettings->named
  *    Describes the way graph-files are named.
  *
  *    named: name of your graph and its type determine its filename
  *    md5  : name of your graph is based on a md5 hash of its source.
  *    sha1 : name of your graph is based on a SHA1 hash of its source.
  *
  *    Default : named
  *
  *  $wgGraphVizSettings->install
  *    Gets you an errormessage if something fails, but maybe ruins your
  *    wiki's look. This message is in English, always.
  *
  *    Default : false
  *
  * Improvements
  * - Selects defaults for Windows or Unix-like automatically.
  * - should runs out of the box
  * - Creates PNG + MAP File
  * - additional storage modes (see discussion below)
  *   - Meaningful filename
  *   - Hash based filename
  *   - Configurable (name/md5/sha1)
  *
  * Storage Modes:
  * MD5:
  * + don't worry about graphnames
  * + pretty fast hash
  * - permanent cleanup necesary (manually or scripted)
  * - md5 is buggy - possibility that 2 graphs have the same hash but
  *   are not the same
  * SHA1:
  * + don't worry about graphnames
  * + no hash-bug as md5
  * - permanent cleanup necessary (manually or scripted)
  * - not so fast as md5
  * Named:
  * + Graphs have a name, now it's used
  * + no permanent cleanup necessary.
  * - Naming Conflicts
  *   a) if you have multiple graphs of the same name in the same
  *      article, you will only get 1 picture - independently if they're
  *            the same or not.
  *   b) possible naming conflicts in obscure cases (that should not happen)
  *      Read code for possibilities / exploits
  * 
  */
 
 class GraphVizSettings {
       public $dotCommand, $named, $install;
 };
 
 $wgGraphVizSettings = new GraphVizSettings();
 
 // Config
 // ------
 if ( ! (stristr (PHP_OS, 'WIN' ) === FALSE) ) {
       $wgGraphVizSettings->dotCommand = 'C:/Program Files/Graphviz2.20/bin/dot';
 } else {
       $wgGraphVizSettings->dotCommand = '/usr/local/bin/dot';
 }
 $wgGraphVizSettings->named = 'named';
 $wgGraphVizSettings->install = false;
 
 // Media Wiki Plugin Stuff
 // -----------------------
 //$wgExtensionFunctions[] = "wfGraphVizExtension";
 
 if ( defined( 'MW_SUPPORTS_PARSERFIRSTCALLINIT' ) ) {
       $wgHooks['ParserFirstCallInit'][] = 'wfGraphVizExtension';
 } else { // Otherwise do things the old fashioned way
       $wgExtensionFunctions[] = 'wfGraphVizExtension';
 }
 
 
 $wgExtensionCredits['parserhook'][] = array(
   'name'=>'Graphviz',
   'author'=>'CoffMan <http://wickle.com>, MasterOfDesaster <mailto://arno.venner@gmail.com>',
   'url'=> 'http://www.mediawiki.org/wiki/Extension:GraphViz',
   'description'=>'Graphviz (http://www.graphviz.org) is a program/language that allows the creation of numerous types of graphs.  This extension allows the embedding of graphviz markup in MediaWiki pages and generates inline images to display.',
   'version'=>'0.4'
 );
 
 function wfGraphVizExtension() {
       global $wgParser;
       $wgParser->setHook( "graphviz", "renderGraphviz" );
       return true;
 }
 
 function renderGraphviz( $timelinesrc, $params, &$parser )      // Raw Script data
 {
       global
             $wgUploadDirectory,      // Storage of the final png & map
             $wgUploadPath,                  // HTML Reference
             $wgGraphVizSettings;      // Plugin Config
             
       $parser->disableCache();
       $output = "";
       //$output = "<pre>Called render</pre>";
       
       // Prepare Directories
       $dest = $wgUploadDirectory."/graphviz/";
       if ( ! is_dir( $dest ) ) { mkdir( $dest, 0777 ); }
 
       $output .= "<pre>Dir=$dest</pre>";
       $storagename = urldecode($_GET['title']).'---';
       $storagename = str_replace("&",'_amp_',$storagename);
       $storagename = str_replace("#",'_shrp_',$storagename);
       $storagename = str_replace("/",'_sd_',$storagename);
       $storagename = str_replace("\\",'_sd_',$storagename);
       
       $wgGraphVizSettings->named = strtolower($wgGraphVizSettings->named);
       
       $output .= "<pre>dotCommand=".$wgGraphVizSettings->dotCommand ."</pre>";
       $output .= "<pre>named=".$wgGraphVizSettings->named ."</pre>";
       
       if($wgGraphVizSettings->named == 'md5') {
             $storagename .= md5($timelinesrc);
       } else if ($wgGraphVizSettings->named == 'sha1') {
             $storagename .= sha1($timelinesrc);
       } else {
             $storagename .= trim(
                   str_replace("\n",'',
                         str_replace("\\",'/',
                               substr($timelinesrc, 0, strpos($timelinesrc,'{') ))      ));
       }
 
       $src = $dest . $storagename;
 
       $imgn = $dest . $storagename . '.png';
       $mapn = $dest . $storagename . '.map';
       
       $output .= "<pre>Src=$src</pre>";
 
       if ( $wgGraphVizSettings->named == 'named' || ! ( file_exists( $src.".png" ) || file_exists($src.".err")))
       {
             $handle = fopen($src, "w");
             $ret2 = fwrite($handle, $timelinesrc);
             $ret3=fclose($handle);
             
             $output.="<pre>Opened and closed $src, handle=$handle, timeelinesrc=$timelinesrc, ret2=$ret2, ret3=$ret3</pre>";
 
             $cmdline    = wfEscapeShellArg($wgGraphVizSettings->dotCommand).' -Tpng   -o '.wfEscapeShellArg($imgn).' '.wfEscapeShellArg($src);
             $cmdlinemap = wfEscapeShellArg($wgGraphVizSettings->dotCommand).' -Tcmapx -o '.wfEscapeShellArg($mapn).' '.wfEscapeShellArg($src);
 
             //$ret = shell_exec($cmdline);
             $WshShell = new COM("WScript.Shell");
             $WshShell->Exec($cmdline);
             $WshShell->Exec($cmdlinemap);
 
             
             $output.="<pre>Ran cmd line. ret=$ret cmd=$cmdline</pre>";
             
             if ($wgGraphVizSettings->install && $ret == "" ) {
                   //echo '<div id="toc"><tt>Timeline error: Executable not found.'.      "\n".'Command line was: '.$cmdline.'</tt></div>';
                   $output.= '<div id="toc"><tt>Timeline error: Executable not found.'.      "\n".'Command line was: '.$cmdline.'</tt></div>';
                   exit;
             }
 
             //$ret = shell_exec($cmdlinemap);
             if ($wgGraphVizSettings->install && $ret == "" ) {
                   echo '<div id="toc"><tt>Timeline error: Executable not found.'.
                         "\n".'Command line was: '.$cmdlinemap.'</tt></div>';
                   exit;
             }
 
             //unlink($src);
       }
       
       @$err=file_get_contents( $src.".err" ); 
 
       if ( $err != "" ) {
             $txt = '<div id="toc"><tt>'.$err.'</tt></div>';
       } else {
             @$map = file_get_contents( $mapn );
             $map  = preg_replace('#<ma(.*)>#',' ',$map);
             $map  = str_replace('</map>','',$map);
 
             $txt  = '<map name="'.$storagename.'">'.$map.'</map>'.
                     '<img src="'.$wgUploadPath.'/graphviz/'.$storagename.'.png"'.
                               ' usemap="#'.$storagename.'" />';
       }
       $txt .= $output;//"<pre>MOOOO</pre>";
       return $txt;
 }
 ?>

[edit] Fix for Darwin

auto-detection fails for Darwin (because it matches "WIN"). Fix:

if ( stristr(PHP_OS, 'WIN' ) && !stristr(PHP_OS, 'Darwin') ) {
        $wgGraphVizSettings->dotCommand = 'C:/Programme/ATT/Graphviz/bin/dot.exe';
} else {
        $wgGraphVizSettings->dotCommand = '/usr/local/bin/dot';
}

[edit] Problems with mediawiki 1.16 (ubuntu 8.10) - dot command not found

I'm currently using the svn version of mediawiki, which is currently at 1.16. Although I have correctly set the location of dot,I keep getting the error: Timeline error: Executable not found. Command line was: '/usr/bin/dot' -Tpng -o 'images/graphviz/Extensions---digraph G.png' 'images/graphviz/Extensions---digraph G' (I will post more info later)

Solution: This problem arises only when you set the $wgGraphVizSettings->install=true because it gives and error when dot output is an empty string "", which is exactly the case of a succesful execution. To fix this we must look at the dot command return value rather than its stdout/stderr output.

--Drunken sapo 14:26, 22 June 2009 (UTC)

[edit] Do away with serverfiles

I have img_auth.php installed on the wiki, hence could not use this extension directly. Don't really want to touch permissions, so I did as suggested here and embedded the graphviz output inside the response html. Just threw together some code to verify this (missing all the error handling and what not, but works fine on sample graphs); feel free to reuse whatever you see fit as I am definitely still missing much functionality. --Vopap 03:56, 23 September 2009 (UTC)

[edit] Display Examples

I tried to display the example graphics that get produced by the examples on this page, but it seemed to just display the text even though I took it out of the 'code' tag. Any reason this would happen? Daviddoria 18:05, 1 November 2009 (UTC)

[edit] Bug in extreme fast redrawings

In case of redrawing the same graphic very fastly, the new graphviz call gets stuck. In the process list, the CMD and graphviz call does not get cleaned, because the created file is locked by the previous call.

A work around is to unlink the bare file ($fname) and the $fname.map before opening the handle to $fname:

if (file_exists($fname)) unlink($fname);
if (file_exists($fname.'.map')) unlink($fname.'.map');

This should solve the issue, but it needs to be tested thoroughly.

[edit] Custom Shapes

Is there any trick to getting a custom shape (image) rendered in the graphs? I have uploaded a "stick figure" image to my mediawiki, and can see it if I include the markup [[File:Stick.png]] in page bodies, but it is not rendered when I include that markup in my graphviz code --srl100 11:18, 14 April 2010 (UTC)

e.g.

<graphviz>
graph G {
    subgraph clusterMain {
        subgraph clusterActor1 {
            color=white;
            label="actor1";
            actor1 [image="[[File:Stick.png]]" shape=none label=""];
        }
        system [shape=circle];
        actor1 -- system;
    }
}
</graphviz>

Update 15 April 2010, by srl100...

It turns out that I needed to include the relative path to the image, in the (hashed) images directory structure.

<graphviz>
graph G {
    subgraph clusterMain {
        subgraph clusterActor1 {
            color=white;
            label="actor1";
            actor1 [image="images/a/aa/Stick.png" shape=none label=""];
        }
        system [shape=circle];
        actor1 -- system;
    }
}
</graphviz>
I think you have to hack this feature into the extension by yourself. Perhaps this can help you on the way: Extension talk:GraphViz#local_wiki_URLs --Danwe 21:39, 16 April 2010 (UTC)

[edit] Category pages and graphviz

The extension cannot work on category pages unless you include the following at line 117:

...
$storagename = str_replace("\\",'_sd_',$storagename);
$storagename = str_replace(":",'_colon_',$storagename); #<-- added...
        
$wgGraphVizSettings->named = strtolower($wgGraphVizSettings->named);
...

[edit] Text inside nodes is pruned

In the Hello World example (and in other examples too), the text inside the nodes is pruned at the bottom.

I assume it is a problem in GraphViz, but I wonder if anyone has found a workaround?

Yeah I'm having this problem too. Using underscores to get round it atm, but it's not ideal

[edit] Install Instructions - path to graphviz

Using MediaWiki v1.16.2 I got an error while following Installation instructions. I changed line (LocalSettings.php) to

include("$IP/extensions/Graphviz/Graphviz.php");

Also. I was using Ubuntu 10.10 with lampp and pictures in graphviz folder was 0 bytes size. To solve this problem: [2]

[edit] What version of MediaWiki is required?

Hi, what version(s) of MediaWiki is required for this extension? I saw the reference to "The documentation says 1.12+" but can't see any documentation. We are currently on 1.5.3, I'm guessing that is too old?

[edit] bugs and filenames

Hi! I'm Extension:Collaboration Diagram developer, I really impressed by the description of new Graphviz extension and tried to update. However here are some problems that took place on my Debian:

  1. By default the extension does nothing except displaying the message: string(5) . It's caused by the line 293 in the source code.
  2. After fixing that I started to get error messages about too long filenames. The extension uses strange method of naming when - it generates filenames like PAGENAME---GRAPHNAME and replaces all '%' to _perc_ . Sure thing it provoked errors on my Russian wiki, where all letters are replaced to codes after calling urlencode function.
  3. If I set $wgGraphVizSettings->named = 'md5'; I get filenames like PAGENAME---md5(GRAPHNAME)

Idea: why not hashing with md5 or sha1 both PAGENAME and GRAPHNAME? And probably it would be better to set

$wgGraphVizSettings->named = 'md5' 

by default because otherwise it will cause problems on non-English wikis. --Katkov Yury 19:29, 7 June 2011 (UTC)


jeroendedauw has put this into svn. Thanks! (r89857 -Diff) --Hummel-riegel 12:00, 11 June 2011 (UTC)

[edit] Problems with SVG

It doesn't seem that SVG output format work. after settings up the following:

 $wgGraphVizSettings->outputType = "svg";
 $wgGraphVizSettings->info = true; 
 $wgAllowTitlesInSVG = true;  
 $wgSVGConverter = 'ImageMagick';
 $wgFileExtensions[] = 'svg';
 $wgAllowExternalImages = true;

I'm still not able to see the pictures of my graphs, although they have being generated by the extension. Any other SVG files that I upload to the wiki displayed correctly because ImageMagick generates thumbnails for them.

So for commonly uploaded SVGs on a page the following html is produced:

<a href="/colldia/index.php/%D0%A4%D0%B0%D0%B9%D0%BB:Bu.svg"
   class="image">
   <img alt="Bu.svg"
        src="/colldia/images/thumb/a/ae/Bu.svg/1063px-Bu.svg.png"
        width="1063"
        height="638"
   />
</a>

However graphviz extension generates for svg filetype the same code as for other fileformats: x

<img class="" 
 style=""
 alt="This is a graph with borders and nodes. Maybe there is an Imagemap used so the nodes may be linking to some Pages." 
 src="/colldia/images/graphviz/a2166e59be54c3b94615e6f3269a4569.svg"
 usemap="#a2166e59be54c3b94615e6f3269a4569" />


In Manual:Image_Administration#SVG they say that it's forbidden to include svg images directly in a wikipage, so that was the source of the bug.

UPDATE

I have fixed it by generating pngs with SvgHandler class and now with the help of Hokstad I can draw graphs like that

Prettygraphviz.png

Where can I send the patches with that? I think it could be really handy.

--Katkov Yury 20:20, 7 June 2011 (UTC)

If you don't want to put it in svn yourself (or have no access like me ;-)) the best way would be to fill a bug on https://bugzilla.wikimedia.org/enter_bug.cgi?product=MediaWiki%20extensions.

--Hummel-riegel 11:58, 11 June 2011 (UTC)

[edit] Problem with Font / Charset

Hi, I have a problem with Graphviz on my wiki. The dot command is found and I can create Graphs, but instead of letters I only see rectangles. Here is a picture of what my graph looks like: http://tinypic.com/r/w8vlhi/7 (Uploadet 20.07.2011)

I hope someone can give my some hints of how to fix this :D

Thanks

Niklas


[edit] Automatically adding Wiki links

I am generating graphs representing sutuctures in my Wiki using Extension:DPL to generate a set of GraphViz dot commands. I am including the generation of the Graphviz commands to add URLs to each node so I can link back to the original pages in the Wiki. However this is a tedious process. I would like Extension:GraphViz to do this for me, i.e. scan the input commands before writing them out to a file and

replace 
        Foo -> Bar
with
        Foo  -> Bar
        Foo [URL=”Foo”]
        Bar [URL=”Bar”]

Is there any interest in this? --Teskeyn 15:10, 26 July 2011 (UTC)

Hi, I think this is an interesting idea. At the moment I don't have any time to do work here, but as everybody could submit a patch, someone could do this.

In my opinion a good and easy solution would be a parameter, to activate or deactivate the replacement process. Not everybody wants to have the Nodes linked to the pages.
A more complex solution would be checking if the name of the page exists. But I'm not sure if this is necessary. --Hummel-riegel 16:24, 26 July 2011 (UTC)

Teskeyn, I would very much love to know how you got DPL and GraphViz to play nice with each other - somehow I cannot get DPL output to be rendered correctly by GraphViz other than C&P it into the source code myself :/ I'd appreciate any hint!

As for your replacement: Why don't you just generate the missing lines for the URL parameter like this?

 {{#dpl:
    |category=...
    |mode=userformat
    |listseparators=,\n  "%PAGE%" [URL="%PAGE%"],,
  }}

--Dnowak 16:24, 20 Sep 2011 CET

[edit] failed to open stream: No such file or directory

Hey all, I have just installed graphviz and this extension, but kept seeing the following error message when I tried to put an example graph in my Sandbox:

Warning: file_get_contents(/path/to/wiki/images/graphviz/Sandbox---digraph+example.map):
failed to open stream: No such file or directory in 
/path/to/wiki/extensions/GraphViz/GraphViz.php on line 420    


The reason here was, the path to my Graphviz installation was wrong (I put /usr/bin/dot/ but correct was /usr/bin/ ). After changing this, the error dissappered. --Kebap 16:39, 6 September 2011 (UTC)

[edit] Why don't we keep pruning simple?

I think all the different ways of storing the generated graph image files are a huge overkill.

Why don't you just use a modified 'named' method. Name each file with the article name and a count or if you must, the graph name and a count. This means if you have several graphs with the same name on one page (which is quite likely if you use templates to generate graphs), you just add a incremented number to the name.

Now we only have to take care of orphaned files, not needed anymore. Preventing that, we just get all files prefixed with the current article name and delete them when the page has been edited/purged (graph re-rendering) or moved/deleted. Of course we can add a caching mechanism, save the graph source with the image and check before rendering again whether the new source is equal to the new source, in that case we don't do anything. Special case would be if the number of graphs with same name changes on a page, in this case the easiest would be to prune all graphs with that name of that page.

Am I missing anything here, wouldn't it be that simple really? --Danwe 09:26, 26 September 2011 (UTC)

[edit] Maintainers?

Is this extension still being maintained? It appears that it could be very useful, especially when combined with Extension:Semantic Result Formats. Badon 06:33, 26 November 2011 (UTC)

Jeroen has maintained it for some time but he has no big plans about developing this extension. I have a project Extension:CollaborationDiagram that uses GraphViz, so I will probably occasionally contribute something here (like SVG support, shadows and gradients). I think without funding or really huge graphviz fans this extension will remain frozen. Katkov Yury 22:21, 28 November 2011 (UTC)
I am using GraphViz extension extensively in my wiki, having templates generating huge graphs with SMW, Loops and Variables extensions. I have already fixed some things but didn't release them yet. I am thinking about re-writing the whole thing, solving the image caching issue and prettifying the extension design. Can't say when that's going to happen right now since I am rather busy with other extensions at the moment. --Danwe 17:00, 29 November 2011 (UTC)

[edit] Running graphviz on web host

Hello, I'm trying to figure out how to properly install & use Graphviz. My wiki is installed a web host, which is running PEAR module Image_GraphViz 1.1.0 stable. In that case, where would the dot binary typically be for $wgGraphVizSettings->execPath? I'm at a loss as to how to properly configure this.Patelmm79 22:07, 20 January 2012 (UTC)

Personal tools
Namespaces
Variants
Actions
Site
Support
Download
Development
Communication
Print/export
Toolbox