3 include( "./LocalSettings.php" );
4 include( "$IP/Setup.php" );
5 header( "Content-type: text/xml; charset={$wgInputEncoding}" );
7 print "?xml version=\"1.0\" encoding=\"utf-8\"?";
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'><?
19 $math = preg_replace ("/\\\\\\\\/", '\\', $math);
21 ?></textarea><br /><input type="submit" value="Preview" name="preview" /></form>
23 function xlinkToMathImage ( $tex, $outputhash )
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" );
39 $fname = "texvc_cgi_renderMath";
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, 0, $fname );
46 if ( wfNumRows( $res ) == 0 )
48 $cmd = "./math/texvc ".escapeshellarg($wgTmpDirectory)." ".
49 escapeshellarg($wgMathDirectory)." ".escapeshellarg($tex)." ".escapeshellarg($wgInputEncoding);
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")) {
58 $conservativeness = 2;
59 else if ($retval == "M")
60 $conservativeness = 1;
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);
75 $conservativeness = 2;
76 else if ($retval == "m")
77 $conservativeness = 1;
79 $conservativeness = 0;
80 $sql_html = "'".mysql_escape_string($outhtml)."'";
83 } else if ($retval == "X") {
85 $mathml = substr ($contents, 33);
87 $sql_mathml = "'".mysql_escape_string($mathml)."'";
88 $conservativeness = 0;
89 } else if ($retval == "+") {
94 $conservativeness = 0;
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" );
104 return "<h3>".$mf." (".$errmsg.substr($contents, 1)."): ".wfEscapeHTML($tex)."</h3>";
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, 0, $fname );
116 // we don't really care if it fails
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;
126 $mathml = "<h3>Failed to generate MathML</h3>";
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);
133 if ( $outhtml == '' )
134 $outhtml = "<h3>Failed to generate HTML</h3>";
136 if ( $conservativeness == 2)
137 $outhtml = "<h3>HTML (conservative)</h3>" . $outhtml;
138 else if ( $conservativeness == 1)
139 $outhtml = "<h3>HTML (moderate)</h3>" . $outhtml;
141 $outhtml = "<h3>HTML (liberal)</h3>" . $outhtml;
144 $outtex = "<h3>Failed to generate TeX</h3>";
146 $outtex = "<h3>TeX</h3>" . wfEscapeHTML($outtex);
148 return $outtex . $outhtml . $mathml . $image;
153 print texvc_cgi_renderMath($math);