r50645 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r50644‎ | r50645 | r50646 >
Date:20:58, 15 May 2009
Author:laner
Status:deferred
Tags:
Comment:
* Added support for user defined plot names
* Added javascript fallback support; a table will be shown if javascript is not available
** Added the tableclass argument, so users can define the class of the fallback-table
* Updated the README to reflect all of the functionality implemented so far
Modified paths:
  • /trunk/extensions/Plotters/Plotters.i18n.php (modified) (history)
  • /trunk/extensions/Plotters/PlottersClass.php (modified) (history)
  • /trunk/extensions/Plotters/PlottersParser.php (modified) (history)
  • /trunk/extensions/Plotters/README (modified) (history)

Diff [purge]

Index: trunk/extensions/Plotters/PlottersParser.php
@@ -36,7 +36,7 @@
3737 // Parse arguments, set defaults, and do sanity checks
3838 $this->argumentArray = array ( "renderer" => "plotkit", "preprocessors" => array(), "preprocessorarguments" => array(),
3939 "script" => "", "scriptarguments" => array(), "datasep" => ",", "width" => "300", "height" => "300", "labels" => array(),
40 - "helpers" => array() );
 40+ "helpers" => array(), "name" => "plot", "tableclass" => "wikitable" );
4141 if ( isset( $argv["renderer"] ) ) {
4242 //TODO: limit this to supported renderers
4343 $this->argumentArray["renderer"] = preg_replace( '/[^A-Z0-9]/i', '', $argv["renderer"] );
@@ -114,6 +114,14 @@
115115 $helper = preg_replace( '/[^A-Z0-9]/i', '', $helper );
116116 }
117117 }
 118+ if ( isset( $argv["name"] ) ) {
 119+ // Sanitize names - alphanumerics only
 120+ $this->argumentArray["name"] = preg_replace( '/[^A-Z0-9]/i', '', $argv["name"] );
 121+ }
 122+ if ( isset( $argv["tableclass"] ) ) {
 123+ // Sanitize tableclass - alphanumerics only
 124+ $this->argumentArray["tableclass"] = preg_replace( '/[^A-Z0-9]/i', '', $argv["tableclass"] );
 125+ }
118126 }
119127
120128 function parseData( $input, $parser ) {
Index: trunk/extensions/Plotters/Plotters.i18n.php
@@ -28,6 +28,8 @@
2929 'plotters-excessively-long-scriptname' => 'The script name is too long. Please define a script that is less than 255 characters.',
3030 'plotters-excessively-long-preprocessorname' => 'The preprocessor name is too long. Please define a preprocessor that is less than 255 characters.',
3131 'plotters-excessively-long-helpername' => 'The helper name is too long. Please define a helper that is less than 255 characters.',
 32+ 'plotters-excessively-long-name' => 'The plot name is too long. Please define a plot name that is less than 255 characters.',
 33+ 'plotters-excessively-long-tableclass' => 'The tableclass is too long. Please define a tableclass that is less than 255 characters.',
3234 'plotters-no-data' => 'No data was provided.',
3335 'plotters-invalid-renderer' => 'An invalid renderer was selected.',
3436 'plotters-errors' => '<b>Plotters error(s):</b>',
Index: trunk/extensions/Plotters/PlottersClass.php
@@ -51,6 +51,7 @@
5252 // Check to ensure scriptname is < 255 characters
5353 $errors .= wfMsg( "plotters-excessively-long-scriptname" ) . "<br />";
5454 }
 55+
5556 // Check preprocessors and helpers
5657 foreach ( $this->argumentArray["preprocessors"] as $preprocessor ) {
5758 if ( strlen( $preprocessor ) > 255 ) {
@@ -62,6 +63,15 @@
6364 $errors .= wfMsg( "plotters-excessively-long-helpername" ) . "<br />";
6465 }
6566 }
 67+
 68+ if ( strlen( $this->argumentArray["name"] ) > 255 ) {
 69+ $errors .= wfMsg( "plotters-excessively-long-name" ) . "<br />";
 70+ }
 71+
 72+ if ( strlen( $this->argumentArray["tableclass"] ) > 255 ) {
 73+ $errors .= wfMsg( "plotters-excessively-long-tableclass" ) . "<br />";
 74+ }
 75+
6676 // Check for data
6777 if ( count( $this->dataArray ) == 0 ) {
6878 $errors .= wfMsg( "plotters-no-data" ) . "<br />";
@@ -107,19 +117,40 @@
108118 }
109119
110120 function renderPlot() {
111 - // TODO: allow user defined graph id
112 - return '<div><canvas id="graph" height="' . $this->argumentArray["height"] . '" width="' . $this->argumentArray["width"] . '"></canvas></div>';
 121+ return '<div id="' . $this->argumentArray["name"] . '-div" style="display: none;"><canvas id="' . $this->argumentArray["name"] . '" height="' . $this->argumentArray["height"] . '" width="' . $this->argumentArray["width"] . '"></canvas></div>';
113122 }
114123
115124 function renderFallback() {
116125 // Return an html table of the data
117 - return '';
 126+ $output = '<table id="' . $this->argumentArray["name"] . '-fallback" class="' . $this->argumentArray["tableclass"] . '">';
 127+ if ( count( $this->argumentArray["labels"] ) > 0 ) {
 128+ $output .= "<tr>";
 129+ foreach ( $this->argumentArray["labels"] as $label ) {
 130+ $output .= "<th>" . $label . "</th>";
 131+ }
 132+ $output .= "</tr>";
 133+ }
 134+ foreach ( $this->dataArray as $dataline ) {
 135+ $output .= "<tr>";
 136+ foreach ( $dataline as $data ) {
 137+ $output .= "<td>" . $data . "</td>";
 138+ }
 139+ $output .= "</tr>";
 140+ }
 141+ $output .= "</table>";
 142+
 143+ return $output;
118144 }
119145
120146 function renderJavascript() {
 147+ $name = $this->argumentArray["name"];
 148+
121149 $output = '<script type="text/javascript">';
122 - // TODO: allow user defined graph id
123 - $output .= 'function drawGraph() {';
 150+
 151+ $output .= 'document.getElementById("' . $this->argumentArray["name"] . '-fallback").style.display = "none";'; // hide fallback table
 152+ $output .= 'document.getElementById("' . $this->argumentArray["name"] . '-div").style.display = "block";'; // show canvas
 153+
 154+ $output .= 'function draw' . $this->argumentArray["name"] . '() {';
124155 $output .= 'var data = [];';
125156
126157 // Prepare data
@@ -140,7 +171,7 @@
141172
142173 // Run preprocessors
143174 foreach ( $this->argumentArray["preprocessors"] as $preprocessor ) {
144 - $output .= 'plotter_' . $preprocessor . '_process( data, labels, ';
 175+ $output .= 'plotter_' . $preprocessor . '_process( "' . $name . '", data, labels, ';
145176 foreach ( $this->argumentArray["preprocessorarguments"] as $argument ) {
146177 $output .= $argument . ', ';
147178 }
@@ -150,7 +181,7 @@
151182 }
152183
153184 // Run script
154 - $output .= 'plotter_' . $this->argumentArray["script"] . '_draw( data, labels, ';
 185+ $output .= 'plotter_' . $this->argumentArray["script"] . '_draw( "' . $name . '", data, labels, ';
155186 foreach ( $this->argumentArray["scriptarguments"] as $argument ) {
156187 $output .= "'" . $argument . "'" . ", ";
157188 }
@@ -160,8 +191,7 @@
161192 $output .= "}";
162193
163194 // Add hook event
164 - // TODO: allow user defined graph id
165 - $output .= 'hookEvent("load", drawGraph);';
 195+ $output .= 'hookEvent("load", draw' . $this->argumentArray["name"] . ');';
166196 $output .= "</script>";
167197
168198 return $output;
Index: trunk/extensions/Plotters/README
@@ -1,31 +1,76 @@
2 -This extension allows users to create client side graphs (like pie, bar, etc)
3 -from data using javascript. The javascript is created by sysops in the
4 -MediaWiki namespace. Adding new scripts works like the Gadgets extension (as
5 -this extension borrows heavily from Gadgets).
 2+This extension allows users to create client side graphs (like pie, bar, etc) from data using javascript. The javascript is created by sysops in the MediaWiki namespace. Adding new scripts works like the Gadgets extension (as this extension borrows heavily from Gadgets).
63
7 -Preprocessing scripts, and plotting scripts are available, and the javascript
8 -function for each should be defined as follows:
 4+Preprocessing scripts, plotting scripts, and helpers are available, and the javascript function for each should be defined as follows:
95
106 /* For preprocessing scripts */
11 -function plotter_<functionname>_process( data, labels, arg1, arg2, ... ) {
12 - ...
 7+function plotter_<functionname>_process( name, data, labels, arguments ) {
 8+ /* example preprocessor: */
 9+ for ( var i = 0; i < data.length; ++i ) {
 10+ for ( var j = 1; j < data[i].length; ++j ) {
 11+ data[i][j] = data[i][j] * 2;
 12+ }
 13+ }
1314 }
1415
1516 /* For plotting scripts */
16 -function plotter_<functionname>_draw( data, labels, arg1, arg2, ... ) {
17 - ...
 17+function plotter_<functionname>_draw( name, data, labels, arguments ) {
 18+ /* example plotkit script: */
 19+ var plotkitlabels = [];
 20+ for ( var i = 0; i < labels.length; ++i ) {
 21+ plotkitlabels[i] = { v:i, label:labels[i] };
 22+ }
 23+ var options = { "xTicks": plotkitlabels };
 24+ var layout = new PlotKit.Layout("bar", options);
 25+ layout.addDataset("data", data);
 26+ layout.evaluate();
 27+ var canvas = MochiKit.DOM.getElement(name);
 28+ var plotter = new PlotKit.SweetCanvasRenderer(canvas, layout, options);
 29+ plotter.render();
1830 }
1931
 32+/* For helpers - this is javascript that is loaded, but not called
 33+ by the plugin; they are for use in functions called by the plugin */
 34+function plotter_<functionname>( <anything_you_want> ) {
 35+ /* ... */
 36+}
 37+
2038 You can use these in a parser function like so:
2139
2240 {{#plot:
23 -|script=<functionname>
 41+|script=<scriptname>
2442 |scriptarguments=arg1,arg2,...
 43+|preprocessors=<preprocessorname1>,<preprocessorname2>,...
 44+|preprocessorarguments=<preprocessor1argument1>,<preprocessor1argument2>,...:preprocessor2argument1>,<preprocessor2argument2>,...:...,...
 45+|helpers=<helpername1>,<helpername2>,...
2546 |labels=label1,label2
2647 |data=1,2
2748 2,4}}
2849
29 -Currently, only PlotKit, and generic canvas drawing is supported; but other javascript plotting libraries
30 -will be supported in the future.
 50+To specify multiple plots in a single article, you need to use the name argument; the default is name=plot; here is an example of multiple plots:
3151
32 -This code is very alpha quality right now; many features may not work.
 52+{{#plot:
 53+|name=plot1
 54+|script=<scriptname>
 55+|data=1,2}}
 56+
 57+{{#plot:
 58+|name=plot2
 59+|script=<scriptname>
 60+|data=2,3}}
 61+
 62+If javascript is disabled, the extension will instead show a table. To specify the CSS class of the fallback table, use the tableclass argument; the default is tableclass=wikitable
 63+
 64+{{#plot:
 65+|script=<scriptname>
 66+|tableclass=sortable
 67+|data=0,1}}
 68+
 69+The height and width of the canvas can be changed with the width and height arguments; the defaults are width=300 height=300
 70+
 71+To change the renderer, use the renderer argument. Currently, only PlotKit (renderer=plotkit), and generic canvas drawing (renderer=generic) is supported; but other javascript plotting libraries will be supported in the future; the default is renderer=plotkit.
 72+
 73+The data separator can be changed using the datasep argument; the default is datasep=,
 74+
 75+script, preprocessors, helpers, name, and tableclass arguments are alphanumeric only, and are limited to 255 characters per script/preprocessor/etc.
 76+
 77+This code is alpha quality right now; many features may not work.

Status & tagging log