minor improvement
[mediawiki.git] / texvc.phtml
blob7a9127185227b3f3a66ff34a7fa3794eeeaec204
1 <?php
2 include( "./LocalSettings.php" );
3 include( "Setup.php" );
4 header( "Content-type: text/xml; charset={$wgInputEncoding}" );
5 print "<";
6 print "?xml version=\"1.0\" encoding=\"utf-8\"?";
7 print ">";
8 ?>
9 <!DOCTYPE html
10     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
11         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
12 <html xmlns="http://www.w3.org/1999/xhtml"><head><title>texvc</title></head><body>
13 <?php
14 print "<form method=\"post\" action=\"{$wgServer}{$wgScriptPath}/texvc_cgi.phtml\">"
16 <textarea name='math' rows='10' cols='80'><?
17     global $math;
18     $math = preg_replace ("/\\\\\\\\/", '\\', $math);
19     print $math;
20 ?></textarea><br /><input type="submit" value="Preview" name="preview" /></form>
21 <?php
22 function xlinkToMathImage ( $tex, $outputhash )
24     global $wgMathPath;
25     return "<img src=\"".$wgMathPath."/".$outputhash.".png\" alt=\"".wfEscapeHTML($tex)."\" />";
28 function texvc_cgi_renderMath( $tex )
30     global $wgMathDirectory, $wgTmpDirectory, $wgInputEncoding;
31     $mf   = wfMsg( "math_failure" );
32     $munk = wfMsg( "math_unknown_error" );
34     $image = "";
35     $outhtml = "";
36     $outtex = "";
38     $fname = "texvc_cgi_renderMath";
40     $md5 = md5($tex);
41     $md5_sql = mysql_escape_string(pack("H32", $md5));
42     $sql = "SELECT math_outputhash,math_html_conservativeness,math_html,math_mathml FROM math WHERE math_inputhash = '".$md5_sql."'";
44     $res = wfQuery( $sql, 0, $fname );
45     if ( wfNumRows( $res ) == 0 )
46     {
47         $cmd = "./math/texvc ".escapeshellarg($wgTmpDirectory)." ".
48                       escapeshellarg($wgMathDirectory)." ".escapeshellarg($tex)." ".escapeshellarg($wgInputEncoding);
49         $contents = `$cmd`;
51         if (strlen($contents) == 0)
52             return "<h3>".$mf." (".$munk."): ".wfEscapeHTML($tex)."</h3>";
53         $retval = substr ($contents, 0, 1);
55         if (($retval == "C") || ($retval == "M") || ($retval == "L")) {
56             if ($retval == "C")
57                 $conservativeness = 2;
58             else if ($retval == "M")
59                 $conservativeness = 1;
60             else
61                 $conservativeness = 0;
62             $outdata = substr ($contents, 33);
64             $i = strpos($outdata, "\000");
66             $outhtml = substr($outdata, 0, $i);
67             $mathml = substr($outdata, $i+1);
69             $sql_html = "'".mysql_escape_string($outhtml)."'";
70             $sql_mathml = "'".mysql_escape_string($mathml)."'";
71         } else if (($retval == "c") || ($retval == "m") || ($retval == "l"))  {
72             $outhtml = substr ($contents, 33);
73             if ($retval == "c")
74                 $conservativeness = 2;
75             else if ($retval == "m")
76                 $conservativeness = 1;
77             else
78                 $conservativeness = 0;
79             $sql_html = "'".mysql_escape_string($outhtml)."'";
80             $mathml = '';
81             $sql_mathml = 'NULL';
82         } else if ($retval == "X") {
83             $outhtml = '';
84             $mathml = substr ($contents, 33);
85             $sql_html = 'NULL';
86             $sql_mathml = "'".mysql_escape_string($mathml)."'";
87             $conservativeness = 0;
88         } else if ($retval == "+") {
89             $outhtml = '';
90             $mathml = '';
91             $sql_html = 'NULL';
92             $sql_mathml = 'NULL';
93             $conservativeness = 0;
94         } else {
95             if ($retval == "E")
96                 $errmsg = wfMsg( "math_lexing_error" );
97             else if ($retval == "S")
98                 $errmsg = wfMsg( "math_syntax_error" );
99             else if ($retval == "F")
100                 $errmsg = wfMsg( "math_unknown_function" );
101             else
102                 $errmsg = $munk;
103             return "<h3>".$mf." (".$errmsg.substr($contents, 1)."): ".wfEscapeHTML($tex)."</h3>";
104         }
106         $outmd5 = substr ($contents, 1, 32);
107         if (!preg_match("/^[a-f0-9]{32}$/", $outmd5))
108             return "<h3>".$mf." (".$munk."): ".wfEscapeHTML($tex)."</h3>";
110         $outmd5_sql = mysql_escape_string(pack("H32", $outmd5));
112         $sql = "INSERT INTO math VALUES ('".$md5_sql."', '".$outmd5_sql."', ".$conservativeness.", ".$sql_html.", ".$sql_mathml.")";
114         $res = wfQuery( $sql, 0, $fname );
115 // we don't really care if it fails
116     } else {
117         $rpage = wfFetchObject ( $res );
118         $outmd5 = unpack ("H32md5", $rpage->math_outputhash);
119         $outmd5 = $outmd5 ['md5'];
120         $outhtml = $rpage->math_html;
121         $conservativeness = $rpage->math_html_conservativeness;
122         $mathml = $rpage->math_mathml;
123     }
124     if ($mathml == '')
125         $mathml = "<h3>Failed to generate MathML</h3>";
126     else
127         $mathml = "<h3>MathML</h3><math xmlns=\"http://www.w3.org/1998/Math/MathML\">$mathml</math>";
128     $image = "<h3>Image</h3>" . xlinkToMathImage ( $tex, $outmd5 );
129     $cmd = "./math/texvc_tex ".escapeshellarg($tex)." ".escapeshellarg($wgInputEncoding);
130     $outtex = `$cmd`;
132     if ( $outhtml == '' )
133         $outhtml = "<h3>Failed to generate HTML</h3>";
134     else
135         if ( $conservativeness == 2)
136             $outhtml = "<h3>HTML (conservative)</h3>" . $outhtml;
137         else if ( $conservativeness == 1)
138             $outhtml = "<h3>HTML (moderate)</h3>" . $outhtml;
139         else
140             $outhtml = "<h3>HTML (liberal)</h3>" . $outhtml;
142     if ( $outtex == '' )
143         $outtex = "<h3>Failed to generate TeX</h3>";
144     else
145         $outtex = "<h3>TeX</h3>" . wfEscapeHTML($outtex);
147     return $outtex . $outhtml . $mathml . $image;
150 global $math;
151 if ($math != '')
152     print texvc_cgi_renderMath($math);
154 </body></html>