Extension:GraphVizAndMSCGen
From MediaWiki.org
|
Release status: beta |
|
|---|---|
| Description | Combination of GraphViz and MSCGen extensions |
| License | No license specified |
| Download | no link |
|
check usage (experimental) |
|
Contents |
[edit] What can this extension do?
This extension is a combination of Extension:GraphViz and Extension:MscGen (so all credit should go to those who went before). Plus some bits have been changed to ensure they both work on Windows. It assumes the binary files for MSCGen and GraphViz are in the same directory. Finally I added processing of the imagemap info that comes out of mscgen to make it work in the same way as GraphViz.
[edit] Usage
See Extension:GraphViz and Extension:MscGen for some examples.
[edit] Download instructions
Please cut and paste the code found below and place it in $IP/extensions/ExtensionName/ExtensionName.php. Note: $IP stands for the root directory of your MediaWiki installation, the same directory that holds LocalSettings.php.
[edit] Installation
To install this extension, add the following to LocalSettings.php:
#add configuration parameters here #setup user rights here require_once("extensions/GraphViz.php"); $wgGraphVizSettings->execPath = "c:\\Program Files\\Graphviz 2.21\\bin\\";
[edit] Configuration parameters
execPath is the path of GraphViz and MSCGen.
[edit] User rights
[edit] Code
<?php /* * This extension is to give the combined effect of GraphViz and MSCGen * for all your graphing needs. It moves towards a framework for this kind * of extension that should allow other graphing products to be used. * * Small improvements made by Matthew Pearson, but based on previous work... * * 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 $execPath, $named, $install; }; $wgGraphVizSettings = new GraphVizSettings(); // Config // ------ // Changed from dotCommand to execPath to allow multiple graph types. /*if ( ! (stristr (PHP_OS, 'WIN' ) === FALSE) ) { $wgGraphVizSettings->dotCommand = 'C:/Programme/ATT/Graphviz/bin/dot.exe'; } else { $wgGraphVizSettings->dotCommand = '/usr/local/bin/dot'; }*/ $wgGraphVizSettings->execPath = '/usr/bin/'; $wgGraphVizSettings->named = 'named'; $wgGraphVizSettings->install = false; // Media Wiki Plugin Stuff // ----------------------- $wgExtensionFunctions[] = "wfGraphVizExtension"; $wgExtensionCredits['parserhook'][] = array( 'name'=>'GraphVizAndMSCGen', 'author'=>'CoffMan <http://wickle.com>, MasterOfDesaster <mailto://arno.venner@gmail.com>', 'url'=> 'http://www.mediawiki.org/wiki/Extension:GraphVizAndMSCGen', '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. <br>Adds the <graphviz[ renderer=renderer_name]> and the <mscgen> tags.', 'version'=>'0.4' ); function wfGraphVizExtension() { global $wgParser; $wgParser->setHook( "graphviz", "renderGraphviz" ); } function renderGraphviz( $timelinesrc, $args, $parser ) // Raw Script data { global $wgUploadDirectory, // Storage of the final png & map $wgUploadPath, // HTML Reference $wgGraphVizSettings; // Plugin Config // Prepare Directories $dest = $wgUploadDirectory."/graphviz/"; if ( ! (stristr (PHP_OS, 'WIN' ) === FALSE) ) { $dest = str_replace("/", '\\', $dest); $isWindows = true; } else { $isWindows = false; } if ( ! is_dir( $dest ) ) { mkdir( $dest, 0777 ); } // Added to allow for different diagram types - MP if ( isset($args['renderer']) ) { $renderer = $args['renderer']; } else { $renderer = 'dot'; } $cmd = $renderer; $mapDashTOption = ' -Tcmapx '; $inputOption = ''; switch($cmd) { case 'circo': case 'dot': case 'fdp': break; case 'mscgen': $inputOption = '-i '; $mapDashTOption = ' -T ismap '; break; case 'neato': case 'twopi': break; default: $cmd = 'dot'; } $cmd = $wgGraphVizSettings->execPath . $cmd; if ( $isWindows ) { $cmd = $cmd . '.exe'; } $storagename = urldecode($_GET['title']).'-'.$renderer.'---'; $storagename = str_replace("&",'_amp_',$storagename); $storagename = str_replace("#",'_shrp_',$storagename); $storagename = str_replace("/",'_sd_',$storagename); $storagename = str_replace("\\",'_sd_',$storagename); $storagename = str_replace(":",'_',$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'; // Added to allow for different diagram types - MP if ( isset($args['renderer']) ) { $renderer = $args['renderer']; } else { $renderer = 'dot'; } $cmd = $renderer; $mapDashTOption = ' -Tcmapx '; $inputOption = ''; switch($cmd) { case 'circo': case 'dot': case 'fdp': break; case 'mscgen': $inputOption = '-i '; $mapDashTOption = ' -T ismap '; break; case 'neato': case 'twopi': break; default: $cmd = 'dot'; } $cmd = $wgGraphVizSettings->execPath . $cmd; if ( $isWindows ) { $cmd = $cmd . '.exe'; } if ( $wgGraphVizSettings->named == 'named' || ! ( file_exists( $src.".png" ) || file_exists( $src.".err" ) ) ) { $timelinesrc = rewriteWikiUrls( $timelinesrc); $handle = fopen($src, "w"); fwrite($handle, $timelinesrc); fclose($handle); $cmdline = wfEscapeShellArg($cmd). ' -T png -o '.wfEscapeShellArg($imgn).' '.$inputOption.wfEscapeShellArg($src); $cmdlinemap = wfEscapeShellArg($cmd). $mapDashTOption.'-o '.wfEscapeShellArg($mapn).' '.$inputOption.wfEscapeShellArg($src); // Modifications made to correctly call cmd-line from Windows - MP #echo $cmdline; #exit; if ( $isWindows ) { $WshShell = new COM("WScript.Shell"); $ret = $WshShell->Exec($cmdline); #$ret = $WshShell->Exec($cmdline)->StdOut->ReadAll; } else { $ret = shell_exec($cmdline); } if ( $wgGraphVizSettings->install && $ret == "" ) { echo '<div id="toc"><tt>'.$ret.'GraphViz error: Executable not found.'. "\n".'Command line was: '.$cmdline.'</tt></div>'; exit; } if ( $isWindows ) { $retmap = $WshShell->Exec($cmdlinemap); #$ret = $WshShell->Exec($cmdlinemap)->StdOut->ReadAll; } else { $retmap = shell_exec($cmdlinemap); } if ( $wgGraphVizSettings->install && $retmap == "" ) { echo '<div id="toc"><tt>'.$retmap.'GraphViz error: Executable not found.'. "\n".'Command line was: '.$cmdlinemap.'</tt></div>'; exit; } if ( $isWindows ) { while ( $ret->Status == 0 || $retmap->Status == 0 ) { usleep(100); } } unlink($src); } @$err=file_get_contents( $src.".err" ); if ( $err != "" ) { $txt = '<div id="toc"><tt>'.$err.'</tt></div>'; } else { if ( false == ($map = file_get_contents( $mapn ) ) ) { $txt = '<div id="toc"><tt>File: '.$mapn.' is missing or empty.</tt></div>'; //return $txt; } $map = preg_replace('#<ma(.*)>#',' ',$map); $map = str_replace('</map>','',$map); if ( $renderer == 'mscgen' ) { $mapbefore = $map; $map = preg_replace('/(\w+)\s([_:%#/\w]+)\s(\d+,\d+)\s(\d+,\d+)/', '<area shape="$1" href="$2" title="$2" alt="$2" coords="$3,$4" />', $map); } $txt = '<map name="'.$storagename.'">'.$map.'</map>'. '<img src="'.$wgUploadPath.'/graphviz/'.$storagename.'.png"'. ' usemap="#'.$storagename.'" />'; } return $txt; } function rewriteWikiUrls( &$source) { $line = preg_replace( '|\[\[([^]]+)\]\]|e', 'Title::newFromText($1)->getFullURL()', $source ); return $line; } ?>
[edit] MscGen
To get MscGen to work with this, you have to make a slight modification to this. In the 'wfGraphVizExtension' function, add:
$wgParser->setHook( "mscgen", "renderMscGen" );
right below the included setHook command. Then, at the end of the file, add the following function:
function renderMscGen( $timelinesrc, $args, $parser ) { $args["renderer"] = "mscgen"; return renderGraphviz($timelinesrc, $args, $parser); }