MDL-48846 filter: Stop reffering to invalid moodle.org/download/mimetex
[moodle.git] / filter / algebra / algebradebug.php
CommitLineData
e25b7ded 1<?php
9b361d24 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
9b361d24 6 require_once("../../config.php");
7
0662bd67 8 if (!filter_is_enabled('algebra')) {
e25b7ded 9 print_error('filternotenabled');
33851b5c 10 }
9b361d24 11
220a90c5 12 require_once($CFG->libdir.'/filelib.php');
13 require_once($CFG->dirroot.'/filter/tex/lib.php');
9b361d24 14
ece1e519
DT
15 $action = optional_param('action', '', PARAM_ALPHANUM);
16 $algebra = optional_param('algebra', '', PARAM_RAW);
17
5e6d46cb 18 require_login();
6ca657a7 19 require_capability('moodle/site:config', context_system::instance());
ece1e519
DT
20 if ($action || $algebra) {
21 require_sesskey();
22 }
5e6d46cb 23
ece1e519 24 if ($algebra && $action) {
9b361d24 25 $md5 = md5($algebra);
ece1e519 26 if ($action == 'ShowDB' || $action == 'DeleteDB') {
219f652b 27 $texcache = $DB->get_record("cache_filters", array("filter"=>"algebra", "md5key"=>$md5));
9b361d24 28 }
ece1e519 29 if ($action == 'ShowDB') {
9b361d24 30 if ($texcache) {
894ff63f 31 $output = "DB cache_filters entry for $algebra\n";
9b361d24 32 $output .= "id = $texcache->id\n";
33 $output .= "filter = $texcache->filter\n";
34 $output .= "version = $texcache->version\n";
35 $output .= "md5key = $texcache->md5key\n";
36 $output .= "rawtext = $texcache->rawtext\n";
37 $output .= "timemodified = $texcache->timemodified\n";
38 } else {
39 $output = "DB cache_filters entry for $algebra not found\n";
40 }
41 }
ece1e519 42 if ($action == 'DeleteDB') {
9b361d24 43 if ($texcache) {
44 $output = "Deleting DB cache_filters entry for $algebra\n";
8618fd2a 45 $result = $DB->delete_records("cache_filters", array("id"=>$texcache->id));
9b361d24 46 if ($result) {
894ff63f 47 $result = 1;
9b361d24 48 } else {
49 $result = 0;
50 }
51 $output .= "Number of records deleted = $result\n";
52 } else {
53 $output = "Could not delete DB cache_filters entry for $algebra\nbecause it could not be found.\n";
54 }
55 }
ece1e519 56 if ($action == 'TeXStage1') {
894ff63f 57 $output = algebra2tex($algebra);
9b361d24 58 }
ece1e519 59 if ($action == 'TexStage2') {
894ff63f 60 $output = algebra2tex($algebra);
9b361d24 61 $output = refineTeX($output);
62 }
ece1e519 63 if ($action == 'ShowImage'|| $action == 'SlashArguments') {
894ff63f 64 $output = algebra2tex($algebra);
9b361d24 65 $output = refineTeX($output);
ece1e519 66 if ($action == 'ShowImage') {
894ff63f 67 tex2image($output, $md5);
380e47f5 68 } else {
69 slasharguments($output, $md5);
70 }
220a90c5 71 } else {
9b361d24 72 outputText($output);
73 }
74 exit;
75 }
76
77function algebra2tex($algebra) {
10ccb7a7 78 global $CFG;
9b361d24 79 $algebra = str_replace('&lt;','<',$algebra);
80 $algebra = str_replace('&gt;','>',$algebra);
81 $algebra = str_replace('<>','#',$algebra);
82 $algebra = str_replace('<=','%',$algebra);
83 $algebra = str_replace('>=','!',$algebra);
1b71c53e 84 $algebra = preg_replace('/([=><%!#] *)-/',"\$1 zeroplace -",$algebra);
e86a754b 85 $algebra = str_replace('delta','zdelta',$algebra);
86 $algebra = str_replace('beta','bita',$algebra);
87 $algebra = str_replace('theta','thita',$algebra);
88 $algebra = str_replace('zeta','zita',$algebra);
89 $algebra = str_replace('eta','xeta',$algebra);
90 $algebra = str_replace('epsilon','zepslon',$algebra);
91 $algebra = str_replace('upsilon','zupslon',$algebra);
9b361d24 92 $algebra = preg_replace('!\r\n?!',' ',$algebra);
10ccb7a7 93 $algebra = escapeshellarg($algebra);
e86a754b 94
9b361d24 95 if ( (PHP_OS == "WINNT") || (PHP_OS == "WIN32") || (PHP_OS == "Windows") ) {
220a90c5 96 $cmd = "cd $CFG->dirroot\\filter\\algebra & algebra2tex.pl x/2";
d7e3f044 97 $test = `$cmd`;
98 if ($test != '\frac{x}{2}') {
71502268 99 echo "There is a problem with either Perl or the script algebra2tex.pl<br/>";
d7e3f044 100 $ecmd = $cmd . " 2>&1";
71502268 101 echo `$ecmd` . "<br/>\n";
102 echo "The shell command<br/>$cmd<br/>returned status = $status<br/>\n";
220a90c5 103 $commandpath = "$CFG->dirroot\\filter\\algebra\\algebra2tex.pl";
d7e3f044 104 if (file_exists($commandpath)) {
71502268 105 echo "The file permissions of algebra2tex.pl are: " . decoct(fileperms($commandpath)) . "<br/>";
d7e3f044 106 }
107 die;
108 }
220a90c5 109 $cmd = "cd $CFG->dirroot\\filter\\algebra & algebra2tex.pl $algebra";
110 } else {
111 $cmd = "cd $CFG->dirroot/filter/algebra; ./algebra2tex.pl x/2";
d7e3f044 112 $test = `$cmd`;
113 if ($test != '\frac{x}{2}') {
71502268 114 echo "There is a problem with either Perl or the script algebra2tex.pl<br/>";
d7e3f044 115 $ecmd = $cmd . " 2>&1";
71502268 116 echo `$ecmd` . "<br/>\n";
117 echo "The shell command<br/>$cmd<br/>returned status = $status<br/>\n";
220a90c5 118 $commandpath = "$CFG->dirroot/filter/algebra/algebra2tex.pl";
d7e3f044 119 if (file_exists($commandpath)) {
71502268 120 echo "The file permissions of algebra2tex.pl are: " . decoct(fileperms($commandpath)) . "<br/>";
d7e3f044 121 }
122 die;
123 }
220a90c5 124 $cmd = "cd $CFG->dirroot/filter/algebra; ./algebra2tex.pl $algebra";
9b361d24 125 }
126 $texexp = `$cmd`;
127 return $texexp;
128}
129
130function refineTeX($texexp) {
1b71c53e 131 $texexp = str_replace('zeroplace','',$texexp);
9b361d24 132 $texexp = str_replace('#','\not= ',$texexp);
133 $texexp = str_replace('%','\leq ',$texexp);
134 $texexp = str_replace('!','\geq ',$texexp);
135 $texexp = str_replace('\left{','{',$texexp);
136 $texexp = str_replace('\right}','}',$texexp);
137 $texexp = str_replace('\fun',' ',$texexp);
138 $texexp = str_replace('infty','\infty',$texexp);
e86a754b 139 $texexp = str_replace('alpha','\alpha',$texexp);
140 $texexp = str_replace('gamma','\gamma',$texexp);
141 $texexp = str_replace('iota','\iota',$texexp);
142 $texexp = str_replace('kappa','\kappa',$texexp);
143 $texexp = str_replace('lambda','\lambda',$texexp);
144 $texexp = str_replace('mu','\mu',$texexp);
145 $texexp = str_replace('nu','\nu',$texexp);
146 $texexp = str_replace('xi','\xi',$texexp);
147 $texexp = str_replace('rho','\rho',$texexp);
148 $texexp = str_replace('sigma','\sigma',$texexp);
149 $texexp = str_replace('tau','\tau',$texexp);
150 $texexp = str_replace('phi','\phi',$texexp);
151 $texexp = str_replace('chi','\chi',$texexp);
152 $texexp = str_replace('psi','\psi',$texexp);
153 $texexp = str_replace('omega','\omega',$texexp);
154 $texexp = str_replace('zdelta','\delta',$texexp);
155 $texexp = str_replace('bita','\beta',$texexp);
156 $texexp = str_replace('thita','\theta',$texexp);
157 $texexp = str_replace('zita','\zeta',$texexp);
158 $texexp = str_replace('xeta','\eta',$texexp);
159 $texexp = str_replace('zepslon','\epsilon',$texexp);
160 $texexp = str_replace('zupslon','\upsilon',$texexp);
9b361d24 161 $texexp = str_replace('\mbox{logten}','\mbox{log}_{10}',$texexp);
162 $texexp = str_replace('\mbox{acos}','\mbox{cos}^{-1}',$texexp);
163 $texexp = str_replace('\mbox{asin}','\mbox{sin}^{-1}',$texexp);
164 $texexp = str_replace('\mbox{atan}','\mbox{tan}^{-1}',$texexp);
165 $texexp = str_replace('\mbox{asec}','\mbox{sec}^{-1}',$texexp);
166 $texexp = str_replace('\mbox{acsc}','\mbox{csc}^{-1}',$texexp);
167 $texexp = str_replace('\mbox{acot}','\mbox{cot}^{-1}',$texexp);
168 $texexp = str_replace('\mbox{acosh}','\mbox{cosh}^{-1}',$texexp);
169 $texexp = str_replace('\mbox{asinh}','\mbox{sinh}^{-1}',$texexp);
170 $texexp = str_replace('\mbox{atanh}','\mbox{tanh}^{-1}',$texexp);
171 $texexp = str_replace('\mbox{asech}','\mbox{sech}^{-1}',$texexp);
172 $texexp = str_replace('\mbox{acsch}','\mbox{csch}^{-1}',$texexp);
173 $texexp = str_replace('\mbox{acoth}','\mbox{coth}^{-1}',$texexp);
174 $texexp = preg_replace('/\\\sqrt{(.+?),(.+?)}/s','\sqrt['. "\$2]{\$1}",$texexp);
175 $texexp = preg_replace('/\\\mbox{abs}\\\left\((.+?)\\\right\)/s',"|\$1|",$texexp);
176 $texexp = preg_replace('/\\\log\\\left\((.+?),(.+?)\\\right\)/s','\log_{'. "\$2}\\left(\$1\\right)",$texexp);
177 $texexp = preg_replace('/(\\\cos|\\\sin|\\\tan|\\\sec|\\\csc|\\\cot)([h]*)\\\left\((.+?),(.+?)\\\right\)/s',"\$1\$2^{". "\$4}\\left(\$3\\right)",$texexp);
178 $texexp = preg_replace('/\\\int\\\left\((.+?),(.+?),(.+?)\\\right\)/s','\int_'. "{\$2}^{\$3}\$1 ",$texexp);
179 $texexp = preg_replace('/\\\int\\\left\((.+?d[a-z])\\\right\)/s','\int '. "\$1 ",$texexp);
180 $texexp = preg_replace('/\\\lim\\\left\((.+?),(.+?),(.+?)\\\right\)/s','\lim_'. "{\$2\\to \$3}\$1 ",$texexp);
181 return $texexp;
182}
183
184function outputText($texexp) {
8a7703ce 185 header("Content-type: text/html; charset=utf-8");
9b361d24 186 echo "<html><body><pre>\n";
187 if ($texexp) {
188 $texexp = str_replace('<','&lt;',$texexp);
189 $texexp = str_replace('>','&gt;',$texexp);
190 $texexp = str_replace('"','&quot;',$texexp);
191 echo "$texexp\n\n";
192 } else {
193 echo "No text output available\n\n";
194 }
195 echo "</pre></body></html>\n";
196}
197
380e47f5 198function tex2image($texexp, $md5, $return=false) {
10ccb7a7 199 global $CFG;
ddea4cf0 200
220a90c5 201 if (!$texexp) {
202 echo 'No tex expresion specified';
203 return;
204 }
205
206 $texexp = '\Large ' . $texexp;
207 $image = $md5 . ".gif";
208 $filetype = 'image/gif';
209 if (!file_exists("$CFG->dataroot/filter/algebra")) {
210 make_upload_directory("filter/algebra");
211 }
212 $pathname = "$CFG->dataroot/filter/algebra/$image";
213 if (file_exists($pathname)) {
214 unlink($pathname);
10ccb7a7 215 }
35716b86
PS
216 $commandpath = filter_tex_get_executable(true);
217 $cmd = filter_tex_get_cmd($pathname, $texexp);
220a90c5 218 system($cmd, $status);
219
10ccb7a7 220 if ($return) {
221 return $image;
222 }
220a90c5 223 if (file_exists($pathname)) {
224 send_file($pathname, $image);
225
10ccb7a7 226 } else {
220a90c5 227 $ecmd = "$cmd 2>&1";
228 echo `$ecmd` . "<br />\n";
229 echo "The shell command<br />$cmd<br />returned status = $status<br />\n";
230 if ($status == 4) {
231 echo "Status corresponds to illegal instruction<br />\n";
232 } else if ($status == 11) {
233 echo "Status corresponds to bus error<br />\n";
234 } else if ($status == 22) {
235 echo "Status corresponds to abnormal termination<br />\n";
236 }
237 if (file_exists($commandpath)) {
238 echo "File size of mimetex executable $commandpath is " . filesize($commandpath) . "<br />";
239 echo "The file permissions are: " . decoct(fileperms($commandpath)) . "<br />";
240 if (function_exists("md5_file")) {
9a58f7cb 241 echo "The md5 checksum of the file is " . md5_file($commandpath) . "<br />";
220a90c5 242 } else {
e072c85e 243 $handle = fopen($commandpath,"rb");
244 $contents = fread($handle,16384);
245 fclose($handle);
9a58f7cb 246 echo "The md5 checksum of the first 16384 bytes is " . md5($contents) . "<br />";
220a90c5 247 }
248 } else {
249 echo "mimetex executable $commandpath not found!<br />";
250 }
251 echo "Image not found!";
10ccb7a7 252 }
9b361d24 253}
380e47f5 254
255function slasharguments($texexp, $md5) {
256 global $CFG;
220a90c5 257 $admin = $CFG->wwwroot.'/'.$CFG->admin.'/settings.php?section=http';
380e47f5 258 $image = tex2image($texexp,$md5,true);
259 echo "<p>If the following image displays correctly, set your ";
220a90c5 260 echo "<a href=\"$admin\" target=\"_blank\">Administration->Server->HTTP</a> ";
6544f8d8 261 echo "setting for slasharguments to file.php/1/pic.jpg: ";
380e47f5 262 echo "<img src=\"pix.php/$image\" align=\"absmiddle\"></p>\n";
6544f8d8 263 echo "<p>Otherwise set it to file.php?file=/1/pic.jpg ";
380e47f5 264 echo "It should display correctly as ";
265 echo "<img src=\"pix.php?file=$image\" align=\"absmiddle\"></p>\n";
266 echo "<p>If neither equation image displays correctly, please seek ";
267 echo "further help at moodle.org at the ";
220a90c5 268 echo "<a href=\"http://moodle.org/mod/forum/view.php?id=752&loginguest=true\" target=\"_blank\">";
380e47f5 269 echo "Mathematics Tools Forum</a></p>";
270}
271
9b361d24 272?>
273
274<html>
275<head><title>Algebra Filter Debugger</title></head>
276<body>
3211569a
SH
277 <p>Please enter an algebraic expression <b>without</b> any surrounding @@ into
278 the text box below. (Click <a href="#help">here for help.</a>)
9b361d24 279 <form action="algebradebug.php" method="get"
280 target="inlineframe">
281 <center>
0013540d
RW
282 <label for="algebra" class="accesshide"><?php print_string('algebraicexpression', 'filter_algebra'); ?></label>
283 <input type="text" id="algebra" name="algebra" size="50"
60af2703 284 value="sin(z)/(x^2+y^2)" />
9b361d24 285 </center>
286 <ol>
ece1e519 287 <li>First click on this button <button type="submit" name="action" value="ShowDB">Show DB Entry</button>
9b361d24 288 to see the cache_filters database entry for this expression.</li>
894ff63f 289 <li>If the database entry looks corrupt, click on this button to delete it:
ece1e519
DT
290 <button type="submit" name="action" value="DeleteDB">Delete DB Entry</button></li>
291 <li>Now click on this button <button type="submit" name="action" value="TeXStage1">First Stage Tex Translation</button>.
9b361d24 292 A preliminary translation into TeX will appear in the box below.</li>
ece1e519 293 <li>Next click on this button <button type="submit" name="action" value="TexStage2">Second Stage Tex Translation</button>.
9b361d24 294 A more refined translation into TeX will appear in the box below.</li>
ece1e519 295 <li>Then click on this button <button type="submit" name="action" value="ShowImage">Show Image</button>
9b361d24 296 to show a graphic image of the algebraic expression.</li>
380e47f5 297 <li>Finally check your slash arguments setting
ece1e519 298 <button type="submit" name="action" value="SlashArguments">Check Slash Arguments</button></li>
9b361d24 299 </ol>
ece1e519 300 <input type="hidden" name="sesskey" value="<?php echo sesskey(); ?>" />
9a58f7cb 301 </form> <br /> <br />
9b361d24 302 <center>
303 <iframe name="inlineframe" align="middle" width="80%" height="200">
4317f92f 304 &lt;p&gt;Something is wrong...&lt;/p&gt;
9b361d24 305 </iframe>
9a58f7cb 306 </center> <br />
307<hr />
9b361d24 308<a name="help">
309<h2>Debugging Help</h2>
310</a>
311<p>First here is a brief overview on how the algebra filter works. It
312takes an algebra expression and first translates it into TeX. It first
313looks for the TeX translation in the Moodle database in the table cache_filters
314in the field rawtext. If not found, it passes the algebraic expression to the
315Perl script algebra2tex.pl, which also uses the Perl library AlgParser.pm.
316It then saves the TeX translation in the database for subsequent uses and
317passes the TeX to the mimetex executable to be converted to a gif image.
318Here are a few common things that can go wrong and some suggestions on how
319you might try to fix them.</p>
320<ol>
321<li>Something had gone wrong on a previous occasion when the filter tried to
322translate this expression. Then the database entry for that expression contains
323a bad TeX translation in the rawtext field (usually blank). You can fix this
324by clicking on &quot;Delete DB Entry&quot;</li>
325<li>The First Stage TeX Translation gives a &quot;No text output available&quot;
326message. If your server is running Windows, this may be due to the fact that
327you haven't installed Perl or didn't install it correctly. If your server is
328running some version of Unix (e.g. Linux), then this may be due to your Perl
329binary being installed in a nonstandard location. To fix this edit the first
330line of the algebra2tex.pl script. Another possible problem which may affect
331both Unix and Windows servers is that the web server doesn't have execute permission
332on the algebra2tex.pl script. In that case change permissions accordingly</li>
333<li>The Second Stage TeX Translation produces malformed TeX. This indicates
334a bug in the algebra filter. Post the original algebraic expression and the
335bad TeX translation in the <a href="http://moodle.org/mod/forum/view.php?id=752">
336Mathematics Tools</a> forum in the Using Moodle course on moodle.org.</li>
337<li>The TeX to gif image conversion process does not work. If your server is
338running Unix, a likely cause is that the mimetex binary you are using is
339incompatible with your operating system. You can try compiling it from the
340C sources downloaded from <a href="http://www.forkosh.com/mimetex.zip">
292df9f6
DP
341http://www.forkosh.com/mimetex.zip</a>. Lastly check the execute permissions
342on your mimetex binary, as outlined in item 2 above.</li>
9b361d24 343</ol>
344</body>
345</html>