Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / filter / algebra / algebradebug.php
blob467a22a62752a77f2b4f6dc321e386af251db258
1 <?PHP // $Id$
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
6 $nomoodlecookie = true; // Because it interferes with caching
8 require_once("../../config.php");
10 if (empty($CFG->textfilters)) {
11 error ('Filter not enabled!');
12 } else {
13 $filters = explode(',', $CFG->textfilters);
14 if (array_search('filter/algebra', $filters) === FALSE) {
15 error ('Filter not enabled!');
19 $CFG->texfilterdir = "filter/tex";
20 $CFG->algebrafilterdir = "filter/algebra";
21 $CFG->algebraimagedir = "filter/algebra";
22 if ( (PHP_OS == "WINNT") || (PHP_OS == "WIN32") || (PHP_OS == "Windows") ) {
23 $CFG->algebrafilterdirwin = "filter\\algebra";
27 $query = urldecode($_SERVER['QUERY_STRING']);
28 error_reporting(E_ALL);
30 if ($query) {
31 $output = $query;
32 $splitpos = strpos($query,'&')-8;
33 $algebra = substr($query,8,$splitpos);
34 $md5 = md5($algebra);
35 if (strpos($query,'ShowDB') || strpos($query,'DeleteDB')) {
36 $texcache = get_record("cache_filters","filter","algebra", "md5key", $md5);
38 if (strpos($query,'ShowDB')) {
39 if ($texcache) {
40 $output = "DB cache_filters entry for $algebra\n";
41 $output .= "id = $texcache->id\n";
42 $output .= "filter = $texcache->filter\n";
43 $output .= "version = $texcache->version\n";
44 $output .= "md5key = $texcache->md5key\n";
45 $output .= "rawtext = $texcache->rawtext\n";
46 $output .= "timemodified = $texcache->timemodified\n";
47 } else {
48 $output = "DB cache_filters entry for $algebra not found\n";
51 if (strpos($query,'DeleteDB')) {
52 if ($texcache) {
53 $output = "Deleting DB cache_filters entry for $algebra\n";
54 $result = delete_records("cache_filters","id",$texcache->id);
55 if ($result) {
56 $result = 1;
57 } else {
58 $result = 0;
60 $output .= "Number of records deleted = $result\n";
61 } else {
62 $output = "Could not delete DB cache_filters entry for $algebra\nbecause it could not be found.\n";
65 if (strpos($query,'TeXStage1')) {
66 $output = algebra2tex($algebra);
68 if (strpos($query,'TeXStage2')) {
69 $output = algebra2tex($algebra);
70 $output = refineTeX($output);
72 if (strpos($query,'ShowImage')||strpos($query,'SlashArguments')) {
73 $output = algebra2tex($algebra);
74 $output = refineTeX($output);
75 if (strpos($query,'ShowImage')) {
76 tex2image($output, $md5);
77 } else {
78 slasharguments($output, $md5);
80 } else {
81 outputText($output);
83 exit;
86 function algebra2tex($algebra) {
87 global $CFG;
88 $algebra = str_replace('&lt;','<',$algebra);
89 $algebra = str_replace('&gt;','>',$algebra);
90 $algebra = str_replace('<>','#',$algebra);
91 $algebra = str_replace('<=','%',$algebra);
92 $algebra = str_replace('>=','!',$algebra);
93 $algebra = preg_replace('/([=><%!#] *)-/',"\$1 zeroplace -",$algebra);
94 $algebra = str_replace('delta','zdelta',$algebra);
95 $algebra = str_replace('beta','bita',$algebra);
96 $algebra = str_replace('theta','thita',$algebra);
97 $algebra = str_replace('zeta','zita',$algebra);
98 $algebra = str_replace('eta','xeta',$algebra);
99 $algebra = str_replace('epsilon','zepslon',$algebra);
100 $algebra = str_replace('upsilon','zupslon',$algebra);
101 $algebra = preg_replace('!\r\n?!',' ',$algebra);
102 $algebra = escapeshellarg($algebra);
104 if ( (PHP_OS == "WINNT") || (PHP_OS == "WIN32") || (PHP_OS == "Windows") ) {
105 $cmd = "cd $CFG->dirroot\\$CFG->algebrafilterdirwin & algebra2tex.pl x/2";
106 $test = `$cmd`;
107 if ($test != '\frac{x}{2}') {
108 echo "There is a problem with either Perl or the script algebra2tex.pl<br/>";
109 $ecmd = $cmd . " 2>&1";
110 echo `$ecmd` . "<br/>\n";
111 echo "The shell command<br/>$cmd<br/>returned status = $status<br/>\n";
112 $commandpath = "$CFG->dirroot\\$CFG->algebrafilterdirwin\\algebra2tex.pl";
113 if (file_exists($commandpath)) {
114 echo "The file permissions of algebra2tex.pl are: " . decoct(fileperms($commandpath)) . "<br/>";
116 die;
118 $cmd = "cd $CFG->dirroot\\$CFG->algebrafilterdirwin & algebra2tex.pl $algebra";
119 } else {
120 $cmd = "cd $CFG->dirroot/$CFG->algebrafilterdir; ./algebra2tex.pl x/2";
121 $test = `$cmd`;
122 if ($test != '\frac{x}{2}') {
123 echo "There is a problem with either Perl or the script algebra2tex.pl<br/>";
124 $ecmd = $cmd . " 2>&1";
125 echo `$ecmd` . "<br/>\n";
126 echo "The shell command<br/>$cmd<br/>returned status = $status<br/>\n";
127 $commandpath = "$CFG->dirroot/$CFG->algebrafilterdir/algebra2tex.pl";
128 if (file_exists($commandpath)) {
129 echo "The file permissions of algebra2tex.pl are: " . decoct(fileperms($commandpath)) . "<br/>";
131 die;
133 $cmd = "cd $CFG->dirroot/$CFG->algebrafilterdir; ./algebra2tex.pl $algebra";
135 $texexp = `$cmd`;
136 return $texexp;
139 function refineTeX($texexp) {
140 $texexp = str_replace('zeroplace','',$texexp);
141 $texexp = str_replace('#','\not= ',$texexp);
142 $texexp = str_replace('%','\leq ',$texexp);
143 $texexp = str_replace('!','\geq ',$texexp);
144 $texexp = str_replace('\left{','{',$texexp);
145 $texexp = str_replace('\right}','}',$texexp);
146 $texexp = str_replace('\fun',' ',$texexp);
147 $texexp = str_replace('infty','\infty',$texexp);
148 $texexp = str_replace('alpha','\alpha',$texexp);
149 $texexp = str_replace('gamma','\gamma',$texexp);
150 $texexp = str_replace('iota','\iota',$texexp);
151 $texexp = str_replace('kappa','\kappa',$texexp);
152 $texexp = str_replace('lambda','\lambda',$texexp);
153 $texexp = str_replace('mu','\mu',$texexp);
154 $texexp = str_replace('nu','\nu',$texexp);
155 $texexp = str_replace('xi','\xi',$texexp);
156 $texexp = str_replace('rho','\rho',$texexp);
157 $texexp = str_replace('sigma','\sigma',$texexp);
158 $texexp = str_replace('tau','\tau',$texexp);
159 $texexp = str_replace('phi','\phi',$texexp);
160 $texexp = str_replace('chi','\chi',$texexp);
161 $texexp = str_replace('psi','\psi',$texexp);
162 $texexp = str_replace('omega','\omega',$texexp);
163 $texexp = str_replace('zdelta','\delta',$texexp);
164 $texexp = str_replace('bita','\beta',$texexp);
165 $texexp = str_replace('thita','\theta',$texexp);
166 $texexp = str_replace('zita','\zeta',$texexp);
167 $texexp = str_replace('xeta','\eta',$texexp);
168 $texexp = str_replace('zepslon','\epsilon',$texexp);
169 $texexp = str_replace('zupslon','\upsilon',$texexp);
170 $texexp = str_replace('\mbox{logten}','\mbox{log}_{10}',$texexp);
171 $texexp = str_replace('\mbox{acos}','\mbox{cos}^{-1}',$texexp);
172 $texexp = str_replace('\mbox{asin}','\mbox{sin}^{-1}',$texexp);
173 $texexp = str_replace('\mbox{atan}','\mbox{tan}^{-1}',$texexp);
174 $texexp = str_replace('\mbox{asec}','\mbox{sec}^{-1}',$texexp);
175 $texexp = str_replace('\mbox{acsc}','\mbox{csc}^{-1}',$texexp);
176 $texexp = str_replace('\mbox{acot}','\mbox{cot}^{-1}',$texexp);
177 $texexp = str_replace('\mbox{acosh}','\mbox{cosh}^{-1}',$texexp);
178 $texexp = str_replace('\mbox{asinh}','\mbox{sinh}^{-1}',$texexp);
179 $texexp = str_replace('\mbox{atanh}','\mbox{tanh}^{-1}',$texexp);
180 $texexp = str_replace('\mbox{asech}','\mbox{sech}^{-1}',$texexp);
181 $texexp = str_replace('\mbox{acsch}','\mbox{csch}^{-1}',$texexp);
182 $texexp = str_replace('\mbox{acoth}','\mbox{coth}^{-1}',$texexp);
183 $texexp = preg_replace('/\\\sqrt{(.+?),(.+?)}/s','\sqrt['. "\$2]{\$1}",$texexp);
184 $texexp = preg_replace('/\\\mbox{abs}\\\left\((.+?)\\\right\)/s',"|\$1|",$texexp);
185 $texexp = preg_replace('/\\\log\\\left\((.+?),(.+?)\\\right\)/s','\log_{'. "\$2}\\left(\$1\\right)",$texexp);
186 $texexp = preg_replace('/(\\\cos|\\\sin|\\\tan|\\\sec|\\\csc|\\\cot)([h]*)\\\left\((.+?),(.+?)\\\right\)/s',"\$1\$2^{". "\$4}\\left(\$3\\right)",$texexp);
187 $texexp = preg_replace('/\\\int\\\left\((.+?),(.+?),(.+?)\\\right\)/s','\int_'. "{\$2}^{\$3}\$1 ",$texexp);
188 $texexp = preg_replace('/\\\int\\\left\((.+?d[a-z])\\\right\)/s','\int '. "\$1 ",$texexp);
189 $texexp = preg_replace('/\\\lim\\\left\((.+?),(.+?),(.+?)\\\right\)/s','\lim_'. "{\$2\\to \$3}\$1 ",$texexp);
190 return $texexp;
193 function outputText($texexp) {
194 header("Content-type: text/html");
195 echo "<html><body><pre>\n";
196 if ($texexp) {
197 $texexp = str_replace('<','&lt;',$texexp);
198 $texexp = str_replace('>','&gt;',$texexp);
199 $texexp = str_replace('"','&quot;',$texexp);
200 echo "$texexp\n\n";
201 } else {
202 echo "No text output available\n\n";
204 echo "</pre></body></html>\n";
207 function tex2image($texexp, $md5, $return=false) {
208 global $CFG;
209 $error_message1 = "Your system is not configured to run mimeTeX. ";
210 $error_message1 .= "You need to download the appropriate<br /> executable ";
211 $error_message1 .= "from <a href=\"http://moodle.org/download/mimetex/\">";
212 $error_message1 .= "http://moodle.org/download/mimetex/</a>, or obtain the ";
213 $error_message1 .= "C source<br /> from <a href=\"http://www.forkosh.com/mimetex.zip\">";
214 $error_message1 .= "http://www.forkosh.com/mimetex.zip</a>, compile it and ";
215 $error_message1 .= "put the executable into your<br /> moodle/filter/tex/ directory. ";
216 $error_message1 .= "You also need to edit your moodle/filter/algebra/pix.php file<br />";
217 $error_message1 .= ' by adding the line<br /><pre> case "' . PHP_OS . "\":\n";
218 $error_message1 .= " \$cmd = \"\\\\\"\$CFG->dirroot/\$CFG->texfilterdir/";
219 $error_message1 .= 'mimetex.' . strtolower(PHP_OS) . "\\\\\" -e \\\\\"\$pathname\\\\\" \". escapeshellarg(\$texexp);";
220 $error_message1 .= "</pre>You also need to add this to your algebradebug.php file.";
222 if ($texexp) {
223 $texexp = '\Large ' . $texexp;
224 $lifetime = 86400;
225 $image = $md5 . ".gif";
226 $filetype = 'image/gif';
227 if (!file_exists("$CFG->dataroot/$CFG->algebraimagedir")) {
228 make_upload_directory($CFG->algebraimagedir);
230 $pathname = "$CFG->dataroot/$CFG->algebraimagedir/$image";
231 if (file_exists($pathname)) {
232 unlink($pathname);
234 $commandpath = "";
235 $cmd = "";
236 $texexp = escapeshellarg($texexp);
237 switch (PHP_OS) {
238 case "Linux":
239 $commandpath="$CFG->dirroot/$CFG->texfilterdir/mimetex.linux";
240 $cmd = "\"$CFG->dirroot/$CFG->texfilterdir/mimetex.linux\" -e \"$pathname\" $texexp";
241 break;
242 case "WINNT":
243 case "WIN32":
244 case "Windows":
245 $commandpath="$CFG->dirroot/$CFG->texfilterdir/mimetex.exe";
246 $cmd = str_replace(' ','^ ',$commandpath);
247 $cmd .= " ++ -e \"$pathname\" $texexp";
248 break;
249 case "Darwin":
250 $commandpath="$CFG->dirroot/$CFG->texfilterdir/mimetex.darwin";
251 $cmd = "\"$CFG->dirroot/$CFG->texfilterdir/mimetex.darwin\" -e \"$pathname\" $texexp";
252 break;
254 if (!$cmd) {
255 if (is_executable("$CFG->dirroot/$CFG->texfilterdir/mimetex")) { /// Use the custom binary
256 $commandpath="$CFG->dirroot/$CFG->texfilterdir/mimetex";
257 $cmd = "$CFG->dirroot/$CFG->texfilterdir/mimetex -e $pathname $texexp";
258 } else {
259 error($error_message1);
262 system($cmd, $status);
264 if ($return) {
265 return $image;
267 if ($texexp && file_exists($pathname)) {
268 $lastmodified = filemtime($pathname);
269 header("Last-Modified: " . gmdate("D, d M Y H:i:s", $lastmodified) . " GMT");
270 header("Expires: " . gmdate("D, d M Y H:i:s", time() + $lifetime) . " GMT");
271 header("Cache-control: max_age = $lifetime"); // a day
272 header("Pragma: ");
273 header("Content-disposition: inline; filename=$image");
274 header("Content-length: ".filesize($pathname));
275 header("Content-type: $filetype");
276 readfile("$pathname");
277 } else {
278 $ecmd = "$cmd 2>&1";
279 echo `$ecmd` . "<br />\n";
280 echo "The shell command<br />$cmd<br />returned status = $status<br />\n";
281 if ($status == 4) {
282 echo "Status corresponds to illegal instruction<br />\n";
283 } else if ($status == 11) {
284 echo "Status corresponds to bus error<br />\n";
285 } else if ($status == 22) {
286 echo "Status corresponds to abnormal termination<br />\n";
288 if (file_exists($commandpath)) {
289 echo "File size of mimetex executable $commandpath is " . filesize($commandpath) . "<br />";
290 echo "The file permissions are: " . decoct(fileperms($commandpath)) . "<br />";
291 if (function_exists("md5_file")) {
292 echo "The md5 checksum of the file is " . md5_file($commandpath) . "<br />";
293 } else {
294 $handle = fopen($commandpath,"rb");
295 $contents = fread($handle,16384);
296 fclose($handle);
297 echo "The md5 checksum of the first 16384 bytes is " . md5($contents) . "<br />";
299 } else {
300 echo "mimetex executable $commandpath not found!<br />";
302 echo "Image not found!";
306 function slasharguments($texexp, $md5) {
307 global $CFG;
308 $admin = $CFG->wwwroot . '/' . $CFG->admin . '/config.php';
309 $image = tex2image($texexp,$md5,true);
310 echo "<p>If the following image displays correctly, set your ";
311 echo "<a href=\"$admin\" target=\"_blank\">Administration->Configuration->Variables</a> ";
312 echo "setting for slasharguments to file.php/1/pic.jpg: ";
313 echo "<img src=\"pix.php/$image\" align=\"absmiddle\"></p>\n";
314 echo "<p>Otherwise set it to file.php?file=/1/pic.jpg ";
315 echo "It should display correctly as ";
316 echo "<img src=\"pix.php?file=$image\" align=\"absmiddle\"></p>\n";
317 echo "<p>If neither equation image displays correctly, please seek ";
318 echo "further help at moodle.org at the ";
319 echo "<a href=\"http://moodle.org/mod/forum/view.php?id=752&username=guest\" target=\"_blank\">";
320 echo "Mathematics Tools Forum</a></p>";
325 <html>
326 <head><title>Algebra Filter Debugger</title></head>
327 <body>
328 <p>Please enter an algebraic expression <b>without</b> any surrounding @@ into
329 the text box below. (Click <a href="#help">here for help.</a>)
330 <form action="algebradebug.php" method="get"
331 target="inlineframe">
332 <center>
333 <input type="text" name="algebra" size="50"
334 value="sin(z)/(x^2+y^2)" />
335 </center>
336 <ol>
337 <li>First click on this button <input type="submit" name="ShowDB" value="Show DB Entry" />
338 to see the cache_filters database entry for this expression.</li>
339 <li>If the database entry looks corrupt, click on this button to delete it:
340 <input type="submit" name="DeleteDB" value="Delete DB Entry" /></li>
341 <li>Now click on this button <input type="submit" name="TeXStage1" value="First Stage Tex Translation" />.
342 A preliminary translation into TeX will appear in the box below.</li>
343 <li>Next click on this button <input type="submit" name="TeXStage2" value="Second Stage Tex Translation" />.
344 A more refined translation into TeX will appear in the box below.</li>
345 <li>Then click on this button <input type="submit" name="ShowImage" value="Show Image" />
346 to show a graphic image of the algebraic expression.</li>
347 <li>Finally check your slash arguments setting
348 <input type="submit" name="SlashArguments" value="Check Slash Arguments" /></li>
349 </ol>
350 </form> <br /> <br />
351 <center>
352 <iframe name="inlineframe" align="middle" width="80%" height="200">
353 &lt;p&gt;Something is wrong...&lt;/p&gt;
354 </iframe>
355 </center> <br />
356 <hr />
357 <a name="help">
358 <h2>Debugging Help</h2>
359 </a>
360 <p>First here is a brief overview on how the algebra filter works. It
361 takes an algebra expression and first translates it into TeX. It first
362 looks for the TeX translation in the Moodle database in the table cache_filters
363 in the field rawtext. If not found, it passes the algebraic expression to the
364 Perl script algebra2tex.pl, which also uses the Perl library AlgParser.pm.
365 It then saves the TeX translation in the database for subsequent uses and
366 passes the TeX to the mimetex executable to be converted to a gif image.
367 Here are a few common things that can go wrong and some suggestions on how
368 you might try to fix them.</p>
369 <ol>
370 <li>Something had gone wrong on a previous occasion when the filter tried to
371 translate this expression. Then the database entry for that expression contains
372 a bad TeX translation in the rawtext field (usually blank). You can fix this
373 by clicking on &quot;Delete DB Entry&quot;</li>
374 <li>The First Stage TeX Translation gives a &quot;No text output available&quot;
375 message. If your server is running Windows, this may be due to the fact that
376 you haven't installed Perl or didn't install it correctly. If your server is
377 running some version of Unix (e.g. Linux), then this may be due to your Perl
378 binary being installed in a nonstandard location. To fix this edit the first
379 line of the algebra2tex.pl script. Another possible problem which may affect
380 both Unix and Windows servers is that the web server doesn't have execute permission
381 on the algebra2tex.pl script. In that case change permissions accordingly</li>
382 <li>The Second Stage TeX Translation produces malformed TeX. This indicates
383 a bug in the algebra filter. Post the original algebraic expression and the
384 bad TeX translation in the <a href="http://moodle.org/mod/forum/view.php?id=752">
385 Mathematics Tools</a> forum in the Using Moodle course on moodle.org.</li>
386 <li>The TeX to gif image conversion process does not work. If your server is
387 running Unix, a likely cause is that the mimetex binary you are using is
388 incompatible with your operating system. You can try compiling it from the
389 C sources downloaded from <a href="http://www.forkosh.com/mimetex.zip">
390 http://www.forkosh.com/mimetex.zip</a>, or looking for an appropriate
391 binary at <a href="http://moodle.org/download/mimetex/">
392 http://moodle.org/download/mimetex/</a>. You may then also need to
393 edit your moodle/filter/algebra/pix.php file to add
394 <br /><?PHP echo "case &quot;" . PHP_OS . "&quot;:" ;?><br ?> to the list of operating systems
395 in the switch (PHP_OS) statement. Windows users may have a problem properly
396 unzipping mimetex.exe. Make sure that mimetex.exe is is <b>PRECISELY</b>
397 433152 bytes in size. If not, download fresh copy from
398 <a href="http://moodle.org/download/mimetex/windows/mimetex.exe">
399 http://moodle.org/download/mimetex/windows/mimetex.exe</a>. Lastly check
400 the execute permissions on your mimetex binary, as outlined in item 2 above.</li>
401 </ol>
402 </body>
403 </html>