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