* (bug 153) Adjust thumbnail size calculations to match consistently;
[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 $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( $wgTmpDirectory ) ) {
46 if( !@mkdir( $wgTmpDirectory ) ) {
47 return $this->_error( 'math_bad_tmpdir' );
48 }
49 } elseif( !is_dir( $wgTmpDirectory ) || !is_writable( $wgTmpDirectory ) ) {
50 return $this->_error( 'math_bad_tmpdir' );
51 }
52
53 if( function_exists( 'is_executable' ) && !is_executable( $wgTexvc ) ) {
54 return $this->_error( 'math_notexvc' );
55 }
56 $cmd = $wgTexvc . ' ' .
57 escapeshellarg( $wgTmpDirectory ).' '.
58 escapeshellarg( $wgTmpDirectory ).' '.
59 escapeshellarg( $this->tex ).' '.
60 escapeshellarg( $wgInputEncoding );
61
62 if ( wfIsWindows() ) {
63 # Invoke it within cygwin sh, because texvc expects sh features in its default shell
64 $cmd = 'sh -c ' . wfEscapeShellArg( $cmd );
65 }
66
67 wfDebug( "TeX: $cmd\n" );
68 $contents = `$cmd`;
69 wfDebug( "TeX output:\n $contents\n---\n" );
70
71 if (strlen($contents) == 0) {
72 return $this->_error( 'math_unknown_error' );
73 }
74
75 $retval = substr ($contents, 0, 1);
76 if (($retval == 'C') || ($retval == 'M') || ($retval == 'L')) {
77 if ($retval == 'C')
78 $this->conservativeness = 2;
79 else if ($retval == 'M')
80 $this->conservativeness = 1;
81 else
82 $this->conservativeness = 0;
83 $outdata = substr ($contents, 33);
84
85 $i = strpos($outdata, "\000");
86
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);
91 if ($retval == 'c')
92 $this->conservativeness = 2;
93 else if ($retval == 'm')
94 $this->conservativeness = 1;
95 else
96 $this->conservativeness = 0;
97 $this->mathml = NULL;
98 } else if ($retval == 'X') {
99 $this->html = NULL;
100 $this->mathml = substr ($contents, 33);
101 $this->conservativeness = 0;
102 } else if ($retval == '+') {
103 $this->html = NULL;
104 $this->mathml = NULL;
105 $this->conservativeness = 0;
106 } else {
107 $errbit = htmlspecialchars( substr($contents, 1) );
108 switch( $retval ) {
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 );
113 }
114 }
115
116 $this->hash = substr ($contents, 1, 32);
117 if (!preg_match("/^[a-f0-9]{32}$/", $this->hash)) {
118 return $this->_error( 'math_unknown_error' );
119 }
120
121 if( !file_exists( "$wgTmpDirectory/{$this->hash}.png" ) ) {
122 return $this->_error( 'math_image_error' );
123 }
124
125 $hashpath = $this->_getHashPath();
126 if( !file_exists( $hashpath ) ) {
127 if( !@wfMkdirParents( $hashpath, 0755 ) ) {
128 return $this->_error( 'math_bad_output' );
129 }
130 } elseif( !is_dir( $hashpath ) || !is_writable( $hashpath ) ) {
131 return $this->_error( 'math_bad_output' );
132 }
133
134 if( !rename( "$wgTmpDirectory/{$this->hash}.png", "$hashpath/{$this->hash}.png" ) ) {
135 return $this->_error( 'math_output_error' );
136 }
137
138 # Now save it back to the DB:
139 if ( !wfReadOnly() ) {
140 $outmd5_sql = pack('H32', $this->hash);
141
142 $md5_sql = pack('H32', $this->md5); # Binary packed, not hex
143
144 $dbw =& wfGetDB( DB_MASTER );
145 $dbw->replace( 'math', array( 'math_inputhash' ),
146 array(
147 'math_inputhash' => $md5_sql,
148 'math_outputhash' => $outmd5_sql,
149 'math_html_conservativeness' => $this->conservativeness,
150 'math_html' => $this->html,
151 'math_mathml' => $this->mathml,
152 ), $fname, array( 'IGNORE' )
153 );
154 }
155
156 }
157
158 return $this->_doRender();
159 }
160
161 function _error( $msg, $append = '' ) {
162 $mf = htmlspecialchars( wfMsg( 'math_failure' ) );
163 $munk = htmlspecialchars( wfMsg( 'math_unknown_error' ) );
164 $errmsg = htmlspecialchars( wfMsg( $msg ) );
165 $source = htmlspecialchars($this->tex);
166 return "<strong class='error'>$mf ($errmsg$append): $source</strong>\n";
167 }
168
169 function _recall() {
170 global $wgMathDirectory;
171 $fname = 'MathRenderer::_recall';
172
173 $this->md5 = md5( $this->tex );
174 $dbr =& wfGetDB( DB_SLAVE );
175 $rpage = $dbr->selectRow( 'math',
176 array( 'math_outputhash','math_html_conservativeness','math_html','math_mathml' ),
177 array( 'math_inputhash' => pack("H32", $this->md5)), # Binary packed, not hex
178 $fname
179 );
180
181 if( $rpage !== false ) {
182 # Tailing 0x20s can get dropped by the database, add it back on if necessary:
183 $xhash = unpack( 'H32md5', $rpage->math_outputhash . " " );
184 $this->hash = $xhash ['md5'];
185
186 $this->conservativeness = $rpage->math_html_conservativeness;
187 $this->html = $rpage->math_html;
188 $this->mathml = $rpage->math_mathml;
189
190 if( file_exists( $this->_getHashPath() . "/{$this->hash}.png" ) ) {
191 return true;
192 }
193
194 if( file_exists( $wgMathDirectory . "/{$this->hash}.png" ) ) {
195 $hashpath = $this->_getHashPath();
196
197 if( !file_exists( $hashpath ) ) {
198 if( !@wfMkdirParents( $hashpath, 0755 ) ) {
199 return false;
200 }
201 } elseif( !is_dir( $hashpath ) || !is_writable( $hashpath ) ) {
202 return false;
203 }
204 if ( function_exists( "link" ) ) {
205 return link ( $wgMathDirectory . "/{$this->hash}.png",
206 $hashpath . "/{$this->hash}.png" );
207 } else {
208 return rename ( $wgMathDirectory . "/{$this->hash}.png",
209 $hashpath . "/{$this->hash}.png" );
210 }
211 }
212
213 }
214
215 # Missing from the database and/or the render cache
216 return false;
217 }
218
219 /**
220 * Select among PNG, HTML, or MathML output depending on
221 */
222 function _doRender() {
223 if( $this->mode == MW_MATH_MATHML && $this->mathml != '' ) {
224 return "<math xmlns='http://www.w3.org/1998/Math/MathML'>{$this->mathml}</math>";
225 }
226 if (($this->mode == MW_MATH_PNG) || ($this->html == '') ||
227 (($this->mode == MW_MATH_SIMPLE) && ($this->conservativeness != 2)) ||
228 (($this->mode == MW_MATH_MODERN || $this->mode == MW_MATH_MATHML) && ($this->conservativeness == 0))) {
229 return $this->_linkToMathImage();
230 } else {
231 return '<span class="texhtml">'.$this->html.'</span>';
232 }
233 }
234
235 function _linkToMathImage() {
236 global $wgMathPath;
237 $url = htmlspecialchars( "$wgMathPath/" . substr($this->hash, 0, 1)
238 .'/'. substr($this->hash, 1, 1) .'/'. substr($this->hash, 2, 1)
239 . "/{$this->hash}.png" );
240 $alt = trim(str_replace("\n", ' ', htmlspecialchars( $this->tex )));
241 return "<img class='tex' src=\"$url\" alt=\"$alt\" />";
242 }
243
244 function _getHashPath() {
245 global $wgMathDirectory;
246 $path = $wgMathDirectory .'/'. substr($this->hash, 0, 1)
247 .'/'. substr($this->hash, 1, 1)
248 .'/'. substr($this->hash, 2, 1);
249 wfDebug( "TeX: getHashPath, hash is: $this->hash, path is: $path\n" );
250 return $path;
251 }
252
253
254 }
255
256 function renderMath( $tex ) {
257 global $wgUser;
258 $math = new MathRenderer( $tex );
259 $math->setOutputMode( $wgUser->getOption('math'));
260 return $math->render();
261 }
262
263 ?>