Difference between revisions of "Ploticus extension"

From FollowTheScore
Jump to: navigation, search
(Example 2 (dynamic version))
 
(8 intermediate revisions by the same user not shown)
Line 1: Line 1:
Note that we use a slightly enhaced version of ''Ploticus''. The main difference is that our version can take data from an embedded template.
+
=== Ploticus Tool ===
 +
[http://ploticus.sourceforge.net/doc/welcome.html PLOTICUS] is a free softwarere package for creating plots and charts.
  
You will find ''Ploticus 1.1'' in one of the next DPL versions.
+
=== Ploticus Extension ===
 +
The [http://www.mediawiki.org/wiki/Extension:Ploticus Mediawiki Extension:Ploticus] allows to embed Ploticus into a Mediawiki installation. When using the ''Ploticus Extension'' you have to deliver a script which contains the data on which the chart shall be based.
  
Until then, here is the source:
+
This is where DPL comes into the game: DPL can be used to extract that data from information contained in the articles of your wiki. For that purpose we made a small modification to the ''Ploticus Extension'': Our modified version can take data not only from a hand-written script but also from the output of a DPL script.
  
 +
Our version of the ''MediaWiki Ploticus Extension'' is contained in the [[Download|Download Archive of DPL]].
 +
 +
== Example 1 (static version) ==
 +
 +
{|
 +
|
 
<pre>
 
<pre>
<?php
+
{{#ploticus:
  
/**
+
# get data from DPL query
* Ploticus.php
+
#proc getdata
* Ploticus extension for just-in-time graph generation
+
   data:
* This extension is inspired from the EasyTimeline and the Gnuplot extensions.
+
"Jack" 151
* Copyright Flavien Scheurer, October 2007
+
"Jim" 23
* Minor Modifications by Gero Scholz, December 2007
+
"Susan" 123
*
 
* Tested on:
 
* - MediaWiki 1.11.0
 
* - Apache 2.2.4 on Windows Server 2003
 
* - PHP 5.2.4
 
* - Not tested on *nix but should work ok by switching path separator from \ to /.
 
* Requirements:
 
* - Ploticus 2.33 (http://ploticus.sourceforge.net/doc/download.html).
 
* Syntax:
 
* <ploticus>...</ploticus> or {{#ploticus: ... }}
 
* Script handbook:
 
* http://ploticus.sourceforge.net/doc/scripthome.html
 
* Installation:
 
*   - Create directory 'yourWiki/extensions/Ploticus'
 
* - Install Ploticus under 'yourWiki/extensions/Ploticus/linux' or 'yourWiki/extensions/Ploticus/windows'
 
*    To use any other location (avoid spaces in the path) adapt the execution path appropriately
 
* - Add in LocalSettings.php:
 
* require_once("$IP/extensions/Ploticus/Ploticus.php");
 
* $wgPloticusSettings->exePath = "$IP/extensions/Ploticus/linux/bin/pl";
 
* # $wgPloticusSettings->exePath = "$IP/extensions/Ploticus/windows/bin/pl.exe";
 
* $wgPloticusSettings->imageFormat = 'png';  // use 'gif' for windows
 
* Todo:
 
* - Add support for clickable maps.
 
* - Cleanup previous generaget files in the Ploticus folder.
 
* - Add support for pretty PNG.
 
* Warning:
 
* - Designed with security in mind, but this is my first public PHP script and should be reviewed!
 
*
 
*
 
* @version 1.0
 
* initial version
 
* @version 1.1
 
* allow the extension to be used also as a parser function; thus templates can be used
 
*          to take data from
 
*
 
*/
 
define('PLOTICUS_VERSION', '0.2');              // current version
 
  
if (!defined('MEDIAWIKI')) die();
+
//  render pie graph
 +
#proc pie
 +
datafield: 2
 +
labelfmtstring: @@1\n@@2\n@@PCT%
 +
labelmode: line+label
 +
center: 3 3
 +
radius: 1.1
 +
colors: dullyellow drabgreen pink
 +
labelfarout: 1.2
 +
explode: 0 0.2 0 0
  
class PloticusSettings {
+
}}
function PloticusSettings () {
+
</pre>
// Set path to the Ploticus executable (should be overridden in LocalSettings.php).
+
|
// $this->exePath = '/mywiki/extensions/Ploticus/linux/bin/pl';
+
{{#ploticus:
$this->exePath = '/mywiki/extensions/Ploticus/windows/bin/pl.exe';
 
 
// Set the image format (gif by default, png not supported on Windows, svg not supported on IE 6,
 
//                      gif not supported by precompiled linux binary).
 
// $this->imageFormat = 'png';  // png or svgz = typical setting for linux
 
$this->imageFormat = 'gif'; //        gif = typical setting for windows
 
}
 
}
 
  
$wgPloticusSettings = new PloticusSettings;
+
# get data from DPL query
 +
#proc getdata
 +
  data:
 +
"Jack" 151
 +
"Jim" 23
 +
"Susan" 123
  
$wgExtensionFunctions[] = 'wfPloticusExtension';
+
//  render pie graph
$wgHooks['LanguageGetMagic'][] = 'wfPloticus_Magic';
+
#proc pie
 +
datafield: 2
 +
labelfmtstring: @@1\n@@2\n@@PCT%
 +
labelmode: line+label
 +
outlinedetails: color=blue
 +
center: 3 3
 +
radius: 1.1
 +
colors: dullyellow drabgreen pink powderblue lavender
 +
labelfarout: 1.2
 +
explode: 0 0.2 0 0
  
$wgExtensionCredits['parserhook'][] = array(
+
}}
'name' => 'Ploticus',
+
|}
  'version' =>  WGRAPH_VERSION,
 
'author' => 'Flavien Scheurer',
 
'url' => 'http://www.mediawiki.org/wiki/Extension:Ploticus',
 
'description' => 'Ploticus extension for just-in-time graph generation<br/>'.
 
'Syntax is &lt;ploticus&gt;...&lt;/ploticus&gt;<br/>'.
 
'Script handbook: http://ploticus.sourceforge.net/doc/scripthome.html',
 
);
 
  
function wfPloticusExtension() {
+
== Example 2 (dynamic version) ==
global $wgParser;
 
$wgParser->setHook('ploticus', 'renderPloticus');
 
# add a parser function hook
 
$wgParser->setFunctionHook( 'ploticus', 'renderPloticusPF' );
 
}
 
  
function wfPloticus_Magic( &$magicWords, $langCode ) {
+
The chart shows the usage count of some articles.
        $magicWords['ploticus'] = array( 0, 'ploticus' );
+
You can visit an article and see how the chart reflects the changed count.
        return true;
 
}
 
  
function renderPloticusPF( &$parser ) {
+
{|
$numargs = func_num_args();
+
|
$arg_list = func_get_args();
+
<pre><nowiki>
if ($numargs < 2) return ('!! #ploticus: empty script. !!');
+
{{#ploticus:
 
// we must get the generated HTML through the parser->strip() function
 
// is there a better way for this?
 
global $wgRawHtml;
 
$wgRawHtml = true;
 
return '<html>'.renderPloticus($arg_list[1]).'</html>';
 
}
 
  
function renderPloticus( $ploticusData ) {
+
# get data from DPL query
global $wgPloticusSettings, $wgUploadDirectory, $wgUploadPath;
+
#proc getdata
// Remove potentially dangerous keywords.
+
  data:
$replaces = array('`'  => '', 'system' => '', 'shell' => '');
+
{{UsageCount|African Union member states}}
$ploticusData = strtr($ploticusData, $replaces);
+
 
// Create the image directory.
+
// render pie graph
$ploticusDirectory = $wgUploadDirectory . '/ploticus/';
+
#proc pie
if (!is_dir($ploticusDirectory)) {
+
datafield: 2
mkdir($ploticusDirectory, 0777);
+
labelfmtstring: @@1\n@@2\n@@PCT%
chmod($ploticusDirectory, 0777);
+
labelmode: line+label
}
+
center: 3 3
// Generate a file name based on the hashed ploticus data.
+
radius: 1.1
$name = md5($ploticusData);
+
colors: dullyellow drabgreen pink
$graphFile = $ploticusDirectory . $name . '.' . $wgPloticusSettings->imageFormat;
+
labelfarout: 1.2
$graphURL = $wgUploadPath . '/ploticus/' . $name . '.' . $wgPloticusSettings->imageFormat;
+
explode: 0 0.2 0 0
// Check if a previous plot is available.
+
 
if (!file_exists($graphFile)) {
+
}}
$dataFile = $ploticusDirectory . $name . '.plo';
+
</nowiki></pre>
$errorFile = $ploticusDirectory . $name . '.txt';
+
|
// Verify that Ploticus is installed.
+
{{#ploticus:
if (!file_exists($wgPloticusSettings->exePath)) {
+
 
return ('<p><strong>Error: Could not find Ploticus in <em>' . $wgPloticusSettings->exePath . '</em></strong></p>');
+
# get data from DPL query
}
+
#proc getdata
// Write the ploticus data to a file.
+
  data:
$handle = fopen($dataFile, 'w');
+
{{UsageCount|African Union member states}}
fwrite($handle, $ploticusData);
+
 
fclose($handle);
+
// render pie graph
//Set the command line.
+
#proc pie
$commandline = wfEscapeShellArg($wgPloticusSettings->exePath) .
+
datafield: 2
' -' . $wgPloticusSettings->imageFormat .
+
labelfmtstring: @@1\n@@2\n@@PCT%
' ' . wfEscapeShellArg($dataFile) .
+
labelmode: line+label
' -o ' . wfEscapeShellArg($graphFile) .
+
outlinedetails: color=blue
' 2>' . wfEscapeShellArg($errorFile);
+
center: 3 3
// Execute Ploticus.
+
radius: 1.1
wfShellExec($commandline);
+
colors: dullyellow drabgreen pink powderblue lavender
// Read the error messages from the error file.
+
labelfarout: 1.2
$errorData = file_get_contents($errorFile);
+
explode: 0 0.2 0 0
// Delete the ploticus data and error files.
+
 
if (file_exists($dataFile)) { unlink($dataFile);}
+
}}
if (file_exists($errorFile)) { unlink($errorFile);}
+
|}
}
 
// Prepare the output.
 
if (isset($errorData) && $errorData != '') {
 
return ('<p><strong>Error processing Ploticus data:</strong><br/><pre>' . $errorData . '</pre></p>');
 
}
 
else {
 
return ('<p><img src="' . $graphURL . '" alt="Ploticus Chart"></p>');
 
}
 
}
 
?>
 
</pre>
 

Latest revision as of 09:38, 20 August 2012

Ploticus Tool

PLOTICUS is a free softwarere package for creating plots and charts.

Ploticus Extension

The Mediawiki Extension:Ploticus allows to embed Ploticus into a Mediawiki installation. When using the Ploticus Extension you have to deliver a script which contains the data on which the chart shall be based.

This is where DPL comes into the game: DPL can be used to extract that data from information contained in the articles of your wiki. For that purpose we made a small modification to the Ploticus Extension: Our modified version can take data not only from a hand-written script but also from the output of a DPL script.

Our version of the MediaWiki Ploticus Extension is contained in the Download Archive of DPL.

Example 1 (static version)

{{#ploticus:

# get data from DPL query
#proc getdata
  data:
"Jack" 151
"Jim" 23
"Susan" 123

//  render pie graph
#proc pie
datafield: 2
labelfmtstring: @@1\n@@2\n@@PCT%
labelmode: line+label
center: 3 3
radius: 1.1
colors: dullyellow drabgreen pink
labelfarout: 1.2
explode: 0 0.2 0 0

}}

{{#ploticus:

  1. get data from DPL query
  2. proc getdata
 data:

"Jack" 151 "Jim" 23 "Susan" 123

// render pie graph

  1. proc pie

datafield: 2 labelfmtstring: @@1\n@@2\n@@PCT% labelmode: line+label outlinedetails: color=blue center: 3 3 radius: 1.1 colors: dullyellow drabgreen pink powderblue lavender labelfarout: 1.2 explode: 0 0.2 0 0

}}

Example 2 (dynamic version)

The chart shows the usage count of some articles. You can visit an article and see how the chart reflects the changed count.

{{#ploticus:

# get data from DPL query
#proc getdata
  data:
{{UsageCount|African Union member states}}

//  render pie graph
#proc pie
datafield: 2
labelfmtstring: @@1\n@@2\n@@PCT%
labelmode: line+label
center: 3 3
radius: 1.1
colors: dullyellow drabgreen pink
labelfarout: 1.2
explode: 0 0.2 0 0

}}

{{#ploticus:

  1. get data from DPL query
  2. proc getdata
 data:

category = African Union member states

"Cameroon" %COUNT%
"Nigeria" %COUNT%
"Somalia" %COUNT%
"Sudan" %COUNT%


// render pie graph

  1. proc pie

datafield: 2 labelfmtstring: @@1\n@@2\n@@PCT% labelmode: line+label outlinedetails: color=blue center: 3 3 radius: 1.1 colors: dullyellow drabgreen pink powderblue lavender labelfarout: 1.2 explode: 0 0.2 0 0

}}