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