[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/filter/tex/ -> texdebug.php (source)

   1  <?PHP // $Id: texdebug.php,v 1.20.2.2 2007/12/25 11:41:46 skodak Exp $
   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  
   6      $nomoodlecookie = true;     // Because it interferes with caching
   7  
   8      require_once("../../config.php");
   9  
  10      if (empty($CFG->textfilters)) {
  11          error ('Filter not enabled!');
  12      } else {
  13          $filters = explode(',', $CFG->textfilters);
  14          if (array_search('filter/tex', $filters) === FALSE) {
  15              error ('Filter not enabled!');
  16          }
  17      }
  18  
  19      require_once($CFG->libdir.'/filelib.php');
  20      require_once($CFG->dirroot.'/filter/tex/lib.php');
  21      require_once($CFG->dirroot.'/filter/tex/latex.php');
  22  
  23      $action = optional_param('action', '', PARAM_ALPHA);
  24      $texexp = optional_param('tex', '', PARAM_RAW);
  25  
  26      $query = urldecode($_SERVER['QUERY_STRING']);
  27      error_reporting(E_ALL);
  28      $output = '';
  29  
  30      // look up in cache if required
  31      if ($action=='ShowDB' or $action=='DeleteDB') {
  32          $md5 = md5($texexp);
  33          $texcache = get_record("cache_filters","filter","tex", "md5key", $md5);
  34      }
  35  
  36      // Action: Show DB Entry
  37      if ($action=='ShowDB') {
  38          if ($texcache) {
  39              $output = "DB cache_filters entry for $texexp\n";
  40              $output .= "id = $texcache->id\n";
  41              $output .= "filter = $texcache->filter\n";
  42              $output .= "version = $texcache->version\n";
  43              $output .= "md5key = $texcache->md5key\n";
  44              $output .= "rawtext = $texcache->rawtext\n";
  45              $output .= "timemodified = $texcache->timemodified\n";
  46          } else {
  47              $output = "DB cache_filters entry for $texexp not found\n";
  48          }
  49      }
  50  
  51      // Action: Delete DB Entry
  52      if ($action=='DeleteDB') {
  53          if ($texcache) {
  54              $output = "Deleting DB cache_filters entry for $texexp\n";
  55              $result =  delete_records("cache_filters","id",$texcache->id);
  56              if ($result) {
  57                  $result = 1;
  58              } else {
  59                  $result = 0;
  60              }
  61              $output .= "Number of records deleted = $result\n";
  62          } else {
  63              $output = "Could not delete DB cache_filters entry for $texexp\nbecause it could not be found.\n";
  64          }
  65      }
  66  
  67      // Action: Show Image
  68      if ($action=='ShowImageMimetex') {
  69          tex2image($texexp);
  70      }
  71  
  72      // Action: Check Slasharguments
  73      if ($action=='SlashArguments') {
  74          slasharguments($texexp);
  75      }
  76  
  77      // Action: Show Tex command line output
  78      if ($action=='ShowImageTex') {
  79          TexOutput($texexp, true);
  80          exit;
  81      }
  82  
  83      // Action: Show Tex command line output
  84      if ($action=='ShowOutputTex') {
  85          if (debugging()) {
  86              TexOutput($texexp);
  87          } else {
  88              echo "Can not output detailed information due to security concerns, please turn on debug mode first.";
  89          }
  90          exit;
  91      }
  92  
  93      if (!empty($action)) {
  94          outputText($output);
  95      }
  96  
  97      // nothing more to do if there was any action
  98      if (!empty($action)) {
  99          exit;
 100      }
 101  
 102  
 103      function outputText($texexp) {
 104          header("Content-type: text/html");
 105          echo "<html><body><pre>\n";
 106          if ($texexp) {
 107              $texexp = str_replace('<', '&lt;', $texexp);
 108              $texexp = str_replace('>', '&gt;', $texexp);
 109              $texexp = str_replace('"', '&quot;', $texexp);
 110              echo "$texexp\n\n";
 111          } else {
 112              echo "No text output available\n\n";
 113          }
 114          echo "</pre></body></html>\n";
 115      }
 116  
 117      function tex2image($texexp, $return=false) {
 118          global $CFG;
 119  
 120          if (!$texexp) {
 121              echo 'No tex expresion specified';
 122              return;
 123          }
 124  
 125          $image  = md5($texexp) . ".gif";
 126          $filetype = 'image/gif';
 127          if (!file_exists("$CFG->dataroot/filter/tex")) {
 128              make_upload_directory("filter/tex");
 129          }
 130          $pathname = "$CFG->dataroot/filter/tex/$image";
 131          if (file_exists($pathname)) {
 132              unlink($pathname);
 133          }
 134  
 135          $texexp = '\Large '.$texexp;
 136          $commandpath = tex_filter_get_executable(true);
 137          $cmd = tex_filter_get_cmd($pathname, $texexp);
 138          system($cmd, $status);
 139  
 140          if ($return) {
 141            return $image;
 142          }
 143  
 144          if (file_exists($pathname)) {
 145              send_file($pathname, $image);
 146  
 147          } else if (debugging()) {
 148              $ecmd = "$cmd 2>&1";
 149              echo `$ecmd` . "<br />\n";
 150              echo "The shell command<br />$cmd<br />returned status = $status<br />\n";
 151              if ($status == 4) {
 152                  echo "Status corresponds to illegal instruction<br />\n";
 153              } else if ($status == 11) {
 154                  echo "Status corresponds to bus error<br />\n";
 155              } else if ($status == 22) {
 156                  echo "Status corresponds to abnormal termination<br />\n";
 157              }
 158              if (file_exists($commandpath)) {
 159                  echo "File size of mimetex executable  $commandpath is " . filesize($commandpath) . "<br />";
 160                  echo "The file permissions are: " . decoct(fileperms($commandpath)) . "<br />";
 161                  if (function_exists("md5_file")) {
 162                      echo "The md5 checksum of the file is " . md5_file($commandpath) . "<br />";
 163                  } else {
 164                      $handle = fopen($commandpath,"rb");
 165                      $contents = fread($handle,16384);
 166                      fclose($handle);
 167                      echo "The md5 checksum of the first 16384 bytes is " . md5($contents) . "<br />";
 168                  }
 169              } else {
 170                  echo "mimetex executable $commandpath not found!<br />";
 171              }
 172              echo "Image not found!";
 173          } else {
 174              echo "Can not output detailed information due to security concerns, please turn on debug mode first.";
 175          }
 176      }
 177  
 178  
 179      // test Tex/Ghostscript output - command execution only
 180      function TexOutput($expression, $graphic=false) {
 181          global $CFG;
 182          $output = '';
 183  
 184          $latex = new latex();
 185  
 186          // first check if it is likely to work at all
 187          $output .= "<h3>Checking executables</h3>\n";
 188          $executables_exist = true;
 189          if (is_file($CFG->filter_tex_pathlatex)) {
 190              $output .= "latex executable ($CFG->filter_tex_pathlatex) is readable<br />\n";
 191          }
 192          else {
 193              $executables_exist = false;
 194              $output .= "<b>Error:</b> latex executable ($CFG->filter_tex_pathlatex) is not readable<br />\n";
 195          }
 196          if (is_file($CFG->filter_tex_pathdvips)) {
 197              $output .= "dvips executable ($CFG->filter_tex_pathdvips) is readable<br />\n";
 198          }
 199          else {
 200              $executables_exist = false;
 201              $output .= "<b>Error:</b> dvips executable ($CFG->filter_tex_pathdvips) is not readable<br />\n";
 202          }
 203          if (is_file($CFG->filter_tex_pathconvert)) {
 204              $output .= "convert executable ($CFG->filter_tex_pathconvert) is readable<br />\n";
 205          }
 206          else {
 207              $executables_exist = false;
 208              $output .= "<b>Error:</b> convert executable ($CFG->filter_tex_pathconvert) is not readable<br />\n";
 209          }
 210  
 211          // knowing that it might work..
 212          $md5 = md5($expression);
 213          $output .= "<p>base filename for expression is '$md5'</p>\n";
 214  
 215          // temporary paths
 216          $tex = "$latex->temp_dir/$md5.tex";
 217          $dvi = "$latex->temp_dir/$md5.dvi";
 218          $ps = "$latex->temp_dir/$md5.ps";
 219          $gif = "$latex->temp_dir/$md5.gif";
 220  
 221          // put the expression as a file into the temp area
 222          $expression = stripslashes($expression);
 223          $expression = html_entity_decode($expression);
 224          $output .= "<p>Processing TeX expression:</p><pre>$expression</pre>\n";
 225          $doc = $latex->construct_latex_document($expression);
 226          $fh = fopen($tex, 'w');
 227          fputs($fh, $doc);
 228          fclose($fh);
 229  
 230          // cd to temp dir
 231          chdir($latex->temp_dir);
 232  
 233          // step 1: latex command
 234          $cmd = "$CFG->filter_tex_pathlatex --interaction=nonstopmode $tex";
 235          $output .= execute($cmd);
 236  
 237          // step 2: dvips command
 238          $cmd = "$CFG->filter_tex_pathdvips -E $dvi -o $ps";
 239          $output .= execute($cmd);
 240  
 241          // step 3: convert command
 242          $cmd = "$CFG->filter_tex_pathconvert -density 240 -trim $ps $gif ";
 243          $output .= execute($cmd);
 244  
 245          if (!$graphic) {
 246              echo($output);
 247          } else {
 248              send_file($gif, "$md5.gif");
 249           }
 250      }
 251  
 252      function execute($cmd) {
 253          exec($cmd, $result, $code);
 254          $output = "<pre>$ $cmd\n";
 255          $lines = implode("\n", $result);
 256          $output .= "OUTPUT: $lines\n";
 257          $output .= "RETURN CODE: $code\n</pre>\n";
 258          return $output;
 259      }
 260  
 261      function slasharguments($texexp) {
 262          global $CFG;
 263          $admin = $CFG->wwwroot.'/'.$CFG->admin.'/settings.php?section=http';
 264          $image = tex2image($texexp,true);
 265          echo "<p>If the following image displays correctly, set your ";
 266          echo "<a href=\"$admin\" target=\"_blank\">Administration->Server->HTTP</a> ";
 267          echo "setting for slasharguments to file.php/1/pic.jpg: ";
 268          echo "<img src=\"$CFG->wwwroot/filter/tex/pix.php/$image\" align=\"absmiddle\"></p>\n";
 269          echo "<p>Otherwise set it to file.php?file=/1/pic.jpg ";
 270          echo "It should display correctly as ";
 271          echo "<img src=\"$CFG->wwwroot/filter/tex/pix.php?file=$image\" align=\"absmiddle\"></p>\n";
 272          echo "<p>If neither equation image displays correctly, please seek ";
 273          echo "further help at moodle.org at the ";
 274          echo "<a href=\"http://moodle.org/mod/forum/view.php?id=752&loginguest=true\" target=\"_blank\">";
 275          echo "Mathematics Tools Forum</a></p>";
 276      }
 277  
 278  ?>
 279  
 280  <html>
 281  <head><title>TeX Filter Debugger</title></head>
 282  <body>
 283    <p>Please enter an algebraic expression <b>without</b> any surrounding $$ into
 284         the text box below. (Click <a href="#help">here for help.</a>)
 285            <form action="texdebug.php" method="get"
 286             target="inlineframe">
 287              <center>
 288               <input type="text" name="tex" size="50"
 289                      value="f(x)=\Bigint_{-\infty}^x~e^{-t^2}dt" />
 290              </center>
 291             <p>The following tests are available:</p>
 292             <ol>
 293             <li><input type="radio" name="action" value="ShowDB" id="ShowDB" />
 294                 <label for="ShowDB">See the cache_filters database entry for this expression (if any).</label></li>
 295             <li><input type="radio" name="DeleteDB" value="DeleteDB" id="DeleteDB" />
 296                 <label for="DeleteDB">Delete the cache_filters database entry for this expression (if any).</label></li>
 297             <li><input type="radio" name="action" value="ShowImageMimetex" id="ShowImageMimetex  checked="checked" />
 298                 <label for="ShowImageMimetex">Show a graphic image of the algebraic expression rendered with mimetex.</label></li>
 299             <li><input type="radio" name="action" value="ShowImageTex" id="ShowImageTex" />
 300                 <label for="ShowImageTex">Show a graphic image of the algebraic expression rendered with Tex/Ghostscript.</label></li>
 301             <li><input type="radio" name="action" value="ShowOutputTex" id="ShowOutputTex" />
 302                 <label for="ShowOutputTex">Show command execution output from the algebraic expression rendered with Tex/Ghostscript.</label></li>
 303             <li><input type="radio" name="action" value="SlashArguments" id="SlashArguments" />
 304                 <label for="SlashArguments">Check slasharguments setting.</label></li>
 305             </ol>
 306             <input type="submit" value="Do it!" />
 307            </form> <br /> <br />
 308         <center>
 309            <iframe name="inlineframe" align="middle" width="80%" height="200">
 310            &lt;p&gt;Something is wrong...&lt;/p&gt; 
 311            </iframe>
 312         </center> <br />
 313  <hr />
 314  <a name="help">
 315  <h2>Debugging Help</h2>
 316  </a>
 317  <p>First a brief overview of how the TeX filter works. The TeX filter first
 318  searches the database cache_filters table to see if this TeX expression had been
 319  processed before. If not, it adds a DB entry for that expression.  It then
 320  replaces the TeX expression by an &lt;img src=&quot;.../filter/tex/pix.php...&quot;&gt;
 321  tag.  The filter/tex/pix.php script then searches the database to find an
 322  appropriate gif image file for that expression and to create one if it doesn't exist.
 323  It will then use either the LaTex/Ghostscript renderer (using external executables
 324  on your system) or the bundled Mimetex executable. The full Latex/Ghostscript
 325  renderer produces better results and is tried first. 
 326  Here are a few common things that can go wrong and some suggestions on how
 327  you might try to fix them.</p>
 328  <ol>
 329  <li>Something had gone wrong on a previous occasion when the filter tried to
 330  process this expression. Then the database entry for that expression contains
 331  a bad TeX expression in the rawtext field (usually blank). You can fix this
 332  by clicking on &quot;Delete DB Entry&quot;</li>
 333  <li>The TeX to gif image conversion process does not work. 
 334  If paths are specified in the filter configuation screen for the three
 335  executables these will be tried first. Note that they still must be correctly
 336  installed and have the correct permissions. In particular make sure that you 
 337  have all the packages installed (e.g., on Debian/Ubuntu you need to install
 338  the 'tetex-extra' package). Running the 'show command execution' test should
 339  give a big clue.
 340  If this fails or is not available, the Mimetex executable is tried. If this 
 341  fails a likely cause is that the mimetex binary you are using is
 342  incompatible with your operating system. You can try compiling it from the
 343  C sources downloaded from <a href="http://www.forkosh.com/mimetex.zip">
 344  http://www.forkosh.com/mimetex.zip</a>, or looking for an appropriate
 345  binary at <a href="http://moodle.org/download/mimetex/">
 346  http://moodle.org/download/mimetex/</a>. You may then also need to
 347  edit your moodle/filter/tex/pix.php file to add 
 348  <br /><?PHP echo "case &quot;" . PHP_OS . "&quot;:" ;?><br ?> to the list of operating systems
 349  in the switch (PHP_OS) statement. Windows users may have a problem properly
 350  unzipping mimetex.exe. Make sure that mimetex.exe is is <b>PRECISELY</b>
 351  433152 bytes in size. If not, download a fresh copy from
 352  <a href="http://moodle.org/download/mimetex/windows/mimetex.exe">
 353  http://moodle.org/download/mimetex/windows/mimetex.exe</a>. 
 354  Another possible problem which may affect
 355  both Unix and Windows servers is that the web server doesn't have execute permission
 356  on the mimetex binary. In that case change permissions accordingly</li>
 357  </ol>
 358  </body>
 359  </html>


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7