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