case
[lhc/web/wiklou.git] / includes / Math.php
1 <?php
2 /**
3 * Contain everything related to <math> </math> parsing
4 * @package MediaWiki
5 */
6
7 /**
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.
11 *
12 * by Tomasz Wegrzanowski, with additions by Brion Vibber (2003, 2004)
13 *
14 * @package MediaWiki
15 */
16 class MathRenderer {
17 var $mode = MW_MATH_MODERN;
18 var $tex = '';
19 var $inputhash = '';
20 var $hash = '';
21 var $html = '';
22 var $mathml = '';
23 var $conservativeness = 0;
24
25 function MathRenderer( $tex ) {
26 $this->tex = $tex;
27 }
28
29 function setOutputMode( $mode ) {
30 $this->mode = $mode;
31 }
32
33 function render() {
34 global $wgMathDirectory, $wgTmpDirectory, $wgInputEncoding;
35 global $wgTexvc;
36 $fname = 'MathRenderer::render';
37
38 if( $this->mode == MW_MATH_SOURCE ) {
39 # No need to render or parse anything more!
40 return ('$ '.htmlspecialchars( $this->tex ).' $');
41 }
42
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' );
48 }
49 } elseif( !is_dir( $wgMathDirectory ) || !is_writable( $wgMathDirectory ) ) {
50 return $this->_error( 'math_bad_output' );
51 }
52 if( !file_exists( $wgTmpDirectory ) ) {
53 if( !@mkdir( $wgTmpDirectory ) ) {
54 return $this->_error( 'math_bad_tmpdir' );
55 }
56 } elseif( !is_dir( $wgTmpDirectory ) || !is_writable( $wgTmpDirectory ) ) {
57 return $this->_error( 'math_bad_tmpdir' );
58 }
59
60 if( function_exists( 'is_executable' ) && !is_executable( $wgTexvc ) ) {
61 return $this->_error( 'math_notexvc' );
62 }
63 $cmd = $wgTexvc . ' ' .
64 escapeshellarg( $wgTmpDirectory ).' '.
65 escapeshellarg( $wgMathDirectory ).' '.
66 escapeshellarg( $this->tex ).' '.
67 escapeshellarg( $wgInputEncoding );
68
69 if ( wfIsWindows() ) {
70 # Invoke it within cygwin sh, because texvc expects sh features in its default shell
71 $cmd = 'sh -c ' . wfEscapeShellArg( $cmd );
72 }
73
74 wfDebug( "TeX: $cmd\n" );
75 $contents = `$cmd`;
76 wfDebug( "TeX output:\n $contents\n---\n" );
77
78 if (strlen($contents) == 0) {
79 return $this->_error( 'math_unknown_error' );
80 }
81
82 $retval = substr ($contents, 0, 1);
83 if (($retval == 'C') || ($retval == 'M') || ($retval == 'L')) {
84 if ($retval == 'C')
85 $this->conservativeness = 2;
86 else if ($retval == 'M')
87 $this->conservativeness = 1;
88 else
89 $this->conservativeness = 0;
90 $outdata = substr ($contents, 33);
91
92 $i = strpos($outdata, "\000");
93
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);
98 if ($retval == 'c')
99 $this->conservativeness = 2;
100 else if ($retval == 'm')
101 $this->conservativeness = 1;
102 else
103 $this->conservativeness = 0;
104 $this->mathml = NULL;
105 } else if ($retval == 'X') {
106 $this->html = NULL;
107 $this->mathml = substr ($contents, 33);
108 $this->conservativeness = 0;
109 } else if ($retval == '+') {
110 $this->html = NULL;
111 $this->mathml = NULL;
112 $this->conservativeness = 0;
113 } else {
114 $errbit = htmlspecialchars( substr($contents, 1) );
115 switch( $retval ) {
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 );
120 }
121 }
122
123 $this->hash = substr ($contents, 1, 32);
124 if (!preg_match("/^[a-f0-9]{32}$/", $this->hash)) {
125 return $this->_error( 'math_unknown_error' );
126 }
127
128 if( !file_exists( "$wgMathDirectory/{$this->hash}.png" ) ) {
129 return $this->_error( 'math_image_error' );
130 }
131
132 # Now save it back to the DB:
133 if ( !wfReadOnly() ) {
134 $outmd5_sql = pack('H32', $this->hash);
135
136 $md5_sql = pack('H32', $this->md5); # Binary packed, not hex
137
138 $dbw =& wfGetDB( DB_MASTER );
139 $dbw->replace( 'math', array( 'math_inputhash' ),
140 array(
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' )
147 );
148 }
149
150 }
151
152 return $this->_doRender();
153 }
154
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";
161 }
162
163 function _recall() {
164 global $wgMathDirectory;
165 $fname = 'MathRenderer::_recall';
166
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
172 $fname
173 );
174
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'];
179
180 $this->conservativeness = $rpage->math_html_conservativeness;
181 $this->html = $rpage->math_html;
182 $this->mathml = $rpage->math_mathml;
183
184 if( file_exists( "$wgMathDirectory/{$this->hash}.png" ) ) {
185 return true;
186 }
187 }
188
189 # Missing from the database and/or the render cache
190 return false;
191 }
192
193 /**
194 * Select among PNG, HTML, or MathML output depending on
195 */
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>";
199 }
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();
204 } else {
205 return '<span class="texhtml">'.$this->html.'</span>';
206 }
207 }
208
209 function _linkToMathImage() {
210 global $wgMathPath;
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\" />";
214 }
215
216 }
217
218 function renderMath( $tex ) {
219 global $wgUser;
220 $math = new MathRenderer( $tex );
221 $math->setOutputMode( $wgUser->getOption('math'));
222 return $math->render();
223 }
224
225 ?>