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 $dbr =& wfGetDB( DB_SLAVE );
32 $mf = wfMsg( "math_failure" );
33 $munk = wfMsg( "math_unknown_error" );
39 $fname = "texvc_cgi_renderMath";
42 $md5_sql = pack("H32", $md5);
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 ) );
48 if ( $rpage === false )
50 $cmd = "./math/texvc ".wfEscapeShellArg($wgTmpDirectory)." ".
51 wfEscapeShellArg($wgMathDirectory)." ".wfEscapeShellArg($tex)." ".wfEscapeShellArg($wgInputEncoding);
54 if (strlen($contents) == 0)
55 return "<h3>".$mf." (".$munk."): ".wfEscapeHTML($tex)."</h3>";
56 $retval = substr ($contents, 0, 1);
58 if (($retval == "C") || ($retval == "M") || ($retval == "L")) {
60 $conservativeness = 2;
61 else if ($retval == "M")
62 $conservativeness = 1;
64 $conservativeness = 0;
65 $outdata = substr ($contents, 33);
67 $i = strpos($outdata, "\000");
69 $outhtml = substr($outdata, 0, $i);
70 $mathml = substr($outdata, $i+1);
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);
77 $conservativeness = 2;
78 else if ($retval == "m")
79 $conservativeness = 1;
81 $conservativeness = 0;
82 #$sql_html = "'".mysql_escape_string($outhtml)."'";
84 #$sql_mathml = 'NULL';
85 } else if ($retval == "X") {
87 $mathml = substr ($contents, 33);
89 #$sql_mathml = "'".mysql_escape_string($mathml)."'";
90 $conservativeness = 0;
91 } else if ($retval == "+") {
95 #$sql_mathml = 'NULL';
96 $conservativeness = 0;
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" );
106 return "<h3>".$mf." (".$errmsg.substr($contents, 1)."): ".wfEscapeHTML($tex)."</h3>";
109 $outmd5 = substr ($contents, 1, 32);
110 if (!preg_match("/^[a-f0-9]{32}$/", $outmd5))
111 return "<h3>".$mf." (".$munk."): ".wfEscapeHTML($tex)."</h3>";
113 $outmd5_sql = pack("H32", $outmd5);
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',
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' )
127 // we don't really care if it fails
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;
136 if ( is_null( $mathml ) || $mathml === '' )
137 $mathml = "<h3>Failed to generate MathML</h3>";
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);
144 if ( is_null( $outhtml ) || $outhtml === '' )
145 $outhtml = "<h3>Failed to generate HTML</h3>";
147 if ( $conservativeness == 2)
148 $outhtml = "<h3>HTML (conservative)</h3>" . $outhtml;
149 else if ( $conservativeness == 1)
150 $outhtml = "<h3>HTML (moderate)</h3>" . $outhtml;
152 $outhtml = "<h3>HTML (liberal)</h3>" . $outhtml;
155 $outtex = "<h3>Failed to generate TeX</h3>";
157 $outtex = "<h3>TeX</h3>" . wfEscapeHTML($outtex);
159 return $outtex . $outhtml . $mathml . $image;
164 print texvc_cgi_renderMath($math);