aboutsummaryrefslogtreecommitdiffstats
path: root/texvc.phtml
blob: 7a9127185227b3f3a66ff34a7fa3794eeeaec204 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
include( "./LocalSettings.php" );
include( "Setup.php" );
header( "Content-type: text/xml; charset={$wgInputEncoding}" );
print "<";
print "?xml version=\"1.0\" encoding=\"utf-8\"?";
print ">";
?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>texvc</title></head><body>
<?php
print "<form method=\"post\" action=\"{$wgServer}{$wgScriptPath}/texvc_cgi.phtml\">"
?>
<textarea name='math' rows='10' cols='80'><?
    global $math;
    $math = preg_replace ("/\\\\\\\\/", '\\', $math);
    print $math;
?></textarea><br /><input type="submit" value="Preview" name="preview" /></form>
<?php
function xlinkToMathImage ( $tex, $outputhash )
{
    global $wgMathPath;
    return "<img src=\"".$wgMathPath."/".$outputhash.".png\" alt=\"".wfEscapeHTML($tex)."\" />";
}

function texvc_cgi_renderMath( $tex )
{
    global $wgMathDirectory, $wgTmpDirectory, $wgInputEncoding;
    $mf   = wfMsg( "math_failure" );
    $munk = wfMsg( "math_unknown_error" );

    $image = "";
    $outhtml = "";
    $outtex = "";

    $fname = "texvc_cgi_renderMath";

    $md5 = md5($tex);
    $md5_sql = mysql_escape_string(pack("H32", $md5));
    $sql = "SELECT math_outputhash,math_html_conservativeness,math_html,math_mathml FROM math WHERE math_inputhash = '".$md5_sql."'";

    $res = wfQuery( $sql, 0, $fname );
    if ( wfNumRows( $res ) == 0 )
    {
	$cmd = "./math/texvc ".escapeshellarg($wgTmpDirectory)." ".
		      escapeshellarg($wgMathDirectory)." ".escapeshellarg($tex)." ".escapeshellarg($wgInputEncoding);
	$contents = `$cmd`;

	if (strlen($contents) == 0)
	    return "<h3>".$mf." (".$munk."): ".wfEscapeHTML($tex)."</h3>";
	$retval = substr ($contents, 0, 1);

	if (($retval == "C") || ($retval == "M") || ($retval == "L")) {
	    if ($retval == "C")
		$conservativeness = 2;
	    else if ($retval == "M")
		$conservativeness = 1;
	    else
		$conservativeness = 0;
	    $outdata = substr ($contents, 33);

	    $i = strpos($outdata, "\000");

	    $outhtml = substr($outdata, 0, $i);
	    $mathml = substr($outdata, $i+1);

	    $sql_html = "'".mysql_escape_string($outhtml)."'";
	    $sql_mathml = "'".mysql_escape_string($mathml)."'";
	} else if (($retval == "c") || ($retval == "m") || ($retval == "l"))  {
	    $outhtml = substr ($contents, 33);
	    if ($retval == "c")
		$conservativeness = 2;
	    else if ($retval == "m")
		$conservativeness = 1;
	    else
		$conservativeness = 0;
	    $sql_html = "'".mysql_escape_string($outhtml)."'";
	    $mathml = '';
	    $sql_mathml = 'NULL';
	} else if ($retval == "X") {
	    $outhtml = '';
	    $mathml = substr ($contents, 33);
	    $sql_html = 'NULL';
	    $sql_mathml = "'".mysql_escape_string($mathml)."'";
	    $conservativeness = 0;
	} else if ($retval == "+") {
	    $outhtml = '';
	    $mathml = '';
	    $sql_html = 'NULL';
	    $sql_mathml = 'NULL';
	    $conservativeness = 0;
	} else {
	    if ($retval == "E")
		$errmsg = wfMsg( "math_lexing_error" );
	    else if ($retval == "S")
		$errmsg = wfMsg( "math_syntax_error" );
	    else if ($retval == "F")
		$errmsg = wfMsg( "math_unknown_function" );
	    else
		$errmsg = $munk;
	    return "<h3>".$mf." (".$errmsg.substr($contents, 1)."): ".wfEscapeHTML($tex)."</h3>";
	}

	$outmd5 = substr ($contents, 1, 32);
	if (!preg_match("/^[a-f0-9]{32}$/", $outmd5))
	    return "<h3>".$mf." (".$munk."): ".wfEscapeHTML($tex)."</h3>";

	$outmd5_sql = mysql_escape_string(pack("H32", $outmd5));

	$sql = "INSERT INTO math VALUES ('".$md5_sql."', '".$outmd5_sql."', ".$conservativeness.", ".$sql_html.", ".$sql_mathml.")";

	$res = wfQuery( $sql, 0, $fname );
// we don't really care if it fails
    } else {
	$rpage = wfFetchObject ( $res );
	$outmd5 = unpack ("H32md5", $rpage->math_outputhash);
	$outmd5 = $outmd5 ['md5'];
	$outhtml = $rpage->math_html;
	$conservativeness = $rpage->math_html_conservativeness;
	$mathml = $rpage->math_mathml;
    }
    if ($mathml == '')
	$mathml = "<h3>Failed to generate MathML</h3>";
    else
	$mathml = "<h3>MathML</h3><math xmlns=\"http://www.w3.org/1998/Math/MathML\">$mathml</math>";
    $image = "<h3>Image</h3>" . xlinkToMathImage ( $tex, $outmd5 );
    $cmd = "./math/texvc_tex ".escapeshellarg($tex)." ".escapeshellarg($wgInputEncoding);
    $outtex = `$cmd`;

    if ( $outhtml == '' )
        $outhtml = "<h3>Failed to generate HTML</h3>";
    else
	if ( $conservativeness == 2)
	    $outhtml = "<h3>HTML (conservative)</h3>" . $outhtml;
	else if ( $conservativeness == 1)
	    $outhtml = "<h3>HTML (moderate)</h3>" . $outhtml;
	else
	    $outhtml = "<h3>HTML (liberal)</h3>" . $outhtml;

    if ( $outtex == '' )
	$outtex = "<h3>Failed to generate TeX</h3>";
    else
	$outtex = "<h3>TeX</h3>" . wfEscapeHTML($outtex);

    return $outtex . $outhtml . $mathml . $image;
}

global $math;
if ($math != '')
    print texvc_cgi_renderMath($math);
?>
</body></html>