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