Difference between revisions of "Ploticus extension"

From FollowTheScore
Jump to: navigation, search
(Example 2 (dynamic version))
 
(7 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.
  
Be careful, you MUST go into EDIT mode of this page and then copy the code.
+
Our version of the ''MediaWiki Ploticus Extension'' is contained in the [[Download|Download Archive of DPL]].
This is due to some tags contained in the source code which confuse the wiki parser ...
 
  
 +
== Example 1 (static version) ==
 +
 +
{|
 +
|
 
<pre>
 
<pre>
<?php
+
{{#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
 +
 
 +
}}
 +
</pre>
 +
|
 +
{{#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
 +
outlinedetails: color=blue
 +
center: 3 3
 +
radius: 1.1
 +
colors: dullyellow drabgreen pink powderblue lavender
 +
labelfarout: 1.2
 +
explode: 0 0.2 0 0
  
/**
+
}}
* Ploticus.php
+
|}
* Ploticus extension for just-in-time graph generation
 
* This extension is inspired from the EasyTimeline and the Gnuplot extensions.
 
* Copyright Flavien Scheurer, October 2007
 
* Minor Modifications by Gero Scholz, December 2007
 
*
 
* 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();
+
== Example 2 (dynamic version) ==
  
class PloticusSettings {
+
The chart shows the usage count of some articles.
function PloticusSettings () {
+
You can visit an article and see how the chart reflects the changed count.
// Set path to the Ploticus executable (should be overridden in LocalSettings.php).
 
// $this->exePath = '/mywiki/extensions/Ploticus/linux/bin/pl';
 
$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;
+
{|
 +
|
 +
<pre><nowiki>
 +
{{#ploticus:
  
$wgExtensionFunctions[] = 'wfPloticusExtension';
+
# get data from DPL query
$wgHooks['LanguageGetMagic'][] = 'wfPloticus_Magic';
+
#proc getdata
 +
  data:
 +
{{UsageCount|African Union member states}}
  
$wgExtensionCredits['parserhook'][] = array(
+
//  render pie graph
'name' => 'Ploticus',
+
#proc pie
  'version' =>  WGRAPH_VERSION,
+
datafield: 2
'author' => 'Flavien Scheurer',
+
labelfmtstring: @@1\n@@2\n@@PCT%
'url' => 'http://www.mediawiki.org/wiki/Extension:Ploticus',
+
labelmode: line+label
'description' => 'Ploticus extension for just-in-time graph generation<br/>'.
+
center: 3 3
'Syntax is &lt;ploticus&gt;...&lt;/ploticus&gt;<br/>'.
+
radius: 1.1
'Script handbook: http://ploticus.sourceforge.net/doc/scripthome.html',
+
colors: dullyellow drabgreen pink
);
+
labelfarout: 1.2
 +
explode: 0 0.2 0 0
  
function wfPloticusExtension() {
+
}}
global $wgParser;
+
</nowiki></pre>
$wgParser->setHook('ploticus', 'renderPloticus');
+
|
# add a parser function hook
+
{{#ploticus:
$wgParser->setFunctionHook( 'ploticus', 'renderPloticusPF' );
 
}
 
  
function wfPloticus_Magic( &$magicWords, $langCode ) {
+
# get data from DPL query
        $magicWords['ploticus'] = array( 0, 'ploticus' );
+
#proc getdata
        return true;
+
  data:
}
+
{{UsageCount|African Union member states}}
  
function renderPloticusPF( &$parser ) {
+
//  render pie graph
$numargs = func_num_args();
+
#proc pie
$arg_list = func_get_args();
+
datafield: 2
if ($numargs < 2) return ('!! #ploticus: empty script. !!');
+
labelfmtstring: @@1\n@@2\n@@PCT%
+
labelmode: line+label
// we must get the generated HTML through the parser->strip() function
+
outlinedetails: color=blue
// is there a better way for this?
+
center: 3 3
global $wgRawHtml;
+
radius: 1.1
$wgRawHtml = true;
+
colors: dullyellow drabgreen pink powderblue lavender
return '<html>'.renderPloticus($arg_list[1]).'</html>';
+
labelfarout: 1.2
}
+
explode: 0 0.2 0 0
  
function renderPloticus( $ploticusData ) {
+
}}
global $wgPloticusSettings, $wgUploadDirectory, $wgUploadPath;
+
|}
// Remove potentially dangerous keywords.
 
$replaces = array('`'  => '', 'system' => '', 'shell' => '');
 
$ploticusData = strtr($ploticusData, $replaces);
 
// Create the image directory.
 
$ploticusDirectory = $wgUploadDirectory . '/ploticus/';
 
if (!is_dir($ploticusDirectory)) {
 
mkdir($ploticusDirectory, 0777);
 
chmod($ploticusDirectory, 0777);
 
}
 
// 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>');
 
}
 
}
 
?>
 
</pre>
 

Latest revision as of 10: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

}}