GraphViz Source

<?php //{{MediaWikiExtension}}<source lang="php">
# CoffMan (http://www.wickle.com) code adapted from timeline extension.
# Timeline extension
# To use, include this file from your LocalSettings.php
# To configure, set members of $wgGraphVizSettings after the inclusion
#
# This version was modified by Kenny Root ( http://the-b.org/ ) to use the
# automatically generated SWIG version of GraphViz for PHP.
#
require_once("gv.php");

$wgExtensionFunctions[] = "wfGraphVizExtension";

function wfGraphVizExtension() {
        global $wgParser;
        $wgParser->setHook( "graphviz", "renderGraphviz" );
}

function renderGraphviz( $text, $params = array(), &$parser)
{
        global $wgUploadDirectory, $wgUploadPath, $IP, $wgGraphVizSettings, $wgArticlePath, $wgTmpDirectory;

        $layout = 'dot';
        if (array_key_exists('layout', $params))
                $layout = $params['layout'];

        $hash = md5( $layout . $text );
        $dest = $wgUploadDirectory."/graphviz/";
        if ( ! is_dir( $dest ) ) { mkdir( $dest, 0777 ); }

        $fname = $dest . $hash;

        if (! file_exists( $fname.".png" ) )
        {
                $G = gv::readstring($text);
                if (!gv::layout($G, $layout))
                        return "<p>Error in layout for Graphviz.</p>";
                if (!gv::render($G, "png", $fname.".png"))
                        return "<p>Error in PNG render for Graphviz.</p>";
                if (!gv::render($G, "cmapx", $fname.".map"))
                        return "<p>Error in map render for Graphviz.</p>";
        }

        @$map = file_get_contents( $fname.".map" );

        $map=preg_replace("#<ma(.*)>#"," ",$map);
        $map=str_replace("</map>","",$map);

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

        if (trim($map) != "") {
                $txt  = "<map name=\"$hash\">{$map}</map>".
                        "<img usemap=\"#{$hash}\" src=\"{$wgUploadPath}/graphviz/{$hash}.{$ext}\" />";
        } else {
                $txt = "<img src=\"{$wgUploadPath}/graphviz/{$hash}.{$ext}\" />";
        }

        return $txt;
}
//</source>

Copyright © Kenny Root. All rights reserved.