diff --git a/filter/tex/texdebug.php b/filter/tex/texdebug.php index 97fe91ad4e2..e4224ecd31e 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); + // 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,'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"; + } + + // 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 = "DB cache_filters entry for $texexp not 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,'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"; - } else { - $output = "Could not delete DB cache_filters entry for $texexp\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 .= "
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: