3 function linkToMathImage ( $tex, $outputhash )
6 return "<img src=\"".$wgMathPath."/".$outputhash.".png\" alt=\"".wfEscapeHTML($tex)."\">";
9 function renderMath( $tex )
11 global $wgUser, $wgMathDirectory, $wgTmpDirectory, $wgInputEncoding;
13 $mf = wfMsg( "math_failure" );
14 $munk = wfMsg( "math_unknown_error" );
16 $fname = "renderMath";
18 $math = $wgUser->getOption("math");
20 return ('$ '.wfEscapeHTML($tex).' $');
24 $md5_sql = mysql_escape_string(pack("H32", $md5));
26 $sql = "SELECT math_outputhash FROM math WHERE math_inputhash = '$md5_sql'";
28 $sql = "SELECT math_outputhash,math_html_conservativeness,math_html FROM math WHERE math_inputhash = '$md5_sql'";
31 $res = wfQuery( $sql, DB_READ
, $fname );
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 );
40 return $rpage->math_html
;
45 # Ensure that the temp and output directories are available before continuing...
46 if( !file_exists( $wgMathDirectory ) ) {
47 if( !@mkdir
( $wgMathDirectory ) ) {
48 return "<b>$mf (" . wfMsg( "math_bad_output" ) . ")</b>";
50 } elseif( !is_dir( $wgMathDirectory ) ||
!is_writable( $wgMathDirectory ) ) {
51 return "<b>$mf (" . wfMsg( "math_bad_output" ) . ")</b>";
53 if( !file_exists( $wgTmpDirectory ) ) {
54 if( !@mkdir
( $wgTmpDirectory ) ) {
55 return "<b>$mf (" . wfMsg( "math_bad_tmpdir" ) . ")</b>";
57 } elseif( !is_dir( $wgTmpDirectory ) ||
!is_writable( $wgTmpDirectory ) ) {
58 return "<b>$mf (" . wfMsg( "math_bad_tmpdir" ) . ")</b>";
61 if( !is_executable( $wgTexvc ) ) {
62 return "<b>$mf (" . wfMsg( "math_notexvc" ) . ")</b>";
65 escapeshellarg($wgTmpDirectory)." ".
66 escapeshellarg($wgMathDirectory)." ".
67 escapeshellarg($tex)." ".
68 escapeshellarg($wgInputEncoding);
69 wfDebug( "TeX: $cmd" );
72 if (strlen($contents) == 0) {
73 return "<b>".$mf." (".$munk."): ".wfEscapeHTML($tex)."</b>";
76 $retval = substr ($contents, 0, 1);
77 if (($retval == "C") ||
($retval == "M") ||
($retval == "L")) {
79 $conservativeness = 2;
80 else if ($retval == "M")
81 $conservativeness = 1;
83 $conservativeness = 0;
84 $outdata = substr ($contents, 33);
86 $i = strpos($outdata, "\000");
88 $outhtml = substr($outdata, 0, $i);
89 $mathml = substr($outdata, $i+
1);
91 $sql_html = "'".mysql_escape_string($outhtml)."'";
92 $sql_mathml = "'".mysql_escape_string($mathml)."'";
93 } else if (($retval == "c") ||
($retval == "m") ||
($retval == "l")) {
94 $outhtml = substr ($contents, 33);
96 $conservativeness = 2;
97 else if ($retval == "m")
98 $conservativeness = 1;
100 $conservativeness = 0;
101 $sql_html = "'".mysql_escape_string($outhtml)."'";
103 $sql_mathml = 'NULL';
104 } else if ($retval == "X") {
106 $mathml = substr ($contents, 33);
108 $sql_mathml = "'".mysql_escape_string($mathml)."'";
109 $conservativeness = 0;
110 } else if ($retval == "+") {
114 $sql_mathml = 'NULL';
115 $conservativeness = 0;
118 $errmsg = wfMsg( "math_lexing_error" );
119 else if ($retval == "S")
120 $errmsg = wfMsg( "math_syntax_error" );
121 else if ($retval == "F")
122 $errmsg = wfMsg( "math_unknown_function" );
125 return "<h3>".$mf." (".$errmsg.substr($contents, 1)."): ".wfEscapeHTML($tex)."</h3>";
128 $outmd5 = substr ($contents, 1, 32);
129 if (!preg_match("/^[a-f0-9]{32}$/", $outmd5)) {
130 return "<b>".$mf." (".$munk."): ".wfEscapeHTML($tex)."</b>";
133 if( !file_exists( "$wgMathDirectory/$outmd5.png" ) ) {
134 $errmsg = wfMsg( "math_image_error" );
135 return "<h3>$mf ($errmsg): " . wfEscapeHTML($tex) . "</h3>";
138 $outmd5_sql = mysql_escape_string(pack("H32", $outmd5));
140 $sql = "REPLACE INTO math VALUES ('".$md5_sql."', '".$outmd5_sql."', ".$conservativeness.", ".$sql_html.", ".$sql_mathml.")";
142 $res = wfQuery( $sql, DB_READ
, $fname );
143 # we don't really care if it fails
145 if (($math == 0) ||
($rpage->math_html
== '') ||
(($math == 1) && ($conservativeness != 2)) ||
(($math == 4) && ($conservativeness == 0)))
146 return linkToMathImage($tex, $outmd5);