fix invalid HTML entities
[lhc/web/wiklou.git] / texvc.phtml
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\">"
15 ?>
16 <textarea name='math' rows='10' cols='80'><?php
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 )
23 {
24 global $wgMathPath;
25 return "<img src=\"".$wgMathPath."/".$outputhash.".png\" alt=\"".wfEscapeHTML($tex)."\" />";
26 }
27
28 function texvc_cgi_renderMath( $tex )
29 {
30 global $wgMathDirectory, $wgTmpDirectory, $wgInputEncoding;
31 $dbr =& wfGetDB( DB_SLAVE );
32 $mf = wfMsg( "math_failure" );
33 $munk = wfMsg( "math_unknown_error" );
34
35 $image = "";
36 $outhtml = "";
37 $outtex = "";
38
39 $fname = "texvc_cgi_renderMath";
40
41 $md5 = md5($tex);
42 $md5_sql = pack("H32", $md5);
43
44 $mathTable = $dbr->tableName( 'math' );
45 $rpage = $dbr->getArray( 'math', array('math_outputhash','math_html_conservativeness','math_html','math_mathml'),
46 array( 'math_inputhash' => $md5_sql ) );
47
48 if ( $rpage === false )
49 {
50 $cmd = "./math/texvc ".wfEscapeShellArg($wgTmpDirectory)." ".
51 wfEscapeShellArg($wgMathDirectory)." ".wfEscapeShellArg($tex)." ".wfEscapeShellArg($wgInputEncoding);
52 $contents = `$cmd`;
53
54 if (strlen($contents) == 0)
55 return "<h3>".$mf." (".$munk."): ".wfEscapeHTML($tex)."</h3>";
56 $retval = substr ($contents, 0, 1);
57
58 if (($retval == "C") || ($retval == "M") || ($retval == "L")) {
59 if ($retval == "C")
60 $conservativeness = 2;
61 else if ($retval == "M")
62 $conservativeness = 1;
63 else
64 $conservativeness = 0;
65 $outdata = substr ($contents, 33);
66
67 $i = strpos($outdata, "\000");
68
69 $outhtml = substr($outdata, 0, $i);
70 $mathml = substr($outdata, $i+1);
71
72 #$sql_html = "'".mysql_escape_string($outhtml)."'";
73 #$sql_mathml = "'".mysql_escape_string($mathml)."'";
74 } else if (($retval == "c") || ($retval == "m") || ($retval == "l")) {
75 $outhtml = substr ($contents, 33);
76 if ($retval == "c")
77 $conservativeness = 2;
78 else if ($retval == "m")
79 $conservativeness = 1;
80 else
81 $conservativeness = 0;
82 #$sql_html = "'".mysql_escape_string($outhtml)."'";
83 $mathml = NULL;
84 #$sql_mathml = 'NULL';
85 } else if ($retval == "X") {
86 $outhtml = NULL;
87 $mathml = substr ($contents, 33);
88 #$sql_html = 'NULL';
89 #$sql_mathml = "'".mysql_escape_string($mathml)."'";
90 $conservativeness = 0;
91 } else if ($retval == "+") {
92 $outhtml = NULL;
93 $mathml = NULL;
94 #$sql_html = 'NULL';
95 #$sql_mathml = 'NULL';
96 $conservativeness = 0;
97 } else {
98 if ($retval == "E")
99 $errmsg = wfMsg( "math_lexing_error" );
100 else if ($retval == "S")
101 $errmsg = wfMsg( "math_syntax_error" );
102 else if ($retval == "F")
103 $errmsg = wfMsg( "math_unknown_function" );
104 else
105 $errmsg = $munk;
106 return "<h3>".$mf." (".$errmsg.substr($contents, 1)."): ".wfEscapeHTML($tex)."</h3>";
107 }
108
109 $outmd5 = substr ($contents, 1, 32);
110 if (!preg_match("/^[a-f0-9]{32}$/", $outmd5))
111 return "<h3>".$mf." (".$munk."): ".wfEscapeHTML($tex)."</h3>";
112
113 $outmd5_sql = pack("H32", $outmd5);
114
115 # Someone may have inserted the same hash since the SELECT, but that's no big deal, just ignore errors
116 $dbw =& wfGetDB( DB_MASTER );
117 $dbw->insertArray( 'math',
118 array(
119 'math_inputhash' => $md5_sql,
120 'math_outputhash' => $outmd5_sql,
121 'math_html_conservativeness' => $conservativeness,
122 'math_html' => $outhtml,
123 'math_mathml' => $mathml,
124 ), $fname, array( 'IGNORE' )
125 );
126
127 // we don't really care if it fails
128 } else {
129 $rpage = $dbr->fetchObject ( $res );
130 $outmd5 = unpack ("H32md5", $rpage->math_outputhash);
131 $outmd5 = $outmd5 ['md5'];
132 $outhtml = $rpage->math_html;
133 $conservativeness = $rpage->math_html_conservativeness;
134 $mathml = $rpage->math_mathml;
135 }
136 if ( is_null( $mathml ) || $mathml === '' )
137 $mathml = "<h3>Failed to generate MathML</h3>";
138 else
139 $mathml = "<h3>MathML</h3><math xmlns=\"http://www.w3.org/1998/Math/MathML\">$mathml</math>";
140 $image = "<h3>Image</h3>" . xlinkToMathImage ( $tex, $outmd5 );
141 $cmd = "./math/texvc_tex ".escapeshellarg($tex)." ".escapeshellarg($wgInputEncoding);
142 $outtex = `$cmd`;
143
144 if ( is_null( $outhtml ) || $outhtml === '' )
145 $outhtml = "<h3>Failed to generate HTML</h3>";
146 else
147 if ( $conservativeness == 2)
148 $outhtml = "<h3>HTML (conservative)</h3>" . $outhtml;
149 else if ( $conservativeness == 1)
150 $outhtml = "<h3>HTML (moderate)</h3>" . $outhtml;
151 else
152 $outhtml = "<h3>HTML (liberal)</h3>" . $outhtml;
153
154 if ( $outtex == '' )
155 $outtex = "<h3>Failed to generate TeX</h3>";
156 else
157 $outtex = "<h3>TeX</h3>" . wfEscapeHTML($outtex);
158
159 return $outtex . $outhtml . $mathml . $image;
160 }
161
162 global $math;
163 if ($math != '')
164 print texvc_cgi_renderMath($math);
165 ?>
166 </body></html>