* Removed the troublesome regular expression in isValidEmailAddr which returns
[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 $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( $wgMathDirectory ) ) {
46 if( !@mkdir( $wgMathDirectory ) ) {
47 return $this->_error( 'math_bad_output' );
48 }
49 } elseif( !is_dir( $wgMathDirectory ) || !is_writable( $wgMathDirectory ) ) {
50 return $this->_error( 'math_bad_output' );
51 }
52 if( !file_exists( $wgTmpDirectory ) ) {
53 if( !@mkdir( $wgTmpDirectory ) ) {
54 return $this->_error( 'math_bad_tmpdir' );
55 }
56 } elseif( !is_dir( $wgTmpDirectory ) || !is_writable( $wgTmpDirectory ) ) {
57 return $this->_error( 'math_bad_tmpdir' );
58 }
59
60 if( !is_executable( $wgTexvc ) ) {
61 return $this->_error( 'math_notexvc' );
62 }
63 $cmd = $wgTexvc.' '.
64 wfEscapeShellArg($wgTmpDirectory).' '.
65 wfEscapeShellArg($wgMathDirectory).' '.
66 wfEscapeShellArg($this->tex).' '.
67 wfEscapeShellArg($wgInputEncoding);
68 wfDebug( 'TeX: '.$cmd );
69 $contents = `$cmd`;
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( "$wgMathDirectory/{$this->hash}.png" ) ) {
122 return $this->_error( 'math_image_error' );
123 }
124
125 # Now save it back to the DB:
126 if ( !wfReadOnly() ) {
127 $outmd5_sql = pack('H32', $this->hash);
128
129 $md5_sql = pack('H32', $this->md5); # Binary packed, not hex
130
131 $dbw =& wfGetDB( DB_MASTER );
132 $dbw->replace( 'math', array( 'math_inputhash' ),
133 array(
134 'math_inputhash' => $md5_sql,
135 'math_outputhash' => $outmd5_sql,
136 'math_html_conservativeness' => $this->conservativeness,
137 'math_html' => $this->html,
138 'math_mathml' => $this->mathml,
139 ), $fname, array( 'IGNORE' )
140 );
141 }
142
143 }
144
145 return $this->_doRender();
146 }
147
148 function _error( $msg, $append = '' ) {
149 $mf = htmlspecialchars( wfMsg( 'math_failure' ) );
150 $munk = htmlspecialchars( wfMsg( 'math_unknown_error' ) );
151 $errmsg = htmlspecialchars( wfMsg( $msg ) );
152 $source = htmlspecialchars($this->tex);
153 return "<strong class='error'>$mf ($errmsg$append): $source</strong>\n";
154 }
155
156 function _recall() {
157 global $wgMathDirectory;
158 $fname = 'MathRenderer::_recall';
159
160 $this->md5 = md5( $this->tex );
161 $dbr =& wfGetDB( DB_SLAVE );
162 $rpage = $dbr->selectRow( 'math',
163 array( 'math_outputhash','math_html_conservativeness','math_html','math_mathml' ),
164 array( 'math_inputhash' => pack("H32", $this->md5)), # Binary packed, not hex
165 $fname
166 );
167
168 if( $rpage !== false ) {
169 # Tailing 0x20s can get dropped by the database, add it back on if necessary:
170 $xhash = unpack( 'H32md5', $rpage->math_outputhash . " " );
171 $this->hash = $xhash ['md5'];
172
173 $this->conservativeness = $rpage->math_html_conservativeness;
174 $this->html = $rpage->math_html;
175 $this->mathml = $rpage->math_mathml;
176
177 if( file_exists( "$wgMathDirectory/{$this->hash}.png" ) ) {
178 return true;
179 }
180 }
181
182 # Missing from the database and/or the render cache
183 return false;
184 }
185
186 /**
187 * Select among PNG, HTML, or MathML output depending on
188 */
189 function _doRender() {
190 if( $this->mode == MW_MATH_MATHML && $this->mathml != '' ) {
191 return "<math xmlns='http://www.w3.org/1998/Math/MathML'>{$this->mathml}</math>";
192 }
193 if (($this->mode == MW_MATH_PNG) || ($this->html == '') ||
194 (($this->mode == MW_MATH_SIMPLE) && ($this->conservativeness != 2)) ||
195 (($this->mode == MW_MATH_MODERN || $this->mode == MW_MATH_MATHML) && ($this->conservativeness == 0))) {
196 return $this->_linkToMathImage();
197 } else {
198 return '<span class="texhtml">'.$this->html.'</span>';
199 }
200 }
201
202 function _linkToMathImage() {
203 global $wgMathPath;
204 $url = htmlspecialchars( "$wgMathPath/{$this->hash}.png" );
205 $alt = trim(str_replace("\n", ' ', htmlspecialchars( $this->tex )));
206 return "<img class='tex' src=\"$url\" alt=\"$alt\" />";
207 }
208
209 }
210
211 function renderMath( $tex ) {
212 global $wgUser;
213 $math = new MathRenderer( $tex );
214 $math->setOutputMode( $wgUser->getOption('math'));
215 return $math->render();
216 }
217
218 ?>