PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/filter/algebra/algebradebug.php

https://bitbucket.org/ngmares/moodle
PHP | 353 lines | 331 code | 19 blank | 3 comment | 60 complexity | 8bd70e606ec9edb6dedd92c25edb711b MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0, MPL-2.0-no-copyleft-exception, GPL-3.0, Apache-2.0, BSD-3-Clause
  1. <?php
  2. // This function fetches math. images from the data directory
  3. // If not, it obtains the corresponding TeX expression from the cache_tex db table
  4. // and uses mimeTeX to create the image file
  5. define('NO_MOODLE_COOKIES', true); // Because it interferes with caching
  6. require_once("../../config.php");
  7. if (!filter_is_enabled('filter/algebra')) {
  8. print_error('filternotenabled');
  9. }
  10. require_once($CFG->libdir.'/filelib.php');
  11. require_once($CFG->dirroot.'/filter/tex/lib.php');
  12. require_login();
  13. require_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM));
  14. $query = urldecode($_SERVER['QUERY_STRING']);
  15. if ($query) {
  16. $output = $query;
  17. $splitpos = strpos($query,'&')-8;
  18. $algebra = substr($query,8,$splitpos);
  19. $md5 = md5($algebra);
  20. if (strpos($query,'ShowDB') || strpos($query,'DeleteDB')) {
  21. $texcache = $DB->get_record("cache_filters", array("filter"=>"algebra", "md5key"=>$md5));
  22. }
  23. if (strpos($query,'ShowDB')) {
  24. if ($texcache) {
  25. $output = "DB cache_filters entry for $algebra\n";
  26. $output .= "id = $texcache->id\n";
  27. $output .= "filter = $texcache->filter\n";
  28. $output .= "version = $texcache->version\n";
  29. $output .= "md5key = $texcache->md5key\n";
  30. $output .= "rawtext = $texcache->rawtext\n";
  31. $output .= "timemodified = $texcache->timemodified\n";
  32. } else {
  33. $output = "DB cache_filters entry for $algebra not found\n";
  34. }
  35. }
  36. if (strpos($query,'DeleteDB')) {
  37. if ($texcache) {
  38. $output = "Deleting DB cache_filters entry for $algebra\n";
  39. $result = $DB->delete_records("cache_filters", array("id"=>$texcache->id));
  40. if ($result) {
  41. $result = 1;
  42. } else {
  43. $result = 0;
  44. }
  45. $output .= "Number of records deleted = $result\n";
  46. } else {
  47. $output = "Could not delete DB cache_filters entry for $algebra\nbecause it could not be found.\n";
  48. }
  49. }
  50. if (strpos($query,'TeXStage1')) {
  51. $output = algebra2tex($algebra);
  52. }
  53. if (strpos($query,'TeXStage2')) {
  54. $output = algebra2tex($algebra);
  55. $output = refineTeX($output);
  56. }
  57. if (strpos($query,'ShowImage')||strpos($query,'SlashArguments')) {
  58. $output = algebra2tex($algebra);
  59. $output = refineTeX($output);
  60. if (strpos($query,'ShowImage')) {
  61. tex2image($output, $md5);
  62. } else {
  63. slasharguments($output, $md5);
  64. }
  65. } else {
  66. outputText($output);
  67. }
  68. exit;
  69. }
  70. function algebra2tex($algebra) {
  71. global $CFG;
  72. $algebra = str_replace('&lt;','<',$algebra);
  73. $algebra = str_replace('&gt;','>',$algebra);
  74. $algebra = str_replace('<>','#',$algebra);
  75. $algebra = str_replace('<=','%',$algebra);
  76. $algebra = str_replace('>=','!',$algebra);
  77. $algebra = preg_replace('/([=><%!#] *)-/',"\$1 zeroplace -",$algebra);
  78. $algebra = str_replace('delta','zdelta',$algebra);
  79. $algebra = str_replace('beta','bita',$algebra);
  80. $algebra = str_replace('theta','thita',$algebra);
  81. $algebra = str_replace('zeta','zita',$algebra);
  82. $algebra = str_replace('eta','xeta',$algebra);
  83. $algebra = str_replace('epsilon','zepslon',$algebra);
  84. $algebra = str_replace('upsilon','zupslon',$algebra);
  85. $algebra = preg_replace('!\r\n?!',' ',$algebra);
  86. $algebra = escapeshellarg($algebra);
  87. if ( (PHP_OS == "WINNT") || (PHP_OS == "WIN32") || (PHP_OS == "Windows") ) {
  88. $cmd = "cd $CFG->dirroot\\filter\\algebra & algebra2tex.pl x/2";
  89. $test = `$cmd`;
  90. if ($test != '\frac{x}{2}') {
  91. echo "There is a problem with either Perl or the script algebra2tex.pl<br/>";
  92. $ecmd = $cmd . " 2>&1";
  93. echo `$ecmd` . "<br/>\n";
  94. echo "The shell command<br/>$cmd<br/>returned status = $status<br/>\n";
  95. $commandpath = "$CFG->dirroot\\filter\\algebra\\algebra2tex.pl";
  96. if (file_exists($commandpath)) {
  97. echo "The file permissions of algebra2tex.pl are: " . decoct(fileperms($commandpath)) . "<br/>";
  98. }
  99. die;
  100. }
  101. $cmd = "cd $CFG->dirroot\\filter\\algebra & algebra2tex.pl $algebra";
  102. } else {
  103. $cmd = "cd $CFG->dirroot/filter/algebra; ./algebra2tex.pl x/2";
  104. $test = `$cmd`;
  105. if ($test != '\frac{x}{2}') {
  106. echo "There is a problem with either Perl or the script algebra2tex.pl<br/>";
  107. $ecmd = $cmd . " 2>&1";
  108. echo `$ecmd` . "<br/>\n";
  109. echo "The shell command<br/>$cmd<br/>returned status = $status<br/>\n";
  110. $commandpath = "$CFG->dirroot/filter/algebra/algebra2tex.pl";
  111. if (file_exists($commandpath)) {
  112. echo "The file permissions of algebra2tex.pl are: " . decoct(fileperms($commandpath)) . "<br/>";
  113. }
  114. die;
  115. }
  116. $cmd = "cd $CFG->dirroot/filter/algebra; ./algebra2tex.pl $algebra";
  117. }
  118. $texexp = `$cmd`;
  119. return $texexp;
  120. }
  121. function refineTeX($texexp) {
  122. $texexp = str_replace('zeroplace','',$texexp);
  123. $texexp = str_replace('#','\not= ',$texexp);
  124. $texexp = str_replace('%','\leq ',$texexp);
  125. $texexp = str_replace('!','\geq ',$texexp);
  126. $texexp = str_replace('\left{','{',$texexp);
  127. $texexp = str_replace('\right}','}',$texexp);
  128. $texexp = str_replace('\fun',' ',$texexp);
  129. $texexp = str_replace('infty','\infty',$texexp);
  130. $texexp = str_replace('alpha','\alpha',$texexp);
  131. $texexp = str_replace('gamma','\gamma',$texexp);
  132. $texexp = str_replace('iota','\iota',$texexp);
  133. $texexp = str_replace('kappa','\kappa',$texexp);
  134. $texexp = str_replace('lambda','\lambda',$texexp);
  135. $texexp = str_replace('mu','\mu',$texexp);
  136. $texexp = str_replace('nu','\nu',$texexp);
  137. $texexp = str_replace('xi','\xi',$texexp);
  138. $texexp = str_replace('rho','\rho',$texexp);
  139. $texexp = str_replace('sigma','\sigma',$texexp);
  140. $texexp = str_replace('tau','\tau',$texexp);
  141. $texexp = str_replace('phi','\phi',$texexp);
  142. $texexp = str_replace('chi','\chi',$texexp);
  143. $texexp = str_replace('psi','\psi',$texexp);
  144. $texexp = str_replace('omega','\omega',$texexp);
  145. $texexp = str_replace('zdelta','\delta',$texexp);
  146. $texexp = str_replace('bita','\beta',$texexp);
  147. $texexp = str_replace('thita','\theta',$texexp);
  148. $texexp = str_replace('zita','\zeta',$texexp);
  149. $texexp = str_replace('xeta','\eta',$texexp);
  150. $texexp = str_replace('zepslon','\epsilon',$texexp);
  151. $texexp = str_replace('zupslon','\upsilon',$texexp);
  152. $texexp = str_replace('\mbox{logten}','\mbox{log}_{10}',$texexp);
  153. $texexp = str_replace('\mbox{acos}','\mbox{cos}^{-1}',$texexp);
  154. $texexp = str_replace('\mbox{asin}','\mbox{sin}^{-1}',$texexp);
  155. $texexp = str_replace('\mbox{atan}','\mbox{tan}^{-1}',$texexp);
  156. $texexp = str_replace('\mbox{asec}','\mbox{sec}^{-1}',$texexp);
  157. $texexp = str_replace('\mbox{acsc}','\mbox{csc}^{-1}',$texexp);
  158. $texexp = str_replace('\mbox{acot}','\mbox{cot}^{-1}',$texexp);
  159. $texexp = str_replace('\mbox{acosh}','\mbox{cosh}^{-1}',$texexp);
  160. $texexp = str_replace('\mbox{asinh}','\mbox{sinh}^{-1}',$texexp);
  161. $texexp = str_replace('\mbox{atanh}','\mbox{tanh}^{-1}',$texexp);
  162. $texexp = str_replace('\mbox{asech}','\mbox{sech}^{-1}',$texexp);
  163. $texexp = str_replace('\mbox{acsch}','\mbox{csch}^{-1}',$texexp);
  164. $texexp = str_replace('\mbox{acoth}','\mbox{coth}^{-1}',$texexp);
  165. $texexp = preg_replace('/\\\sqrt{(.+?),(.+?)}/s','\sqrt['. "\$2]{\$1}",$texexp);
  166. $texexp = preg_replace('/\\\mbox{abs}\\\left\((.+?)\\\right\)/s',"|\$1|",$texexp);
  167. $texexp = preg_replace('/\\\log\\\left\((.+?),(.+?)\\\right\)/s','\log_{'. "\$2}\\left(\$1\\right)",$texexp);
  168. $texexp = preg_replace('/(\\\cos|\\\sin|\\\tan|\\\sec|\\\csc|\\\cot)([h]*)\\\left\((.+?),(.+?)\\\right\)/s',"\$1\$2^{". "\$4}\\left(\$3\\right)",$texexp);
  169. $texexp = preg_replace('/\\\int\\\left\((.+?),(.+?),(.+?)\\\right\)/s','\int_'. "{\$2}^{\$3}\$1 ",$texexp);
  170. $texexp = preg_replace('/\\\int\\\left\((.+?d[a-z])\\\right\)/s','\int '. "\$1 ",$texexp);
  171. $texexp = preg_replace('/\\\lim\\\left\((.+?),(.+?),(.+?)\\\right\)/s','\lim_'. "{\$2\\to \$3}\$1 ",$texexp);
  172. return $texexp;
  173. }
  174. function outputText($texexp) {
  175. header("Content-type: text/html; charset=utf-8");
  176. echo "<html><body><pre>\n";
  177. if ($texexp) {
  178. $texexp = str_replace('<','&lt;',$texexp);
  179. $texexp = str_replace('>','&gt;',$texexp);
  180. $texexp = str_replace('"','&quot;',$texexp);
  181. echo "$texexp\n\n";
  182. } else {
  183. echo "No text output available\n\n";
  184. }
  185. echo "</pre></body></html>\n";
  186. }
  187. function tex2image($texexp, $md5, $return=false) {
  188. global $CFG;
  189. if (!$texexp) {
  190. echo 'No tex expresion specified';
  191. return;
  192. }
  193. $texexp = '\Large ' . $texexp;
  194. $image = $md5 . ".gif";
  195. $filetype = 'image/gif';
  196. if (!file_exists("$CFG->dataroot/filter/algebra")) {
  197. make_upload_directory("filter/algebra");
  198. }
  199. $pathname = "$CFG->dataroot/filter/algebra/$image";
  200. if (file_exists($pathname)) {
  201. unlink($pathname);
  202. }
  203. $commandpath = filter_tex_get_executable(true);
  204. $cmd = filter_tex_get_cmd($pathname, $texexp);
  205. system($cmd, $status);
  206. if ($return) {
  207. return $image;
  208. }
  209. if (file_exists($pathname)) {
  210. send_file($pathname, $image);
  211. } else {
  212. $ecmd = "$cmd 2>&1";
  213. echo `$ecmd` . "<br />\n";
  214. echo "The shell command<br />$cmd<br />returned status = $status<br />\n";
  215. if ($status == 4) {
  216. echo "Status corresponds to illegal instruction<br />\n";
  217. } else if ($status == 11) {
  218. echo "Status corresponds to bus error<br />\n";
  219. } else if ($status == 22) {
  220. echo "Status corresponds to abnormal termination<br />\n";
  221. }
  222. if (file_exists($commandpath)) {
  223. echo "File size of mimetex executable $commandpath is " . filesize($commandpath) . "<br />";
  224. echo "The file permissions are: " . decoct(fileperms($commandpath)) . "<br />";
  225. if (function_exists("md5_file")) {
  226. echo "The md5 checksum of the file is " . md5_file($commandpath) . "<br />";
  227. } else {
  228. $handle = fopen($commandpath,"rb");
  229. $contents = fread($handle,16384);
  230. fclose($handle);
  231. echo "The md5 checksum of the first 16384 bytes is " . md5($contents) . "<br />";
  232. }
  233. } else {
  234. echo "mimetex executable $commandpath not found!<br />";
  235. }
  236. echo "Image not found!";
  237. }
  238. }
  239. function slasharguments($texexp, $md5) {
  240. global $CFG;
  241. $admin = $CFG->wwwroot.'/'.$CFG->admin.'/settings.php?section=http';
  242. $image = tex2image($texexp,$md5,true);
  243. echo "<p>If the following image displays correctly, set your ";
  244. echo "<a href=\"$admin\" target=\"_blank\">Administration->Server->HTTP</a> ";
  245. echo "setting for slasharguments to file.php/1/pic.jpg: ";
  246. echo "<img src=\"pix.php/$image\" align=\"absmiddle\"></p>\n";
  247. echo "<p>Otherwise set it to file.php?file=/1/pic.jpg ";
  248. echo "It should display correctly as ";
  249. echo "<img src=\"pix.php?file=$image\" align=\"absmiddle\"></p>\n";
  250. echo "<p>If neither equation image displays correctly, please seek ";
  251. echo "further help at moodle.org at the ";
  252. echo "<a href=\"http://moodle.org/mod/forum/view.php?id=752&loginguest=true\" target=\"_blank\">";
  253. echo "Mathematics Tools Forum</a></p>";
  254. }
  255. ?>
  256. <html>
  257. <head><title>Algebra Filter Debugger</title></head>
  258. <body>
  259. <p>Please enter an algebraic expression <b>without</b> any surrounding @@ into
  260. the text box below. (Click <a href="#help">here for help.</a>)
  261. <form action="algebradebug.php" method="get"
  262. target="inlineframe">
  263. <center>
  264. <input type="text" name="algebra" size="50"
  265. value="sin(z)/(x^2+y^2)" />
  266. </center>
  267. <ol>
  268. <li>First click on this button <input type="submit" name="ShowDB" value="Show DB Entry" />
  269. to see the cache_filters database entry for this expression.</li>
  270. <li>If the database entry looks corrupt, click on this button to delete it:
  271. <input type="submit" name="DeleteDB" value="Delete DB Entry" /></li>
  272. <li>Now click on this button <input type="submit" name="TeXStage1" value="First Stage Tex Translation" />.
  273. A preliminary translation into TeX will appear in the box below.</li>
  274. <li>Next click on this button <input type="submit" name="TeXStage2" value="Second Stage Tex Translation" />.
  275. A more refined translation into TeX will appear in the box below.</li>
  276. <li>Then click on this button <input type="submit" name="ShowImage" value="Show Image" />
  277. to show a graphic image of the algebraic expression.</li>
  278. <li>Finally check your slash arguments setting
  279. <input type="submit" name="SlashArguments" value="Check Slash Arguments" /></li>
  280. </ol>
  281. </form> <br /> <br />
  282. <center>
  283. <iframe name="inlineframe" align="middle" width="80%" height="200">
  284. &lt;p&gt;Something is wrong...&lt;/p&gt;
  285. </iframe>
  286. </center> <br />
  287. <hr />
  288. <a name="help">
  289. <h2>Debugging Help</h2>
  290. </a>
  291. <p>First here is a brief overview on how the algebra filter works. It
  292. takes an algebra expression and first translates it into TeX. It first
  293. looks for the TeX translation in the Moodle database in the table cache_filters
  294. in the field rawtext. If not found, it passes the algebraic expression to the
  295. Perl script algebra2tex.pl, which also uses the Perl library AlgParser.pm.
  296. It then saves the TeX translation in the database for subsequent uses and
  297. passes the TeX to the mimetex executable to be converted to a gif image.
  298. Here are a few common things that can go wrong and some suggestions on how
  299. you might try to fix them.</p>
  300. <ol>
  301. <li>Something had gone wrong on a previous occasion when the filter tried to
  302. translate this expression. Then the database entry for that expression contains
  303. a bad TeX translation in the rawtext field (usually blank). You can fix this
  304. by clicking on &quot;Delete DB Entry&quot;</li>
  305. <li>The First Stage TeX Translation gives a &quot;No text output available&quot;
  306. message. If your server is running Windows, this may be due to the fact that
  307. you haven't installed Perl or didn't install it correctly. If your server is
  308. running some version of Unix (e.g. Linux), then this may be due to your Perl
  309. binary being installed in a nonstandard location. To fix this edit the first
  310. line of the algebra2tex.pl script. Another possible problem which may affect
  311. both Unix and Windows servers is that the web server doesn't have execute permission
  312. on the algebra2tex.pl script. In that case change permissions accordingly</li>
  313. <li>The Second Stage TeX Translation produces malformed TeX. This indicates
  314. a bug in the algebra filter. Post the original algebraic expression and the
  315. bad TeX translation in the <a href="http://moodle.org/mod/forum/view.php?id=752">
  316. Mathematics Tools</a> forum in the Using Moodle course on moodle.org.</li>
  317. <li>The TeX to gif image conversion process does not work. If your server is
  318. running Unix, a likely cause is that the mimetex binary you are using is
  319. incompatible with your operating system. You can try compiling it from the
  320. C sources downloaded from <a href="http://www.forkosh.com/mimetex.zip">
  321. http://www.forkosh.com/mimetex.zip</a>, or looking for an appropriate
  322. binary at <a href="http://moodle.org/download/mimetex/">
  323. http://moodle.org/download/mimetex/</a>. You may then also need to
  324. edit your moodle/filter/algebra/pix.php file to add
  325. <br /><?PHP echo "case &quot;" . PHP_OS . "&quot;:" ;?><br ?> to the list of operating systems
  326. in the switch (PHP_OS) statement. Windows users may have a problem properly
  327. unzipping mimetex.exe. Make sure that mimetex.exe is is <b>PRECISELY</b>
  328. 433152 bytes in size. If not, download fresh copy from
  329. <a href="http://moodle.org/download/mimetex/windows/mimetex.exe">
  330. http://moodle.org/download/mimetex/windows/mimetex.exe</a>. Lastly check
  331. the execute permissions on your mimetex binary, as outlined in item 2 above.</li>
  332. </ol>
  333. </body>
  334. </html>