Cripple the wiki text stuff for now. It doesn't SEEM dangerous but I haven't tested...
[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 $errmsg = htmlspecialchars( wfMsg( $msg ) );
164 $source = htmlspecialchars( str_replace( "\n", ' ', $this->tex ) );
165 return "<strong class='error'>$mf ($errmsg$append): $source</strong>\n";
166 }
167
168 function _recall() {
169 global $wgMathDirectory;
170 $fname = 'MathRenderer::_recall';
171
172 $this->md5 = md5( $this->tex );
173 $dbr =& wfGetDB( DB_SLAVE );
174 $rpage = $dbr->selectRow( 'math',
175 array( 'math_outputhash','math_html_conservativeness','math_html','math_mathml' ),
176 array( 'math_inputhash' => pack("H32", $this->md5)), # Binary packed, not hex
177 $fname
178 );
179
180 if( $rpage !== false ) {
181 # Tailing 0x20s can get dropped by the database, add it back on if necessary:
182 $xhash = unpack( 'H32md5', $rpage->math_outputhash . " " );
183 $this->hash = $xhash ['md5'];
184
185 $this->conservativeness = $rpage->math_html_conservativeness;
186 $this->html = $rpage->math_html;
187 $this->mathml = $rpage->math_mathml;
188
189 if( file_exists( $this->_getHashPath() . "/{$this->hash}.png" ) ) {
190 return true;
191 }
192
193 if( file_exists( $wgMathDirectory . "/{$this->hash}.png" ) ) {
194 $hashpath = $this->_getHashPath();
195
196 if( !file_exists( $hashpath ) ) {
197 if( !@wfMkdirParents( $hashpath, 0755 ) ) {
198 return false;
199 }
200 } elseif( !is_dir( $hashpath ) || !is_writable( $hashpath ) ) {
201 return false;
202 }
203 if ( function_exists( "link" ) ) {
204 return link ( $wgMathDirectory . "/{$this->hash}.png",
205 $hashpath . "/{$this->hash}.png" );
206 } else {
207 return rename ( $wgMathDirectory . "/{$this->hash}.png",
208 $hashpath . "/{$this->hash}.png" );
209 }
210 }
211
212 }
213
214 # Missing from the database and/or the render cache
215 return false;
216 }
217
218 /**
219 * Select among PNG, HTML, or MathML output depending on
220 */
221 function _doRender() {
222 if( $this->mode == MW_MATH_MATHML && $this->mathml != '' ) {
223 return "<math xmlns='http://www.w3.org/1998/Math/MathML'>{$this->mathml}</math>";
224 }
225 if (($this->mode == MW_MATH_PNG) || ($this->html == '') ||
226 (($this->mode == MW_MATH_SIMPLE) && ($this->conservativeness != 2)) ||
227 (($this->mode == MW_MATH_MODERN || $this->mode == MW_MATH_MATHML) && ($this->conservativeness == 0))) {
228 return $this->_linkToMathImage();
229 } else {
230 return '<span class="texhtml">'.$this->html.'</span>';
231 }
232 }
233
234 function _linkToMathImage() {
235 global $wgMathPath;
236 $url = htmlspecialchars( "$wgMathPath/" . substr($this->hash, 0, 1)
237 .'/'. substr($this->hash, 1, 1) .'/'. substr($this->hash, 2, 1)
238 . "/{$this->hash}.png" );
239 $alt = trim(str_replace("\n", ' ', htmlspecialchars( $this->tex )));
240 return "<img class='tex' src=\"$url\" alt=\"$alt\" />";
241 }
242
243 function _getHashPath() {
244 global $wgMathDirectory;
245 $path = $wgMathDirectory .'/'. substr($this->hash, 0, 1)
246 .'/'. substr($this->hash, 1, 1)
247 .'/'. substr($this->hash, 2, 1);
248 wfDebug( "TeX: getHashPath, hash is: $this->hash, path is: $path\n" );
249 return $path;
250 }
251
252 function renderMath( $tex ) {
253 global $wgUser;
254 $math = new MathRenderer( $tex );
255 $math->setOutputMode( $wgUser->getOption('math'));
256 return $math->render();
257 }
258 }
259 ?>