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( !is_executable( $wgTexvc ) ) {
61 return $this->_error( 'math_notexvc' );
64 wfEscapeShellArg($wgTmpDirectory).' '.
65 wfEscapeShellArg($wgMathDirectory).' '.
66 wfEscapeShellArg($this->tex
).' '.
67 wfEscapeShellArg($wgInputEncoding);
68 wfDebug( 'TeX: '.$cmd );
71 if (strlen($contents) == 0) {
72 return $this->_error( 'math_unknown_error' );
75 $retval = substr ($contents, 0, 1);
76 if (($retval == 'C') ||
($retval == 'M') ||
($retval == 'L')) {
78 $this->conservativeness
= 2;
79 else if ($retval == 'M')
80 $this->conservativeness
= 1;
82 $this->conservativeness
= 0;
83 $outdata = substr ($contents, 33);
85 $i = strpos($outdata, "\000");
87 $this->html
= substr($outdata, 0, $i);
88 $this->mathml
= substr($outdata, $i+
1);
89 } else if (($retval == 'c') ||
($retval == 'm') ||
($retval == 'l')) {
90 $this->html
= substr ($contents, 33);
92 $this->conservativeness
= 2;
93 else if ($retval == 'm')
94 $this->conservativeness
= 1;
96 $this->conservativeness
= 0;
98 } else if ($retval == 'X') {
100 $this->mathml
= substr ($contents, 33);
101 $this->conservativeness
= 0;
102 } else if ($retval == '+') {
104 $this->mathml
= NULL;
105 $this->conservativeness
= 0;
107 $errbit = htmlspecialchars( substr($contents, 1) );
109 case 'E': return $this->_error( 'math_lexing_error', $errbit );
110 case 'S': return $this->_error( 'math_syntax_error', $errbit );
111 case 'F': return $this->_error( 'math_unknown_function', $errbit );
112 default: return $this->_error( 'math_unknown_error', $errbit );
116 $this->hash
= substr ($contents, 1, 32);
117 if (!preg_match("/^[a-f0-9]{32}$/", $this->hash
)) {
118 return $this->_error( 'math_unknown_error' );
121 if( !file_exists( "$wgMathDirectory/{$this->hash}.png" ) ) {
122 return $this->_error( 'math_image_error' );
125 # Now save it back to the DB:
126 if ( !wfReadOnly() ) {
127 $outmd5_sql = pack('H32', $this->hash
);
129 $md5_sql = pack('H32', $this->md5
); # Binary packed, not hex
131 $dbw =& wfGetDB( DB_MASTER
);
132 $dbw->replace( 'math', array( 'math_inputhash' ),
134 'math_inputhash' => $md5_sql,
135 'math_outputhash' => $outmd5_sql,
136 'math_html_conservativeness' => $this->conservativeness
,
137 'math_html' => $this->html
,
138 'math_mathml' => $this->mathml
,
139 ), $fname, array( 'IGNORE' )
145 return $this->_doRender();
148 function _error( $msg, $append = '' ) {
149 $mf = htmlspecialchars( wfMsg( 'math_failure' ) );
150 $munk = htmlspecialchars( wfMsg( 'math_unknown_error' ) );
151 $errmsg = htmlspecialchars( wfMsg( $msg ) );
152 $source = htmlspecialchars($this->tex
);
153 return "<strong class='error'>$mf ($errmsg$append): $source</strong>\n";
157 global $wgMathDirectory;
158 $fname = 'MathRenderer::_recall';
160 $this->md5
= md5( $this->tex
);
161 $dbr =& wfGetDB( DB_SLAVE
);
162 $rpage = $dbr->selectRow( 'math',
163 array( 'math_outputhash','math_html_conservativeness','math_html','math_mathml' ),
164 array( 'math_inputhash' => pack("H32", $this->md5
)), # Binary packed, not hex
168 if( $rpage !== false ) {
169 # Tailing 0x20s can get dropped by the database, add it back on if necessary:
170 $xhash = unpack( 'H32md5', $rpage->math_outputhash
. " " );
171 $this->hash
= $xhash ['md5'];
173 $this->conservativeness
= $rpage->math_html_conservativeness
;
174 $this->html
= $rpage->math_html
;
175 $this->mathml
= $rpage->math_mathml
;
177 if( file_exists( "$wgMathDirectory/{$this->hash}.png" ) ) {
182 # Missing from the database and/or the render cache
187 * Select among PNG, HTML, or MathML output depending on
189 function _doRender() {
190 if( $this->mode
== MW_MATH_MATHML
&& $this->mathml
!= '' ) {
191 return "<math xmlns='http://www.w3.org/1998/Math/MathML'>{$this->mathml}</math>";
193 if (($this->mode
== MW_MATH_PNG
) ||
($this->html
== '') ||
194 (($this->mode
== MW_MATH_SIMPLE
) && ($this->conservativeness
!= 2)) ||
195 (($this->mode
== MW_MATH_MODERN ||
$this->mode
== MW_MATH_MATHML
) && ($this->conservativeness
== 0))) {
196 return $this->_linkToMathImage();
198 return '<span class="texhtml">'.$this->html
.'</span>';
202 function _linkToMathImage() {
204 $url = htmlspecialchars( "$wgMathPath/{$this->hash}.png" );
205 $alt = trim(str_replace("\n", ' ', htmlspecialchars( $this->tex
)));
206 return "<img class='tex' src=\"$url\" alt=\"$alt\" />";
211 function renderMath( $tex ) {
213 $math = new MathRenderer( $tex );
214 $math->setOutputMode( $wgUser->getOption('math'));
215 return $math->render();