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.
7 # by Tomasz Wegrzanowski, with additions by Brion Vibber (2003, 2004)
11 var $mode = MW_MATH_MODERN
;
17 var $conservativeness = 0;
19 function MathRenderer( $tex ) {
23 function setOutputMode( $mode ) {
28 global $wgMathDirectory, $wgTmpDirectory, $wgInputEncoding;
31 if( $this->mode
== MW_MATH_SOURCE
) {
32 # No need to render or parse anything more!
33 return ('$ '.htmlspecialchars( $tex ).' $');
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" );
42 } elseif( !is_dir( $wgMathDirectory ) ||
!is_writable( $wgMathDirectory ) ) {
43 return $this->_error( "math_bad_output" );
45 if( !file_exists( $wgTmpDirectory ) ) {
46 if( !@mkdir
( $wgTmpDirectory ) ) {
47 return $this->_error( "math_bad_tmpdir" );
49 } elseif( !is_dir( $wgTmpDirectory ) ||
!is_writable( $wgTmpDirectory ) ) {
50 return $this->_error( "math_bad_tmpdir" );
53 if( !is_executable( $wgTexvc ) ) {
54 return $this->_error( "math_notexvc" );
57 escapeshellarg($wgTmpDirectory)." ".
58 escapeshellarg($wgMathDirectory)." ".
59 escapeshellarg($this->tex
)." ".
60 escapeshellarg($wgInputEncoding);
61 wfDebug( "TeX: $cmd" );
64 if (strlen($contents) == 0) {
65 return $this->_error( "math_unknown_error" );
68 $retval = substr ($contents, 0, 1);
69 if (($retval == "C") ||
($retval == "M") ||
($retval == "L")) {
71 $this->conservativeness
= 2;
72 else if ($retval == "M")
73 $this->conservativeness
= 1;
75 $this->conservativeness
= 0;
76 $outdata = substr ($contents, 33);
78 $i = strpos($outdata, "\000");
80 $this->html
= substr($outdata, 0, $i);
81 $this->mathml
= substr($outdata, $i+
1);
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);
88 $this->conservativeness
= 2;
89 else if ($retval == "m")
90 $this->conservativeness
= 1;
92 $this->conservativeness
= 0;
93 $sql_html = "'".wfStrencode($this->html
)."'";
96 } else if ($retval == "X") {
98 $this->mathml
= substr ($contents, 33);
100 $sql_mathml = "'".wfStrencode($this->mathml
)."'";
101 $this->conservativeness
= 0;
102 } else if ($retval == "+") {
106 $sql_mathml = 'NULL';
107 $this->conservativeness
= 0;
109 $errbit = htmlspecialchars( substr($contents, 1) );
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 );
118 $this->hash
= substr ($contents, 1, 32);
119 if (!preg_match("/^[a-f0-9]{32}$/", $this->hash
)) {
120 return $this->_error( "math_unknown_error" );
123 if( !file_exists( "$wgMathDirectory/{$this->hash}.png" ) ) {
124 return $this->_error( "math_image_error" );
127 # Now save it back to the DB:
128 $outmd5_sql = wfStrencode(pack("H32", $this->hash
));
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.")";
133 $res = wfQuery( $sql, DB_WRITE
, "MathRenderer::render" );
134 # we don't really care if it fails
137 return $this->_doRender();
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";
149 global $wgMathDirectory;
151 $this->md5
= md5( $this->tex
);
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" );
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'];
162 $this->conservativeness
= $rpage->math_html_conservativeness
;
163 $this->html
= $rpage->math_html
;
164 $this->mathml
= $rpage->math_mathml
;
166 if( file_exists( "$wgMathDirectory/{$this->hash}.png" ) ) {
171 # Missing from the database and/or the render cache
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>";
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();
189 function _linkToMathImage() {
191 $url = htmlspecialchars( "$wgMathPath/{$this->hash}.png" );
192 $alt = htmlspecialchars( $this->tex
);
193 return "<img class='tex' src=\"$url\" alt=\"$alt\" />";
198 function renderMath( $tex ) {
200 $math = new MathRenderer( $tex );
201 $math->setOutputMode( $wgUser->getOption("math"));
202 return $math->render();