3 * Contain everything related to <math> </math> parsing
7 * Takes LaTeX fragments, sends them to a helper program (texvc) for rendering
8 * to rasterized PNG and HTML and MathML approximations. An appropriate
9 * rendering form is picked and returned.
11 * by Tomasz Wegrzanowski, with additions by Brion Vibber (2003, 2004)
14 var $mode = MW_MATH_MODERN
;
20 var $conservativeness = 0;
22 function MathRenderer( $tex ) {
26 function setOutputMode( $mode ) {
31 global $wgMathDirectory, $wgTmpDirectory, $wgInputEncoding;
34 if( $this->mode
== MW_MATH_SOURCE
) {
35 # No need to render or parse anything more!
36 return ('$ '.htmlspecialchars( $this->tex
).' $');
39 if( !$this->_recall() ) {
40 # Ensure that the temp and output directories are available before continuing...
41 if( !file_exists( $wgMathDirectory ) ) {
42 if( !@mkdir
( $wgMathDirectory ) ) {
43 return $this->_error( 'math_bad_output' );
45 } elseif( !is_dir( $wgMathDirectory ) ||
!is_writable( $wgMathDirectory ) ) {
46 return $this->_error( 'math_bad_output' );
48 if( !file_exists( $wgTmpDirectory ) ) {
49 if( !@mkdir
( $wgTmpDirectory ) ) {
50 return $this->_error( 'math_bad_tmpdir' );
52 } elseif( !is_dir( $wgTmpDirectory ) ||
!is_writable( $wgTmpDirectory ) ) {
53 return $this->_error( 'math_bad_tmpdir' );
56 if( !is_executable( $wgTexvc ) ) {
57 return $this->_error( 'math_notexvc' );
60 wfEscapeShellArg($wgTmpDirectory).' '.
61 wfEscapeShellArg($wgMathDirectory).' '.
62 wfEscapeShellArg($this->tex
).' '.
63 wfEscapeShellArg($wgInputEncoding);
64 wfDebug( 'TeX: '.$cmd );
67 if (strlen($contents) == 0) {
68 return $this->_error( 'math_unknown_error' );
71 $retval = substr ($contents, 0, 1);
72 if (($retval == 'C') ||
($retval == 'M') ||
($retval == 'L')) {
74 $this->conservativeness
= 2;
75 else if ($retval == 'M')
76 $this->conservativeness
= 1;
78 $this->conservativeness
= 0;
79 $outdata = substr ($contents, 33);
81 $i = strpos($outdata, "\000");
83 $this->html
= substr($outdata, 0, $i);
84 $this->mathml
= substr($outdata, $i+
1);
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;
94 } else if ($retval == 'X') {
96 $this->mathml
= substr ($contents, 33);
97 $this->conservativeness
= 0;
98 } else if ($retval == '+') {
99 $this->outhtml
= NULL;
100 $this->mathml
= NULL;
101 $this->conservativeness
= 0;
103 $errbit = htmlspecialchars( substr($contents, 1) );
105 case 'E': return $this->_error( 'math_lexing_error', $errbit );
106 case 'S': return $this->_error( 'math_syntax_error', $errbit );
107 case 'F': return $this->_error( 'math_unknown_function', $errbit );
108 default: return $this->_error( 'math_unknown_error', $errbit );
112 $this->hash
= substr ($contents, 1, 32);
113 if (!preg_match("/^[a-f0-9]{32}$/", $this->hash
)) {
114 return $this->_error( 'math_unknown_error' );
117 if( !file_exists( "$wgMathDirectory/{$this->hash}.png" ) ) {
118 return $this->_error( 'math_image_error' );
121 # Now save it back to the DB:
122 $outmd5_sql = pack('H32', $this->hash
);
124 $md5_sql = pack('H32', $this->md5
); # Binary packed, not hex
126 $dbw =& wfGetDB( DB_MASTER
);
127 $dbw->replace( 'math', array( 'math_inputhash' ),
129 'math_inputhash' => $md5_sql,
130 'math_outputhash' => $outmd5_sql,
131 'math_html_conservativeness' => $this->conservativeness
,
132 'math_html' => $outhtml,
133 'math_mathml' => $mathml,
134 ), $fname, array( 'IGNORE' )
139 return $this->_doRender();
142 function _error( $msg, $append = '' ) {
143 $mf = htmlspecialchars( wfMsg( 'math_failure' ) );
144 $munk = htmlspecialchars( wfMsg( 'math_unknown_error' ) );
145 $errmsg = htmlspecialchars( wfMsg( $msg ) );
146 $source = htmlspecialchars($this->tex
);
147 return "<strong class='error'>$mf ($errmsg$append): $source</strong>\n";
151 global $wgMathDirectory;
152 $fname = 'MathRenderer::_recall';
154 $this->md5
= md5( $this->tex
);
155 $dbr =& wfGetDB( DB_SLAVE
);
156 $rpage = $dbr->getArray( 'math',
157 array( 'math_outputhash','math_html_conservativeness','math_html','math_mathml' ),
158 array( 'math_inputhash' => pack("H32", $this->md5
)), # Binary packed, not hex
162 if( $rpage !== false ) {
163 # Tailing 0x20s can get dropped by the database, add it back on if necessary:
164 $xhash = unpack( 'H32md5', $rpage->math_outputhash
. " " );
165 $this->hash
= $xhash ['md5'];
167 $this->conservativeness
= $rpage->math_html_conservativeness
;
168 $this->html
= $rpage->math_html
;
169 $this->mathml
= $rpage->math_mathml
;
171 if( file_exists( "$wgMathDirectory/{$this->hash}.png" ) ) {
176 # Missing from the database and/or the render cache
181 * Select among PNG, HTML, or MathML output depending on
183 function _doRender() {
184 if( $this->mode
== MW_MATH_MATHML
&& $this->mathml
!= '' ) {
185 return "<math xmlns='http://www.w3.org/1998/Math/MathML'>{$this->mathml}</math>";
187 if (($this->mode
== MW_MATH_PNG
) ||
($this->html
== '') ||
188 (($this->mode
== MW_MATH_SIMPLE
) && ($this->conservativeness
!= 2)) ||
189 (($this->mode
== MW_MATH_MODERN ||
$this->mode
== MW_MATH_MATHML
) && ($this->conservativeness
== 0))) {
190 return $this->_linkToMathImage();
192 return '<span class="texhtml">'.$this->html
.'</span>';
196 function _linkToMathImage() {
198 $url = htmlspecialchars( "$wgMathPath/{$this->hash}.png" );
199 $alt = trim(str_replace("\n", ' ', htmlspecialchars( $this->tex
)));
200 return "<img class='tex' src=\"$url\" alt=\"$alt\" />";
205 function renderMath( $tex ) {
207 $math = new MathRenderer( $tex );
208 $math->setOutputMode( $wgUser->getOption('math'));
209 return $math->render();