User:Hummel-riegel/GraphViz

This page is under construction Attention: This Extension is currently work in progress - The code may be functional but I don't give any guarantees Please visit the Extensions Extension:Graphviz oder Extension:GraphVizAndMSCGen for the actual code that I want to improve here.

Feel free to add your own stuff or to modifiy this page!

= Extension:GraphViz =

Graphviz 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. Mscgen is a small program that parses Message Sequence Chart descriptions. Unlike Graphviz, this program does no clever layout operations or spline routing as this is not needed.

Automatic graph drawing has many important applications in software engineering, database and web design, networking, and in visual interfaces for many other domains. You can use the provided language (e.g. dot) and create graphs with that within the wiki. There are also some other extensions that provide other interfaces for individual solutions.

Features

 * works out of the box for many Windows/Linux Systems (graphviz has to be in the right path)
 * additional support for mscgen
 * supports multiple graphs on one page
 * supports wiki-links in graphs
 * allows pruning of old images

Contributions
In 2004 the User Coffman created an extension to mediawiki in response to a basic need : Render Graphs online. He found the utility Graphviz on another wiki, and thought about adopting it to mediawiki (the wiki he actually used). Exploring the Graphviz tool he discovered an incredible tool to making Graphs. Later on many people improved the extension on their own or provided little bugfixes as snippets on the dicussion page. This led to several functional solutions for different use cases and to a bit of chaos. Now (Fall 2010) I, (Thomas Hummel), tried to merge the most common solutions so that we finally have another working solution for several OSes in one file. As I am not that well known to PHP, there might be several long-winded or impractical lines of code - feel free to send me your improvements or comments (Discussion-Page, Message or whatever). I started with the "improved version" from an unknown Coder and tried to fix some things on my own and then included parts of the code of other "improvements" and standalone solutions. Good sources were for example Extension:GraphVizAndMSCGen by Matthewpearson.

Graphviz

 * 1) First of all you need at least one of the external tools "Graphviz" or "MscGen" that renders your Graph.
 * 2) Windows: Get your copy here: http://www.graphviz.org/Download.php or here http://www.mcternan.me.uk/mscgen/
 * 3) Unix: Same links as for windows but normally it should be preinstalled (at least graphviz - search for dot) or can be found in the package manager.
 * 4) Then Copy the Code found here /Graphviz.php into a file called Graphviz.php and and place it in  .  ##Note: $IP stands for the root directory of your MediaWiki installation, the same directory that holds LocalSettings.php.
 * 5) On Ubuntu 10.10 an earlier version of the Graphviz-Extension has been added to the default (see also Manual:Running_MediaWiki_on_Ubuntu) - although this is a very old version, so you'd better replace it with the one provided /Graphviz.php here.
 * 6) Add these lines to LocalSettings.php:
 * Windows Default: "C:/Program Files/Graphviz/bin/" (without dot.exe) or "C:\\Program Files\\Graphviz\\bin\\"
 * Other Platform : "/usr/bin/" or "/usr/local/bin/" (where the dot binary is)


 * Make sure that write permissions for the "$IP/images" directory are granted.
 * Make sure that internet users get read/execute access on your executables if you use server installations. (esp. under windows)

MscGen
The same procedure as with Graphviz - the path to MscGen has to be provided in the LocalSettings.php.

Differences to previous versions

 * The varable $dotCommand that points only to the dot executable is replaced (like in Extension:GraphVizAndMSCGen) with $execPath and $mscgenPath that let you choose between several renderers.

Configuration
There are some additional configuration parameters that help you to define a basic behaviour of the extension. If you don't use these parameters they contain a default value that leads to a (hopefully) good usage of the extension. For each parameter add the line  in your LocalSettings.php after the  -line.

Each of these parameters beside the execPath and mscgenPath can be changed for each individual graph created. They are used in the LocalSettings.php only for the default behaviour of all graphs in this wiki.

missing: name, border, position, renderer, ....

Naming of the Images

 * named
 * hashes
 * md5 duplicates
 * prune necessary

Drawing Graphs
Basically you only need to write your Graph in the dot language (or any other supported language) and put it into  or.

Mscgen
Keep in mind that mscgen uses a language _similar_ to dot but it isn't really dot. So you have to write your own code for graphviz and mscgen to display the same graph. (Furthermore the goals of graphviz and mscgen are not the same)

Another limitation when using dot is that you can't use multiple graphs on one page (if you don't use hashed image-names).

Easy Graphs
-> here AtoB example

Multiple Graphs
This extension supports multiple graphs of graphviz on one wiki-page.

If you name the pictures by the graph-names (named='named') you only have to name the graphs different. That means that the following two graphs can be put together on on page.

This doesn't work for mscgen-Graphs, because custom names aren't supported. But you can put one mscgen-Graph and multiple graphviz-graphs on one page.

If your pictures are named with md5- or sha1-hashes then you can have multiple graphs of each type (graphviz and/or mscgen) on your page. Keep in mind that you should clean up your image-directory from time to time, because the hash changes when the code has been modified. (See )

Wiki-Links
-> linking -> custom shapes

Image-Types
-> svg

Renderer
-> graphviz -> mscgen

Examples
http://www.wikischool.de/wiki/WikiSchool:Graphviz http://wiki.zum.de/Hilfe:Graphviz

Pruning
Each time we create a new graph we're going to check the Image-Path for pruning. There are several options on how to prune. Configure Pruning with the adequate parameters in LocalSettings.php (see ).

With the default settings you don't need any pruning, because each graph is saved accoring to its name. If you update the Graph the image-file will be replaced so there will be no unnecessary file. Reasons for pruning can be: limited storage-space for images, renamed or removed graphs, graphs are named with a hash.

However the best solution for pruning would be to clean up the images manually from time to time or do it automatically by a cronjob. This saves much cpu-load.

Code
/Graphviz.php

Development
Since I'm not a very experienced developer it would be nice if some "experts" could review the code or improve it on some edges.

I'm also happy if you'd notify me about bugs or problems you have (e.g. on the Discussion page).

Important points

 * Check for Security Leaks
 * especially there has been mentioned: This [former] plugin does not properly sanitize user input when generating the "named" version of the files. This can lead to clobbered files and/or more serious vulnerabilities. --Andy753421 04:54, 27 July 2010 (UTC)
 * See Security for developers for specific types of vulnerabilities

Things that should be done

 * Change the old version on the ubuntu default extensions package
 * What to do with graphs that are too large? e.g. for the GET-request? Maybe some attribute in the -Tag that this is partial code and that it waits? Or at least shortening some commands, links, ...?
 * using popen/proc_open instead of shell_exec for php save-mode if possible

Nice to have features

 * add uploadDir to variables
 * Provide this on a svn-Server?
 * On my local installation a graph is rerendered very often (each hour or so) even if nothing has changed on the page. (Is this only my fault?)
 * Can we reduce cpu-load with preventing graphviz to create a new image if the page hasn't changed?
 * Can I then prune all images that are older than an hour? Or do proper installations use a cache that maybe lasts a week or so?
 * Error when setting $wgGraphVizSettings->install=true on ubuntu.
 * Generate Graphs without extra tools (directly in the browser): Canvas?, Base 64 Encoding see also, Extension:Graph, ...
 * custom shapes for nodes in Graphviz aren't supported yet (http://www.graphviz.org/Documentation/html/shapehowto.html)
 * improve pruning
 * save pruning settings (maybe settings-file in graphviz-folder or images folder, because of write access?)
 * don't prune for each graph but only check date of last pruning
 * remove files older than some date
 * use the image formatting with wiki syntax with the help of MW-Renderer, so that it doesn't stay hardcoded

Alternatives
If you don't like this extension there are some others that I find remarkable (although I tried to put the main functionality of all of them into this extenstion).
 * The Extension by Gregory Szorc] is absolutely well coded and supports auto-pruning.
 * Extension:Graph This Extension lets you draw ASCII-Art, SVG, and Graphviz-Images without(?) the use of the Graphviz-Tool
 * Extension:FreeMind uses the FreeMind-Tool to draw graphs
 * Extension:Semantic_Graph also uses the FreeMind-Tool to draw (as an alternative to the

Projects based on this extension

 * The "Semantic Result Formats"-Extension is used in conjunction with the Semantic MediaWiki extension, that bundles a number of further result formats for SMW's inline queries.
 * Extension:Semantic Result Formats/graph_format (displays connections between pages (as graph)
 * Extension:Semantic Result Formats/process_format (displays process graphs)