Extension:GraphViz/Archive

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manual - list
Crystal Clear app error.png
GraphViz

Release status: unstable

Implementation Tag
Description Render Graphs online
Author(s) Coffman
License FDL
Download No link
Example http://www.graphviz.org/Gallery.php

I (Coffman) have recently (2-oct-2004) created a extension to mediawiki in response to a basic need : Render Graphs online

I found the utility Graphviz on another wiki, and thought about adopting it to mediawiki (the wiki I actually use). Exploring the Graphviz tool I discovered an incredible tool to making Graphs. Automatic graph drawing has many important applications in software engineering, database and web design, networking, and in visual interfaces for many other domains.

You can view a lot of samples at the original page.

Of course the only requisite for this plugin to work is the graphviz program; you can download it from here

You can find information on how to write a graph on the original page at here

Some of the issues with using this extension on Windows have been fixed in Extension:GraphVizAndMSCGen, which also combines support for the MSCGen tool.

Contents

[edit] Requisites

Graphviz 1.12, download from here

[edit] Install instructions

1 Download Graphviz.php.

An Update can be found here.
Please look here for an improvement for all the improvements and discuss.

2 Copy Graphviz.php to the $mediawiki/extensions directory.

3 Add these lines to LocalSettings.php: (the path may vary depending on your distribution)

include("extensions/Graphviz.php");
$wgGraphVizSettings->dotCommand = "/usr/bin/dot";
  • Make sure that write permissions for the "$IP/images" directory are granted.

[edit] Windows installations

Download the graphviz.php, as above, and save it into the mediawiki/extensions directory.

1 The standard Graphviz.php file has a bug in it that will prevent the graphs from appearing. Edit it with a editor like notepad. Around line 72 you'll see

                if (substr(php_uname(), 0, 7) == "Windows") {
                        $ext = "gif";
                } else {
                        $ext = "png";
                }

Change it to

                        $ext = "png";

2 Another bug is due to the fact that Windows permits spaces in directory names. Around line 47 you'll see

               $ret = `{$cmdline}`;

this must be changed to

               $ret = `"{$cmdline}"`;

The addition of quotation marks fixes the problem.

3 The $wgGraphVizSettings->dotCommand must correctly indicate where dot.exe is. The exact directory name will vary depending on the version of Graphviz downloaded, so correct this line appropriately:

 $wgGraphVizSettings->dotCommand = "c:\\Program Files\\Graphviz2.26.3\\bin\\";

[edit] Online samples

[edit] Other rendering engines

I wanted to use other layout engines besides dot(1), so I hacked Graphviz.php to allow a Unix-style interpreter specification for the other rendering engines in Graphviz. This is backwards compatible (since it is a legal dot comment) and avoids coming up with new syntax.

The interpreter spec must immediately follow the opening tag: "<graphviz>#!/sw/bin/neato" for example. I explicitly check for allowed rendering engines to avoid script injection security issues.

Annotated examples at: http://www.EcoReality.org/wiki/Test:Graphviz

Enjoy! --Bytesmiths 14:53, 15 Mar 2005 (UTC)

Here is my code:

<?php
# CoffMan (http://www.wickle.com) code adapted from timeline extension.
# JWS_050315: added support for other Graphviz layout engines.

class GraphVizSettings {
    var $execPath;
};
$wgGraphVizSettings = new GraphVizSettings;
$wgGraphVizSettings->execPath = '/sw/bin/';

$wgExtensionFunctions[] = 'wfGraphVizExtension';

function wfGraphVizExtension() {
        global $wgParser;
        $wgParser->setHook('graphviz', 'renderGraphviz');
        $wgParser->setHook('graphvizl', 'renderGraphvizLeft');
        $wgParser->setHook('graphvizr', 'renderGraphvizRight');
}

function renderGraphvizLeft($dotsrc) {
    return renderGraphviz($dotsrc, 'style="float:left"');
}

function renderGraphvizRight($dotsrc) {
    return renderGraphviz($dotsrc, 'style="float:right"');
}

function renderGraphviz($dotsrc, $style='')
{
        $cmd = '';
        global $wgUploadDirectory, $wgUploadPath, $IP, $wgGraphVizSettings, $wgArticlePath, $wgTmpDirectory;
        $hash = md5($dotsrc);
        $dest = $wgUploadDirectory . '/graphviz/';
        if(!is_dir($dest)) mkdir($dest, 0777);
        if(!is_dir($wgTmpDirectory)) mkdir($wgTmpDirectory, 0777);

    if(!strncmp($dotsrc, '#!/', 3))
        $cmd = substr($dotsrc, 2, strpos($dotsrc, '
') -2);
    switch($cmd) {
        case $wgGraphVizSettings->execPath . 'circo':
        case $wgGraphVizSettings->execPath . 'dot':
        case $wgGraphVizSettings->execPath . 'fdp':
        case $wgGraphVizSettings->execPath . 'neato':
        case $wgGraphVizSettings->execPath . 'twopi':
            break;
        default:
            $cmd = $wgGraphVizSettings->execPath . 'dot';
    }
        $fname = $dest . $hash;
        if(!(file_exists($fname . '.png') || file_exists($fname . '.err')))
        {
                $handle = fopen($fname, 'w');
                fwrite($handle, $dotsrc);
                fclose($handle);

                $cmdline = wfEscapeShellArg($cmd)
                      . ' -Tpng -o '
                      . wfEscapeShellArg($fname . '.png')
                      . ' '
                      . wfEscapeShellArg($fname);
                $cmdlinemap = wfEscapeShellArg($cmd)
                      .  ' -Tcmapx -o '
                      . wfEscapeShellArg($fname. '.map')
                      . ' '
                      . wfEscapeShellArg($fname);
                $ret = shell_exec($cmdline);
                $ret = `{$cmdlinemap}`;

                unlink($fname);
        }
        
        @$err=file_get_contents($fname . '.err');

        if($err != '') {
                $txt = "<div id=\"toc\"><tt>$err</tt></div>";
        } else {
                @$map = file_get_contents($fname . '.map');
                $map = preg_replace('#<ma(.*)>#', ' ', $map);

                if(substr(php_uname(), 0, 7) == 'Windows') {
                        $ext = 'gif';
                } else {
                        $ext = 'png';
                }
                
                $txt  = "<map id=\"$hash\" name=\"$hash\">{$map}"
            . "<img usemap=\"#{$hash}\" src=\"{$wgUploadPath}/graphviz/{$hash}.{$ext}\" {$style}>";
        }
        return $txt;
}
?>

[edit] Alternates extensions

There is another Graphviz extension (by Gregory Szorc) located at http://web.archive.org/web/20071217001132/http://opensource.case.edu/svn/MediaWikiHacks/extensions/Graphviz/trunk/Graphviz.php (wayback machine version). This version features automatic pruning of Graphviz files on the filesystem among other small improvements.

An update of this alternate version is available here: http://www2.hds.utc.fr/~rherault/index.php/Graphviz_Extension (NO ACCESS). Support for png and svg, support for border and image position

[edit] Examples

<graphviz border='frame' format='svg'>
digraph G {Hello->World!}
</graphviz>
<graphviz border='frame' format='svg'>
digraph G {Hello->World!}
</graphviz>

<graphviz renderer='neato' caption='Hello Neato'>
graph G {
   run -- intr;
   intr -- runbl;
   runbl -- run;
   run -- kernel;
   kernel -- zombie;
   kernel -- sleep;
   kernel -- runmem;
   sleep -- swap;
   swap -- runswap;
   runswap -- new;
   runswap -- runmem;
   new -- runmem;
   sleep -- runmem;
}
</graphviz>

<graphviz caption='Hello SVG and PNG' alt='phylogenetic tree' format='svg+png'>
digraph G {
                node [shape=plaintext];
                Mollusca [URL="Mollusca"];
                Neomeniomorpha [URL="Neomeniomorpha"];
                X1 [shape=point,label=""];
                Caudofoveata [URL="Caudofoveata"];
                Testaria [URL="Testaria"];
                Polyplacophora [URL="Polyplacophora"];
                Conchifera [URL="Conchifera"];
                Tryblidiida [URL="Tryblidiida"];
                Ganglioneura [URL="Ganglioneura"];
                Bivalvia [URL="Bivalvia"];
                X2 [shape=point,label=""];
                X3 [shape=point,label=""];
                Scaphopoda [URL="Scaphopoda"];
                Cephalopoda [URL="Cephalopoda"];
                Gastropoda [URL="Gastropoda"];
                Mollusca->X1->Testaria->Conchifera->Ganglioneura->X2->Gastropoda
                Mollusca->Neomeniomorpha
                X1->Caudofoveata
                Testaria->Polyplacophora
                Conchifera->Tryblidiida
                Ganglioneura ->Bivalvia
                X2->X3->Cephalopoda
                X3->Scaphopoda
}
</graphviz>

Additional Extension in following flickr gallery: http://www.flickr.com/photos/kentbye/sets/72157601523153827/

[edit] See also

Personal tools
Namespaces

Variants
Actions
Navigation
Support
Download
Development
Communication
Print/export
Toolbox