wfQuery now takes three parameters -- one extra for DB replication purposes
[lhc/web/wiklou.git] / includes / Math.php
1 <?
2
3 function linkToMathImage ( $tex, $outputhash )
4 {
5 global $wgMathPath;
6 return "<img src=\"".$wgMathPath."/".$outputhash.".png\" alt=\"".wfEscapeHTML($tex)."\">";
7 }
8
9 function renderMath( $tex )
10 {
11 global $wgUser, $wgMathDirectory, $wgTmpDirectory, $wgInputEncoding;
12 $mf = wfMsg( "math_failure" );
13 $munk = wfMsg( "math_unknown_error" );
14
15 $fname = "renderMath";
16
17 $math = $wgUser->getOption("math");
18 if ($math == 3)
19 return ('$ '.wfEscapeHTML($tex).' $');
20
21 $md5 = md5($tex);
22 $md5_sql = mysql_escape_string(pack("H32", $md5));
23 if ($math == 0)
24 $sql = "SELECT math_outputhash FROM math WHERE math_inputhash = '".$md5_sql."'";
25 else
26 $sql = "SELECT math_outputhash,math_html_conservativeness,math_html FROM math WHERE math_inputhash = '".$md5_sql."'";
27
28 $res = wfQuery( $sql, DB_READ, $fname );
29 if ( wfNumRows( $res ) == 0 )
30 {
31 $cmd = "./math/texvc ".escapeshellarg($wgTmpDirectory)." ".
32 escapeshellarg($wgMathDirectory)." ".escapeshellarg($tex)." ".escapeshellarg($wgInputEncoding);
33 $contents = `$cmd`;
34
35 if (strlen($contents) == 0)
36 return "<b>".$mf." (".$munk."): ".wfEscapeHTML($tex)."</b>";
37 $retval = substr ($contents, 0, 1);
38 if (($retval == "C") || ($retval == "M") || ($retval == "L")) {
39 if ($retval == "C")
40 $conservativeness = 2;
41 else if ($retval == "M")
42 $conservativeness = 1;
43 else
44 $conservativeness = 0;
45 $outdata = substr ($contents, 33);
46
47 $i = strpos($outdata, "\000");
48
49 $outhtml = substr($outdata, 0, $i);
50 $mathml = substr($outdata, $i+1);
51
52 $sql_html = "'".mysql_escape_string($outhtml)."'";
53 $sql_mathml = "'".mysql_escape_string($mathml)."'";
54 } else if (($retval == "c") || ($retval == "m") || ($retval == "l")) {
55 $outhtml = substr ($contents, 33);
56 if ($retval == "c")
57 $conservativeness = 2;
58 else if ($retval == "m")
59 $conservativeness = 1;
60 else
61 $conservativeness = 0;
62 $sql_html = "'".mysql_escape_string($outhtml)."'";
63 $mathml = '';
64 $sql_mathml = 'NULL';
65 } else if ($retval == "X") {
66 $outhtml = '';
67 $mathml = substr ($contents, 33);
68 $sql_html = 'NULL';
69 $sql_mathml = "'".mysql_escape_string($mathml)."'";
70 $conservativeness = 0;
71 } else if ($retval == "+") {
72 $outhtml = '';
73 $mathml = '';
74 $sql_html = 'NULL';
75 $sql_mathml = 'NULL';
76 $conservativeness = 0;
77 } else {
78 if ($retval == "E")
79 $errmsg = wfMsg( "math_lexing_error" );
80 else if ($retval == "S")
81 $errmsg = wfMsg( "math_syntax_error" );
82 else if ($retval == "F")
83 $errmsg = wfMsg( "math_unknown_function" );
84 else
85 $errmsg = $munk;
86 return "<h3>".$mf." (".$errmsg.substr($contents, 1)."): ".wfEscapeHTML($tex)."</h3>";
87 }
88
89 $outmd5 = substr ($contents, 1, 32);
90 if (!preg_match("/^[a-f0-9]{32}$/", $outmd5))
91 return "<b>".$mf." (".$munk."): ".wfEscapeHTML($tex)."</b>";
92
93 $outmd5_sql = mysql_escape_string(pack("H32", $outmd5));
94
95 $sql = "REPLACE INTO math VALUES ('".$md5_sql."', '".$outmd5_sql."', ".$conservativeness.", ".$sql_html.", ".$sql_mathml.")";
96
97 $res = wfQuery( $sql, DB_WRITE, $fname );
98 # we don't really care if it fails
99
100 if (($math == 0) || ($rpage->math_html == '') || (($math == 1) && ($conservativeness != 2)) || (($math == 4) && ($conservativeness == 0)))
101 return linkToMathImage($tex, $outmd5);
102 else
103 return $outhtml;
104 } else {
105 $rpage = wfFetchObject ( $res );
106 $outputhash = unpack( "H32md5", $rpage->math_outputhash . " " );
107 $outputhash = $outputhash ['md5'];
108
109 if (($math == 0) || ($rpage->math_html == '') || (($math == 1) && ($rpage->math_html_conservativeness != 2)) || (($math == 4) && ($rpage->math_html_conservativeness == 0)))
110 return linkToMathImage ( $tex, $outputhash );
111 else
112 return $rpage->math_html;
113 }
114 }
115
116 ?>