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 $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( $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( function_exists( 'is_executable' ) && !is_executable( $wgTexvc ) ) {
54 return $this->_error( 'math_notexvc' );
56 $cmd = $wgTexvc . ' ' .
57 escapeshellarg( $wgTmpDirectory ).' '.
58 escapeshellarg( $wgTmpDirectory ).' '.
59 escapeshellarg( $this->tex
).' '.
60 escapeshellarg( $wgInputEncoding );
62 if ( wfIsWindows() ) {
63 # Invoke it within cygwin sh, because texvc expects sh features in its default shell
64 $cmd = 'sh -c ' . wfEscapeShellArg( $cmd );
67 wfDebug( "TeX: $cmd\n" );
69 wfDebug( "TeX output:\n $contents\n---\n" );
71 if (strlen($contents) == 0) {
72 return $this->_error( 'math_unknown_error' );
75 $retval = substr ($contents, 0, 1);
77 if (($retval == 'C') ||
($retval == 'M') ||
($retval == 'L')) {
79 $this->conservativeness
= 2;
80 } else if ($retval == 'M') {
81 $this->conservativeness
= 1;
83 $this->conservativeness
= 0;
85 $outdata = substr ($contents, 33);
87 $i = strpos($outdata, "\000");
89 $this->html
= substr($outdata, 0, $i);
90 $this->mathml
= substr($outdata, $i+
1);
91 } else if (($retval == 'c') ||
($retval == 'm') ||
($retval == 'l')) {
92 $this->html
= substr ($contents, 33);
94 $this->conservativeness
= 2;
95 } else if ($retval == 'm') {
96 $this->conservativeness
= 1;
98 $this->conservativeness
= 0;
100 $this->mathml
= NULL;
101 } else if ($retval == 'X') {
103 $this->mathml
= substr ($contents, 33);
104 $this->conservativeness
= 0;
105 } else if ($retval == '+') {
107 $this->mathml
= NULL;
108 $this->conservativeness
= 0;
110 $errbit = htmlspecialchars( substr($contents, 1) );
112 case 'E': $errmsg = $this->_error( 'math_lexing_error', $errbit );
113 case 'S': $errmsg = $this->_error( 'math_syntax_error', $errbit );
114 case 'F': $errmsg = $this->_error( 'math_unknown_function', $errbit );
115 default: $errmsg = $this->_error( 'math_unknown_error', $errbit );
120 $this->hash
= substr ($contents, 1, 32);
123 wfRunHooks( 'MathAfterTexvc', array( &$this, &$errmsg ) );
129 if (!preg_match("/^[a-f0-9]{32}$/", $this->hash
)) {
130 return $this->_error( 'math_unknown_error' );
133 if( !file_exists( "$wgTmpDirectory/{$this->hash}.png" ) ) {
134 return $this->_error( 'math_image_error' );
137 $hashpath = $this->_getHashPath();
138 if( !file_exists( $hashpath ) ) {
139 if( !@wfMkdirParents
( $hashpath, 0755 ) ) {
140 return $this->_error( 'math_bad_output' );
142 } elseif( !is_dir( $hashpath ) ||
!is_writable( $hashpath ) ) {
143 return $this->_error( 'math_bad_output' );
146 if( !rename( "$wgTmpDirectory/{$this->hash}.png", "$hashpath/{$this->hash}.png" ) ) {
147 return $this->_error( 'math_output_error' );
150 # Now save it back to the DB:
151 if ( !wfReadOnly() ) {
152 $outmd5_sql = pack('H32', $this->hash
);
154 $md5_sql = pack('H32', $this->md5
); # Binary packed, not hex
156 $dbw =& wfGetDB( DB_MASTER
);
157 $dbw->replace( 'math', array( 'math_inputhash' ),
159 'math_inputhash' => $md5_sql,
160 'math_outputhash' => $outmd5_sql,
161 'math_html_conservativeness' => $this->conservativeness
,
162 'math_html' => $this->html
,
163 'math_mathml' => $this->mathml
,
164 ), $fname, array( 'IGNORE' )
170 return $this->_doRender();
173 function _error( $msg, $append = '' ) {
174 $mf = htmlspecialchars( wfMsg( 'math_failure' ) );
175 $errmsg = htmlspecialchars( wfMsg( $msg ) );
176 $source = htmlspecialchars( str_replace( "\n", ' ', $this->tex
) );
177 return "<strong class='error'>$mf ($errmsg$append): $source</strong>\n";
181 global $wgMathDirectory;
182 $fname = 'MathRenderer::_recall';
184 $this->md5
= md5( $this->tex
);
185 $dbr =& wfGetDB( DB_SLAVE
);
186 $rpage = $dbr->selectRow( 'math',
187 array( 'math_outputhash','math_html_conservativeness','math_html','math_mathml' ),
188 array( 'math_inputhash' => pack("H32", $this->md5
)), # Binary packed, not hex
192 if( $rpage !== false ) {
193 # Tailing 0x20s can get dropped by the database, add it back on if necessary:
194 $xhash = unpack( 'H32md5', $rpage->math_outputhash
. " " );
195 $this->hash
= $xhash ['md5'];
197 $this->conservativeness
= $rpage->math_html_conservativeness
;
198 $this->html
= $rpage->math_html
;
199 $this->mathml
= $rpage->math_mathml
;
201 if( file_exists( $this->_getHashPath() . "/{$this->hash}.png" ) ) {
205 if( file_exists( $wgMathDirectory . "/{$this->hash}.png" ) ) {
206 $hashpath = $this->_getHashPath();
208 if( !file_exists( $hashpath ) ) {
209 if( !@wfMkdirParents
( $hashpath, 0755 ) ) {
212 } elseif( !is_dir( $hashpath ) ||
!is_writable( $hashpath ) ) {
215 if ( function_exists( "link" ) ) {
216 return link ( $wgMathDirectory . "/{$this->hash}.png",
217 $hashpath . "/{$this->hash}.png" );
219 return rename ( $wgMathDirectory . "/{$this->hash}.png",
220 $hashpath . "/{$this->hash}.png" );
226 # Missing from the database and/or the render cache
231 * Select among PNG, HTML, or MathML output depending on
233 function _doRender() {
234 if( $this->mode
== MW_MATH_MATHML
&& $this->mathml
!= '' ) {
235 return "<math xmlns='http://www.w3.org/1998/Math/MathML'>{$this->mathml}</math>";
237 if (($this->mode
== MW_MATH_PNG
) ||
($this->html
== '') ||
238 (($this->mode
== MW_MATH_SIMPLE
) && ($this->conservativeness
!= 2)) ||
239 (($this->mode
== MW_MATH_MODERN ||
$this->mode
== MW_MATH_MATHML
) && ($this->conservativeness
== 0))) {
240 return $this->_linkToMathImage();
242 return '<span class="texhtml">'.$this->html
.'</span>';
246 function _linkToMathImage() {
248 $url = htmlspecialchars( "$wgMathPath/" . substr($this->hash
, 0, 1)
249 .'/'. substr($this->hash
, 1, 1) .'/'. substr($this->hash
, 2, 1)
250 . "/{$this->hash}.png" );
251 $alt = trim(str_replace("\n", ' ', htmlspecialchars( $this->tex
)));
252 return "<img class='tex' src=\"$url\" alt=\"$alt\" />";
255 function _getHashPath() {
256 global $wgMathDirectory;
257 $path = $wgMathDirectory .'/'. substr($this->hash
, 0, 1)
258 .'/'. substr($this->hash
, 1, 1)
259 .'/'. substr($this->hash
, 2, 1);
260 wfDebug( "TeX: getHashPath, hash is: $this->hash, path is: $path\n" );
264 public static function renderMath( $tex ) {
266 $math = new MathRenderer( $tex );
267 $math->setOutputMode( $wgUser->getOption('math'));
268 return $math->render();