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 $hashpath = $this->_getHashPath();
47 if( !file_exists( $hashpath ) ) {
48 if( !@wfMkdirParents
( $hashpath, 0755 ) ) {
49 return $this->_error( 'math_bad_output' );
51 } elseif( !is_dir( $hashpath ) ||
!is_writable( $hashpath ) ) {
52 return $this->_error( 'math_bad_output' );
54 if( !file_exists( $wgTmpDirectory ) ) {
55 if( !@mkdir
( $wgTmpDirectory ) ) {
56 return $this->_error( 'math_bad_tmpdir' );
58 } elseif( !is_dir( $wgTmpDirectory ) ||
!is_writable( $wgTmpDirectory ) ) {
59 return $this->_error( 'math_bad_tmpdir' );
62 if( function_exists( 'is_executable' ) && !is_executable( $wgTexvc ) ) {
63 return $this->_error( 'math_notexvc' );
65 $cmd = $wgTexvc . ' ' .
66 escapeshellarg( $wgTmpDirectory ).' '.
67 escapeshellarg( $hashpath ).' '.
68 escapeshellarg( $this->tex
).' '.
69 escapeshellarg( $wgInputEncoding );
71 if ( wfIsWindows() ) {
72 # Invoke it within cygwin sh, because texvc expects sh features in its default shell
73 $cmd = 'sh -c ' . wfEscapeShellArg( $cmd );
76 wfDebug( "TeX: $cmd\n" );
78 wfDebug( "TeX output:\n $contents\n---\n" );
80 if (strlen($contents) == 0) {
81 return $this->_error( 'math_unknown_error' );
84 $retval = substr ($contents, 0, 1);
85 if (($retval == 'C') ||
($retval == 'M') ||
($retval == 'L')) {
87 $this->conservativeness
= 2;
88 else if ($retval == 'M')
89 $this->conservativeness
= 1;
91 $this->conservativeness
= 0;
92 $outdata = substr ($contents, 33);
94 $i = strpos($outdata, "\000");
96 $this->html
= substr($outdata, 0, $i);
97 $this->mathml
= substr($outdata, $i+
1);
98 } else if (($retval == 'c') ||
($retval == 'm') ||
($retval == 'l')) {
99 $this->html
= substr ($contents, 33);
101 $this->conservativeness
= 2;
102 else if ($retval == 'm')
103 $this->conservativeness
= 1;
105 $this->conservativeness
= 0;
106 $this->mathml
= NULL;
107 } else if ($retval == 'X') {
109 $this->mathml
= substr ($contents, 33);
110 $this->conservativeness
= 0;
111 } else if ($retval == '+') {
113 $this->mathml
= NULL;
114 $this->conservativeness
= 0;
116 $errbit = htmlspecialchars( substr($contents, 1) );
118 case 'E': return $this->_error( 'math_lexing_error', $errbit );
119 case 'S': return $this->_error( 'math_syntax_error', $errbit );
120 case 'F': return $this->_error( 'math_unknown_function', $errbit );
121 default: return $this->_error( 'math_unknown_error', $errbit );
125 $this->hash
= substr ($contents, 1, 32);
126 if (!preg_match("/^[a-f0-9]{32}$/", $this->hash
)) {
127 return $this->_error( 'math_unknown_error' );
130 if( !file_exists( "$hashpath/{$this->hash}.png" ) ) {
131 return $this->_error( 'math_image_error' );
134 # Now save it back to the DB:
135 if ( !wfReadOnly() ) {
136 $outmd5_sql = pack('H32', $this->hash
);
138 $md5_sql = pack('H32', $this->md5
); # Binary packed, not hex
140 $dbw =& wfGetDB( DB_MASTER
);
141 $dbw->replace( 'math', array( 'math_inputhash' ),
143 'math_inputhash' => $md5_sql,
144 'math_outputhash' => $outmd5_sql,
145 'math_html_conservativeness' => $this->conservativeness
,
146 'math_html' => $this->html
,
147 'math_mathml' => $this->mathml
,
148 ), $fname, array( 'IGNORE' )
154 return $this->_doRender();
157 function _error( $msg, $append = '' ) {
158 $mf = htmlspecialchars( wfMsg( 'math_failure' ) );
159 $munk = htmlspecialchars( wfMsg( 'math_unknown_error' ) );
160 $errmsg = htmlspecialchars( wfMsg( $msg ) );
161 $source = htmlspecialchars($this->tex
);
162 return "<strong class='error'>$mf ($errmsg$append): $source</strong>\n";
166 global $wgMathDirectory;
167 $fname = 'MathRenderer::_recall';
169 $this->md5
= md5( $this->tex
);
170 $dbr =& wfGetDB( DB_SLAVE
);
171 $rpage = $dbr->selectRow( 'math',
172 array( 'math_outputhash','math_html_conservativeness','math_html','math_mathml' ),
173 array( 'math_inputhash' => pack("H32", $this->md5
)), # Binary packed, not hex
177 if( $rpage !== false ) {
178 # Tailing 0x20s can get dropped by the database, add it back on if necessary:
179 $xhash = unpack( 'H32md5', $rpage->math_outputhash
. " " );
180 $this->hash
= $xhash ['md5'];
182 $this->conservativeness
= $rpage->math_html_conservativeness
;
183 $this->html
= $rpage->math_html
;
184 $this->mathml
= $rpage->math_mathml
;
186 if( file_exists( $this->_getHashPath() . "/{$this->hash}.png" ) ) {
190 if( file_exists( $wgMathDirectory . "/{$this->hash}.png" ) ) {
191 $hashpath = $this->_getHashPath();
193 if( !file_exists( $hashpath ) ) {
194 if( !@wfMkdirParents
( $hashpath, 0755 ) ) {
197 } elseif( !is_dir( $hashpath ) ||
!is_writable( $hashpath ) ) {
200 if ( function_exists( "link" ) ) {
201 return link ( $wgMathDirectory . "/{$this->hash}.png",
202 $hashpath . "/{$this->hash}.png" );
204 return rename ( $wgMathDirectory . "/{$this->hash}.png",
205 $hashpath . "/{$this->hash}.png" );
211 # Missing from the database and/or the render cache
216 * Select among PNG, HTML, or MathML output depending on
218 function _doRender() {
219 if( $this->mode
== MW_MATH_MATHML
&& $this->mathml
!= '' ) {
220 return "<math xmlns='http://www.w3.org/1998/Math/MathML'>{$this->mathml}</math>";
222 if (($this->mode
== MW_MATH_PNG
) ||
($this->html
== '') ||
223 (($this->mode
== MW_MATH_SIMPLE
) && ($this->conservativeness
!= 2)) ||
224 (($this->mode
== MW_MATH_MODERN ||
$this->mode
== MW_MATH_MATHML
) && ($this->conservativeness
== 0))) {
225 return $this->_linkToMathImage();
227 return '<span class="texhtml">'.$this->html
.'</span>';
231 function _linkToMathImage() {
233 $url = htmlspecialchars( "$wgMathPath/" . substr($this->hash
, 0, 1)
234 .'/'. substr($this->hash
, 1, 1) .'/'. substr($this->hash
, 2, 1)
235 . "/{$this->hash}.png" );
236 $alt = trim(str_replace("\n", ' ', htmlspecialchars( $this->tex
)));
237 return "<img class='tex' src=\"$url\" alt=\"$alt\" />";
240 function _getHashPath() {
241 global $wgMathDirectory;
242 return $wgMathDirectory .'/'. substr($this->hash
, 0, 1)
243 .'/'. substr($this->hash
, 1, 1)
244 .'/'. substr($this->hash
, 2, 1);
250 function renderMath( $tex ) {
252 $math = new MathRenderer( $tex );
253 $math->setOutputMode( $wgUser->getOption('math'));
254 return $math->render();