Extension:MscGen

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manual - list
Crystal Clear action run.png
MscGen

Release status: unknown

Implementation Tag
Description This extension allows to display Message Sequence Chart's.
Author(s) Ruud Schramp
License No license specified
Download see below

Check usage (experimental)

MscGen extension allows to display Message Sequence Chart's. It allows to display message flows. Flowchart was my first attempt to include such in MediaWiki, but recently I found the neat tool from http://www.mcternan.me.uk/mscgen/index.html which had been inspired by graphViz.

The code needs some cleaning up, but works.

Contents

[edit] Installation

This code should be installed in extensions/MSC.php and then included in LocalSettings.php.
The Mscgen package must be installed. In the source below, update $wgMSCSettings->mscgenCommand = "/usr/bin/mscgen" with the path to the mscgen executable.

[edit] Source

<?php
# CoffMan (http://www.wickle.com) code adapted from EasyTimeline extension.
# To use, include this file from your LocalSettings.php
# To configure, set members of $wgGraphVizSettings after the inclusion
# July 2006, Ruud Schramp
# modified GraphViz extension to use mscgen
# http://www.mcternan.me.uk/mscgen/index.html

$wgExtensionCredits['parserhook'][] = array(
        'name' => 'MscGen',
        'author' => 'Ruud Schramp',
        'url' => 'http://www.mediawiki.org/wiki/Extension:MscGen',
        'description' => 'Allows to display Message Sequence Chart\'s',
);
 
class MSCSettings {
    var $mscgenCommand;
};
 
$wgMSCSettings = new MSCSettings;
$wgMSCSettings->mscgenCommand = "/usr/bin/mscgen";
 
$wgExtensionFunctions[] = "wfMSCExtension";
 
function wfMSCExtension() {
    global $wgParser;
    $wgParser->setHook( "mscgen", "renderMscGen" );
}
 
function renderMscGen( $timelinesrc ) {
    global $wgUploadDirectory, $wgUploadPath, $IP, $wgMSCSettings, $wgArticlePath, $wgTmpDirectory;
    $hash = md5( $timelinesrc );
    $dest = $wgUploadDirectory."/mscgen/";
    if ( ! is_dir( $dest ) ) { mkdir( $dest, 0777 ); }
    if ( ! is_dir( $wgTmpDirectory ) ) { mkdir( $wgTmpDirectory, 0777 ); }
 
    $fname = $dest . $hash;
//  echo $fname;
    if ( ! ( file_exists( $fname.".png" ) || file_exists( $fname.".err" ) ) ) {
        $handle = fopen($fname, "w");
        fwrite($handle, $timelinesrc);
        fclose($handle);
 
        $cmdline = wfEscapeShellArg( $wgMSCSettings->mscgenCommand) .
          " -T png -o " . wfEscapeShellArg( $fname. ".png") . " " .
          " -i " . wfEscapeShellArg( $fname ) . " 2>&1  >" . wfEscapeShellArg( $fname. ".err") . " && rm " . wfEscapeShellArg( $fname. ".err");
        $ret = `{$cmdline}`;
        #      echo $cmdline;
        #      exit;
        #      break;
        #      echo "ADIOS";
        if ($ret) unlink ( $fname. ".err");
 
        unlink($fname);
 
    }
 
    @$err=file_get_contents( $fname.".err" );
 
    if ( $err != "" ) {
        $err_esc=htmlentities($err); 
        $txt = "<div id=\"toc\"><tt>$err_esc</tt></div>";
    } else {
        $txt  = "<img usemap=\"#{$hash}\" src=\"{$wgUploadPath}/mscgen/{$hash}.png\">";
    }
    return $txt;
}

[edit] Conversion from Flowchart to Mscgen

You can use the following perl script to convert from flowchart to mscgen

 # program to convert <flowchart> </flowchart> to 
 # <mscgen> </mscgen>
 # author: shishir birmiwal
 # ref: http://meta.wikimedia.org/wiki/MscGen 
 #      http://meta.wikimedia.org/wiki/Flowchart
 # method of use: 
 # put flowchart syntax text into a file, say flowchart.txt
 #  (don't forget to include <flowchart> </flowchart>)
 # call using
 #    '''perl -w flowchart2mscgen.pl flowchart.txt > mscgen.txt'''
 
 $inside_flowchart = 0;
 
 foreach $line (<>)
 {
   if ($line=~m/\<\s*flowchart\s*\>/ig)
   {
      $line=~s/:/,/g;
      chomp $line;
      $line.=";\n";
      $line=~s/\<\s*flowchart\s*\>/\<mscgen\>\nmsc\ \{\n\ \ \ /ig;
      $inside_flowchart = 1;
      print $line;
      $line="";
   }
 
   if ($line=~m/\<\s*\/flowchart\s*\>/ig)
   {
        $line=~s/\<\s*\/flowchart\s*\>/\}\n\<\/mscgen\>/ig;
        $inside_flowchart = 0;
   }
 
   if ($inside_flowchart == 1)
   {
        if ($line=~m/^\s*(.+)\s+(.+)\s+(.+)/g)
        {
            print "   $1->$2 \t[ label = \"$3\" ]; \n";
        }
   }
   else
   {
      print $line;
   }
 
 }

[edit] See also

Personal tools
Namespaces
Variants
Actions
Site
Support
Download
Development
Communication
Print/export
Toolbox