Fix for compatibility with short_open_tag = Off
[lhc/web/wiklou.git] / includes / Math.php
1 <?php
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 global $wgTexvc;
13 $mf = wfMsg( "math_failure" );
14 $munk = wfMsg( "math_unknown_error" );
15
16 $fname = "renderMath";
17
18 $math = $wgUser->getOption("math");
19 if( $math == 3 ) {
20 return ('$ '.wfEscapeHTML($tex).' $');
21 }
22
23 $md5 = md5($tex);
24 $md5_sql = mysql_escape_string(pack("H32", $md5));
25 if ($math == 0) {
26 $sql = "SELECT math_outputhash FROM math WHERE math_inputhash = '$md5_sql'";
27 } else {
28 $sql = "SELECT math_outputhash,math_html_conservativeness,math_html FROM math WHERE math_inputhash = '$md5_sql'";
29 }
30
31 $res = wfQuery( $sql, DB_READ, $fname );
32
33 if( $rpage = wfFetchObject ( $res ) ) {
34 $outputhash = unpack( "H32md5", $rpage->math_outputhash . " " );
35 $outputhash = $outputhash ['md5'];
36 if( file_exists( "$wgMathDirectory/$outputhash.png" ) ) {
37 if (($math == 0) || ($rpage->math_html == '') || (($math == 1) && ($rpage->math_html_conservativeness != 2)) || (($math == 4) && ($rpage->math_html_conservativeness == 0))) {
38 return linkToMathImage ( $tex, $outputhash );
39 } else {
40 return $rpage->math_html;
41 }
42 }
43 }
44
45 $cmd = $wgTexvc." ".
46 escapeshellarg($wgTmpDirectory)." ".
47 escapeshellarg($wgMathDirectory)." ".
48 escapeshellarg($tex)." ".
49 escapeshellarg($wgInputEncoding);
50 wfDebug( "TeX: $cmd" );
51 $contents = `$cmd`;
52
53 if (strlen($contents) == 0) {
54 return "<b>".$mf." (".$munk."): ".wfEscapeHTML($tex)."</b>";
55 }
56
57 $retval = substr ($contents, 0, 1);
58 if (($retval == "C") || ($retval == "M") || ($retval == "L")) {
59 if ($retval == "C")
60 $conservativeness = 2;
61 else if ($retval == "M")
62 $conservativeness = 1;
63 else
64 $conservativeness = 0;
65 $outdata = substr ($contents, 33);
66
67 $i = strpos($outdata, "\000");
68
69 $outhtml = substr($outdata, 0, $i);
70 $mathml = substr($outdata, $i+1);
71
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);
76 if ($retval == "c")
77 $conservativeness = 2;
78 else if ($retval == "m")
79 $conservativeness = 1;
80 else
81 $conservativeness = 0;
82 $sql_html = "'".mysql_escape_string($outhtml)."'";
83 $mathml = '';
84 $sql_mathml = 'NULL';
85 } else if ($retval == "X") {
86 $outhtml = '';
87 $mathml = substr ($contents, 33);
88 $sql_html = 'NULL';
89 $sql_mathml = "'".mysql_escape_string($mathml)."'";
90 $conservativeness = 0;
91 } else if ($retval == "+") {
92 $outhtml = '';
93 $mathml = '';
94 $sql_html = 'NULL';
95 $sql_mathml = 'NULL';
96 $conservativeness = 0;
97 } else {
98 if ($retval == "E")
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" );
104 else
105 $errmsg = $munk;
106 return "<h3>".$mf." (".$errmsg.substr($contents, 1)."): ".wfEscapeHTML($tex)."</h3>";
107 }
108
109 $outmd5 = substr ($contents, 1, 32);
110 if (!preg_match("/^[a-f0-9]{32}$/", $outmd5)) {
111 return "<b>".$mf." (".$munk."): ".wfEscapeHTML($tex)."</b>";
112 }
113
114 if( !file_exists( "$wgMathDirectory/$outmd5.png" ) ) {
115 $errmsg = wfMsg( "math_image_error" );
116 return "<h3>$mf ($errmsg): " . wfEscapeHTML($tex) . "</h3>";
117 }
118
119 $outmd5_sql = mysql_escape_string(pack("H32", $outmd5));
120
121 $sql = "REPLACE INTO math VALUES ('".$md5_sql."', '".$outmd5_sql."', ".$conservativeness.", ".$sql_html.", ".$sql_mathml.")";
122
123 $res = wfQuery( $sql, DB_READ, $fname );
124 # we don't really care if it fails
125
126 if (($math == 0) || ($rpage->math_html == '') || (($math == 1) && ($conservativeness != 2)) || (($math == 4) && ($conservativeness == 0)))
127 return linkToMathImage($tex, $outmd5);
128 else
129 return $outhtml;
130 }
131
132 ?>