User talk:Hummel-riegel

From MediaWiki.org
Jump to: navigation, search

GraphViz-Version by Bytesmiths [edit]

Date
2011-01-18

Hi, you left a note on my talk page about the GraphVis extension.

Here is the version I modified and use. Hope this is helpful to you!

<?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;
}
?>