Category namespaces
[lhc/web/wiklou.git] / includes / Math.php
1 <?php
2
3 function linkToMathImage ( $tex, $outputhash )
4 {
5 global $wgMathPath;
6 $url = htmlspecialchars( "$wgMathPath/$outputhash.png" );
7 $alt = htmlspecialchars( $tex );
8 return "<img class='tex' src=\"$url\" alt=\"$alt\" />";
9 }
10
11 function renderMath( $tex )
12 {
13 global $wgUser, $wgMathDirectory, $wgTmpDirectory, $wgInputEncoding;
14 global $wgTexvc;
15 $mf = wfMsg( "math_failure" );
16 $munk = wfMsg( "math_unknown_error" );
17
18 $fname = "renderMath";
19
20 $math = $wgUser->getOption("math");
21 if( $math == 3 ) {
22 return ('$ '.wfEscapeHTML($tex).' $');
23 }
24
25 $md5 = md5($tex);
26 $md5_sql = mysql_escape_string(pack("H32", $md5));
27 if ($math == 0) {
28 $sql = "SELECT math_outputhash FROM math WHERE math_inputhash = '$md5_sql'";
29 } else {
30 $sql = "SELECT math_outputhash,math_html_conservativeness,math_html FROM math WHERE math_inputhash = '$md5_sql'";
31 }
32
33 $res = wfQuery( $sql, DB_READ, $fname );
34
35 if( $rpage = wfFetchObject ( $res ) ) {
36 $outputhash = unpack( "H32md5", $rpage->math_outputhash . " " );
37 $outputhash = $outputhash ['md5'];
38 if( file_exists( "$wgMathDirectory/$outputhash.png" ) ) {
39 if (($math == 0) || ($rpage->math_html == '') || (($math == 1) && ($rpage->math_html_conservativeness != 2)) || (($math == 4) && ($rpage->math_html_conservativeness == 0))) {
40 return linkToMathImage ( $tex, $outputhash );
41 } else {
42 return $rpage->math_html;
43 }
44 }
45 }
46
47 # Ensure that the temp and output directories are available before continuing...
48 if( !file_exists( $wgMathDirectory ) ) {
49 if( !@mkdir( $wgMathDirectory ) ) {
50 return "<b>$mf (" . wfMsg( "math_bad_output" ) . ")</b>";
51 }
52 } elseif( !is_dir( $wgMathDirectory ) || !is_writable( $wgMathDirectory ) ) {
53 return "<b>$mf (" . wfMsg( "math_bad_output" ) . ")</b>";
54 }
55 if( !file_exists( $wgTmpDirectory ) ) {
56 if( !@mkdir( $wgTmpDirectory ) ) {
57 return "<b>$mf (" . wfMsg( "math_bad_tmpdir" ) . ")</b>";
58 }
59 } elseif( !is_dir( $wgTmpDirectory ) || !is_writable( $wgTmpDirectory ) ) {
60 return "<b>$mf (" . wfMsg( "math_bad_tmpdir" ) . ")</b>";
61 }
62
63 if( !is_executable( $wgTexvc ) ) {
64 return "<b>$mf (" . wfMsg( "math_notexvc" ) . ")</b>";
65 }
66 $cmd = $wgTexvc." ".
67 escapeshellarg($wgTmpDirectory)." ".
68 escapeshellarg($wgMathDirectory)." ".
69 escapeshellarg($tex)." ".
70 escapeshellarg($wgInputEncoding);
71 wfDebug( "TeX: $cmd" );
72 $contents = `$cmd`;
73
74 if (strlen($contents) == 0) {
75 return "<b>".$mf." (".$munk."): ".wfEscapeHTML($tex)."</b>";
76 }
77
78 $retval = substr ($contents, 0, 1);
79 if (($retval == "C") || ($retval == "M") || ($retval == "L")) {
80 if ($retval == "C")
81 $conservativeness = 2;
82 else if ($retval == "M")
83 $conservativeness = 1;
84 else
85 $conservativeness = 0;
86 $outdata = substr ($contents, 33);
87
88 $i = strpos($outdata, "\000");
89
90 $outhtml = substr($outdata, 0, $i);
91 $mathml = substr($outdata, $i+1);
92
93 $sql_html = "'".mysql_escape_string($outhtml)."'";
94 $sql_mathml = "'".mysql_escape_string($mathml)."'";
95 } else if (($retval == "c") || ($retval == "m") || ($retval == "l")) {
96 $outhtml = substr ($contents, 33);
97 if ($retval == "c")
98 $conservativeness = 2;
99 else if ($retval == "m")
100 $conservativeness = 1;
101 else
102 $conservativeness = 0;
103 $sql_html = "'".mysql_escape_string($outhtml)."'";
104 $mathml = '';
105 $sql_mathml = 'NULL';
106 } else if ($retval == "X") {
107 $outhtml = '';
108 $mathml = substr ($contents, 33);
109 $sql_html = 'NULL';
110 $sql_mathml = "'".mysql_escape_string($mathml)."'";
111 $conservativeness = 0;
112 } else if ($retval == "+") {
113 $outhtml = '';
114 $mathml = '';
115 $sql_html = 'NULL';
116 $sql_mathml = 'NULL';
117 $conservativeness = 0;
118 } else {
119 if ($retval == "E")
120 $errmsg = wfMsg( "math_lexing_error" );
121 else if ($retval == "S")
122 $errmsg = wfMsg( "math_syntax_error" );
123 else if ($retval == "F")
124 $errmsg = wfMsg( "math_unknown_function" );
125 else
126 $errmsg = $munk;
127 return "<h3>".$mf." (".$errmsg.substr($contents, 1)."): ".wfEscapeHTML($tex)."</h3>";
128 }
129
130 $outmd5 = substr ($contents, 1, 32);
131 if (!preg_match("/^[a-f0-9]{32}$/", $outmd5)) {
132 return "<b>".$mf." (".$munk."): ".wfEscapeHTML($tex)."</b>";
133 }
134
135 if( !file_exists( "$wgMathDirectory/$outmd5.png" ) ) {
136 $errmsg = wfMsg( "math_image_error" );
137 return "<h3>$mf ($errmsg): " . wfEscapeHTML($tex) . "</h3>";
138 }
139
140 $outmd5_sql = mysql_escape_string(pack("H32", $outmd5));
141
142 $sql = "REPLACE INTO math VALUES ('".$md5_sql."', '".$outmd5_sql."', ".$conservativeness.", ".$sql_html.", ".$sql_mathml.")";
143
144 $res = wfQuery( $sql, DB_READ, $fname );
145 # we don't really care if it fails
146
147 if (($math == 0) || ($rpage->math_html == '') || (($math == 1) && ($conservativeness != 2)) || (($math == 4) && ($conservativeness == 0)))
148 return linkToMathImage($tex, $outmd5);
149 else
150 return $outhtml;
151 }
152
153 ?>