* Redundant script that isn't used by anything
[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 $errmsg = '';
77 if (($retval == 'C') || ($retval == 'M') || ($retval == 'L')) {
78 if ($retval == 'C') {
79 $this->conservativeness = 2;
80 } else if ($retval == 'M') {
81 $this->conservativeness = 1;
82 } else {
83 $this->conservativeness = 0;
84 }
85 $outdata = substr ($contents, 33);
86
87 $i = strpos($outdata, "\000");
88
89 $this->html = substr($outdata, 0, $i);
90 $this->mathml = substr($outdata, $i+1);
91 } else if (($retval == 'c') || ($retval == 'm') || ($retval == 'l')) {
92 $this->html = substr ($contents, 33);
93 if ($retval == 'c') {
94 $this->conservativeness = 2;
95 } else if ($retval == 'm') {
96 $this->conservativeness = 1;
97 } else {
98 $this->conservativeness = 0;
99 }
100 $this->mathml = NULL;
101 } else if ($retval == 'X') {
102 $this->html = NULL;
103 $this->mathml = substr ($contents, 33);
104 $this->conservativeness = 0;
105 } else if ($retval == '+') {
106 $this->html = NULL;
107 $this->mathml = NULL;
108 $this->conservativeness = 0;
109 } else {
110 $errbit = htmlspecialchars( substr($contents, 1) );
111 switch( $retval ) {
112 case 'E': $errmsg = $this->_error( 'math_lexing_error', $errbit );
113 case 'S': $errmsg = $this->_error( 'math_syntax_error', $errbit );
114 case 'F': $errmsg = $this->_error( 'math_unknown_function', $errbit );
115 default: $errmsg = $this->_error( 'math_unknown_error', $errbit );
116 }
117 }
118
119 if ( !$errmsg ) {
120 $this->hash = substr ($contents, 1, 32);
121 }
122
123 wfRunHooks( 'MathAfterTexvc', array( &$this, &$errmsg ) );
124
125 if ( $errmsg ) {
126 return $errmsg;
127 }
128
129 if (!preg_match("/^[a-f0-9]{32}$/", $this->hash)) {
130 return $this->_error( 'math_unknown_error' );
131 }
132
133 if( !file_exists( "$wgTmpDirectory/{$this->hash}.png" ) ) {
134 return $this->_error( 'math_image_error' );
135 }
136
137 $hashpath = $this->_getHashPath();
138 if( !file_exists( $hashpath ) ) {
139 if( !@wfMkdirParents( $hashpath, 0755 ) ) {
140 return $this->_error( 'math_bad_output' );
141 }
142 } elseif( !is_dir( $hashpath ) || !is_writable( $hashpath ) ) {
143 return $this->_error( 'math_bad_output' );
144 }
145
146 if( !rename( "$wgTmpDirectory/{$this->hash}.png", "$hashpath/{$this->hash}.png" ) ) {
147 return $this->_error( 'math_output_error' );
148 }
149
150 # Now save it back to the DB:
151 if ( !wfReadOnly() ) {
152 $outmd5_sql = pack('H32', $this->hash);
153
154 $md5_sql = pack('H32', $this->md5); # Binary packed, not hex
155
156 $dbw =& wfGetDB( DB_MASTER );
157 $dbw->replace( 'math', array( 'math_inputhash' ),
158 array(
159 'math_inputhash' => $md5_sql,
160 'math_outputhash' => $outmd5_sql,
161 'math_html_conservativeness' => $this->conservativeness,
162 'math_html' => $this->html,
163 'math_mathml' => $this->mathml,
164 ), $fname, array( 'IGNORE' )
165 );
166 }
167
168 }
169
170 return $this->_doRender();
171 }
172
173 function _error( $msg, $append = '' ) {
174 $mf = htmlspecialchars( wfMsg( 'math_failure' ) );
175 $errmsg = htmlspecialchars( wfMsg( $msg ) );
176 $source = htmlspecialchars( str_replace( "\n", ' ', $this->tex ) );
177 return "<strong class='error'>$mf ($errmsg$append): $source</strong>\n";
178 }
179
180 function _recall() {
181 global $wgMathDirectory;
182 $fname = 'MathRenderer::_recall';
183
184 $this->md5 = md5( $this->tex );
185 $dbr =& wfGetDB( DB_SLAVE );
186 $rpage = $dbr->selectRow( 'math',
187 array( 'math_outputhash','math_html_conservativeness','math_html','math_mathml' ),
188 array( 'math_inputhash' => pack("H32", $this->md5)), # Binary packed, not hex
189 $fname
190 );
191
192 if( $rpage !== false ) {
193 # Tailing 0x20s can get dropped by the database, add it back on if necessary:
194 $xhash = unpack( 'H32md5', $rpage->math_outputhash . " " );
195 $this->hash = $xhash ['md5'];
196
197 $this->conservativeness = $rpage->math_html_conservativeness;
198 $this->html = $rpage->math_html;
199 $this->mathml = $rpage->math_mathml;
200
201 if( file_exists( $this->_getHashPath() . "/{$this->hash}.png" ) ) {
202 return true;
203 }
204
205 if( file_exists( $wgMathDirectory . "/{$this->hash}.png" ) ) {
206 $hashpath = $this->_getHashPath();
207
208 if( !file_exists( $hashpath ) ) {
209 if( !@wfMkdirParents( $hashpath, 0755 ) ) {
210 return false;
211 }
212 } elseif( !is_dir( $hashpath ) || !is_writable( $hashpath ) ) {
213 return false;
214 }
215 if ( function_exists( "link" ) ) {
216 return link ( $wgMathDirectory . "/{$this->hash}.png",
217 $hashpath . "/{$this->hash}.png" );
218 } else {
219 return rename ( $wgMathDirectory . "/{$this->hash}.png",
220 $hashpath . "/{$this->hash}.png" );
221 }
222 }
223
224 }
225
226 # Missing from the database and/or the render cache
227 return false;
228 }
229
230 /**
231 * Select among PNG, HTML, or MathML output depending on
232 */
233 function _doRender() {
234 if( $this->mode == MW_MATH_MATHML && $this->mathml != '' ) {
235 return "<math xmlns='http://www.w3.org/1998/Math/MathML'>{$this->mathml}</math>";
236 }
237 if (($this->mode == MW_MATH_PNG) || ($this->html == '') ||
238 (($this->mode == MW_MATH_SIMPLE) && ($this->conservativeness != 2)) ||
239 (($this->mode == MW_MATH_MODERN || $this->mode == MW_MATH_MATHML) && ($this->conservativeness == 0))) {
240 return $this->_linkToMathImage();
241 } else {
242 return '<span class="texhtml">'.$this->html.'</span>';
243 }
244 }
245
246 function _linkToMathImage() {
247 global $wgMathPath;
248 $url = htmlspecialchars( "$wgMathPath/" . substr($this->hash, 0, 1)
249 .'/'. substr($this->hash, 1, 1) .'/'. substr($this->hash, 2, 1)
250 . "/{$this->hash}.png" );
251 $alt = trim(str_replace("\n", ' ', htmlspecialchars( $this->tex )));
252 return "<img class='tex' src=\"$url\" alt=\"$alt\" />";
253 }
254
255 function _getHashPath() {
256 global $wgMathDirectory;
257 $path = $wgMathDirectory .'/'. substr($this->hash, 0, 1)
258 .'/'. substr($this->hash, 1, 1)
259 .'/'. substr($this->hash, 2, 1);
260 wfDebug( "TeX: getHashPath, hash is: $this->hash, path is: $path\n" );
261 return $path;
262 }
263
264 public static function renderMath( $tex ) {
265 global $wgUser;
266 $math = new MathRenderer( $tex );
267 $math->setOutputMode( $wgUser->getOption('math'));
268 return $math->render();
269 }
270 }
271 ?>