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( $this->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 wfEscapeShellArg($wgTmpDirectory)." ".
58 wfEscapeShellArg($wgMathDirectory)." ".
59 wfEscapeShellArg($this->tex
)." ".
60 wfEscapeShellArg($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);
82 } else if (($retval == "c") ||
($retval == "m") ||
($retval == "l")) {
83 $this->html
= substr ($contents, 33);
85 $this->conservativeness
= 2;
86 else if ($retval == "m")
87 $this->conservativeness
= 1;
89 $this->conservativeness
= 0;
91 } else if ($retval == "X") {
93 $this->mathml
= substr ($contents, 33);
94 $this->conservativeness
= 0;
95 } else if ($retval == "+") {
96 $this->outhtml
= NULL;
98 $this->conservativeness
= 0;
100 $errbit = htmlspecialchars( substr($contents, 1) );
102 case "E": return $this->_error( "math_lexing_error", $errbit );
103 case "S": return $this->_error( "math_syntax_error", $errbit );
104 case "F": return $this->_error( "math_unknown_function", $errbit );
105 default: return $this->_error( "math_unknown_error", $errbit );
109 $this->hash
= substr ($contents, 1, 32);
110 if (!preg_match("/^[a-f0-9]{32}$/", $this->hash
)) {
111 return $this->_error( "math_unknown_error" );
114 if( !file_exists( "$wgMathDirectory/{$this->hash}.png" ) ) {
115 return $this->_error( "math_image_error" );
118 # Now save it back to the DB:
119 $outmd5_sql = pack("H32", $this->hash
);
121 $md5_sql = pack("H32", $this->md5
); # Binary packed, not hex
123 $dbw =& wfGetDB( DB_WRITE
);
124 $dbw->replace( 'math', array( 'math_inputhash' ),
126 'math_inputhash' => $md5_sql,
127 'math_outputhash' => $outmd5_sql,
128 'math_html_conservativeness' => $this->conservativeness
,
129 'math_html' => $outhtml,
130 'math_mathml' => $mathml,
131 ), $fname, array( 'IGNORE' )
136 return $this->_doRender();
139 function _error( $msg, $append = "" ) {
140 $mf = htmlspecialchars( wfMsg( "math_failure" ) );
141 $munk = htmlspecialchars( wfMsg( "math_unknown_error" ) );
142 $errmsg = htmlspecialchars( wfMsg( $msg ) );
143 $source = htmlspecialchars($this->tex
);
144 return "<strong class='error'>$mf ($errmsg$append): $source</strong>\n";
148 global $wgMathDirectory;
149 $fname = 'MathRenderer::_recall';
151 $this->md5
= md5( $this->tex
);
152 $dbr =& wfGetDB( DB_READ
);
153 $rpage = $dbr->getArray( 'math',
154 array( 'math_outputhash','math_html_conservativeness','math_html','math_mathml' ),
155 array( 'math_inputhash' => pack("H32", $this->md5
)), # Binary packed, not hex
159 if( $rpage !== false ) {
160 # Tailing 0x20s can get dropped by the database, add it back on if necessary:
161 $xhash = unpack( "H32md5", $rpage->math_outputhash
. " " );
162 $this->hash
= $xhash ['md5'];
164 $this->conservativeness
= $rpage->math_html_conservativeness
;
165 $this->html
= $rpage->math_html
;
166 $this->mathml
= $rpage->math_mathml
;
168 if( file_exists( "$wgMathDirectory/{$this->hash}.png" ) ) {
173 # Missing from the database and/or the render cache
177 # Select among PNG, HTML, or MathML output depending on
178 function _doRender() {
179 if( $this->mode
== MW_MATH_MATHML
&& $this->mathml
!= '' ) {
180 return "<math xmlns='http://www.w3.org/1998/Math/MathML'>{$this->mathml}</math>";
182 if (($this->mode
== MW_MATH_PNG
) ||
($this->html
== '') ||
183 (($this->mode
== MW_MATH_SIMPLE
) && ($this->conservativeness
!= 2)) ||
184 (($this->mode
== MW_MATH_MODERN ||
$this->mode
== MW_MATH_MATHML
) && ($this->conservativeness
== 0))) {
185 return $this->_linkToMathImage();
187 return '<span class="texhtml">'.$this->html
.'</span>';
191 function _linkToMathImage() {
193 $url = htmlspecialchars( "$wgMathPath/{$this->hash}.png" );
194 $alt = trim(str_replace("\n", ' ', htmlspecialchars( $this->tex
)));
195 return "<img class='tex' src=\"$url\" alt=\"$alt\" />";
200 function renderMath( $tex ) {
202 $math = new MathRenderer( $tex );
203 $math->setOutputMode( $wgUser->getOption("math"));
204 return $math->render();