|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This function fetches math. images from the data directory 00003 // If not, it obtains the corresponding TeX expression from the cache_tex db table 00004 // and uses mimeTeX to create the image file 00005 00006 define('NO_MOODLE_COOKIES', true); // Because it interferes with caching 00007 00008 require_once("../../config.php"); 00009 00010 if (!filter_is_enabled('filter/algebra')) { 00011 print_error('filternotenabled'); 00012 } 00013 00014 require_once($CFG->libdir.'/filelib.php'); 00015 require_once($CFG->dirroot.'/filter/tex/lib.php'); 00016 00017 require_login(); 00018 require_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM)); 00019 00020 $query = urldecode($_SERVER['QUERY_STRING']); 00021 00022 if ($query) { 00023 $output = $query; 00024 $splitpos = strpos($query,'&')-8; 00025 $algebra = substr($query,8,$splitpos); 00026 $md5 = md5($algebra); 00027 if (strpos($query,'ShowDB') || strpos($query,'DeleteDB')) { 00028 $texcache = $DB->get_record("cache_filters", array("filter"=>"algebra", "md5key"=>$md5)); 00029 } 00030 if (strpos($query,'ShowDB')) { 00031 if ($texcache) { 00032 $output = "DB cache_filters entry for $algebra\n"; 00033 $output .= "id = $texcache->id\n"; 00034 $output .= "filter = $texcache->filter\n"; 00035 $output .= "version = $texcache->version\n"; 00036 $output .= "md5key = $texcache->md5key\n"; 00037 $output .= "rawtext = $texcache->rawtext\n"; 00038 $output .= "timemodified = $texcache->timemodified\n"; 00039 } else { 00040 $output = "DB cache_filters entry for $algebra not found\n"; 00041 } 00042 } 00043 if (strpos($query,'DeleteDB')) { 00044 if ($texcache) { 00045 $output = "Deleting DB cache_filters entry for $algebra\n"; 00046 $result = $DB->delete_records("cache_filters", array("id"=>$texcache->id)); 00047 if ($result) { 00048 $result = 1; 00049 } else { 00050 $result = 0; 00051 } 00052 $output .= "Number of records deleted = $result\n"; 00053 } else { 00054 $output = "Could not delete DB cache_filters entry for $algebra\nbecause it could not be found.\n"; 00055 } 00056 } 00057 if (strpos($query,'TeXStage1')) { 00058 $output = algebra2tex($algebra); 00059 } 00060 if (strpos($query,'TeXStage2')) { 00061 $output = algebra2tex($algebra); 00062 $output = refineTeX($output); 00063 } 00064 if (strpos($query,'ShowImage')||strpos($query,'SlashArguments')) { 00065 $output = algebra2tex($algebra); 00066 $output = refineTeX($output); 00067 if (strpos($query,'ShowImage')) { 00068 tex2image($output, $md5); 00069 } else { 00070 slasharguments($output, $md5); 00071 } 00072 } else { 00073 outputText($output); 00074 } 00075 exit; 00076 } 00077 00078 function algebra2tex($algebra) { 00079 global $CFG; 00080 $algebra = str_replace('<','<',$algebra); 00081 $algebra = str_replace('>','>',$algebra); 00082 $algebra = str_replace('<>','#',$algebra); 00083 $algebra = str_replace('<=','%',$algebra); 00084 $algebra = str_replace('>=','!',$algebra); 00085 $algebra = preg_replace('/([=><%!#] *)-/',"\$1 zeroplace -",$algebra); 00086 $algebra = str_replace('delta','zdelta',$algebra); 00087 $algebra = str_replace('beta','bita',$algebra); 00088 $algebra = str_replace('theta','thita',$algebra); 00089 $algebra = str_replace('zeta','zita',$algebra); 00090 $algebra = str_replace('eta','xeta',$algebra); 00091 $algebra = str_replace('epsilon','zepslon',$algebra); 00092 $algebra = str_replace('upsilon','zupslon',$algebra); 00093 $algebra = preg_replace('!\r\n?!',' ',$algebra); 00094 $algebra = escapeshellarg($algebra); 00095 00096 if ( (PHP_OS == "WINNT") || (PHP_OS == "WIN32") || (PHP_OS == "Windows") ) { 00097 $cmd = "cd $CFG->dirroot\\filter\\algebra & algebra2tex.pl x/2"; 00098 $test = `$cmd`; 00099 if ($test != '\frac{x}{2}') { 00100 echo "There is a problem with either Perl or the script algebra2tex.pl<br/>"; 00101 $ecmd = $cmd . " 2>&1"; 00102 echo `$ecmd` . "<br/>\n"; 00103 echo "The shell command<br/>$cmd<br/>returned status = $status<br/>\n"; 00104 $commandpath = "$CFG->dirroot\\filter\\algebra\\algebra2tex.pl"; 00105 if (file_exists($commandpath)) { 00106 echo "The file permissions of algebra2tex.pl are: " . decoct(fileperms($commandpath)) . "<br/>"; 00107 } 00108 die; 00109 } 00110 $cmd = "cd $CFG->dirroot\\filter\\algebra & algebra2tex.pl $algebra"; 00111 } else { 00112 $cmd = "cd $CFG->dirroot/filter/algebra; ./algebra2tex.pl x/2"; 00113 $test = `$cmd`; 00114 if ($test != '\frac{x}{2}') { 00115 echo "There is a problem with either Perl or the script algebra2tex.pl<br/>"; 00116 $ecmd = $cmd . " 2>&1"; 00117 echo `$ecmd` . "<br/>\n"; 00118 echo "The shell command<br/>$cmd<br/>returned status = $status<br/>\n"; 00119 $commandpath = "$CFG->dirroot/filter/algebra/algebra2tex.pl"; 00120 if (file_exists($commandpath)) { 00121 echo "The file permissions of algebra2tex.pl are: " . decoct(fileperms($commandpath)) . "<br/>"; 00122 } 00123 die; 00124 } 00125 $cmd = "cd $CFG->dirroot/filter/algebra; ./algebra2tex.pl $algebra"; 00126 } 00127 $texexp = `$cmd`; 00128 return $texexp; 00129 } 00130 00131 function refineTeX($texexp) { 00132 $texexp = str_replace('zeroplace','',$texexp); 00133 $texexp = str_replace('#','\not= ',$texexp); 00134 $texexp = str_replace('%','\leq ',$texexp); 00135 $texexp = str_replace('!','\geq ',$texexp); 00136 $texexp = str_replace('\left{','{',$texexp); 00137 $texexp = str_replace('\right}','}',$texexp); 00138 $texexp = str_replace('\fun',' ',$texexp); 00139 $texexp = str_replace('infty','\infty',$texexp); 00140 $texexp = str_replace('alpha','\alpha',$texexp); 00141 $texexp = str_replace('gamma','\gamma',$texexp); 00142 $texexp = str_replace('iota','\iota',$texexp); 00143 $texexp = str_replace('kappa','\kappa',$texexp); 00144 $texexp = str_replace('lambda','\lambda',$texexp); 00145 $texexp = str_replace('mu','\mu',$texexp); 00146 $texexp = str_replace('nu','\nu',$texexp); 00147 $texexp = str_replace('xi','\xi',$texexp); 00148 $texexp = str_replace('rho','\rho',$texexp); 00149 $texexp = str_replace('sigma','\sigma',$texexp); 00150 $texexp = str_replace('tau','\tau',$texexp); 00151 $texexp = str_replace('phi','\phi',$texexp); 00152 $texexp = str_replace('chi','\chi',$texexp); 00153 $texexp = str_replace('psi','\psi',$texexp); 00154 $texexp = str_replace('omega','\omega',$texexp); 00155 $texexp = str_replace('zdelta','\delta',$texexp); 00156 $texexp = str_replace('bita','\beta',$texexp); 00157 $texexp = str_replace('thita','\theta',$texexp); 00158 $texexp = str_replace('zita','\zeta',$texexp); 00159 $texexp = str_replace('xeta','\eta',$texexp); 00160 $texexp = str_replace('zepslon','\epsilon',$texexp); 00161 $texexp = str_replace('zupslon','\upsilon',$texexp); 00162 $texexp = str_replace('\mbox{logten}','\mbox{log}_{10}',$texexp); 00163 $texexp = str_replace('\mbox{acos}','\mbox{cos}^{-1}',$texexp); 00164 $texexp = str_replace('\mbox{asin}','\mbox{sin}^{-1}',$texexp); 00165 $texexp = str_replace('\mbox{atan}','\mbox{tan}^{-1}',$texexp); 00166 $texexp = str_replace('\mbox{asec}','\mbox{sec}^{-1}',$texexp); 00167 $texexp = str_replace('\mbox{acsc}','\mbox{csc}^{-1}',$texexp); 00168 $texexp = str_replace('\mbox{acot}','\mbox{cot}^{-1}',$texexp); 00169 $texexp = str_replace('\mbox{acosh}','\mbox{cosh}^{-1}',$texexp); 00170 $texexp = str_replace('\mbox{asinh}','\mbox{sinh}^{-1}',$texexp); 00171 $texexp = str_replace('\mbox{atanh}','\mbox{tanh}^{-1}',$texexp); 00172 $texexp = str_replace('\mbox{asech}','\mbox{sech}^{-1}',$texexp); 00173 $texexp = str_replace('\mbox{acsch}','\mbox{csch}^{-1}',$texexp); 00174 $texexp = str_replace('\mbox{acoth}','\mbox{coth}^{-1}',$texexp); 00175 $texexp = preg_replace('/\\\sqrt{(.+?),(.+?)}/s','\sqrt['. "\$2]{\$1}",$texexp); 00176 $texexp = preg_replace('/\\\mbox{abs}\\\left\((.+?)\\\right\)/s',"|\$1|",$texexp); 00177 $texexp = preg_replace('/\\\log\\\left\((.+?),(.+?)\\\right\)/s','\log_{'. "\$2}\\left(\$1\\right)",$texexp); 00178 $texexp = preg_replace('/(\\\cos|\\\sin|\\\tan|\\\sec|\\\csc|\\\cot)([h]*)\\\left\((.+?),(.+?)\\\right\)/s',"\$1\$2^{". "\$4}\\left(\$3\\right)",$texexp); 00179 $texexp = preg_replace('/\\\int\\\left\((.+?),(.+?),(.+?)\\\right\)/s','\int_'. "{\$2}^{\$3}\$1 ",$texexp); 00180 $texexp = preg_replace('/\\\int\\\left\((.+?d[a-z])\\\right\)/s','\int '. "\$1 ",$texexp); 00181 $texexp = preg_replace('/\\\lim\\\left\((.+?),(.+?),(.+?)\\\right\)/s','\lim_'. "{\$2\\to \$3}\$1 ",$texexp); 00182 return $texexp; 00183 } 00184 00185 function outputText($texexp) { 00186 header("Content-type: text/html; charset=utf-8"); 00187 echo "<html><body><pre>\n"; 00188 if ($texexp) { 00189 $texexp = str_replace('<','<',$texexp); 00190 $texexp = str_replace('>','>',$texexp); 00191 $texexp = str_replace('"','"',$texexp); 00192 echo "$texexp\n\n"; 00193 } else { 00194 echo "No text output available\n\n"; 00195 } 00196 echo "</pre></body></html>\n"; 00197 } 00198 00199 function tex2image($texexp, $md5, $return=false) { 00200 global $CFG; 00201 00202 if (!$texexp) { 00203 echo 'No tex expresion specified'; 00204 return; 00205 } 00206 00207 $texexp = '\Large ' . $texexp; 00208 $image = $md5 . ".gif"; 00209 $filetype = 'image/gif'; 00210 if (!file_exists("$CFG->dataroot/filter/algebra")) { 00211 make_upload_directory("filter/algebra"); 00212 } 00213 $pathname = "$CFG->dataroot/filter/algebra/$image"; 00214 if (file_exists($pathname)) { 00215 unlink($pathname); 00216 } 00217 $commandpath = filter_tex_get_executable(true); 00218 $cmd = filter_tex_get_cmd($pathname, $texexp); 00219 system($cmd, $status); 00220 00221 if ($return) { 00222 return $image; 00223 } 00224 if (file_exists($pathname)) { 00225 send_file($pathname, $image); 00226 00227 } else { 00228 $ecmd = "$cmd 2>&1"; 00229 echo `$ecmd` . "<br />\n"; 00230 echo "The shell command<br />$cmd<br />returned status = $status<br />\n"; 00231 if ($status == 4) { 00232 echo "Status corresponds to illegal instruction<br />\n"; 00233 } else if ($status == 11) { 00234 echo "Status corresponds to bus error<br />\n"; 00235 } else if ($status == 22) { 00236 echo "Status corresponds to abnormal termination<br />\n"; 00237 } 00238 if (file_exists($commandpath)) { 00239 echo "File size of mimetex executable $commandpath is " . filesize($commandpath) . "<br />"; 00240 echo "The file permissions are: " . decoct(fileperms($commandpath)) . "<br />"; 00241 if (function_exists("md5_file")) { 00242 echo "The md5 checksum of the file is " . md5_file($commandpath) . "<br />"; 00243 } else { 00244 $handle = fopen($commandpath,"rb"); 00245 $contents = fread($handle,16384); 00246 fclose($handle); 00247 echo "The md5 checksum of the first 16384 bytes is " . md5($contents) . "<br />"; 00248 } 00249 } else { 00250 echo "mimetex executable $commandpath not found!<br />"; 00251 } 00252 echo "Image not found!"; 00253 } 00254 } 00255 00256 function slasharguments($texexp, $md5) { 00257 global $CFG; 00258 $admin = $CFG->wwwroot.'/'.$CFG->admin.'/settings.php?section=http'; 00259 $image = tex2image($texexp,$md5,true); 00260 echo "<p>If the following image displays correctly, set your "; 00261 echo "<a href=\"$admin\" target=\"_blank\">Administration->Server->HTTP</a> "; 00262 echo "setting for slasharguments to file.php/1/pic.jpg: "; 00263 echo "<img src=\"pix.php/$image\" align=\"absmiddle\"></p>\n"; 00264 echo "<p>Otherwise set it to file.php?file=/1/pic.jpg "; 00265 echo "It should display correctly as "; 00266 echo "<img src=\"pix.php?file=$image\" align=\"absmiddle\"></p>\n"; 00267 echo "<p>If neither equation image displays correctly, please seek "; 00268 echo "further help at moodle.org at the "; 00269 echo "<a href=\"http://moodle.org/mod/forum/view.php?id=752&loginguest=true\" target=\"_blank\">"; 00270 echo "Mathematics Tools Forum</a></p>"; 00271 } 00272 00273 ?> 00274 00275 <html> 00276 <head><title>Algebra Filter Debugger</title></head> 00277 <body> 00278 <p>Please enter an algebraic expression <b>without</b> any surrounding @@ into 00279 the text box below. (Click <a href="#help">here for help.</a>) 00280 <form action="algebradebug.php" method="get" 00281 target="inlineframe"> 00282 <center> 00283 <input type="text" name="algebra" size="50" 00284 value="sin(z)/(x^2+y^2)" /> 00285 </center> 00286 <ol> 00287 <li>First click on this button <input type="submit" name="ShowDB" value="Show DB Entry" /> 00288 to see the cache_filters database entry for this expression.</li> 00289 <li>If the database entry looks corrupt, click on this button to delete it: 00290 <input type="submit" name="DeleteDB" value="Delete DB Entry" /></li> 00291 <li>Now click on this button <input type="submit" name="TeXStage1" value="First Stage Tex Translation" />. 00292 A preliminary translation into TeX will appear in the box below.</li> 00293 <li>Next click on this button <input type="submit" name="TeXStage2" value="Second Stage Tex Translation" />. 00294 A more refined translation into TeX will appear in the box below.</li> 00295 <li>Then click on this button <input type="submit" name="ShowImage" value="Show Image" /> 00296 to show a graphic image of the algebraic expression.</li> 00297 <li>Finally check your slash arguments setting 00298 <input type="submit" name="SlashArguments" value="Check Slash Arguments" /></li> 00299 </ol> 00300 </form> <br /> <br /> 00301 <center> 00302 <iframe name="inlineframe" align="middle" width="80%" height="200"> 00303 <p>Something is wrong...</p> 00304 </iframe> 00305 </center> <br /> 00306 <hr /> 00307 <a name="help"> 00308 <h2>Debugging Help</h2> 00309 </a> 00310 <p>First here is a brief overview on how the algebra filter works. It 00311 takes an algebra expression and first translates it into TeX. It first 00312 looks for the TeX translation in the Moodle database in the table cache_filters 00313 in the field rawtext. If not found, it passes the algebraic expression to the 00314 Perl script algebra2tex.pl, which also uses the Perl library AlgParser.pm. 00315 It then saves the TeX translation in the database for subsequent uses and 00316 passes the TeX to the mimetex executable to be converted to a gif image. 00317 Here are a few common things that can go wrong and some suggestions on how 00318 you might try to fix them.</p> 00319 <ol> 00320 <li>Something had gone wrong on a previous occasion when the filter tried to 00321 translate this expression. Then the database entry for that expression contains 00322 a bad TeX translation in the rawtext field (usually blank). You can fix this 00323 by clicking on "Delete DB Entry"</li> 00324 <li>The First Stage TeX Translation gives a "No text output available" 00325 message. If your server is running Windows, this may be due to the fact that 00326 you haven't installed Perl or didn't install it correctly. If your server is 00327 running some version of Unix (e.g. Linux), then this may be due to your Perl 00328 binary being installed in a nonstandard location. To fix this edit the first 00329 line of the algebra2tex.pl script. Another possible problem which may affect 00330 both Unix and Windows servers is that the web server doesn't have execute permission 00331 on the algebra2tex.pl script. In that case change permissions accordingly</li> 00332 <li>The Second Stage TeX Translation produces malformed TeX. This indicates 00333 a bug in the algebra filter. Post the original algebraic expression and the 00334 bad TeX translation in the <a href="http://moodle.org/mod/forum/view.php?id=752"> 00335 Mathematics Tools</a> forum in the Using Moodle course on moodle.org.</li> 00336 <li>The TeX to gif image conversion process does not work. If your server is 00337 running Unix, a likely cause is that the mimetex binary you are using is 00338 incompatible with your operating system. You can try compiling it from the 00339 C sources downloaded from <a href="http://www.forkosh.com/mimetex.zip"> 00340 http://www.forkosh.com/mimetex.zip</a>, or looking for an appropriate 00341 binary at <a href="http://moodle.org/download/mimetex/"> 00342 http://moodle.org/download/mimetex/</a>. You may then also need to 00343 edit your moodle/filter/algebra/pix.php file to add 00344 <br /><?PHP echo "case "" . PHP_OS . "":" ;?><br ?> to the list of operating systems 00345 in the switch (PHP_OS) statement. Windows users may have a problem properly 00346 unzipping mimetex.exe. Make sure that mimetex.exe is is <b>PRECISELY</b> 00347 433152 bytes in size. If not, download fresh copy from 00348 <a href="http://moodle.org/download/mimetex/windows/mimetex.exe"> 00349 http://moodle.org/download/mimetex/windows/mimetex.exe</a>. Lastly check 00350 the execute permissions on your mimetex binary, as outlined in item 2 above.</li> 00351 </ol> 00352 </body> 00353 </html>