r50615 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r50614‎ | r50615 | r50616 >
Date:03:02, 15 May 2009
Author:laner
Status:deferred
Tags:
Comment:
* Add support for user defined data separator
* Add javascript functions for un-html encoding user defined data after it has been loaded on the page
Modified paths:
  • /trunk/extensions/Plotters/Plotters.php (modified) (history)
  • /trunk/extensions/Plotters/PlottersClass.php (modified) (history)
  • /trunk/extensions/Plotters/PlottersParser.php (modified) (history)
  • /trunk/extensions/Plotters/libs (added) (history)
  • /trunk/extensions/Plotters/libs/fixencoding.js (added) (history)

Diff [purge]

Index: trunk/extensions/Plotters/PlottersParser.php
@@ -57,11 +57,10 @@
5858 foreach ( $arguments as $argument ) {
5959 $subargumentarr = explode( ',', $argument );
6060 foreach ( $subargumentarr as &$singleargument ) {
61 - $singleargument = htmlentities( $singleargument, ENT_QUOTES );
62 -
6361 // Fix escaped separators
6462 $singleargument = preg_replace( "/§UNIQ§/", ":", $singleargument );
6563 $singleargument = preg_replace( "/§UNIQ2§/", ",", $singleargument );
 64+ $singleargument = htmlentities( $singleargument, ENT_QUOTES );
6665 }
6766 $this->argumentArray["preprocessorarguments"][] = $subargumentarr;
6867 }
@@ -78,16 +77,15 @@
7978 // Parse and sanitize arguments
8079 $arguments = explode( ',', $argv["scriptarguments"] );
8180 foreach ( $arguments as $argument ) {
82 - $argument = htmlentities( $argument, ENT_QUOTES );
83 -
8481 // Fix escaped separators
8582 $argument = preg_replace( "/§UNIQ§/", ",", $argument );
 83+ $argument = htmlentities( $argument, ENT_QUOTES );
8684 $this->argumentArray["scriptarguments"][] = $argument;
8785 }
8886 Plotter::debug( 'plot script argument values: ', $this->argumentArray["scriptarguments"] );
8987 }
9088 if ( isset( $argv["datasep"] ) ) {
91 - $this->argumentArray["datasep"] = htmlentities( $argv["datasep"], ENT_QUOTES );
 89+ $this->argumentArray["datasep"] = $argv["datasep"];
9290 }
9391 if ( isset( $argv["width"] ) ) {
9492 $this->argumentArray["width"] = preg_replace( '/[^0-9]/', '', $argv["width"] );
@@ -102,10 +100,9 @@
103101 // Parse and sanitize arguments
104102 $labels = explode( ',', $argv["labels"] );
105103 foreach ( $labels as $label ) {
106 - $label = htmlentities( $label, ENT_QUOTES );
107 -
108104 // Fix escaped separators
109105 $label = preg_replace( "/§UNIQ§/", ",", $label );
 106+ $label = htmlentities( $label, ENT_QUOTES );
110107 $this->argumentArray["labels"][] = $label;
111108 }
112109 }
@@ -119,12 +116,11 @@
120117 // Parse and sanitize data
121118 $lines = preg_split( "/\n/", $input, -1, PREG_SPLIT_NO_EMPTY );
122119 foreach ( $lines as $line ) {
123 - $values = explode( ',', $line );
 120+ $values = explode( $sep, $line );
124121 foreach ( $values as &$value ) {
125 - $value = htmlentities( $value, ENT_QUOTES );
126 -
127122 // Fix escaped separators
128 - $value = preg_replace( "/§UNIQ§/", "\\$sep", $value );
 123+ $value = preg_replace( "/§UNIQ§/", "$sep", $value );
 124+ $value = htmlentities( $value, ENT_QUOTES );
129125 }
130126 $this->dataArray[] = $values;
131127 Plotter::debug( 'plot data values: ', $values );
Index: trunk/extensions/Plotters/Plotters.php
@@ -50,7 +50,8 @@
5151 $wgSpecialPageGroups['Plotters'] = 'wiki';
5252
5353 // sane defaults. always initialize to avoid register_globals vulnerabilities
54 -$wgPlotterExtensionPath = $wgScriptPath . '/extensions/Plotter';
 54+$wgPlotterExtensionPath = $wgScriptPath . '/extensions/Plotters';
 55+$wgPlotterJavascriptPath = $wgScriptPath . '/extensions/Plotters';
5556
5657 function wfPlottersArticleSaveComplete( &$article, &$wgUser, &$text ) {
5758 // update cache if MediaWiki:Plotters-definition was edited
Index: trunk/extensions/Plotters/PlottersClass.php
@@ -95,10 +95,11 @@
9696 for ( $i = 0; $i < count( $this->argumentArray["labels"] ); $i++ ) {
9797 $output .= "labels[$i] = '" . $this->argumentArray["labels"][$i] . "';";
9898 }
 99+ $output .= "fix_encoding( labels );";
99100
100101 // Run preprocessors
101102 foreach ( $this->argumentArray["preprocessors"] as $preprocessor ) {
102 - $output .= 'data = plotter_' . $preprocessor . '_process( data, labels, ';
 103+ $output .= 'plotter_' . $preprocessor . '_process( data, labels, ';
103104 foreach ( $this->argumentArray["preprocessorarguments"] as $argument ) {
104105 $output .= $argument . ', ';
105106 }
@@ -126,9 +127,10 @@
127128 }
128129
129130 static function setPlotterHeaders( &$outputPage ) {
 131+ global $wgPlotterJavascriptPath;
130132 global $wgPlotterExtensionPath;
131133
132 - $extensionpath = $wgPlotterExtensionPath;
 134+ $extensionpath = $wgPlotterJavascriptPath;
133135
134136 // Add mochikit (required by PlotKit)
135137 $outputPage->addScript( '<script src="' . $extensionpath . '/mochikit/MochiKit.js" type="text/javascript"></script>' );
@@ -139,6 +141,9 @@
140142 $outputPage->addScript( '<script src="' . $extensionpath . '/plotkit/Canvas.js" type="text/javascript"></script>' );
141143 $outputPage->addScript( '<script src="' . $extensionpath . '/plotkit/SweetCanvas.js" type="text/javascript"></script>' );
142144
 145+ // Add javascript to fix encoding
 146+ $outputPage->addScript( '<script src="' . $wgPlotterExtensionPath . '/libs/fixencoding.js" type="text/javascript"></script>' );
 147+
143148 return true;
144149 }
145150
Index: trunk/extensions/Plotters/libs/fixencoding.js
@@ -0,0 +1,232 @@
 2+/*
 3+ * More info at: http://kevin.vanzonneveld.net/techblog/category/php2js
 4+ *
 5+ * php.js is copyright 2008 Kevin van Zonneveld.
 6+ *
 7+ * Portions copyright Ates Goral (http://magnetiq.com), Legaev Andrey,
 8+ * _argos, Jonas Raoni Soares Silva (http://www.jsfromhell.com),
 9+ * Webtoolkit.info (http://www.webtoolkit.info/), Carlos R. L. Rodrigues, Ash
 10+ * Searle (http://hexmen.com/blog/), Tyler Akins (http://rumkin.com), mdsjack
 11+ * (http://www.mdsjack.bo.it), Alexander Ermolaev
 12+ * (http://snippets.dzone.com/user/AlexanderErmolaev), Andrea Giammarchi
 13+ * (http://webreflection.blogspot.com), Bayron Guevara, Cord, David, Karol
 14+ * Kowalski, Leslie Hoare, Lincoln Ramsay, Mick@el, Nick Callen, Peter-Paul
 15+ * Koch (http://www.quirksmode.org/js/beat.html), Philippe Baumann, Steve
 16+ * Clay, booeyOH
 17+ *
 18+ * Licensed under the MIT (MIT-LICENSE.txt) license.
 19+ *
 20+ * Permission is hereby granted, free of charge, to any person obtaining a
 21+ * copy of this software and associated documentation files (the
 22+ * "Software"), to deal in the Software without restriction, including
 23+ * without limitation the rights to use, copy, modify, merge, publish,
 24+ * distribute, sublicense, and/or sell copies of the Software, and to
 25+ * permit persons to whom the Software is furnished to do so, subject to
 26+ * the following conditions:
 27+ *
 28+ * The above copyright notice and this permission notice shall be included
 29+ * in all copies or substantial portions of the Software.
 30+ *
 31+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 32+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 33+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 34+ * IN NO EVENT SHALL KEVIN VAN ZONNEVELD BE LIABLE FOR ANY CLAIM, DAMAGES
 35+ * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 36+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 37+ * OTHER DEALINGS IN THE SOFTWARE.
 38+ */
 39+
 40+function get_html_translation_table(table, quote_style) {
 41+ // http://kevin.vanzonneveld.net
 42+ // + original by: Philip Peterson
 43+ // + revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
 44+ // + bugfixed by: noname
 45+ // + bugfixed by: Alex
 46+ // + bugfixed by: Marco
 47+ // + bugfixed by: madipta
 48+ // + improved by: KELAN
 49+ // + improved by: Brett Zamir (http://brettz9.blogspot.com)
 50+ // % note: It has been decided that we're not going to add global
 51+ // % note: dependencies to php.js. Meaning the constants are not
 52+ // % note: real constants, but strings instead. integers are also supported if someone
 53+ // % note: chooses to create the constants themselves.
 54+ // * example 1: get_html_translation_table('HTML_SPECIALCHARS');
 55+ // * returns 1: {'"': '&quot;', '&': '&amp;', '<': '&lt;', '>': '&gt;'}
 56+
 57+ var entities = {}, histogram = {}, decimal = 0, symbol = '';
 58+ var constMappingTable = {}, constMappingQuoteStyle = {};
 59+ var useTable = {}, useQuoteStyle = {};
 60+
 61+ // Translate arguments
 62+ constMappingTable[0] = 'HTML_SPECIALCHARS';
 63+ constMappingTable[1] = 'HTML_ENTITIES';
 64+ constMappingQuoteStyle[0] = 'ENT_NOQUOTES';
 65+ constMappingQuoteStyle[2] = 'ENT_COMPAT';
 66+ constMappingQuoteStyle[3] = 'ENT_QUOTES';
 67+
 68+ useTable = !isNaN(table) ? constMappingTable[table] : table ? table.toUpperCase() : 'HTML_SPECIALCHARS';
 69+ useQuoteStyle = !isNaN(quote_style) ? constMappingQuoteStyle[quote_style] : quote_style ? quote_style.toUpperCase() : 'ENT_COMPAT';
 70+
 71+ if (useTable !== 'HTML_SPECIALCHARS' && useTable !== 'HTML_ENTITIES') {
 72+ throw Error("Table: "+useTable+' not supported');
 73+ // return false;
 74+ }
 75+
 76+ // ascii decimals for better compatibility
 77+ entities['38'] = '&amp;';
 78+ if (useQuoteStyle !== 'ENT_NOQUOTES') {
 79+ entities['34'] = '&quot;';
 80+ }
 81+ if (useQuoteStyle === 'ENT_QUOTES') {
 82+ entities['39'] = '&#039;';
 83+ }
 84+ entities['60'] = '&lt;';
 85+ entities['62'] = '&gt;';
 86+
 87+ if (useTable === 'HTML_ENTITIES') {
 88+ entities['160'] = '&nbsp;';
 89+ entities['161'] = '&iexcl;';
 90+ entities['162'] = '&cent;';
 91+ entities['163'] = '&pound;';
 92+ entities['164'] = '&curren;';
 93+ entities['165'] = '&yen;';
 94+ entities['166'] = '&brvbar;';
 95+ entities['167'] = '&sect;';
 96+ entities['168'] = '&uml;';
 97+ entities['169'] = '&copy;';
 98+ entities['170'] = '&ordf;';
 99+ entities['171'] = '&laquo;';
 100+ entities['172'] = '&not;';
 101+ entities['173'] = '&shy;';
 102+ entities['174'] = '&reg;';
 103+ entities['175'] = '&macr;';
 104+ entities['176'] = '&deg;';
 105+ entities['177'] = '&plusmn;';
 106+ entities['178'] = '&sup2;';
 107+ entities['179'] = '&sup3;';
 108+ entities['180'] = '&acute;';
 109+ entities['181'] = '&micro;';
 110+ entities['182'] = '&para;';
 111+ entities['183'] = '&middot;';
 112+ entities['184'] = '&cedil;';
 113+ entities['185'] = '&sup1;';
 114+ entities['186'] = '&ordm;';
 115+ entities['187'] = '&raquo;';
 116+ entities['188'] = '&frac14;';
 117+ entities['189'] = '&frac12;';
 118+ entities['190'] = '&frac34;';
 119+ entities['191'] = '&iquest;';
 120+ entities['192'] = '&Agrave;';
 121+ entities['193'] = '&Aacute;';
 122+ entities['194'] = '&Acirc;';
 123+ entities['195'] = '&Atilde;';
 124+ entities['196'] = '&Auml;';
 125+ entities['197'] = '&Aring;';
 126+ entities['198'] = '&AElig;';
 127+ entities['199'] = '&Ccedil;';
 128+ entities['200'] = '&Egrave;';
 129+ entities['201'] = '&Eacute;';
 130+ entities['202'] = '&Ecirc;';
 131+ entities['203'] = '&Euml;';
 132+ entities['204'] = '&Igrave;';
 133+ entities['205'] = '&Iacute;';
 134+ entities['206'] = '&Icirc;';
 135+ entities['207'] = '&Iuml;';
 136+ entities['208'] = '&ETH;';
 137+ entities['209'] = '&Ntilde;';
 138+ entities['210'] = '&Ograve;';
 139+ entities['211'] = '&Oacute;';
 140+ entities['212'] = '&Ocirc;';
 141+ entities['213'] = '&Otilde;';
 142+ entities['214'] = '&Ouml;';
 143+ entities['215'] = '&times;';
 144+ entities['216'] = '&Oslash;';
 145+ entities['217'] = '&Ugrave;';
 146+ entities['218'] = '&Uacute;';
 147+ entities['219'] = '&Ucirc;';
 148+ entities['220'] = '&Uuml;';
 149+ entities['221'] = '&Yacute;';
 150+ entities['222'] = '&THORN;';
 151+ entities['223'] = '&szlig;';
 152+ entities['224'] = '&agrave;';
 153+ entities['225'] = '&aacute;';
 154+ entities['226'] = '&acirc;';
 155+ entities['227'] = '&atilde;';
 156+ entities['228'] = '&auml;';
 157+ entities['229'] = '&aring;';
 158+ entities['230'] = '&aelig;';
 159+ entities['231'] = '&ccedil;';
 160+ entities['232'] = '&egrave;';
 161+ entities['233'] = '&eacute;';
 162+ entities['234'] = '&ecirc;';
 163+ entities['235'] = '&euml;';
 164+ entities['236'] = '&igrave;';
 165+ entities['237'] = '&iacute;';
 166+ entities['238'] = '&icirc;';
 167+ entities['239'] = '&iuml;';
 168+ entities['240'] = '&eth;';
 169+ entities['241'] = '&ntilde;';
 170+ entities['242'] = '&ograve;';
 171+ entities['243'] = '&oacute;';
 172+ entities['244'] = '&ocirc;';
 173+ entities['245'] = '&otilde;';
 174+ entities['246'] = '&ouml;';
 175+ entities['247'] = '&divide;';
 176+ entities['248'] = '&oslash;';
 177+ entities['249'] = '&ugrave;';
 178+ entities['250'] = '&uacute;';
 179+ entities['251'] = '&ucirc;';
 180+ entities['252'] = '&uuml;';
 181+ entities['253'] = '&yacute;';
 182+ entities['254'] = '&thorn;';
 183+ entities['255'] = '&yuml;';
 184+ }
 185+
 186+ // ascii decimals to real symbols
 187+ for (decimal in entities) {
 188+ symbol = String.fromCharCode(decimal);
 189+ histogram[symbol] = entities[decimal];
 190+ }
 191+
 192+ return histogram;
 193+}
 194+
 195+function html_entity_decode( string, quote_style ) {
 196+ // http://kevin.vanzonneveld.net
 197+ // + original by: john (http://www.jd-tech.net)
 198+ // + input by: ger
 199+ // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
 200+ // + revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
 201+ // + bugfixed by: Onno Marsman
 202+ // + improved by: marc andreu
 203+ // + revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
 204+ // - depends on: get_html_translation_table
 205+ // * example 1: html_entity_decode('Kevin &amp; van Zonneveld');
 206+ // * returns 1: 'Kevin & van Zonneveld'
 207+ // * example 2: html_entity_decode('&amp;lt;');
 208+ // * returns 2: '&lt;'
 209+
 210+ var histogram = {}, symbol = '', tmp_str = '', entity = '';
 211+ tmp_str = string.toString();
 212+
 213+ if (false === (histogram = get_html_translation_table('HTML_ENTITIES', quote_style))) {
 214+ return false;
 215+ }
 216+
 217+ // &amp; must be the last character when decoding!
 218+ delete(histogram['&']);
 219+ histogram['&'] = '&amp;';
 220+
 221+ for (symbol in histogram) {
 222+ entity = histogram[symbol];
 223+ tmp_str = tmp_str.split(entity).join(symbol);
 224+ }
 225+
 226+ return tmp_str;
 227+}
 228+
 229+function fix_encoding( data_arr ) {
 230+ for ( var i = 0; i < data_arr.length; ++i ) {
 231+ data_arr[i] = html_entity_decode( data_arr[i], 'ENT_QUOTES' );
 232+ }
 233+}
Property changes on: trunk/extensions/Plotters/libs/fixencoding.js
___________________________________________________________________
Name: svn:eol-style
1234 + native

Status & tagging log