Difference between revisions of "Ploticus extension"

From FollowTheScore
Jump to: navigation, search
(New page: Note that we use a slightly enhaced version of ''Ploticus''. The main difference is that our version can take data from an embedded template. You will find ''Ploticus 1.1'' in one of the ...)
 
(Example 2 (dynamic version))
 
(9 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.
  
<pre><nowiki>
+
Our version of the ''MediaWiki Ploticus Extension'' is contained in the [[Download|Download Archive of DPL]].
<?php
+
 
 +
== Example 1 (static version) ==
 +
 
 +
{|
 +
|
 +
<pre>
 +
{{#ploticus:
 +
 
 +
# get data from DPL query
 +
#proc getdata
 +
  data:
 +
"Jack" 151
 +
"Jim" 23
 +
"Susan" 123
  
/**
+
//  render pie graph
* Ploticus.php
+
#proc pie
* Ploticus extension for just-in-time graph generation
+
datafield: 2
* This extension is inspired from the EasyTimeline and the Gnuplot extensions.
+
labelfmtstring: @@1\n@@2\n@@PCT%
* Copyright Flavien Scheurer, October 2007
+
labelmode: line+label
* Minor Modifications by Gero Scholz, December 2007
+
center: 3 3
*
+
radius: 1.1
* Tested on:
+
colors: dullyellow drabgreen pink
* - MediaWiki 1.11.0
+
labelfarout: 1.2
* - Apache 2.2.4 on Windows Server 2003
+
explode: 0 0.2 0 0
* - 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();
+
}}
 +
</pre>
 +
|
 +
{{#ploticus:
  
class PloticusSettings {
+
# get data from DPL query
function PloticusSettings () {
+
#proc getdata
// Set path to the Ploticus executable (should be overridden in LocalSettings.php).
+
  data:
// $this->exePath = '/mywiki/extensions/Ploticus/linux/bin/pl';
+
"Jack" 151
$this->exePath = '/mywiki/extensions/Ploticus/windows/bin/pl.exe';
+
"Jim" 23
+
"Susan" 123
// 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;
+
//  render pie graph
 +
#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
  
$wgExtensionFunctions[] = 'wfPloticusExtension';
+
}}
$wgHooks['LanguageGetMagic'][] = 'wfPloticus_Magic';
+
|}
  
$wgExtensionCredits['parserhook'][] = array(
+
== Example 2 (dynamic version) ==
'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() {
+
The chart shows the usage count of some articles.
global $wgParser;
+
You can visit an article and see how the chart reflects the changed count.
$wgParser->setHook('ploticus', 'renderPloticus');
 
# add a parser function hook
 
$wgParser->setFunctionHook( 'ploticus', 'renderPloticusPF' );
 
}
 
  
function wfPloticus_Magic( &$magicWords, $langCode ) {
+
{|
        $magicWords['ploticus'] = array( 0, 'ploticus' );
+
|
        return true;
+
<pre><nowiki>
}
+
{{#ploticus:
  
function renderPloticusPF( &$parser ) {
+
# get data from DPL query
$numargs = func_num_args();
+
#proc getdata
$arg_list = func_get_args();
+
  data:
if ($numargs < 2) return ('!! #ploticus: empty script. !!');
+
{{UsageCount|African Union member states}}
 
// 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 ) {
+
//  render pie graph
global $wgPloticusSettings, $wgUploadDirectory, $wgUploadPath;
+
#proc pie
// Remove potentially dangerous keywords.
+
datafield: 2
$replaces = array('`' => '', 'system' => '', 'shell' => '');
+
labelfmtstring: @@1\n@@2\n@@PCT%
$ploticusData = strtr($ploticusData, $replaces);
+
labelmode: line+label
// Create the image directory.
+
center: 3 3
$ploticusDirectory = $wgUploadDirectory . '/ploticus/';
+
radius: 1.1
if (!is_dir($ploticusDirectory)) {
+
colors: dullyellow drabgreen pink
mkdir($ploticusDirectory, 0777);
+
labelfarout: 1.2
chmod($ploticusDirectory, 0777);
+
explode: 0 0.2 0 0
}
+
 
// Generate a file name based on the hashed ploticus data.
+
}}
$name = md5($ploticusData);
 
$graphFile = $ploticusDirectory . $name . '.' . $wgPloticusSettings->imageFormat;
 
$graphURL = $wgUploadPath . '/ploticus/' . $name . '.' . $wgPloticusSettings->imageFormat;
 
// Check if a previous plot is available.
 
if (!file_exists($graphFile)) {
 
$dataFile = $ploticusDirectory . $name . '.plo';
 
$errorFile = $ploticusDirectory . $name . '.txt';
 
// Verify that Ploticus is installed.
 
if (!file_exists($wgPloticusSettings->exePath)) {
 
return ('<p><strong>Error: Could not find Ploticus in <em>' . $wgPloticusSettings->exePath . '</em></strong></p>');
 
}
 
// Write the ploticus data to a file.
 
$handle = fopen($dataFile, 'w');
 
fwrite($handle, $ploticusData);
 
fclose($handle);
 
//Set the command line.
 
$commandline = wfEscapeShellArg($wgPloticusSettings->exePath) .
 
' -' . $wgPloticusSettings->imageFormat .
 
' ' . wfEscapeShellArg($dataFile) .
 
' -o ' . wfEscapeShellArg($graphFile) .
 
' 2>' . wfEscapeShellArg($errorFile);
 
// Execute Ploticus.
 
wfShellExec($commandline);
 
// Read the error messages from the error file.
 
$errorData = file_get_contents($errorFile);
 
// 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>');
 
}
 
}
 
?>
 
 
</nowiki></pre>
 
</nowiki></pre>
 +
|
 +
{{#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
 +
outlinedetails: color=blue
 +
center: 3 3
 +
radius: 1.1
 +
colors: dullyellow drabgreen pink powderblue lavender
 +
labelfarout: 1.2
 +
explode: 0 0.2 0 0
 +
 +
}}
 +
|}

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

}}