2 include( "./LocalSettings.php" );
3 include( "Setup.php" );
4 header( "Content-type: text/xml; charset={$wgInputEncoding}" );
6 print "?xml version=\"1.0\" encoding=\"utf-8\"?";
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>
14 print "<form method=\"post\" action=\"{$wgServer}{$wgScriptPath}/texvc_cgi.phtml\">"
16 <textarea name='math' rows='10' cols='80'><?
18 $math = preg_replace ("/\\\\\\\\/", '\\', $math);
20 ?></textarea><br /><input type="submit" value="Preview" name="preview" /></form>
22 function xlinkToMathImage ( $tex, $outputhash )
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" );
38 $fname = "texvc_cgi_renderMath";
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 )
47 $cmd = "./math/texvc ".escapeshellarg($wgTmpDirectory)." ".
48 escapeshellarg($wgMathDirectory)." ".escapeshellarg($tex)." ".escapeshellarg($wgInputEncoding);
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")) {
57 $conservativeness = 2;
58 else if ($retval == "M")
59 $conservativeness = 1;
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);
74 $conservativeness = 2;
75 else if ($retval == "m")
76 $conservativeness = 1;
78 $conservativeness = 0;
79 $sql_html = "'".mysql_escape_string($outhtml)."'";
82 } else if ($retval == "X") {
84 $mathml = substr ($contents, 33);
86 $sql_mathml = "'".mysql_escape_string($mathml)."'";
87 $conservativeness = 0;
88 } else if ($retval == "+") {
93 $conservativeness = 0;
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" );
103 return "<h3>".$mf." (".$errmsg.substr($contents, 1)."): ".wfEscapeHTML($tex)."</h3>";
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
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;
125 $mathml = "<h3>Failed to generate MathML</h3>";
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);
132 if ( $outhtml == '' )
133 $outhtml = "<h3>Failed to generate HTML</h3>";
135 if ( $conservativeness == 2)
136 $outhtml = "<h3>HTML (conservative)</h3>" . $outhtml;
137 else if ( $conservativeness == 1)
138 $outhtml = "<h3>HTML (moderate)</h3>" . $outhtml;
140 $outhtml = "<h3>HTML (liberal)</h3>" . $outhtml;
143 $outtex = "<h3>Failed to generate TeX</h3>";
145 $outtex = "<h3>TeX</h3>" . wfEscapeHTML($outtex);
147 return $outtex . $outhtml . $mathml . $image;
152 print texvc_cgi_renderMath($math);