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!');
13 $filters = explode(',', $CFG->textfilters
);
14 if (array_search('filter/algebra', $filters) === FALSE) {
15 error ('Filter not enabled!');
19 require_once($CFG->libdir
.'/filelib.php');
20 require_once($CFG->dirroot
.'/filter/tex/lib.php');
22 $query = urldecode($_SERVER['QUERY_STRING']);
23 error_reporting(E_ALL
);
27 $splitpos = strpos($query,'&')-8;
28 $algebra = substr($query,8,$splitpos);
30 if (strpos($query,'ShowDB') ||
strpos($query,'DeleteDB')) {
31 $texcache = get_record("cache_filters","filter","algebra", "md5key", $md5);
33 if (strpos($query,'ShowDB')) {
35 $output = "DB cache_filters entry for $algebra\n";
36 $output .= "id = $texcache->id\n";
37 $output .= "filter = $texcache->filter\n";
38 $output .= "version = $texcache->version\n";
39 $output .= "md5key = $texcache->md5key\n";
40 $output .= "rawtext = $texcache->rawtext\n";
41 $output .= "timemodified = $texcache->timemodified\n";
43 $output = "DB cache_filters entry for $algebra not found\n";
46 if (strpos($query,'DeleteDB')) {
48 $output = "Deleting DB cache_filters entry for $algebra\n";
49 $result = delete_records("cache_filters","id",$texcache->id
);
55 $output .= "Number of records deleted = $result\n";
57 $output = "Could not delete DB cache_filters entry for $algebra\nbecause it could not be found.\n";
60 if (strpos($query,'TeXStage1')) {
61 $output = algebra2tex($algebra);
63 if (strpos($query,'TeXStage2')) {
64 $output = algebra2tex($algebra);
65 $output = refineTeX($output);
67 if (strpos($query,'ShowImage')||
strpos($query,'SlashArguments')) {
68 $output = algebra2tex($algebra);
69 $output = refineTeX($output);
70 if (strpos($query,'ShowImage')) {
71 tex2image($output, $md5);
73 slasharguments($output, $md5);
81 function algebra2tex($algebra) {
83 $algebra = str_replace('<','<',$algebra);
84 $algebra = str_replace('>','>',$algebra);
85 $algebra = str_replace('<>','#',$algebra);
86 $algebra = str_replace('<=','%',$algebra);
87 $algebra = str_replace('>=','!',$algebra);
88 $algebra = preg_replace('/([=><%!#] *)-/',"\$1 zeroplace -",$algebra);
89 $algebra = str_replace('delta','zdelta',$algebra);
90 $algebra = str_replace('beta','bita',$algebra);
91 $algebra = str_replace('theta','thita',$algebra);
92 $algebra = str_replace('zeta','zita',$algebra);
93 $algebra = str_replace('eta','xeta',$algebra);
94 $algebra = str_replace('epsilon','zepslon',$algebra);
95 $algebra = str_replace('upsilon','zupslon',$algebra);
96 $algebra = preg_replace('!\r\n?!',' ',$algebra);
97 $algebra = escapeshellarg($algebra);
99 if ( (PHP_OS
== "WINNT") ||
(PHP_OS
== "WIN32") ||
(PHP_OS
== "Windows") ) {
100 $cmd = "cd $CFG->dirroot\\filter\\algebra & algebra2tex.pl x/2";
102 if ($test != '\frac{x}{2}') {
103 echo "There is a problem with either Perl or the script algebra2tex.pl<br/>";
104 $ecmd = $cmd . " 2>&1";
105 echo `
$ecmd`
. "<br/>\n";
106 echo "The shell command<br/>$cmd<br/>returned status = $status<br/>\n";
107 $commandpath = "$CFG->dirroot\\filter\\algebra\\algebra2tex.pl";
108 if (file_exists($commandpath)) {
109 echo "The file permissions of algebra2tex.pl are: " . decoct(fileperms($commandpath)) . "<br/>";
113 $cmd = "cd $CFG->dirroot\\filter\\algebra & algebra2tex.pl $algebra";
115 $cmd = "cd $CFG->dirroot/filter/algebra; ./algebra2tex.pl x/2";
117 if ($test != '\frac{x}{2}') {
118 echo "There is a problem with either Perl or the script algebra2tex.pl<br/>";
119 $ecmd = $cmd . " 2>&1";
120 echo `
$ecmd`
. "<br/>\n";
121 echo "The shell command<br/>$cmd<br/>returned status = $status<br/>\n";
122 $commandpath = "$CFG->dirroot/filter/algebra/algebra2tex.pl";
123 if (file_exists($commandpath)) {
124 echo "The file permissions of algebra2tex.pl are: " . decoct(fileperms($commandpath)) . "<br/>";
128 $cmd = "cd $CFG->dirroot/filter/algebra; ./algebra2tex.pl $algebra";
134 function refineTeX($texexp) {
135 $texexp = str_replace('zeroplace','',$texexp);
136 $texexp = str_replace('#','\not= ',$texexp);
137 $texexp = str_replace('%','\leq ',$texexp);
138 $texexp = str_replace('!','\geq ',$texexp);
139 $texexp = str_replace('\left{','{',$texexp);
140 $texexp = str_replace('\right}','}',$texexp);
141 $texexp = str_replace('\fun',' ',$texexp);
142 $texexp = str_replace('infty','\infty',$texexp);
143 $texexp = str_replace('alpha','\alpha',$texexp);
144 $texexp = str_replace('gamma','\gamma',$texexp);
145 $texexp = str_replace('iota','\iota',$texexp);
146 $texexp = str_replace('kappa','\kappa',$texexp);
147 $texexp = str_replace('lambda','\lambda',$texexp);
148 $texexp = str_replace('mu','\mu',$texexp);
149 $texexp = str_replace('nu','\nu',$texexp);
150 $texexp = str_replace('xi','\xi',$texexp);
151 $texexp = str_replace('rho','\rho',$texexp);
152 $texexp = str_replace('sigma','\sigma',$texexp);
153 $texexp = str_replace('tau','\tau',$texexp);
154 $texexp = str_replace('phi','\phi',$texexp);
155 $texexp = str_replace('chi','\chi',$texexp);
156 $texexp = str_replace('psi','\psi',$texexp);
157 $texexp = str_replace('omega','\omega',$texexp);
158 $texexp = str_replace('zdelta','\delta',$texexp);
159 $texexp = str_replace('bita','\beta',$texexp);
160 $texexp = str_replace('thita','\theta',$texexp);
161 $texexp = str_replace('zita','\zeta',$texexp);
162 $texexp = str_replace('xeta','\eta',$texexp);
163 $texexp = str_replace('zepslon','\epsilon',$texexp);
164 $texexp = str_replace('zupslon','\upsilon',$texexp);
165 $texexp = str_replace('\mbox{logten}','\mbox{log}_{10}',$texexp);
166 $texexp = str_replace('\mbox{acos}','\mbox{cos}^{-1}',$texexp);
167 $texexp = str_replace('\mbox{asin}','\mbox{sin}^{-1}',$texexp);
168 $texexp = str_replace('\mbox{atan}','\mbox{tan}^{-1}',$texexp);
169 $texexp = str_replace('\mbox{asec}','\mbox{sec}^{-1}',$texexp);
170 $texexp = str_replace('\mbox{acsc}','\mbox{csc}^{-1}',$texexp);
171 $texexp = str_replace('\mbox{acot}','\mbox{cot}^{-1}',$texexp);
172 $texexp = str_replace('\mbox{acosh}','\mbox{cosh}^{-1}',$texexp);
173 $texexp = str_replace('\mbox{asinh}','\mbox{sinh}^{-1}',$texexp);
174 $texexp = str_replace('\mbox{atanh}','\mbox{tanh}^{-1}',$texexp);
175 $texexp = str_replace('\mbox{asech}','\mbox{sech}^{-1}',$texexp);
176 $texexp = str_replace('\mbox{acsch}','\mbox{csch}^{-1}',$texexp);
177 $texexp = str_replace('\mbox{acoth}','\mbox{coth}^{-1}',$texexp);
178 $texexp = preg_replace('/\\\sqrt{(.+?),(.+?)}/s','\sqrt['. "\$2]{\$1}",$texexp);
179 $texexp = preg_replace('/\\\mbox{abs}\\\left\((.+?)\\\right\)/s',"|\$1|",$texexp);
180 $texexp = preg_replace('/\\\log\\\left\((.+?),(.+?)\\\right\)/s','\log_{'. "\$2}\\left(\$1\\right)",$texexp);
181 $texexp = preg_replace('/(\\\cos|\\\sin|\\\tan|\\\sec|\\\csc|\\\cot)([h]*)\\\left\((.+?),(.+?)\\\right\)/s',"\$1\$2^{". "\$4}\\left(\$3\\right)",$texexp);
182 $texexp = preg_replace('/\\\int\\\left\((.+?),(.+?),(.+?)\\\right\)/s','\int_'. "{\$2}^{\$3}\$1 ",$texexp);
183 $texexp = preg_replace('/\\\int\\\left\((.+?d[a-z])\\\right\)/s','\int '. "\$1 ",$texexp);
184 $texexp = preg_replace('/\\\lim\\\left\((.+?),(.+?),(.+?)\\\right\)/s','\lim_'. "{\$2\\to \$3}\$1 ",$texexp);
188 function outputText($texexp) {
189 header("Content-type: text/html");
190 echo "<html><body><pre>\n";
192 $texexp = str_replace('<','<',$texexp);
193 $texexp = str_replace('>','>',$texexp);
194 $texexp = str_replace('"','"',$texexp);
197 echo "No text output available\n\n";
199 echo "</pre></body></html>\n";
202 function tex2image($texexp, $md5, $return=false) {
206 echo 'No tex expresion specified';
210 $texexp = '\Large ' . $texexp;
211 $image = $md5 . ".gif";
212 $filetype = 'image/gif';
213 if (!file_exists("$CFG->dataroot/filter/algebra")) {
214 make_upload_directory("filter/algebra");
216 $pathname = "$CFG->dataroot/filter/algebra/$image";
217 if (file_exists($pathname)) {
220 $commandpath = tex_filter_get_executable(true);
221 $cmd = tex_filter_get_cmd($pathname, $texexp);
222 system($cmd, $status);
227 if (file_exists($pathname)) {
228 send_file($pathname, $image);
232 echo `
$ecmd`
. "<br />\n";
233 echo "The shell command<br />$cmd<br />returned status = $status<br />\n";
235 echo "Status corresponds to illegal instruction<br />\n";
236 } else if ($status == 11) {
237 echo "Status corresponds to bus error<br />\n";
238 } else if ($status == 22) {
239 echo "Status corresponds to abnormal termination<br />\n";
241 if (file_exists($commandpath)) {
242 echo "File size of mimetex executable $commandpath is " . filesize($commandpath) . "<br />";
243 echo "The file permissions are: " . decoct(fileperms($commandpath)) . "<br />";
244 if (function_exists("md5_file")) {
245 echo "The md5 checksum of the file is " . md5_file($commandpath) . "<br />";
247 $handle = fopen($commandpath,"rb");
248 $contents = fread($handle,16384);
250 echo "The md5 checksum of the first 16384 bytes is " . md5($contents) . "<br />";
253 echo "mimetex executable $commandpath not found!<br />";
255 echo "Image not found!";
259 function slasharguments($texexp, $md5) {
261 $admin = $CFG->wwwroot
.'/'.$CFG->admin
.'/settings.php?section=http';
262 $image = tex2image($texexp,$md5,true);
263 echo "<p>If the following image displays correctly, set your ";
264 echo "<a href=\"$admin\" target=\"_blank\">Administration->Server->HTTP</a> ";
265 echo "setting for slasharguments to file.php/1/pic.jpg: ";
266 echo "<img src=\"pix.php/$image\" align=\"absmiddle\"></p>\n";
267 echo "<p>Otherwise set it to file.php?file=/1/pic.jpg ";
268 echo "It should display correctly as ";
269 echo "<img src=\"pix.php?file=$image\" align=\"absmiddle\"></p>\n";
270 echo "<p>If neither equation image displays correctly, please seek ";
271 echo "further help at moodle.org at the ";
272 echo "<a href=\"http://moodle.org/mod/forum/view.php?id=752&loginguest=true\" target=\"_blank\">";
273 echo "Mathematics Tools Forum</a></p>";
279 <head
><title
>Algebra Filter Debugger
</title
></head
>
281 <p
>Please enter an algebraic expression
<b
>without
</b
> any surrounding
@@ into
282 the text box below
. (Click
<a href
="#help">here
for help
.</a
>)
283 <form action
="algebradebug.php" method
="get"
284 target
="inlineframe">
286 <input type
="text" name
="algebra" size
="50"
287 value
="sin(z)/(x^2+y^2)" />
290 <li
>First click on this button
<input type
="submit" name
="ShowDB" value
="Show DB Entry" />
291 to see the cache_filters database entry
for this expression
.</li
>
292 <li
>If the database entry looks corrupt
, click on this button to delete it
:
293 <input type
="submit" name
="DeleteDB" value
="Delete DB Entry" /></li
>
294 <li
>Now click on this button
<input type
="submit" name
="TeXStage1" value
="First Stage Tex Translation" />.
295 A preliminary translation into TeX will appear in the box below
.</li
>
296 <li
>Next click on this button
<input type
="submit" name
="TeXStage2" value
="Second Stage Tex Translation" />.
297 A more refined translation into TeX will appear in the box below
.</li
>
298 <li
>Then click on this button
<input type
="submit" name
="ShowImage" value
="Show Image" />
299 to show a graphic image of the algebraic expression
.</li
>
300 <li
>Finally check your slash arguments setting
301 <input type
="submit" name
="SlashArguments" value
="Check Slash Arguments" /></li
>
303 </form
> <br
/> <br
/>
305 <iframe name
="inlineframe" align
="middle" width
="80%" height
="200">
306 <
;p
>
;Something is wrong
...<
;/p
>
;
311 <h2
>Debugging Help
</h2
>
313 <p
>First here is a brief overview on how the algebra filter works
. It
314 takes an algebra expression
and first translates it into TeX
. It first
315 looks
for the TeX translation in the Moodle database in the table cache_filters
316 in the field rawtext
. If not found
, it passes the algebraic expression to the
317 Perl script algebra2tex
.pl
, which also uses the Perl library AlgParser
.pm
.
318 It then saves the TeX translation in the database
for subsequent uses
and
319 passes the TeX to the mimetex executable to be converted to a gif image
.
320 Here are a few common things that can go wrong
and some suggestions on how
321 you might
try to fix them
.</p
>
323 <li
>Something had gone wrong on a previous occasion when the filter tried to
324 translate this expression
. Then the database entry
for that expression contains
325 a bad TeX translation in the rawtext
field (usually blank
). You can fix this
326 by clicking on
"
;Delete DB Entry
"
;</li
>
327 <li
>The First Stage TeX Translation gives a
"
;No text output available
"
;
328 message
. If your server is running Windows
, this may be due to the fact that
329 you haven
't installed Perl or didn't install it correctly
. If your server is
330 running some version of
Unix (e
.g
. Linux
), then this may be due to your Perl
331 binary being installed in a nonstandard location
. To fix this edit the first
332 line of the algebra2tex
.pl script
. Another possible problem which may affect
333 both Unix
and Windows servers is that the web server doesn
't have execute permission
334 on the algebra2tex.pl script. In that case change permissions accordingly</li>
335 <li>The Second Stage TeX Translation produces malformed TeX. This indicates
336 a bug in the algebra filter. Post the original algebraic expression and the
337 bad TeX translation in the <a href="http://moodle.org/mod/forum/view.php?id=752">
338 Mathematics Tools</a> forum in the Using Moodle course on moodle.org.</li>
339 <li>The TeX to gif image conversion process does not work. If your server is
340 running Unix, a likely cause is that the mimetex binary you are using is
341 incompatible with your operating system. You can try compiling it from the
342 C sources downloaded from <a href="http://www.forkosh.com/mimetex.zip">
343 http://www.forkosh.com/mimetex.zip</a>, or looking for an appropriate
344 binary at <a href="http://moodle.org/download/mimetex/">
345 http://moodle.org/download/mimetex/</a>. You may then also need to
346 edit your moodle/filter/algebra/pix.php file to add
347 <br /><?PHP echo "case "" . PHP_OS . "":" ;?><br ?> to the list of operating systems
348 in the switch (PHP_OS) statement. Windows users may have a problem properly
349 unzipping mimetex.exe. Make sure that mimetex.exe is is <b>PRECISELY</b>
350 433152 bytes in size. If not, download fresh copy from
351 <a href="http://moodle.org/download/mimetex/windows/mimetex.exe">
352 http://moodle.org/download/mimetex/windows/mimetex.exe</a>. Lastly check
353 the execute permissions on your mimetex binary, as outlined in item 2 above.</li>