From d93a90dcfd3e8cc06cebdf35cb3709b613e83988 Mon Sep 17 00:00:00 2001 From: thepurpleblob Date: Fri, 7 Sep 2007 14:24:18 +0000 Subject: [PATCH] MDL-11147: Debug screen now supports LaTeX/Ghostscript renderer. Merged from STABLE_18 --- filter/tex/texdebug.php | 232 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 184 insertions(+), 48 deletions(-) diff --git a/filter/tex/texdebug.php b/filter/tex/texdebug.php index 97fe91ad4..e4224ecd3 100644 --- a/filter/tex/texdebug.php +++ b/filter/tex/texdebug.php @@ -6,6 +6,7 @@ $nomoodlecookie = true; // Because it interferes with caching require_once("../../config.php"); + require( 'latex.php' ); if (empty($CFG->textfilters)) { error ('Filter not enabled!'); @@ -18,52 +19,80 @@ $CFG->texfilterdir = "filter/tex"; $CFG->teximagedir = "filter/tex"; + + $param = null; + $param->action = optional_param( 'action','',PARAM_ALPHA ); + $param->tex = optional_param( 'tex','' ); $query = urldecode($_SERVER['QUERY_STRING']); error_reporting(E_ALL); + $output = ''; - if ($query) { - $output = $query; - $splitpos = strpos($query,'&')-4; - $texexp = substr($query,4,$splitpos); - $md5 = md5($texexp); - if (strpos($query,'ShowDB') || strpos($query,'DeleteDB')) { - $texcache = get_record("cache_filters","filter","tex", "md5key", $md5); - } - if (strpos($query,'ShowDB')) { - if ($texcache) { - $output = "DB cache_filters entry for $texexp\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 $texexp not found\n"; - } + // look up in cache if required + if ($param->action=='ShowDB' or $param->action=='DeleteDB') { + $md5 = md5($param->tex); + $texcache = get_record("cache_filters","filter","tex", "md5key", $md5); + } + + // Action: Show DB Entry + if ($param->action=='ShowDB') { + if ($texcache) { + $output = "DB cache_filters entry for $param->tex\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 $param->tex not found\n"; } - if (strpos($query,'DeleteDB')) { - if ($texcache) { - $output = "Deleting DB cache_filters entry for $texexp\n"; - $result = delete_records("cache_filters","id",$texcache->id); - if ($result) { - $result = 1; - } else { - $result = 0; - } - $output .= "Number of records deleted = $result\n"; + } + + // Action: Delete DB Entry + if ($param->action=='DeleteDB') { + if ($texcache) { + $output = "Deleting DB cache_filters entry for $param->tex\n"; + $result = delete_records("cache_filters","id",$texcache->id); + if ($result) { + $result = 1; } else { - $output = "Could not delete DB cache_filters entry for $texexp\nbecause it could not be found.\n"; + $result = 0; } + $output .= "Number of records deleted = $result\n"; + } else { + $output = "Could not delete DB cache_filters entry for $param->tex\nbecause it could not be found.\n"; } - if (strpos($query,'ShowImage')) { - tex2image($texexp); - } else if (strpos($query,'SlashArguments')) { - slasharguments($texexp); - } else { - outputText($output); - } + } + + // Action: Show Image + if ($param->action=='ShowImageMimetex') { + tex2image($param->tex); + } + + // Action: Check Slasharguments + if ($param->action=='SlashArguments') { + slasharguments($param->tex); + } + + // Action: Show Tex command line output + if ($param->action=='ShowImageTex') { + TexOutput($param->tex, true); + exit; + } + + // Action: Show Tex command line output + if ($param->action=='ShowOutputTex') { + TexOutput($param->tex); + exit; + } + + if (!empty($param->action)) { + outputText($output); + } + + // nothing more to do if there was any action + if (!empty($param->action)) { exit; } @@ -181,6 +210,97 @@ } } + + // test Tex/Ghostscript output - command execution only + function TexOutput( $expression, $graphic=false ) { + global $CFG; + $output = ''; + + $latex = new latex(); + + // first check if it is likely to work at all + $output .= "

Checking executables

\n"; + $executables_exist = true; + if (is_file($CFG->filter_tex_pathlatex)) { + $output .= "latex executable ($CFG->filter_tex_pathlatex) is readable
\n"; + } + else { + $executables_exist = false; + $output .= "Error: latex executable ($CFG->filter_tex_pathlatex) is not readable
\n"; + } + if (is_file($CFG->filter_tex_pathdvips)) { + $output .= "dvips executable ($CFG->filter_tex_pathdvips) is readable
\n"; + } + else { + $executables_exist = false; + $output .= "Error: dvips executable ($CFG->filter_tex_pathdvips) is not readable
\n"; + } + if (is_file($CFG->filter_tex_pathconvert)) { + $output .= "convert executable ($CFG->filter_tex_pathconvert) is readable
\n"; + } + else { + $executables_exist = false; + $output .= "Error: convert executable ($CFG->filter_tex_pathconvert) is not readable
\n"; + } + + // knowing that it might work.. + $md5 = md5( $expression ); + $output .= "

base filename for expression is '$md5'

\n"; + + // temporary paths + $tex = "$latex->temp_dir/$md5.tex"; + $dvi = "$latex->temp_dir/$md5.dvi"; + $ps = "$latex->temp_dir/$md5.ps"; + $gif = "$latex->temp_dir/$md5.gif"; + + // put the expression as a file into the temp area + $doc = $latex->construct_latex_document( $expression ); + $fh = fopen( $tex, 'w' ); + fputs( $fh, $doc ); + fclose( $fh ); + + // cd to temp dir + chdir( $latex->temp_dir ); + + // step 1: latex command + $cmd = "$CFG->filter_tex_pathlatex --interaction=nonstopmode $tex"; + $output .= execute( $cmd ); + + // step 2: dvips command + $cmd = "$CFG->filter_tex_pathdvips -E $dvi -o $ps"; + $output .= execute( $cmd ); + + // step 3: convert command + $cmd = "$CFG->filter_tex_pathconvert -density 240 -trim $ps $gif "; + $output .= execute( $cmd ); + + if (!$graphic) { + echo( $output ); + } else { + $lastmodified = filemtime($gif); + $lifetime = 86400; + $filetype = 'image/gif'; + $image = "$md5.gif"; + 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($gif)); + header("Content-type: $filetype"); + readfile("$gif"); + } + } + + function execute( $cmd ) { + exec( $cmd, $result, $code ); + $output = "
$ $cmd\n";
+        $lines = implode( "\n", $result );
+        $output .= "OUTPUT: $lines\n";
+        $output .= "RETURN CODE: $code\n
\n"; + return $output; + } + function slasharguments($texexp) { global $CFG; $admin = $CFG->wwwroot . '/' . $CFG->admin . '/config.php'; @@ -211,16 +331,22 @@ +

The following tests are available:

    -
  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. Then click on this button - to show a graphic image of the algebraic expression.
  6. -
  7. Finally check your slash arguments setting -
  8. +
  9. + See the cache_filters database entry for this expression (if any).
  10. +
  11. + Delete the cache_filters database entry for this expression (if any).
  12. +
  13. + Show a graphic image of the algebraic expression rendered with mimetex.
  14. +
  15. + Show a graphic image of the algebraic expression rendered with Tex/Ghostscript.
  16. +
  17. + Show command execution output from the algebraic expression rendered with Tex/Ghostscript.
  18. +
  19. + Check slasharguments setting.
+