Extension:GraphViz/Archive

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.

Requisites
Graphviz 1.12, download from here

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.

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\\";


 * Additional Windows notes

Online samples

 * http://www.wickle.com/wikis/index.php/Graphviz_extension (a copy of this page, 404 at 2011-09-14)
 * http://www.EcoReality.org/wiki/Test:Graphviz (comments)
 * http://www.wikischool.de/wiki/WikiSchool:Graphviz
 * http://www.nerux.org/wiki/Graphviz and http://www.nerux.org/wiki/Accueil

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: " #!/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:

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 = "$err "; } else { @$map = file_get_contents($fname . '.map'); $map = preg_replace('##', ' ', $map);

if(substr(php_uname, 0, 7) == 'Windows') { $ext = 'gif'; } else { $ext = 'png'; }		$txt = "{$map}" . ""; }	return $txt; } ?>

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

Examples
 digraph G {Hello->World!}

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