Bugfix suggested by Laurent GUERBY on wikitech-l
[lhc/web/wiklou.git] / includes / Math.php
1 <?php
2 #
3 # Takes LaTeX fragments, sends them to a helper program (texvc) for rendering
4 # to rasterized PNG and HTML and MathML approximations. An appropriate
5 # rendering form is picked and returned.
6 #
7 # by Tomasz Wegrzanowski, with additions by Brion Vibber (2003, 2004)
8 #
9
10 class MathRenderer {
11 var $mode = MW_MATH_MODERN;
12 var $tex = "";
13 var $inputhash = "";
14 var $hash = "";
15 var $html = "";
16 var $mathml = "";
17 var $conservativeness = 0;
18
19 function MathRenderer( $tex ) {
20 $this->tex = $tex;
21 }
22
23 function setOutputMode( $mode ) {
24 $this->mode = $mode;
25 }
26
27 function render() {
28 global $wgMathDirectory, $wgTmpDirectory, $wgInputEncoding;
29 global $wgTexvc;
30
31 if( $this->mode == MW_MATH_SOURCE ) {
32 # No need to render or parse anything more!
33 return ('$ '.htmlspecialchars( $this->tex ).' $');
34 }
35
36 if( !$this->_recall() ) {
37 # Ensure that the temp and output directories are available before continuing...
38 if( !file_exists( $wgMathDirectory ) ) {
39 if( !@mkdir( $wgMathDirectory ) ) {
40 return $this->_error( "math_bad_output" );
41 }
42 } elseif( !is_dir( $wgMathDirectory ) || !is_writable( $wgMathDirectory ) ) {
43 return $this->_error( "math_bad_output" );
44 }
45 if( !file_exists( $wgTmpDirectory ) ) {
46 if( !@mkdir( $wgTmpDirectory ) ) {
47 return $this->_error( "math_bad_tmpdir" );
48 }
49 } elseif( !is_dir( $wgTmpDirectory ) || !is_writable( $wgTmpDirectory ) ) {
50 return $this->_error( "math_bad_tmpdir" );
51 }
52
53 if( !is_executable( $wgTexvc ) ) {
54 return $this->_error( "math_notexvc" );
55 }
56 $cmd = $wgTexvc." ".
57 escapeshellarg($wgTmpDirectory)." ".
58 escapeshellarg($wgMathDirectory)." ".
59 escapeshellarg($this->tex)." ".
60 escapeshellarg($wgInputEncoding);
61 wfDebug( "TeX: $cmd" );
62 $contents = `$cmd`;
63
64 if (strlen($contents) == 0) {
65 return $this->_error( "math_unknown_error" );
66 }
67
68 $retval = substr ($contents, 0, 1);
69 if (($retval == "C") || ($retval == "M") || ($retval == "L")) {
70 if ($retval == "C")
71 $this->conservativeness = 2;
72 else if ($retval == "M")
73 $this->conservativeness = 1;
74 else
75 $this->conservativeness = 0;
76 $outdata = substr ($contents, 33);
77
78 $i = strpos($outdata, "\000");
79
80 $this->html = substr($outdata, 0, $i);
81 $this->mathml = substr($outdata, $i+1);
82
83 $sql_html = "'".wfStrencode($this->html)."'";
84 $sql_mathml = "'".wfStrencode($this->mathml)."'";
85 } else if (($retval == "c") || ($retval == "m") || ($retval == "l")) {
86 $this->html = substr ($contents, 33);
87 if ($retval == "c")
88 $this->conservativeness = 2;
89 else if ($retval == "m")
90 $this->conservativeness = 1;
91 else
92 $this->conservativeness = 0;
93 $sql_html = "'".wfStrencode($this->html)."'";
94 $this->mathml = '';
95 $sql_mathml = 'NULL';
96 } else if ($retval == "X") {
97 $outhtml = '';
98 $this->mathml = substr ($contents, 33);
99 $sql_html = 'NULL';
100 $sql_mathml = "'".wfStrencode($this->mathml)."'";
101 $this->conservativeness = 0;
102 } else if ($retval == "+") {
103 $this->outhtml = '';
104 $this->mathml = '';
105 $sql_html = 'NULL';
106 $sql_mathml = 'NULL';
107 $this->conservativeness = 0;
108 } else {
109 $errbit = htmlspecialchars( substr($contents, 1) );
110 switch( $retval ) {
111 case "E": return $this->_error( "math_lexing_error", $errbit );
112 case "S": return $this->_error( "math_syntax_error", $errbit );
113 case "F": return $this->_error( "math_unknown_function", $errbit );
114 default: return $this->_error( "math_unknown_error", $errbit );
115 }
116 }
117
118 $this->hash = substr ($contents, 1, 32);
119 if (!preg_match("/^[a-f0-9]{32}$/", $this->hash)) {
120 return $this->_error( "math_unknown_error" );
121 }
122
123 if( !file_exists( "$wgMathDirectory/{$this->hash}.png" ) ) {
124 return $this->_error( "math_image_error" );
125 }
126
127 # Now save it back to the DB:
128 $outmd5_sql = wfStrencode(pack("H32", $this->hash));
129
130 $md5_sql = wfStrencode( pack("H32", $this->md5) ); # Binary packed, not hex
131 $sql = "REPLACE INTO math VALUES ('".$md5_sql."', '".$outmd5_sql."', ".$this->conservativeness.", ".$sql_html.", ".$sql_mathml.")";
132
133 $res = wfQuery( $sql, DB_WRITE, "MathRenderer::render" );
134 # we don't really care if it fails
135 }
136
137 return $this->_doRender();
138 }
139
140 function _error( $msg, $append = "" ) {
141 $mf = htmlspecialchars( wfMsg( "math_failure" ) );
142 $munk = htmlspecialchars( wfMsg( "math_unknown_error" ) );
143 $errmsg = htmlspecialchars( wfMsg( $msg ) );
144 $source = htmlspecialchars($this->tex);
145 return "<strong class='error'>$mf ($errmsg$append): $source</strong>\n";
146 }
147
148 function _recall() {
149 global $wgMathDirectory;
150
151 $this->md5 = md5( $this->tex );
152
153 $md5_sql = wfStrencode( pack("H32", $this->md5) ); # Binary packed, not hex
154 $sql = "SELECT math_outputhash,math_html_conservativeness,math_html,math_mathml FROM math WHERE math_inputhash = '$md5_sql'";
155 $res = wfQuery( $sql, DB_READ, "MathRenderer::_recall" );
156
157 if( $rpage = wfFetchObject ( $res ) ) {
158 # Tailing 0x20s can get dropped by the database, add it back on if necessary:
159 $xhash = unpack( "H32md5", $rpage->math_outputhash . " " );
160 $this->hash = $xhash ['md5'];
161
162 $this->conservativeness = $rpage->math_html_conservativeness;
163 $this->html = $rpage->math_html;
164 $this->mathml = $rpage->math_mathml;
165
166 if( file_exists( "$wgMathDirectory/{$this->hash}.png" ) ) {
167 return true;
168 }
169 }
170
171 # Missing from the database and/or the render cache
172 return false;
173 }
174
175 # Select among PNG, HTML, or MathML output depending on
176 function _doRender() {
177 if( $this->mode == MW_MATH_MATHML && $this->mathml != '' ) {
178 return "<math xmlns='http://www.w3.org/1998/Math/MathML'>{$this->mathml}</math>";
179 }
180 if (($this->mode == MW_MATH_PNG) || ($this->html == '') ||
181 (($this->mode == MW_MATH_SIMPLE) && ($this->conservativeness != 2)) ||
182 (($this->mode == MW_MATH_MODERN || $this->mode == MW_MATH_MATHML) && ($this->conservativeness == 0))) {
183 return $this->_linkToMathImage();
184 } else {
185 return $this->html;
186 }
187 }
188
189 function _linkToMathImage() {
190 global $wgMathPath;
191 $url = htmlspecialchars( "$wgMathPath/{$this->hash}.png" );
192 $alt = trim(str_replace("\n", ' ', htmlspecialchars( $this->tex )));
193 return "<img class='tex' src=\"$url\" alt=\"$alt\" />";
194 }
195
196 }
197
198 function renderMath( $tex ) {
199 global $wgUser;
200 $math = new MathRenderer( $tex );
201 $math->setOutputMode( $wgUser->getOption("math"));
202 return $math->render();
203 }
204
205 ?>