798b569e560860bc53947001d946918402d2cc36
[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 # 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>";
49 }
50 } elseif( !is_dir( $wgMathDirectory ) || !is_writable( $wgMathDirectory ) ) {
51 return "<b>$mf (" . wfMsg( "math_bad_output" ) . ")</b>";
52 }
53 if( !file_exists( $wgTmpDirectory ) ) {
54 if( !@mkdir( $wgTmpDirectory ) ) {
55 return "<b>$mf (" . wfMsg( "math_bad_tmpdir" ) . ")</b>";
56 }
57 } elseif( !is_dir( $wgTmpDirectory ) || !is_writable( $wgTmpDirectory ) ) {
58 return "<b>$mf (" . wfMsg( "math_bad_tmpdir" ) . ")</b>";
59 }
60
61 if( !is_executable( $wgTexvc ) ) {
62 return "<b>$mf (" . wfMsg( "math_notexvc" ) . ")</b>";
63 }
64 $cmd = $wgTexvc." ".
65 escapeshellarg($wgTmpDirectory)." ".
66 escapeshellarg($wgMathDirectory)." ".
67 escapeshellarg($tex)." ".
68 escapeshellarg($wgInputEncoding);
69 wfDebug( "TeX: $cmd" );
70 $contents = `$cmd`;
71
72 if (strlen($contents) == 0) {
73 return "<b>".$mf." (".$munk."): ".wfEscapeHTML($tex)."</b>";
74 }
75
76 $retval = substr ($contents, 0, 1);
77 if (($retval == "C") || ($retval == "M") || ($retval == "L")) {
78 if ($retval == "C")
79 $conservativeness = 2;
80 else if ($retval == "M")
81 $conservativeness = 1;
82 else
83 $conservativeness = 0;
84 $outdata = substr ($contents, 33);
85
86 $i = strpos($outdata, "\000");
87
88 $outhtml = substr($outdata, 0, $i);
89 $mathml = substr($outdata, $i+1);
90
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);
95 if ($retval == "c")
96 $conservativeness = 2;
97 else if ($retval == "m")
98 $conservativeness = 1;
99 else
100 $conservativeness = 0;
101 $sql_html = "'".mysql_escape_string($outhtml)."'";
102 $mathml = '';
103 $sql_mathml = 'NULL';
104 } else if ($retval == "X") {
105 $outhtml = '';
106 $mathml = substr ($contents, 33);
107 $sql_html = 'NULL';
108 $sql_mathml = "'".mysql_escape_string($mathml)."'";
109 $conservativeness = 0;
110 } else if ($retval == "+") {
111 $outhtml = '';
112 $mathml = '';
113 $sql_html = 'NULL';
114 $sql_mathml = 'NULL';
115 $conservativeness = 0;
116 } else {
117 if ($retval == "E")
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" );
123 else
124 $errmsg = $munk;
125 return "<h3>".$mf." (".$errmsg.substr($contents, 1)."): ".wfEscapeHTML($tex)."</h3>";
126 }
127
128 $outmd5 = substr ($contents, 1, 32);
129 if (!preg_match("/^[a-f0-9]{32}$/", $outmd5)) {
130 return "<b>".$mf." (".$munk."): ".wfEscapeHTML($tex)."</b>";
131 }
132
133 if( !file_exists( "$wgMathDirectory/$outmd5.png" ) ) {
134 $errmsg = wfMsg( "math_image_error" );
135 return "<h3>$mf ($errmsg): " . wfEscapeHTML($tex) . "</h3>";
136 }
137
138 $outmd5_sql = mysql_escape_string(pack("H32", $outmd5));
139
140 $sql = "REPLACE INTO math VALUES ('".$md5_sql."', '".$outmd5_sql."', ".$conservativeness.", ".$sql_html.", ".$sql_mathml.")";
141
142 $res = wfQuery( $sql, DB_READ, $fname );
143 # we don't really care if it fails
144
145 if (($math == 0) || ($rpage->math_html == '') || (($math == 1) && ($conservativeness != 2)) || (($math == 4) && ($conservativeness == 0)))
146 return linkToMathImage($tex, $outmd5);
147 else
148 return $outhtml;
149 }
150
151 ?>