Extension talk:GraphViz
From MediaWiki.org
I've added a link to Extension:GraphVizAndMSCGen, which is has some improvements for Windows and allows the use of MSCGen graphs too. Please move around these links if required or integrate the code from my extension into this one, etc.. Matthewpearson 16:36, 10 December 2008 (UTC)
Any user names refer to users of that site, who are not necessarily users of MediaWiki.org (even if they share the same username).
[edit] <graphviz> tags
I have installed GraphViz on a Debain server using apt.
I seem to have most/all of the executables installed and have put Graphviz.php in the extensions directory. Unfortunately it seems that MediaWiki is not interpreting the <graphviz> tags.
Is there a 'preferred' way to test the extension(s)?
I trust you also added
include("extensions/Graphviz.php");
$wgGraphVizSettings->dotCommand = "$DOT_PATH/dot";
to your LocalSettings.php. After that you can test if it works with the code:
<graphviz>
digraph G{
A -> B
}
</graphviz>
If you see a graph with a blob A connected with an arrow to a blob B then it works.
Some of my experiences:
- The current sources of Graphviz.php are in Wicle's CVS.
- When compiling the Graphviz from the sources I needed the --with-mylibgd to get dot generate .png files.
- The graphs seen in IslandSeeds look better because that engine runs on Mac (see [1]} and its follow-up.
--Coriolan 05:37, 14 Jun 2005 (UTC)
Hi,
- i have installed this extension on MW 1.13.0 and Ubuntu Hardy
- i have added the path to dot /usr/bin/dot
- i have included the php to LocalSettings
but, nothing works, <graphviz>...</graphviz> seems to be unreaded. Nothing inside is shown.
- pngs are created, but damaged
- the *.map files are empty <map id="G" name="G"> </map>
If I am using MW 1.7.3 I do have the same problem
btw. GraphViz Category Browser is not running, too The Special Page is there, but empty
GraphViz (/usr/bin/dot) is running without trouble
--ViktorD
[edit] Windows
If we only want to directly use the GIF file, please modified as following
...
str_replace("\\",'/',
substr($timelinesrc, 0, strpos($timelinesrc,'{') ))));
}
$ext = ".gif";
$src = $dest . $storagename;
$imgn = $dest . $storagename . $ext;
$mapn = $dest . $storagename . '.map';
if ( $wgGraphVizSettings->named == 'named' || ! ( file_exists( $src.".gif" ) || file_exists( $src.".err" ) ) )
{
$handle = fopen($src, "w");
fwrite($handle, $timelinesrc);
fclose($handle);
$cmdline = wfEscapeShellArg($wgGraphVizSettings->dotCommand). ' -Tgif -o '.wfEscapeShellArg($imgn).' '.wfEscapeShellArg($src);
$cmdlinemap = wfEscapeShellArg($wgGraphVizSettings->dotCommand). ' -Tcmapx -o '.wfEscapeShellArg($mapn).' '.wfEscapeShellArg($src);
$WshShell = new COM("WScript.Shell");
$WshShell->Exec($cmdline);
$WshShell->Exec($cmdlinemap);
}
...
$map = str_replace('</map>',,$map);
$txt = '<map name="'.$storagename.'">'.$map.'</map>'.'<img src="'.$wgUploadPath.'/graphviz/'.$storagename.'.gif"'.' usemap="#'.$storagename.'" />';
}
unlink($src);
return $txt;
}
-- By Bennix, Fudan University, 2008
You must enable internet users to have read/execute access to cmd.exe before trying to use the Graphviz extension. Matt 16:44, 30 April 2008 (UTC)
What's the point in this?
if (substr(php_uname(), 0, 7) == "Windows") {
$ext = "gif";
} else {
$ext = "png";
}
.png is used everywhere else in the file, so this actually makes it break! I removed it and changed the $txt line to always use .png.
Also I found it necessary to use
$ret = `"{$cmdline}"`;
instead of
$ret = `{$cmdline}`;
because the default windows install directory for Graphviz is "c:\Program Files\Graphviz..." which contains a space. Without the quotes, windows assumes everything following the space is a parameter, which generates only a "'c:\Program' is not recognized as an internal or external command, operable program or batch file." error.
Thanks for the extension, it looks like it could be very useful once I get the hang of it!
--ProgMania 23:20, 3 September 2005 (UTC)
COM("WScript.Shell") instead of shell_exec()
Using using the improved version found below on this page, I had problem getting this to work on a Windows Server 2003 machine with Apache 2.2 and PHP 5.2.6. The shell_exec() command refused to execute the dot program for some reason. Replacing the two shell_exec() calls with this code works for me.
$WshShell = new COM("WScript.Shell");
$WshShell->Exec($cmdline);
$WshShell->Exec($cmdlinemap);
--Oskar, 2008-05-19
I found all the instructions above do not work on my Windows installation. However, the instructions found here worked the first time: http://www.wickle.com/wiki/index.php/Windows_Graphviz_Installation_Instructions. (Segraves, 2009-02-26)
[edit] safe_mode and antialiased rendering
please use popen() instead of shell_exec(), so that php safe_mode can work. not all of us want to run our servers as loose as a mexican whore.
i've also modified this plugin to use GraphicsMagick to render antialiased graphs from dot's svg output, it works very nice indeed. --207.109.251.117 11:26, 6 November 2005 (UTC)
-
- Can you please provide the modified plugin? --202.1.193.10 17:45, 13 January 2006 (UTC)
[edit] Some Help Getting Graphviz to Work
I have an unusual set up as in I have a Uniform Server's WAMP ( Windows Apache MySQL PHP ) running from a Thumbdrive.
Mediawiki ver 1.5.
Graphviz.php extension installed
The Graphviz is installed in the psedo drive W:\usr\local\ATT\Graphviz
- The dot.exe is therefore W:\usr\local\ATT\Graphviz\bin
While adding in <graphviz></graphviz> elements does have a Dos Prompt window pop-up momentarily, the resulting graph was never generated.
Has anyone encountered this problem? And found a solution?
Suggestions?
[edit] Directories and rights
My experiences from an install under FreeBSD 7.1:
- Installed GraphViz from ports
- (cd /usr/ports/graphics/graphviz/; make install clean)
- Path for dot is: /usr/local/bin/dot, use that in LoacalSettings.php
- create dir for images: .../wikiurl/images/graphviz/
- replace ".../wikiurl/" with the full path for your wiki installation
- chown www .../wikiurl/images/graphviz/
- a cron entry to cleanup .../wikiurl/images/graphviz/ is a good idea
- delete all png/gif/maps etc older than 7 days:
- find .../wikiurl/images/graphviz/ -mtime +7 -type file -delete
--Schatchaos 20:20, 24 January 2009 (UTC)
[edit] Deletion of unused images
Hi is it possible to delete all the unused pngs ? i got only 20 images with graphviz, but the creationprocess produced over 400 files...(testing and stuff :) ) Is it possible to delete those ? They mess up my box :-/
Note that the above conversation may have been edited or added to since the transfer. If in doubt, check the edit history.
[edit] Size limit and work-arounds
I'm using the GraphViz extension w/ great delight (hoping to get my company's employees to document some of their formal business processes using the GraphViz extension on the company's MediaWiki-based wiki thing). However, I have been hitting an apparent size limit on the size of the graph you can specify using the GraphViz extension (go figure... there's a size limit on GET request lengths, after all).
So, for a simple work-around, I'm advising my users to split up their graphs into several subgraphs (only slightly nasty-looking, and there's probably some slick way to make it look almost like one connected graph).
For a more complicated work-around, I'm considering making a revised version of the tag(s) (e.g. <dot_graph>) that would "optimize" the graph code for reduced length (only on conversion into raw HTML), doing things like stripping out comments and renaming labeled nodes (to give them a super-short name while still allowing verbose node names in the original Wiki markup).
Just a thought. Nothing's really happening yet, and I thought it would be nice to at least warn people about the length limit (above). Mlibby 17:04, 22 December 2006 (UTC)
- Maybe I'll include an approach like this, too: http://meta.wikimedia.org/wiki/User_talk:Cygon/Graphviz_Extension#Shorting%20the%20URLs Mlibby 17:14, 22 December 2006 (UTC)
[edit] Use with MW1.10
Has anyone been able to make any of the GraphViz extensions work with MediaWiki version 1.10? I have tried both this one and the one at http://www2.hds.utc.fr/~rherault/index.php/Graphviz_Extension, and neither works. Pages show up without content (completely white) as soon as I activate the extension in LocalSettings. Any advice would be gratefully accepted. 160.33.43.231 July 2nd, 2007, 12:00pm PDT
[edit] GRAFCET
Can we create GRAFCET diagrams with this extension? (original question reformulated from French!)
- The only reference I could find in english about this is here
- A german Grafcet information page is here.
There seems no way to render these kind of diagrams with GraphViz yet.
[edit] Improvement
Well, I decided that it might be a good thing to incorporate those issues seen above in a version that works out of the box AND does not require anything but "standard php" compared to those two alternate versions: Alternate Version and Updated alternate Version -- the Links are dead!
This is an update ... for those who do not want to take/require the more complex version or do not have access to its required SPL module.
Known Issues
- Well this plugin has a few issues with special characters in combination with "named naming". Just don't use them. This is true because Filesystem restrict other characters as URL's. Anyway that filename will only be used for referencing and is not displayed, so you don't have to worry if it is a workaround.
<?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:/Programme/ATT/Graphviz/bin/dot.exe';
} else {
$wgGraphVizSettings->dotCommand = '/usr/local/bin/dot';
}
$wgGraphVizSettings->named = 'named';
$wgGraphVizSettings->install = false;
// Media Wiki Plugin Stuff
// -----------------------
$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" );
}
function renderGraphviz( $timelinesrc ) // Raw Script data
{
global
$wgUploadDirectory, // Storage of the final png & map
$wgUploadPath, // HTML Reference
$wgGraphVizSettings; // Plugin Config
// Prepare Directories
$dest = $wgUploadDirectory."/graphviz/";
if ( ! is_dir( $dest ) ) { mkdir( $dest, 0777 ); }
$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);
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';
if ( $wgGraphVizSettings->named == 'named' || ! ( file_exists( $src.".png" ) || file_exists( $src.".err" ) ) )
{
$handle = fopen($src, "w");
fwrite($handle, $timelinesrc);
fclose($handle);
$cmdline = wfEscapeShellArg($wgGraphVizSettings->dotCommand).
' -Tpng -o '.wfEscapeShellArg($imgn).' '.wfEscapeShellArg($src);
$cmdlinemap = wfEscapeShellArg($wgGraphVizSettings->dotCommand).
' -Tcmapx -o '.wfEscapeShellArg($mapn).' '.wfEscapeShellArg($src);
$ret = shell_exec($cmdline);
if ($wgGraphVizSettings->install && $ret == "" ) {
echo '<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.'" />';
}
return $txt;
}
?>
[edit] Fixed Special Char problem
Well here is a manual patch.
[edit] Make Named Files Umlaut-safe
Look for:
function renderGraphviz( $timelinesrc )
Replace these lines:
$storagename = urldecode($_GET['title']).'---';
$storagename = str_replace("&",'_amp_',$storagename);
$storagename = str_replace("#",'_shrp_',$storagename);
$storagename = str_replace("/",'_sd_',$storagename);
$storagename = str_replace("\\",'_sd_',$storagename);
With that line
$storagename = str_replace("%",'_perc_',urlencode($_GET['title'])).'---';
Then replace this block:
} else {
$storagename .= trim(
str_replace("\n",'',
str_replace("\\",'/',
substr($timelinesrc, 0, strpos($timelinesrc,'{') )
)
)
);
}
With that one
} else { // 'named'
$storagename .= str_replace("%",'_perc_',
urlencode(
trim(
str_replace("\n",'',
str_replace("\\",'/',
substr($timelinesrc, 0, strpos($timelinesrc,'{'))
)
)
)
)
);
}
[edit] A typo
There is also a type in the above listed file:
if ( $wgGraphVizSettings->named == 'named' | ! ( file_exists( $src.".png" ) || file_exists( $src.".err" ) ) )
Should be that:
if ( $wgGraphVizSettings->named == 'named' || ! ( file_exists( $src.".png" ) || file_exists( $src.".err" ) ) )
- Fixed. (This appears to be not related to #Fixed Special Char problem) --Alvin-cs 12:19, 17 November 2007 (UTC)
[edit] On Updating
The update will work without noticing. Old (and unchanged) pages will stay the same and reference old images, a graph that is updated will automatically create a file in the new naming shema. If you worry about diskspace ... you can easily detect old files and new files.
- Old files contain
- _amp_, _shrp_, _sd_ instead of &, \ and /
- New files
- Replacing "_perc_" -> with "%" makes them valid urls.
- Independent files
- Usually don't have any of those things.
- Had problem with ':' (COLON) in file names under windows. This happens when the article is not
in the default namespace. What about adding the following line:
$storagename = str_replace(":",'_colon_',$storagename);
around line 113? --195.206.74.130 13:08, 7 February 2008 (UTC)
[edit] local wiki URLs
A rather crude hack to allow local wiki url tags (--Dannyh 23:18, 26 March 2008 (UTC)). Suppose you want your graph to contain local links like the following:
<graphviz>
digraph G {
Boss [URL = "[[User:Boss]]"]
John [URL="[[User:John]]" ]
Boss->John
}
</graphviz>
Then add the following function to GraphViz.php
function rewriteWikiUrls( &$source)
{
$line = preg_replace(
'|\[\[([^]]+)\]\]|e',
'Title::newFromText("$1")->getFullURL()',
$source
);
return $line;
}
And then, right before this block:
$handle = fopen($fname, "w"); fwrite($handle, $timelinesrc); fclose($handle);
Add the following line:
$timelinesrc = rewriteWikiUrls( $timelinesrc);
[edit] Wikilinks from within graph
A neat feature would be to be able to use wikilinks inside the graphviz code. Is this by any means feasible? --Berland 13:23, 3 October 2008 (UTC)
[edit] Broken with MW 1.12?
Is this broken again with MW 1.12? I'm getting no images. I tried both the main page version and the one added above on this page. --Cymen 15:31, 3 October 2008 (UTC)
Update: The RedHat version of graphviz requires the graphviz-gd package for PNG (and more) suport. I modified the script to replace spaces (' ') with underscore ('_') too.
$storagename = str_replace(' ','_',$storagename); #new
$src = $dest . $storagename; # not modifed
And to print an error if it can't write to disk:
$handle = fopen($src, 'w');
if (! $handle) return 'Error writing graphviz file to disk.';
fwrite($handle, $timelinesrc);
--Cymen 16:07, 3 October 2008 (UTC)
[edit] Suse 11 + MW 1.13.2
I think this plug in will be great, once it works which is what I can't do.
I installed a Fresh Suse 11 distro and put MW 1.13.2 on it, configured correctly as far as I can tell, the graphviz plugin picks up the tags, and generates the images, which are correct, but they will not display in the wiki page.
The error I keep getting is:
Timeline error: Executable not found. Command line was: '/usr/local/bin/dot' -Tpng -o '/srv/www/htdocs/wiki/images/graphviz/Test---graph+G.png' '/srv/www/htdocs/wiki/images/graphviz/Test---graph+G'
I'm no expert in php since I think if I were I'd be able to figure this out, but if someone could lend a hand I'd be very grateful.
The original documentationf or the plugin states to use /usr/bin for the dot command, however since mine is generating the files from /usr/local/bin, I don't see why I would need to change it.
Thanks
You have to change the path in Graphviz.php and use this one /usr/bin because dot in suse is there.
xp190 38.113.160.194 20:57, 3 October 2008 (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)
| Code |
|---|
<?php $wgMapCount = 0; $wgExtensionFunctions[] = 'efGraphSetup'; function efGraphSetup() { global $wgParser; $wgParser->setHook( 'graph', 'efGraphRender' ); return true; } function efGraphRender( $input, $args, $parser ) { $process = proc_open('dot -Tjpeg', array( 0 => array("pipe", "r"), // stdin is a pipe that the child will read from 1 => array("pipe", "w"), // stdout is a pipe that the child will write to 2 => array("file", "/dev/null", "a") // stderr is a file to write to ), $pipes, '/tmp', null); if (is_resource($process)) { fwrite($pipes[0], $input); fclose($pipes[0]); $picture = stream_get_contents($pipes[1]); fclose($pipes[1]); $return_value = proc_close($process); } if (ereg("\[href|\[URL", $input)){ global $wgMapCount; $wgMapCount++; $process = proc_open('dot -Tcmapx', array( 0 => array("pipe", "r"), // stdin is a pipe that the child will read from 1 => array("pipe", "w"), // stdout is a pipe that the child will write to 2 => array("file", "/dev/null", "a") // stderr is a file to write to ), $pipes, '/tmp', null); if (is_resource($process)) { fwrite($pipes[0], $input); fclose($pipes[0]); $map = stream_get_contents($pipes[1]); fclose($pipes[1]); $return_value = proc_close($process); } $map = ereg_replace('<map id=\"[^\"]*\" name=\"[^\"]*\">', '<map id="map' . $wgMapCount . '" name="map' . $wgMapCount . '">', $map); $enc_res = '<img usemap="#map' . $wgMapCount . '" src="data:image/jpeg;base64,' . chunk_split(base64_encode($picture)) . '" />' . $map; } else { $enc_res = '<img src="data:image/jpeg;base64,' . chunk_split(base64_encode($picture)) . '" />'; } return $enc_res; } |
[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.