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;
37 if( $this->mode
== MW_MATH_SOURCE
) {
38 # No need to render or parse anything more!
39 return ('$ '.htmlspecialchars( $this->tex
).' $');
42 if( !$this->_recall() ) {
43 # Ensure that the temp and output directories are available before continuing...
44 if( !file_exists( $wgMathDirectory ) ) {
45 if( !@mkdir
( $wgMathDirectory ) ) {
46 return $this->_error( 'math_bad_output' );
48 } elseif( !is_dir( $wgMathDirectory ) ||
!is_writable( $wgMathDirectory ) ) {
49 return $this->_error( 'math_bad_output' );
51 if( !file_exists( $wgTmpDirectory ) ) {
52 if( !@mkdir
( $wgTmpDirectory ) ) {
53 return $this->_error( 'math_bad_tmpdir' );
55 } elseif( !is_dir( $wgTmpDirectory ) ||
!is_writable( $wgTmpDirectory ) ) {
56 return $this->_error( 'math_bad_tmpdir' );
59 if( !is_executable( $wgTexvc ) ) {
60 return $this->_error( 'math_notexvc' );
63 wfEscapeShellArg($wgTmpDirectory).' '.
64 wfEscapeShellArg($wgMathDirectory).' '.
65 wfEscapeShellArg($this->tex
).' '.
66 wfEscapeShellArg($wgInputEncoding);
67 wfDebug( 'TeX: '.$cmd );
70 if (strlen($contents) == 0) {
71 return $this->_error( 'math_unknown_error' );
74 $retval = substr ($contents, 0, 1);
75 if (($retval == 'C') ||
($retval == 'M') ||
($retval == 'L')) {
77 $this->conservativeness
= 2;
78 else if ($retval == 'M')
79 $this->conservativeness
= 1;
81 $this->conservativeness
= 0;
82 $outdata = substr ($contents, 33);
84 $i = strpos($outdata, "\000");
86 $this->html
= substr($outdata, 0, $i);
87 $this->mathml
= substr($outdata, $i+
1);
88 } else if (($retval == 'c') ||
($retval == 'm') ||
($retval == 'l')) {
89 $this->html
= substr ($contents, 33);
91 $this->conservativeness
= 2;
92 else if ($retval == 'm')
93 $this->conservativeness
= 1;
95 $this->conservativeness
= 0;
97 } else if ($retval == 'X') {
99 $this->mathml
= substr ($contents, 33);
100 $this->conservativeness
= 0;
101 } else if ($retval == '+') {
102 $this->outhtml
= NULL;
103 $this->mathml
= NULL;
104 $this->conservativeness
= 0;
106 $errbit = htmlspecialchars( substr($contents, 1) );
108 case 'E': return $this->_error( 'math_lexing_error', $errbit );
109 case 'S': return $this->_error( 'math_syntax_error', $errbit );
110 case 'F': return $this->_error( 'math_unknown_function', $errbit );
111 default: return $this->_error( 'math_unknown_error', $errbit );
115 $this->hash
= substr ($contents, 1, 32);
116 if (!preg_match("/^[a-f0-9]{32}$/", $this->hash
)) {
117 return $this->_error( 'math_unknown_error' );
120 if( !file_exists( "$wgMathDirectory/{$this->hash}.png" ) ) {
121 return $this->_error( 'math_image_error' );
124 # Now save it back to the DB:
125 $outmd5_sql = pack('H32', $this->hash
);
127 $md5_sql = pack('H32', $this->md5
); # Binary packed, not hex
129 $dbw =& wfGetDB( DB_MASTER
);
130 $dbw->replace( 'math', array( 'math_inputhash' ),
132 'math_inputhash' => $md5_sql,
133 'math_outputhash' => $outmd5_sql,
134 'math_html_conservativeness' => $this->conservativeness
,
135 'math_html' => $outhtml,
136 'math_mathml' => $mathml,
137 ), $fname, array( 'IGNORE' )
142 return $this->_doRender();
145 function _error( $msg, $append = '' ) {
146 $mf = htmlspecialchars( wfMsg( 'math_failure' ) );
147 $munk = htmlspecialchars( wfMsg( 'math_unknown_error' ) );
148 $errmsg = htmlspecialchars( wfMsg( $msg ) );
149 $source = htmlspecialchars($this->tex
);
150 return "<strong class='error'>$mf ($errmsg$append): $source</strong>\n";
154 global $wgMathDirectory;
155 $fname = 'MathRenderer::_recall';
157 $this->md5
= md5( $this->tex
);
158 $dbr =& wfGetDB( DB_SLAVE
);
159 $rpage = $dbr->selectRow( 'math',
160 array( 'math_outputhash','math_html_conservativeness','math_html','math_mathml' ),
161 array( 'math_inputhash' => pack("H32", $this->md5
)), # Binary packed, not hex
165 if( $rpage !== false ) {
166 # Tailing 0x20s can get dropped by the database, add it back on if necessary:
167 $xhash = unpack( 'H32md5', $rpage->math_outputhash
. " " );
168 $this->hash
= $xhash ['md5'];
170 $this->conservativeness
= $rpage->math_html_conservativeness
;
171 $this->html
= $rpage->math_html
;
172 $this->mathml
= $rpage->math_mathml
;
174 if( file_exists( "$wgMathDirectory/{$this->hash}.png" ) ) {
179 # Missing from the database and/or the render cache
184 * Select among PNG, HTML, or MathML output depending on
186 function _doRender() {
187 if( $this->mode
== MW_MATH_MATHML
&& $this->mathml
!= '' ) {
188 return "<math xmlns='http://www.w3.org/1998/Math/MathML'>{$this->mathml}</math>";
190 if (($this->mode
== MW_MATH_PNG
) ||
($this->html
== '') ||
191 (($this->mode
== MW_MATH_SIMPLE
) && ($this->conservativeness
!= 2)) ||
192 (($this->mode
== MW_MATH_MODERN ||
$this->mode
== MW_MATH_MATHML
) && ($this->conservativeness
== 0))) {
193 return $this->_linkToMathImage();
195 return '<span class="texhtml">'.$this->html
.'</span>';
199 function _linkToMathImage() {
201 $url = htmlspecialchars( "$wgMathPath/{$this->hash}.png" );
202 $alt = trim(str_replace("\n", ' ', htmlspecialchars( $this->tex
)));
203 return "<img class='tex' src=\"$url\" alt=\"$alt\" />";
208 function renderMath( $tex ) {
210 $math = new MathRenderer( $tex );
211 $math->setOutputMode( $wgUser->getOption('math'));
212 return $math->render();