PageRenderTime 79ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/WikiPlot.php

http://wikiplot.googlecode.com/
PHP | 263 lines | 110 code | 29 blank | 124 comment | 13 complexity | fee03991cd020c4fc5bd450823749e4e MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /*
  3. Copyright (C) 2006 by the WikiPlot project authors (See http://code.google.com/p/WikiPlot).
  4. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
  5. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  6. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  7. */
  8. /**
  9. * The MediaWiki extension
  10. *
  11. * This is the MediaWiki extension it self, everything else is just functions and liberaries for this file.
  12. *
  13. * @package WikiPlot
  14. * @license http://www.gnu.org/licenses/gpl.txt GNU General Public License
  15. * @author WikiPlot development team.
  16. * @copyright Copyright 2006, WikiPlot development team.
  17. */
  18. /**
  19. *Include plot.class.php
  20. *
  21. *Requires PlotClass to render plots.
  22. */
  23. require_once("PlotClass/plot.class.php");
  24. /**
  25. *Include xml.class.php
  26. *
  27. *Requires XMLParser to parse xml to plot.
  28. */
  29. require_once("xml.class.php");
  30. /**
  31. *Include cache.class.php
  32. *
  33. *Requires Cache to control the cache.
  34. */
  35. require_once("cache.class.php");
  36. /**
  37. *Register the WikiPlot extension
  38. *
  39. *Makes sure the extension is called when MediaWiki is started.
  40. */
  41. $wgExtensionFunctions[] = "wfWikiPlotExtension";
  42. /**
  43. *Add hooks
  44. *
  45. *Adds hooks so MediaWiki will perform callback, when it hits the wikiplot tag.
  46. */
  47. function wfWikiPlotExtension() {
  48. global $wgParser;
  49. $wgParser->setHook( "wikiplot", "RenderWikiPlot" );
  50. }
  51. /**
  52. *Deserialize boolean
  53. *
  54. *Deserializes a boolean value from string, this function is used when you want to deserialize parameters given in the WikiML.
  55. *If it is impossible to deserialize the value, the output object is not initialized at all.
  56. *
  57. *@access private
  58. *@param string $value The string you wish to deserialize.
  59. *@param boolean &$SetTo The variable you want the values parsed to.
  60. */
  61. function WikiPlotDeserializeBoolean($value,&$SetTo)
  62. {
  63. if($value == "true")
  64. {
  65. $SetTo = true;
  66. }
  67. elseif($value == "false")
  68. {
  69. $SetTo = false;
  70. }
  71. }
  72. /**
  73. *Deserialize String
  74. *
  75. *Deserializes a string value from string, this function is used when you want to deserialize parameters given in the WikiML.
  76. *If it is impossible to deserialize the value, the output object is not initialized at all. Usualy this function does nothing.
  77. *
  78. *@access private
  79. *@param string $value The string you wish to deserialize.
  80. *@param string &$SetTo The variable you want the values parsed to.
  81. */
  82. function WikiPlotDeserializeString($value,&$SetTo)
  83. {
  84. if(is_string($value))
  85. {
  86. $SetTo = $value;
  87. }
  88. }
  89. /**
  90. *Deserialize Coordiante
  91. *
  92. *Deserializes a 2 integers from string, this function is used when you want to deserialize parameters given in the WikiML.
  93. *If it is impossible to deserialize the value, the output object is not initialized at all.
  94. *
  95. *@access private
  96. *@param string $value The string you wish to deserialize.
  97. *@param integer &$SetTo1 The variable you want the values parsed to.
  98. *@param integer &$SetTo2 The variable you want the values parsed to.
  99. */
  100. function WikiPlotDeserializeMixed($value,&$SetTo1,&$SetTo2)
  101. {
  102. if(!is_null($value))
  103. {
  104. $values = explode(";",$value,2);
  105. if(is_numeric($values[0])&&is_numeric($values[1]))
  106. {
  107. $SetTo1 = $values[0];
  108. $SetTo2 = $values[1];
  109. }
  110. }
  111. }
  112. /**
  113. *Deserialize Integer
  114. *
  115. *Deserializes a integer value from string, this function is used when you want to deserialize parameters given in the WikiML.
  116. *If it is impossible to deserialize the value, the output object is not initialized at all. Usualy this function does nothing at all, just checks to see if the value can be parsed as an integer.
  117. *
  118. *@access private
  119. *@param string $value The string you wish to deserialize.
  120. *@param Integer &$SetTo The variable you want the values parsed to.
  121. */
  122. function WikiPlotDeserializeInteger($value,&$SetTo)
  123. {
  124. if(!is_null($value))
  125. {
  126. if(is_numeric($value))
  127. {
  128. $SetTo = $value;
  129. }
  130. }
  131. }
  132. /**
  133. *Deserialize Color
  134. *
  135. *Deserializes an array representation of a rgb color from string, this function is used when you want to deserialize parameters given in the WikiML.
  136. *This function can deserialize colors written as "255,255,255" (rgb) or "#000000" (hex).
  137. *If it is impossible to deserialize the value, the output object is not initialized at all.
  138. *
  139. *@access private
  140. *@param string $value The string you wish to deserialize.
  141. *@param array &$SetTo The variable you want the values parsed to.
  142. */
  143. function WikiPlotDeserializeColor($value,&$SetTo)
  144. {
  145. if(!is_null($value))
  146. {
  147. $values = explode(",",$value,3);
  148. if(is_numeric($values[0])&&is_numeric($values[1])&&is_numeric($values[2]))
  149. {
  150. $SetTo = array($values[0],$values[1],$values[2]);
  151. }
  152. elseif(strstr($value,"#"))
  153. {
  154. $red = hexdec(substr($val, 1 , 2));
  155. $green = hexdec(substr($val, 3 , 2));
  156. $blue = hexdec(substr($val, 5 , 2));
  157. $SetTo = array($red,$green,$blue);
  158. }
  159. }
  160. }
  161. /**
  162. *RenderWikiPlot CallBack function
  163. *
  164. *This is the function that handles MediaWiki callbacks, and renders the actual plot.
  165. *
  166. *@access private
  167. *@param string $input The content of the wikiplot tag
  168. *@param array $argv Hash-array of the parameters of the wikiplot tag, with parameter-name as key and parameter-value as value.
  169. *@param Parser $parser The parser of MediaWiki, if null parser is obtained from global variable
  170. *@uses WikiPlotDeserializeBoolean()
  171. *@uses WikiPlotDeserializeString()
  172. *@uses WikiPlotDeserializeMixed()
  173. *@uses WikiPlotDeserializeInteger()
  174. *@uses WikiPlotDeserializeColor()
  175. *@uses XMLParser
  176. *@uses Plot
  177. *@uses Graph
  178. *@uses Cache
  179. *@return string HTML that can be directly inserted into any website.
  180. */
  181. function RenderWikiPlot($input, $argv, $parser = null)
  182. {
  183. //Get parser if not given as parameter
  184. if (!$parser) $parser =& $GLOBALS['wgParser'];
  185. /*Currently the parser*/
  186. //Creating instance of plot
  187. $Plot = new Plot();
  188. //Getting and deserializing parameters
  189. WikiPlotDeserializeBoolean($argv["grid"],$Plot->EnableGrid);
  190. WikiPlotDeserializeBoolean($argv["axis"],$Plot->EnableAxis);
  191. WikiPlotDeserializeString($argv["caption"],$Plot->Caption);
  192. WikiPlotDeserializeMixed($argv["xspan"],$Plot->MinX,$Plot->MaxX);
  193. WikiPlotDeserializeMixed($argv["yspan"],$Plot->MinY,$Plot->MaxY);
  194. WikiPlotDeserializeMixed($argv["gridspace"],$Plot->XGridSpace,$Plot->YGridSpace);
  195. WikiPlotDeserializeInteger($argv["height"],$Plot->Height);
  196. WikiPlotDeserializeInteger($argv["width"],$Plot->Width);
  197. WikiPlotDeserializeInteger($argv["captionfont"],$Plot->CaptionFont);
  198. WikiPlotDeserializeInteger($argv["gridfont"],$Plot->GridFont);
  199. WikiPlotDeserializeColor($argv["gridcolor"],$Plot->GridColor);
  200. //Parsing Xml
  201. $XmlParser = new XMLParser($input);
  202. $Graphs = $XmlParser->CreateInputArray();
  203. foreach($Graphs as $Graph)
  204. {
  205. $G = new Graph;
  206. if(!is_array($Graph[1]))
  207. {
  208. $G->Exp = $Graph[1];
  209. WikiPlotDeserializeString($Graph[0]["label"],$G->Label);
  210. WikiPlotDeserializeColor($Graph[0]["color"],$G->Color);
  211. }else{
  212. $G->Exp = $Graph[0];
  213. }
  214. array_push($Plot->Graphs,$G);
  215. }
  216. //Render the plot
  217. //Get instance of cache
  218. $cache = new cache();
  219. //Url of the current plot
  220. $PlotURL = "";
  221. $PlotFileName = $Plot->GetHash() . ".png";
  222. if(!$cache->FileExist($PlotFileName))
  223. {
  224. $Plot->SaveAs($cache->CachePath($PlotFileName));
  225. }else{
  226. $PlotURL = $cache->FileURL($PlotFileName);
  227. }
  228. $output = "<a href='$PlotURL' class='image' title='See the plot'><img src='$PlotURL'></a>";
  229. return $output;
  230. }
  231. ?>