Extension:Gnuplot

From MediaWiki.org

Jump to: navigation, search
Zeichen 206.svg WARNING: the code or configuration described here poses a major security risk.

Problem: open to arbitrary code execution attacks
Solution: sanitize user input to remove gnuplot commands that allow arbitrary code to be run (this may not be fully possible)
Signed: Ryan lane 15:20, 23 June 2009 (UTC)

Wikimedia-logo-meta.png

This page was recently moved from MetaWiki.
It probably requires cleanup – please feel free to help out. In addition, some links on the page may be red; respective pages might be found at Meta. Remove this template once cleanup is complete.

Contents

[edit] Purpose

The Gnuplot-Extension allows to produce and display plots in a mediawiki instance. It uses the free program Gnuplot for creating the plots. It allows to plot 2- or 3-dimensional data, within the potentials of the Gnuplot program itself (Gnuplot Homepage).

[edit] Word of Warning

SECURITY RISK

The gnuplot script accepts potentially devlish inputs (rm -rf /) as actual commands into its script. You really need to be careful what audience you allow to use this extension, and make sure you have a pretty tight leash on the user that runs your webhost.

[edit] Use

  • Include the Gnuplot code between the tags <gnuplot> ... </gnuplot>.
  • The only required gnuplot command between these tags is the 'plot' command, e.g. plot sin(x). The plot-command should be the last command in order for any settings to take effect.
  • There is no need to specify a file name for the image (in gnuplot code: set output '<filename>.png'). If the file name is not specified then a hash value is chosen as name automatically. However: If the image shall be used on other wiki pages give a sensible name, i.e. by set output 'my_sensible_name.png'.
  • If you specify the name, 'set output' may not appear right after the <gnuplot> opening-tag.
  • You can plot functions, e.g. ...
<gnuplot>plot sin(x)</gnuplot>
<gnuplot>plot x**2-3</gnuplot>
  • ... as well as data. There are two ways of plotting data:
    1. Plot data from a file
      • Create a file containing the data in columns. Upload this file via Special:Upload.
      • Use the gnuplot plot command, specifying the filename with the keyword src: e.g.
        <gnuplot>plot 'src:<DataFile>.gpl' using 2:3</gnuplot> (2:3 specifies to plot the second against the third column).
    2. Plot inline data, i.e. data that is given directly between the <gnuplot> and </gnuplot> tags.
      • The input file for the plot command is the special file '-'. Data is given line by line, terminated by 'e' in a new line. A typical plot command for inline data could be as follows:
<gnuplot>
 plot '-' using 1:2 t 'quadratic approximation' with linesp lt 1 lw 3, \
 '-' using 1:2 t 'cubic approximation' with linesp lt 2 lw 3
 1 2
 2 4
 3 8
 e
 1 3
 2 9
 3 27
 e
</gnuplot>

For more information of gnuplot commands, see the Gnuplot Documentation.

A good manual can be found at Gnuplot Tutorial.

[edit] Sample plots

The following graphs give a (very) small impression of plots possible with this extension.

  • Plotting functions
 <gnuplot>
   set output 'quadFuncs.png'
   set size 0.4,0.4
   set xlabel "x"
   set ylabel "y"
   plot [x=-4:4] x**2-3, -x**2
 </gnuplot>

File:TwoQuadFuncs.png

  • Plotting data from a file (after having uploaded the file budgetSample.txt containing the data (see below) - the extension of the file must be supported for file upload)
 <gnuplot>
   set output 'budgetExample.png'
   set xlabel "Month of 2004"
   set ylabel "Amount [Euro]"
   plot 'src:budgetSample_gpl.txt' using 1:2 title 'planned',\
  'src:budgetSample_gpl.txt' using 1:3 title 'interpolated',\
  'src:budgetSample_gpl.txt' using 1:4 title 'spent' 
 </gnuplot>
 

File:Budget plot example.png

budgetSample_gpl.txt:

# month planned interpolated spent
1       513300  42775        50000
2       513300  85550        83200
3       513300  128325       137650
4       513300  171100       187655
5       513300  213875       246585
6       513300  256650       265475
7       513300  299425       309870
8       513300  342200       363976
9       513300  384975       403692
10      513300  427750       459872
11      513300  470525       491234
12      513300  513300       536829
  • Plotting inline data
 <gnuplot>
 set output 'func_approx.png'
  plot '-' using 1:2 t 'quadratic approximation' with linesp lt 1 lw 3, \
  '-' using 1:2 t 'cubic approximation' with linesp lt 2 lw 3
  1 2
  2 4
  3 8
  4 16
  e
  1 3
  2 9
  3 27
  4 81
  e
 </gnuplot>
 

File:Inline data plot example.png

[edit] Install instructions

($WIKI is the wiki home directory)

  • Prerequisite: Gnuplot 4.0 download
  • OR: install your Distribution-Package with support for gd ggi plotutils gnuplot
  • Store the script below in the file Gnuplot.php to the directory $WIKI/extensions
cd $WIKI/extensions
svn co http://svn.wikimedia.org/svnroot/mediawiki/trunk/extensions/Gnuplot
include("extensions/Gnuplot/Gnuplot.php");
$wgGnuplotCommand = '<yourGnuplotPath>';
Windows example path:
$wgGnuplotCommand = "C:\\Program files\\xampp\\gnuplot\\bin\\pgnuplot.exe";
  • Make sure that your directory for file uploads is writable (usually: $WIKI/images), which it usually will be.
  • If you want to allow data to be plotted from a file, then file uploading must be enabled: the type of file for the gnuplot input data (the file extension, e.g. 'dat') must be included by adding the following command to $WIKI/LocalSettings.php (Add all extensions you want to allow for file upload, e.g. 'jpg', 'ogg', ...):
$wgFileExtensions = array('dat', 'png', '...');

[edit] Default Settings

  • In Gnuplot.php, the default setting is
 set terminal png color
 set size 0.5,0.5
  • Note that you can overwrite these settings by your gnuplot call in the wiki markup.

[edit] The script

The script is available from MediaWiki SVN: http://svn.wikimedia.org/viewvc/mediawiki/trunk/extensions/Gnuplot (More information on SVN). Store the files in your local $WIKI/extensions/Gnuplot directory.

[edit] See also