From 9b361d24b3457e9295eab342c2c39e6579331727 Mon Sep 17 00:00:00 2001 From: fiedorow Date: Thu, 19 Feb 2004 18:03:41 +0000 Subject: [PATCH] Added debugger for algebra filter --- filter/algebra/algebradebug.php | 270 ++++++++++++++++++++++++++++++++ filter/algebra/filter.php | 1 - 2 files changed, 270 insertions(+), 1 deletion(-) create mode 100644 filter/algebra/algebradebug.php diff --git a/filter/algebra/algebradebug.php b/filter/algebra/algebradebug.php new file mode 100644 index 0000000000..f4301e0164 --- /dev/null +++ b/filter/algebra/algebradebug.php @@ -0,0 +1,270 @@ +texfilterdir = "filter/tex"; + $CFG->algebrafilterdir = "filter/algebra"; + $CFG->algebraimagedir = "filter/algebra"; + + error_reporting(E_ALL); + $query = urldecode($_SERVER['QUERY_STRING']); + + if ($query) { + $output = $query; + $splitpos = strpos($query,'&')-8; + $algebra = substr($query,8,$splitpos); + $md5 = md5($algebra); + if (strpos($query,'ShowDB') || strpos($query,'DeleteDB')) { + $texcache = get_record("cache_filters","filter","algebra", "md5key", $md5); + } + if (strpos($query,'ShowDB')) { + if ($texcache) { + $output = "DB cache_filters entry for $algebra\n"; + $output .= "id = $texcache->id\n"; + $output .= "filter = $texcache->filter\n"; + $output .= "version = $texcache->version\n"; + $output .= "md5key = $texcache->md5key\n"; + $output .= "rawtext = $texcache->rawtext\n"; + $output .= "timemodified = $texcache->timemodified\n"; + } else { + $output = "DB cache_filters entry for $algebra not found\n"; + } + } + if (strpos($query,'DeleteDB')) { + if ($texcache) { + $output = "Deleting DB cache_filters entry for $algebra\n"; + $result = delete_records("cache_filters","id",$texcache->id); + if ($result) { + $result = 1; + } else { + $result = 0; + } + $output .= "Number of records deleted = $result\n"; + } else { + $output = "Could not delete DB cache_filters entry for $algebra\nbecause it could not be found.\n"; + } + } + if (strpos($query,'TeXStage1')) { + $output = algebra2tex($algebra); + } + if (strpos($query,'TeXStage2')) { + $output = algebra2tex($algebra); + $output = refineTeX($output); + } + if (strpos($query,'ShowImage')) { + $output = algebra2tex($algebra); + $output = refineTeX($output); + tex2image($output); + } else { + outputText($output); + } + exit; + } + +function algebra2tex($algebra) { + Global $CFG; + $algebra = str_replace('<','<',$algebra); + $algebra = str_replace('>','>',$algebra); + $algebra = str_replace('<>','#',$algebra); + $algebra = str_replace('<=','%',$algebra); + $algebra = str_replace('>=','!',$algebra); + $algebra = preg_replace('!\r\n?!',' ',$algebra); + if ( (PHP_OS == "WINNT") || (PHP_OS == "WIN32") || (PHP_OS == "Windows") ) { + $algebra = "\"". str_replace('"','\"',$algebra) . "\""; + $cmd = "cd $CFG->dirroot/$CFG->algebrafilterdir & algebra2tex.pl $algebra"; + } else { + $algebra = escapeshellarg($algebra); + $cmd = "cd $CFG->dirroot/$CFG->algebrafilterdir; ./algebra2tex.pl $algebra"; + } + $texexp = `$cmd`; + return $texexp; +} + +function refineTeX($texexp) { + $texexp = str_replace('#','\not= ',$texexp); + $texexp = str_replace('%','\leq ',$texexp); + $texexp = str_replace('!','\geq ',$texexp); + $texexp = str_replace('\left{','{',$texexp); + $texexp = str_replace('\right}','}',$texexp); + $texexp = str_replace('\fun',' ',$texexp); + $texexp = str_replace('infty','\infty',$texexp); + $texexp = str_replace('\mbox{logten}','\mbox{log}_{10}',$texexp); + $texexp = str_replace('\mbox{acos}','\mbox{cos}^{-1}',$texexp); + $texexp = str_replace('\mbox{asin}','\mbox{sin}^{-1}',$texexp); + $texexp = str_replace('\mbox{atan}','\mbox{tan}^{-1}',$texexp); + $texexp = str_replace('\mbox{asec}','\mbox{sec}^{-1}',$texexp); + $texexp = str_replace('\mbox{acsc}','\mbox{csc}^{-1}',$texexp); + $texexp = str_replace('\mbox{acot}','\mbox{cot}^{-1}',$texexp); + $texexp = str_replace('\mbox{acosh}','\mbox{cosh}^{-1}',$texexp); + $texexp = str_replace('\mbox{asinh}','\mbox{sinh}^{-1}',$texexp); + $texexp = str_replace('\mbox{atanh}','\mbox{tanh}^{-1}',$texexp); + $texexp = str_replace('\mbox{asech}','\mbox{sech}^{-1}',$texexp); + $texexp = str_replace('\mbox{acsch}','\mbox{csch}^{-1}',$texexp); + $texexp = str_replace('\mbox{acoth}','\mbox{coth}^{-1}',$texexp); + $texexp = preg_replace('/\\\sqrt{(.+?),(.+?)}/s','\sqrt['. "\$2]{\$1}",$texexp); + $texexp = preg_replace('/\\\mbox{abs}\\\left\((.+?)\\\right\)/s',"|\$1|",$texexp); + $texexp = preg_replace('/\\\log\\\left\((.+?),(.+?)\\\right\)/s','\log_{'. "\$2}\\left(\$1\\right)",$texexp); + $texexp = preg_replace('/(\\\cos|\\\sin|\\\tan|\\\sec|\\\csc|\\\cot)([h]*)\\\left\((.+?),(.+?)\\\right\)/s',"\$1\$2^{". "\$4}\\left(\$3\\right)",$texexp); + $texexp = preg_replace('/\\\int\\\left\((.+?),(.+?),(.+?)\\\right\)/s','\int_'. "{\$2}^{\$3}\$1 ",$texexp); + $texexp = preg_replace('/\\\int\\\left\((.+?d[a-z])\\\right\)/s','\int '. "\$1 ",$texexp); + $texexp = preg_replace('/\\\lim\\\left\((.+?),(.+?),(.+?)\\\right\)/s','\lim_'. "{\$2\\to \$3}\$1 ",$texexp); + return $texexp; +} + +function outputText($texexp) { + header("Content-type: text/html"); + echo "
\n";
+  if ($texexp) {
+    $texexp = str_replace('<','<',$texexp);
+    $texexp = str_replace('>','>',$texexp);
+    $texexp = str_replace('"','"',$texexp);
+    echo "$texexp\n\n";
+  } else {
+    echo "No text output available\n\n";
+  }
+  echo "
\n"; +} + +function tex2image($texexp) { + global $CFG; + if ($texexp) { + $texexp = '\Large ' . $texexp; + $lifetime = 86400; + $image = md5($texexp) . ".gif"; + $filetype = 'image/gif'; + if (!file_exists("$CFG->dataroot/$CFG->algebraimagedir")) { + make_upload_directory($CFG->algebraimagedir); + } + $pathname = "$CFG->dataroot/$CFG->algebraimagedir/$image"; + $windows = 0; + switch (PHP_OS) { + case "Linux": + $cmd = "$CFG->dirroot/$CFG->texfilterdir/mimetex.linux -e $pathname ". escapeshellarg($texexp); + break; + case "WINNT": + case "WIN32": + case "Windows": + $windows = 1; + $texexp = str_replace('"','\"',$texexp); + $cmd = "$CFG->dirroot/$CFG->texfilterdir/mimetex.exe -e $pathname \"$texexp\""; + break; + case "Darwin": + $cmd = "$CFG->dirroot/$CFG->texfilterdir/mimetex.darwin -e $pathname ". escapeshellarg($texexp); + break; + } + system($cmd); + } + if ($texexp && file_exists($pathname)) { + $lastmodified = filemtime($pathname); + header("Last-Modified: " . gmdate("D, d M Y H:i:s", $lastmodified) . " GMT"); + header("Expires: " . gmdate("D, d M Y H:i:s", time() + $lifetime) . " GMT"); + header("Cache-control: max_age = $lifetime"); // a day + header("Pragma: "); + header("Content-disposition: inline; filename=$image"); + header("Content-length: ".filesize($pathname)); + header("Content-type: $filetype"); + readfile("$pathname"); + } else { + if (!$windows) { + $cmd = "$cmd 2>&1"; + echo `$cmd` . "
\n"; + } + echo "Image not found!"; + } +} +?> + + +Algebra Filter Debugger + +dirroot/filter/algebra/pix.php"; + $PHP_OS = PHP_OS; + $handle = fopen($filename,"r"); + $contents = fread($handle, filesize($filename)); + fclose($handle); + if (!strpos($contents,'case "'. $PHP_OS . '":')) { + echo "WARNING! case \"$PHP_OS\": NOT found in pix.php!!!

"; + } +?> +

Please enter an algebraic expression without any surrounding @@ into + the text box below. (Click here for help.) +

+
+ +
+
    +
  1. First click on this button + to see the cache_filters database entry for this expression.
  2. +
  3. If the database entry looks corrupt, click on this button to delete it: +
  4. +
  5. Now click on this button . + A preliminary translation into TeX will appear in the box below.
  6. +
  7. Next click on this button . + A more refined translation into TeX will appear in the box below.
  8. +
  9. Finally click on this button + to show a graphic image of the algebraic expression.
  10. +
+


+
+ +

+
+ +

Debugging Help

+
+

First here is a brief overview on how the algebra filter works. It +takes an algebra expression and first translates it into TeX. It first +looks for the TeX translation in the Moodle database in the table cache_filters +in the field rawtext. If not found, it passes the algebraic expression to the +Perl script algebra2tex.pl, which also uses the Perl library AlgParser.pm. +It then saves the TeX translation in the database for subsequent uses and +passes the TeX to the mimetex executable to be converted to a gif image. +Here are a few common things that can go wrong and some suggestions on how +you might try to fix them.

+
    +
  1. Something had gone wrong on a previous occasion when the filter tried to +translate this expression. Then the database entry for that expression contains +a bad TeX translation in the rawtext field (usually blank). You can fix this +by clicking on "Delete DB Entry"
  2. +
  3. The First Stage TeX Translation gives a "No text output available" +message. If your server is running Windows, this may be due to the fact that +you haven't installed Perl or didn't install it correctly. If your server is +running some version of Unix (e.g. Linux), then this may be due to your Perl +binary being installed in a nonstandard location. To fix this edit the first +line of the algebra2tex.pl script. Another possible problem which may affect +both Unix and Windows servers is that the web server doesn't have execute permission +on the algebra2tex.pl script. In that case change permissions accordingly
  4. +
  5. The Second Stage TeX Translation produces malformed TeX. This indicates +a bug in the algebra filter. Post the original algebraic expression and the +bad TeX translation in the +Mathematics Tools forum in the Using Moodle course on moodle.org.
  6. +
  7. The TeX to gif image conversion process does not work. If your server is +running Unix, a likely cause is that the mimetex binary you are using is +incompatible with your operating system. You can try compiling it from the +C sources downloaded from +http://www.forkosh.com/mimetex.zip, or looking for an appropriate +binary at +http://moodle.org/download/mimetex/. You may then also need to +edit your moodle/filter/algebra/pix.php file to add +

    to the list of operating systems +in the switch (PHP_OS) statement. Windows users may have a problem properly +unzipping mimetex.exe. Make sure that mimetex.exe is is PRECISELY +328192 bytes in size. If not, download fresh copy from + +http://moodle.org/download/mimetex/windows/mimetex.exe. Lastly check +the execute permissions on your mimetex binary, as outlined in item 2 above.
  8. +
+ + diff --git a/filter/algebra/filter.php b/filter/algebra/filter.php index 3d4de01d66..fdedc56574 100644 --- a/filter/algebra/filter.php +++ b/filter/algebra/filter.php @@ -172,7 +172,6 @@ function algebra_filter ($courseid, $text) { $texexp = str_replace('\mbox{acoth}','\mbox{coth}^{-1}',$texexp); $texexp = preg_replace('/\\\sqrt{(.+?),(.+?)}/s','\sqrt['. "\$2]{\$1}",$texexp); $texexp = preg_replace('/\\\mbox{abs}\\\left\((.+?)\\\right\)/s',"|\$1|",$texexp); - $texexp = preg_replace('/\\\mbox{abs}\\\left\((.+?)\\\right\)/s',"|\$1|",$texexp); $texexp = preg_replace('/\\\log\\\left\((.+?),(.+?)\\\right\)/s','\log_{'. "\$2}\\left(\$1\\right)",$texexp); $texexp = preg_replace('/(\\\cos|\\\sin|\\\tan|\\\sec|\\\csc|\\\cot)([h]*)\\\left\((.+?),(.+?)\\\right\)/s',"\$1\$2^{". "\$4}\\left(\$3\\right)",$texexp); $texexp = preg_replace('/\\\int\\\left\((.+?),(.+?),(.+?)\\\right\)/s','\int_'. "{\$2}^{\$3}\$1 ",$texexp); -- 2.39.5