3 * Contain everything related to <math> </math> parsing
8 * Takes LaTeX fragments, sends them to a helper program (texvc) for rendering
9 * to rasterized PNG and HTML and MathML approximations. An appropriate
10 * rendering form is picked and returned.
12 * by Tomasz Wegrzanowski, with additions by Brion Vibber (2003, 2004)
17 var $mode = MW_MATH_MODERN
;
23 var $conservativeness = 0;
25 function MathRenderer( $tex ) {
29 function setOutputMode( $mode ) {
34 global $wgMathDirectory, $wgTmpDirectory, $wgInputEncoding;
36 $fname = 'MathRenderer::render';
38 if( $this->mode
== MW_MATH_SOURCE
) {
39 # No need to render or parse anything more!
40 return ('$ '.htmlspecialchars( $this->tex
).' $');
43 if( !$this->_recall() ) {
44 # Ensure that the temp and output directories are available before continuing...
45 if( !file_exists( $wgMathDirectory ) ) {
46 if( !@mkdir
( $wgMathDirectory ) ) {
47 return $this->_error( 'math_bad_output' );
49 } elseif( !is_dir( $wgMathDirectory ) ||
!is_writable( $wgMathDirectory ) ) {
50 return $this->_error( 'math_bad_output' );
52 if( !file_exists( $wgTmpDirectory ) ) {
53 if( !@mkdir
( $wgTmpDirectory ) ) {
54 return $this->_error( 'math_bad_tmpdir' );
56 } elseif( !is_dir( $wgTmpDirectory ) ||
!is_writable( $wgTmpDirectory ) ) {
57 return $this->_error( 'math_bad_tmpdir' );
60 if( function_exists( 'is_executable' ) && !is_executable( $wgTexvc ) ) {
61 return $this->_error( 'math_notexvc' );
63 $cmd = $wgTexvc . ' ' .
64 escapeshellarg( $wgTmpDirectory ).' '.
65 escapeshellarg( $wgMathDirectory ).' '.
66 escapeshellarg( $this->tex
).' '.
67 escapeshellarg( $wgInputEncoding );
69 if ( wfIsWindows() ) {
70 # Invoke it within cygwin sh, because texvc expects sh features in its default shell
71 $cmd = 'sh -c ' . wfEscapeShellArg( $cmd );
74 wfDebug( "TeX: $cmd\n" );
76 wfDebug( "TeX output:\n $contents\n---\n" );
78 if (strlen($contents) == 0) {
79 return $this->_error( 'math_unknown_error' );
82 $retval = substr ($contents, 0, 1);
83 if (($retval == 'C') ||
($retval == 'M') ||
($retval == 'L')) {
85 $this->conservativeness
= 2;
86 else if ($retval == 'M')
87 $this->conservativeness
= 1;
89 $this->conservativeness
= 0;
90 $outdata = substr ($contents, 33);
92 $i = strpos($outdata, "\000");
94 $this->html
= substr($outdata, 0, $i);
95 $this->mathml
= substr($outdata, $i+
1);
96 } else if (($retval == 'c') ||
($retval == 'm') ||
($retval == 'l')) {
97 $this->html
= substr ($contents, 33);
99 $this->conservativeness
= 2;
100 else if ($retval == 'm')
101 $this->conservativeness
= 1;
103 $this->conservativeness
= 0;
104 $this->mathml
= NULL;
105 } else if ($retval == 'X') {
107 $this->mathml
= substr ($contents, 33);
108 $this->conservativeness
= 0;
109 } else if ($retval == '+') {
111 $this->mathml
= NULL;
112 $this->conservativeness
= 0;
114 $errbit = htmlspecialchars( substr($contents, 1) );
116 case 'E': return $this->_error( 'math_lexing_error', $errbit );
117 case 'S': return $this->_error( 'math_syntax_error', $errbit );
118 case 'F': return $this->_error( 'math_unknown_function', $errbit );
119 default: return $this->_error( 'math_unknown_error', $errbit );
123 $this->hash
= substr ($contents, 1, 32);
124 if (!preg_match("/^[a-f0-9]{32}$/", $this->hash
)) {
125 return $this->_error( 'math_unknown_error' );
128 if( !file_exists( "$wgMathDirectory/{$this->hash}.png" ) ) {
129 return $this->_error( 'math_image_error' );
132 # Now save it back to the DB:
133 if ( !wfReadOnly() ) {
134 $outmd5_sql = pack('H32', $this->hash
);
136 $md5_sql = pack('H32', $this->md5
); # Binary packed, not hex
138 $dbw =& wfGetDB( DB_MASTER
);
139 $dbw->replace( 'math', array( 'math_inputhash' ),
141 'math_inputhash' => $md5_sql,
142 'math_outputhash' => $outmd5_sql,
143 'math_html_conservativeness' => $this->conservativeness
,
144 'math_html' => $this->html
,
145 'math_mathml' => $this->mathml
,
146 ), $fname, array( 'IGNORE' )
152 return $this->_doRender();
155 function _error( $msg, $append = '' ) {
156 $mf = htmlspecialchars( wfMsg( 'math_failure' ) );
157 $munk = htmlspecialchars( wfMsg( 'math_unknown_error' ) );
158 $errmsg = htmlspecialchars( wfMsg( $msg ) );
159 $source = htmlspecialchars($this->tex
);
160 return "<strong class='error'>$mf ($errmsg$append): $source</strong>\n";
164 global $wgMathDirectory;
165 $fname = 'MathRenderer::_recall';
167 $this->md5
= md5( $this->tex
);
168 $dbr =& wfGetDB( DB_SLAVE
);
169 $rpage = $dbr->selectRow( 'math',
170 array( 'math_outputhash','math_html_conservativeness','math_html','math_mathml' ),
171 array( 'math_inputhash' => pack("H32", $this->md5
)), # Binary packed, not hex
175 if( $rpage !== false ) {
176 # Tailing 0x20s can get dropped by the database, add it back on if necessary:
177 $xhash = unpack( 'H32md5', $rpage->math_outputhash
. " " );
178 $this->hash
= $xhash ['md5'];
180 $this->conservativeness
= $rpage->math_html_conservativeness
;
181 $this->html
= $rpage->math_html
;
182 $this->mathml
= $rpage->math_mathml
;
184 if( file_exists( "$wgMathDirectory/{$this->hash}.png" ) ) {
189 # Missing from the database and/or the render cache
194 * Select among PNG, HTML, or MathML output depending on
196 function _doRender() {
197 if( $this->mode
== MW_MATH_MATHML
&& $this->mathml
!= '' ) {
198 return "<math xmlns='http://www.w3.org/1998/Math/MathML'>{$this->mathml}</math>";
200 if (($this->mode
== MW_MATH_PNG
) ||
($this->html
== '') ||
201 (($this->mode
== MW_MATH_SIMPLE
) && ($this->conservativeness
!= 2)) ||
202 (($this->mode
== MW_MATH_MODERN ||
$this->mode
== MW_MATH_MATHML
) && ($this->conservativeness
== 0))) {
203 return $this->_linkToMathImage();
205 return '<span class="texhtml">'.$this->html
.'</span>';
209 function _linkToMathImage() {
211 $url = htmlspecialchars( "$wgMathPath/{$this->hash}.png" );
212 $alt = trim(str_replace("\n", ' ', htmlspecialchars( $this->tex
)));
213 return "<img class='tex' src=\"$url\" alt=\"$alt\" />";
218 function renderMath( $tex ) {
220 $math = new MathRenderer( $tex );
221 $math->setOutputMode( $wgUser->getOption('math'));
222 return $math->render();