Correcting bugs due to double-prefixing table names. Removing obsolete Database membe...
[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
37 if( $this->mode == MW_MATH_SOURCE ) {
38 # No need to render or parse anything more!
39 return ('$ '.htmlspecialchars( $this->tex ).' $');
40 }
41
42 if( !$this->_recall() ) {
43 # Ensure that the temp and output directories are available before continuing...
44 if( !file_exists( $wgMathDirectory ) ) {
45 if( !@mkdir( $wgMathDirectory ) ) {
46 return $this->_error( 'math_bad_output' );
47 }
48 } elseif( !is_dir( $wgMathDirectory ) || !is_writable( $wgMathDirectory ) ) {
49 return $this->_error( 'math_bad_output' );
50 }
51 if( !file_exists( $wgTmpDirectory ) ) {
52 if( !@mkdir( $wgTmpDirectory ) ) {
53 return $this->_error( 'math_bad_tmpdir' );
54 }
55 } elseif( !is_dir( $wgTmpDirectory ) || !is_writable( $wgTmpDirectory ) ) {
56 return $this->_error( 'math_bad_tmpdir' );
57 }
58
59 if( !is_executable( $wgTexvc ) ) {
60 return $this->_error( 'math_notexvc' );
61 }
62 $cmd = $wgTexvc.' '.
63 wfEscapeShellArg($wgTmpDirectory).' '.
64 wfEscapeShellArg($wgMathDirectory).' '.
65 wfEscapeShellArg($this->tex).' '.
66 wfEscapeShellArg($wgInputEncoding);
67 wfDebug( 'TeX: '.$cmd );
68 $contents = `$cmd`;
69
70 if (strlen($contents) == 0) {
71 return $this->_error( 'math_unknown_error' );
72 }
73
74 $retval = substr ($contents, 0, 1);
75 if (($retval == 'C') || ($retval == 'M') || ($retval == 'L')) {
76 if ($retval == 'C')
77 $this->conservativeness = 2;
78 else if ($retval == 'M')
79 $this->conservativeness = 1;
80 else
81 $this->conservativeness = 0;
82 $outdata = substr ($contents, 33);
83
84 $i = strpos($outdata, "\000");
85
86 $this->html = substr($outdata, 0, $i);
87 $this->mathml = substr($outdata, $i+1);
88 } else if (($retval == 'c') || ($retval == 'm') || ($retval == 'l')) {
89 $this->html = substr ($contents, 33);
90 if ($retval == 'c')
91 $this->conservativeness = 2;
92 else if ($retval == 'm')
93 $this->conservativeness = 1;
94 else
95 $this->conservativeness = 0;
96 $this->mathml = NULL;
97 } else if ($retval == 'X') {
98 $outhtml = NULL;
99 $this->mathml = substr ($contents, 33);
100 $this->conservativeness = 0;
101 } else if ($retval == '+') {
102 $this->outhtml = NULL;
103 $this->mathml = NULL;
104 $this->conservativeness = 0;
105 } else {
106 $errbit = htmlspecialchars( substr($contents, 1) );
107 switch( $retval ) {
108 case 'E': return $this->_error( 'math_lexing_error', $errbit );
109 case 'S': return $this->_error( 'math_syntax_error', $errbit );
110 case 'F': return $this->_error( 'math_unknown_function', $errbit );
111 default: return $this->_error( 'math_unknown_error', $errbit );
112 }
113 }
114
115 $this->hash = substr ($contents, 1, 32);
116 if (!preg_match("/^[a-f0-9]{32}$/", $this->hash)) {
117 return $this->_error( 'math_unknown_error' );
118 }
119
120 if( !file_exists( "$wgMathDirectory/{$this->hash}.png" ) ) {
121 return $this->_error( 'math_image_error' );
122 }
123
124 # Now save it back to the DB:
125 $outmd5_sql = pack('H32', $this->hash);
126
127 $md5_sql = pack('H32', $this->md5); # Binary packed, not hex
128
129 $dbw =& wfGetDB( DB_MASTER );
130 $dbw->replace( 'math', array( 'math_inputhash' ),
131 array(
132 'math_inputhash' => $md5_sql,
133 'math_outputhash' => $outmd5_sql,
134 'math_html_conservativeness' => $this->conservativeness,
135 'math_html' => $outhtml,
136 'math_mathml' => $mathml,
137 ), $fname, array( 'IGNORE' )
138 );
139
140 }
141
142 return $this->_doRender();
143 }
144
145 function _error( $msg, $append = '' ) {
146 $mf = htmlspecialchars( wfMsg( 'math_failure' ) );
147 $munk = htmlspecialchars( wfMsg( 'math_unknown_error' ) );
148 $errmsg = htmlspecialchars( wfMsg( $msg ) );
149 $source = htmlspecialchars($this->tex);
150 return "<strong class='error'>$mf ($errmsg$append): $source</strong>\n";
151 }
152
153 function _recall() {
154 global $wgMathDirectory;
155 $fname = 'MathRenderer::_recall';
156
157 $this->md5 = md5( $this->tex );
158 $dbr =& wfGetDB( DB_SLAVE );
159 $rpage = $dbr->selectRow( 'math',
160 array( 'math_outputhash','math_html_conservativeness','math_html','math_mathml' ),
161 array( 'math_inputhash' => pack("H32", $this->md5)), # Binary packed, not hex
162 $fname
163 );
164
165 if( $rpage !== false ) {
166 # Tailing 0x20s can get dropped by the database, add it back on if necessary:
167 $xhash = unpack( 'H32md5', $rpage->math_outputhash . " " );
168 $this->hash = $xhash ['md5'];
169
170 $this->conservativeness = $rpage->math_html_conservativeness;
171 $this->html = $rpage->math_html;
172 $this->mathml = $rpage->math_mathml;
173
174 if( file_exists( "$wgMathDirectory/{$this->hash}.png" ) ) {
175 return true;
176 }
177 }
178
179 # Missing from the database and/or the render cache
180 return false;
181 }
182
183 /**
184 * Select among PNG, HTML, or MathML output depending on
185 */
186 function _doRender() {
187 if( $this->mode == MW_MATH_MATHML && $this->mathml != '' ) {
188 return "<math xmlns='http://www.w3.org/1998/Math/MathML'>{$this->mathml}</math>";
189 }
190 if (($this->mode == MW_MATH_PNG) || ($this->html == '') ||
191 (($this->mode == MW_MATH_SIMPLE) && ($this->conservativeness != 2)) ||
192 (($this->mode == MW_MATH_MODERN || $this->mode == MW_MATH_MATHML) && ($this->conservativeness == 0))) {
193 return $this->_linkToMathImage();
194 } else {
195 return '<span class="texhtml">'.$this->html.'</span>';
196 }
197 }
198
199 function _linkToMathImage() {
200 global $wgMathPath;
201 $url = htmlspecialchars( "$wgMathPath/{$this->hash}.png" );
202 $alt = trim(str_replace("\n", ' ', htmlspecialchars( $this->tex )));
203 return "<img class='tex' src=\"$url\" alt=\"$alt\" />";
204 }
205
206 }
207
208 function renderMath( $tex ) {
209 global $wgUser;
210 $math = new MathRenderer( $tex );
211 $math->setOutputMode( $wgUser->getOption('math'));
212 return $math->render();
213 }
214
215 ?>