Index: trunk/phase3/includes/Math.php |
— | — | @@ -0,0 +1,116 @@ |
| 2 | +<? |
| 3 | + |
| 4 | +function linkToMathImage ( $tex, $outputhash ) |
| 5 | +{ |
| 6 | + global $wgMathPath; |
| 7 | + return "<img src=\"".$wgMathPath."/".$outputhash.".png\" alt=\"".wfEscapeHTML($tex)."\">"; |
| 8 | +} |
| 9 | + |
| 10 | +function renderMath( $tex ) |
| 11 | +{ |
| 12 | + global $wgUser, $wgMathDirectory, $wgTmpDirectory, $wgInputEncoding; |
| 13 | + $mf = wfMsg( "math_failure" ); |
| 14 | + $munk = wfMsg( "math_unknown_error" ); |
| 15 | + |
| 16 | + $fname = "renderMath"; |
| 17 | + |
| 18 | + $math = $wgUser->getOption("math"); |
| 19 | + if ($math == 3) |
| 20 | + return ('$ '.wfEscapeHTML($tex).' $'); |
| 21 | + |
| 22 | + $md5 = md5($tex); |
| 23 | + $md5_sql = mysql_escape_string(pack("H32", $md5)); |
| 24 | + if ($math == 0) |
| 25 | + $sql = "SELECT math_outputhash FROM math WHERE math_inputhash = '".$md5_sql."'"; |
| 26 | + else |
| 27 | + $sql = "SELECT math_outputhash,math_html_conservativeness,math_html FROM math WHERE math_inputhash = '".$md5_sql."'"; |
| 28 | + |
| 29 | + $res = wfQuery( $sql, $fname ); |
| 30 | + if ( wfNumRows( $res ) == 0 ) |
| 31 | + { |
| 32 | + $cmd = "./math/texvc ".escapeshellarg($wgTmpDirectory)." ". |
| 33 | + escapeshellarg($wgMathDirectory)." ".escapeshellarg($tex)." ".escapeshellarg($wgInputEncoding); |
| 34 | + $contents = `$cmd`; |
| 35 | + |
| 36 | + if (strlen($contents) == 0) |
| 37 | + return "<b>".$mf." (".$munk."): ".wfEscapeHTML($tex)."</b>"; |
| 38 | + $retval = substr ($contents, 0, 1); |
| 39 | + if (($retval == "C") || ($retval == "M") || ($retval == "L")) { |
| 40 | + if ($retval == "C") |
| 41 | + $conservativeness = 2; |
| 42 | + else if ($retval == "M") |
| 43 | + $conservativeness = 1; |
| 44 | + else |
| 45 | + $conservativeness = 0; |
| 46 | + $outdata = substr ($contents, 33); |
| 47 | + |
| 48 | + $i = strpos($outdata, "\000"); |
| 49 | + |
| 50 | + $outhtml = substr($outdata, 0, $i); |
| 51 | + $mathml = substr($outdata, $i+1); |
| 52 | + |
| 53 | + $sql_html = "'".mysql_escape_string($outhtml)."'"; |
| 54 | + $sql_mathml = "'".mysql_escape_string($mathml)."'"; |
| 55 | + } else if (($retval == "c") || ($retval == "m") || ($retval == "l")) { |
| 56 | + $outhtml = substr ($contents, 33); |
| 57 | + if ($retval == "c") |
| 58 | + $conservativeness = 2; |
| 59 | + else if ($retval == "m") |
| 60 | + $conservativeness = 1; |
| 61 | + else |
| 62 | + $conservativeness = 0; |
| 63 | + $sql_html = "'".mysql_escape_string($outhtml)."'"; |
| 64 | + $mathml = ''; |
| 65 | + $sql_mathml = 'NULL'; |
| 66 | + } else if ($retval == "X") { |
| 67 | + $outhtml = ''; |
| 68 | + $mathml = substr ($contents, 33); |
| 69 | + $sql_html = 'NULL'; |
| 70 | + $sql_mathml = "'".mysql_escape_string($mathml)."'"; |
| 71 | + $conservativeness = 0; |
| 72 | + } else if ($retval == "+") { |
| 73 | + $outhtml = ''; |
| 74 | + $mathml = ''; |
| 75 | + $sql_html = 'NULL'; |
| 76 | + $sql_mathml = 'NULL'; |
| 77 | + $conservativeness = 0; |
| 78 | + } else { |
| 79 | + if ($retval == "E") |
| 80 | + $errmsg = wfMsg( "math_lexing_error" ); |
| 81 | + else if ($retval == "S") |
| 82 | + $errmsg = wfMsg( "math_syntax_error" ); |
| 83 | + else if ($retval == "F") |
| 84 | + $errmsg = wfMsg( "math_unknown_function" ); |
| 85 | + else |
| 86 | + $errmsg = $munk; |
| 87 | + return "<h3>".$mf." (".$errmsg.substr($contents, 1)."): ".wfEscapeHTML($tex)."</h3>"; |
| 88 | + } |
| 89 | + |
| 90 | + $outmd5 = substr ($contents, 1, 32); |
| 91 | + if (!preg_match("/^[a-f0-9]{32}$/", $outmd5)) |
| 92 | + return "<b>".$mf." (".$munk."): ".wfEscapeHTML($tex)."</b>"; |
| 93 | + |
| 94 | + $outmd5_sql = mysql_escape_string(pack("H32", $outmd5)); |
| 95 | + |
| 96 | + $sql = "REPLACE INTO math VALUES ('".$md5_sql."', '".$outmd5_sql."', ".$conservativeness.", ".$sql_html.", ".$sql_mathml.")"; |
| 97 | + |
| 98 | + $res = wfQuery( $sql, $fname ); |
| 99 | + # we don't really care if it fails |
| 100 | + |
| 101 | + if (($math == 0) || ($rpage->math_html == '') || (($math == 1) && ($conservativeness != 2)) || (($math == 4) && ($conservativeness == 0))) |
| 102 | + return linkToMathImage($tex, $outmd5); |
| 103 | + else |
| 104 | + return $outhtml; |
| 105 | + } else { |
| 106 | + $rpage = wfFetchObject ( $res ); |
| 107 | + $outputhash = unpack( "H32md5", $rpage->math_outputhash . " " ); |
| 108 | + $outputhash = $outputhash ['md5']; |
| 109 | + |
| 110 | + if (($math == 0) || ($rpage->math_html == '') || (($math == 1) && ($rpage->math_html_conservativeness != 2)) || (($math == 4) && ($rpage->math_html_conservativeness == 0))) |
| 111 | + return linkToMathImage ( $tex, $outputhash ); |
| 112 | + else |
| 113 | + return $rpage->math_html; |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +?> |
Property changes on: trunk/phase3/includes/Math.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 118 | + native |
Added: svn:keywords |
2 | 119 | + Author Date Id Revision |
Index: trunk/phase3/includes/OutputPage.php |
— | — | @@ -1,119 +1,8 @@ |
2 | 2 | <? |
3 | 3 | # See design.doc |
4 | 4 | |
5 | | -function linkToMathImage ( $tex, $outputhash ) |
6 | | -{ |
7 | | - global $wgMathPath; |
8 | | - return "<img src=\"".$wgMathPath."/".$outputhash.".png\" alt=\"".wfEscapeHTML($tex)."\">"; |
9 | | -} |
| 5 | +if($wgUseTeX) include_once( "Math.php" ); |
10 | 6 | |
11 | | -function renderMath( $tex ) |
12 | | -{ |
13 | | - global $wgUser, $wgMathDirectory, $wgTmpDirectory, $wgInputEncoding; |
14 | | - $mf = wfMsg( "math_failure" ); |
15 | | - $munk = wfMsg( "math_unknown_error" ); |
16 | | - |
17 | | - $fname = "renderMath"; |
18 | | - |
19 | | - $math = $wgUser->getOption("math"); |
20 | | - if ($math == 3) |
21 | | - return ('$ '.wfEscapeHTML($tex).' $'); |
22 | | - |
23 | | - $md5 = md5($tex); |
24 | | - $md5_sql = mysql_escape_string(pack("H32", $md5)); |
25 | | - if ($math == 0) |
26 | | - $sql = "SELECT math_outputhash FROM math WHERE math_inputhash = '".$md5_sql."'"; |
27 | | - else |
28 | | - $sql = "SELECT math_outputhash,math_html_conservativeness,math_html FROM math WHERE math_inputhash = '".$md5_sql."'"; |
29 | | - |
30 | | - $res = wfQuery( $sql, $fname ); |
31 | | - if ( wfNumRows( $res ) == 0 ) |
32 | | - { |
33 | | - $cmd = "./math/texvc ".escapeshellarg($wgTmpDirectory)." ". |
34 | | - escapeshellarg($wgMathDirectory)." ".escapeshellarg($tex)." ".escapeshellarg($wgInputEncoding); |
35 | | - $contents = `$cmd`; |
36 | | - |
37 | | - if (strlen($contents) == 0) |
38 | | - return "<b>".$mf." (".$munk."): ".wfEscapeHTML($tex)."</b>"; |
39 | | - $retval = substr ($contents, 0, 1); |
40 | | - if (($retval == "C") || ($retval == "M") || ($retval == "L")) { |
41 | | - if ($retval == "C") |
42 | | - $conservativeness = 2; |
43 | | - else if ($retval == "M") |
44 | | - $conservativeness = 1; |
45 | | - else |
46 | | - $conservativeness = 0; |
47 | | - $outdata = substr ($contents, 33); |
48 | | - |
49 | | - $i = strpos($outdata, "\000"); |
50 | | - |
51 | | - $outhtml = substr($outdata, 0, $i); |
52 | | - $mathml = substr($outdata, $i+1); |
53 | | - |
54 | | - $sql_html = "'".mysql_escape_string($outhtml)."'"; |
55 | | - $sql_mathml = "'".mysql_escape_string($mathml)."'"; |
56 | | - } else if (($retval == "c") || ($retval == "m") || ($retval == "l")) { |
57 | | - $outhtml = substr ($contents, 33); |
58 | | - if ($retval == "c") |
59 | | - $conservativeness = 2; |
60 | | - else if ($retval == "m") |
61 | | - $conservativeness = 1; |
62 | | - else |
63 | | - $conservativeness = 0; |
64 | | - $sql_html = "'".mysql_escape_string($outhtml)."'"; |
65 | | - $mathml = ''; |
66 | | - $sql_mathml = 'NULL'; |
67 | | - } else if ($retval == "X") { |
68 | | - $outhtml = ''; |
69 | | - $mathml = substr ($contents, 33); |
70 | | - $sql_html = 'NULL'; |
71 | | - $sql_mathml = "'".mysql_escape_string($mathml)."'"; |
72 | | - $conservativeness = 0; |
73 | | - } else if ($retval == "+") { |
74 | | - $outhtml = ''; |
75 | | - $mathml = ''; |
76 | | - $sql_html = 'NULL'; |
77 | | - $sql_mathml = 'NULL'; |
78 | | - $conservativeness = 0; |
79 | | - } else { |
80 | | - if ($retval == "E") |
81 | | - $errmsg = wfMsg( "math_lexing_error" ); |
82 | | - else if ($retval == "S") |
83 | | - $errmsg = wfMsg( "math_syntax_error" ); |
84 | | - else if ($retval == "F") |
85 | | - $errmsg = wfMsg( "math_unknown_function" ); |
86 | | - else |
87 | | - $errmsg = $munk; |
88 | | - return "<h3>".$mf." (".$errmsg.substr($contents, 1)."): ".wfEscapeHTML($tex)."</h3>"; |
89 | | - } |
90 | | - |
91 | | - $outmd5 = substr ($contents, 1, 32); |
92 | | - if (!preg_match("/^[a-f0-9]{32}$/", $outmd5)) |
93 | | - return "<b>".$mf." (".$munk."): ".wfEscapeHTML($tex)."</b>"; |
94 | | - |
95 | | - $outmd5_sql = mysql_escape_string(pack("H32", $outmd5)); |
96 | | - |
97 | | - $sql = "REPLACE INTO math VALUES ('".$md5_sql."', '".$outmd5_sql."', ".$conservativeness.", ".$sql_html.", ".$sql_mathml.")"; |
98 | | - |
99 | | - $res = wfQuery( $sql, $fname ); |
100 | | - # we don't really care if it fails |
101 | | - |
102 | | - if (($math == 0) || ($rpage->math_html == '') || (($math == 1) && ($conservativeness != 2)) || (($math == 4) && ($conservativeness == 0))) |
103 | | - return linkToMathImage($tex, $outmd5); |
104 | | - else |
105 | | - return $outhtml; |
106 | | - } else { |
107 | | - $rpage = wfFetchObject ( $res ); |
108 | | - $outputhash = unpack( "H32md5", $rpage->math_outputhash . " " ); |
109 | | - $outputhash = $outputhash ['md5']; |
110 | | - |
111 | | - if (($math == 0) || ($rpage->math_html == '') || (($math == 1) && ($rpage->math_html_conservativeness != 2)) || (($math == 4) && ($rpage->math_html_conservativeness == 0))) |
112 | | - return linkToMathImage ( $tex, $outputhash ); |
113 | | - else |
114 | | - return $rpage->math_html; |
115 | | - } |
116 | | -} |
117 | | - |
118 | 7 | class OutputPage { |
119 | 8 | var $mHeaders, $mCookies, $mMetatags, $mKeywords; |
120 | 9 | var $mLinktags, $mPagetitle, $mBodytext, $mDebugtext; |
— | — | @@ -942,8 +831,8 @@ |
943 | 832 | $e2 = "/^([{$tc}]+)]](.*)\$/sD"; |
944 | 833 | wfProfileOut(); |
945 | 834 | |
946 | | - wfProfileIn( "$fname-loop" ); |
947 | 835 | foreach ( $a as $line ) { |
| 836 | + wfProfileIn( "$fname-loop" ); |
948 | 837 | if ( preg_match( $e1, $line, $m ) ) { # page with alternate text |
949 | 838 | |
950 | 839 | $text = $m[2]; |
— | — | @@ -957,6 +846,7 @@ |
958 | 847 | |
959 | 848 | else { # Invalid form; output directly |
960 | 849 | $s .= "[[" . $line ; |
| 850 | + wfProfileOut(); |
961 | 851 | continue; |
962 | 852 | } |
963 | 853 | if(substr($m[1],0,1)=="/") { # subpage |
— | — | @@ -1023,9 +913,9 @@ |
1024 | 914 | if ( "" == $text ) { $text = $link; } |
1025 | 915 | $s .= $sk->makeLink( $link, $text, "", $trail ); |
1026 | 916 | } |
| 917 | + wfProfileOut(); |
1027 | 918 | } |
1028 | 919 | wfProfileOut(); |
1029 | | - wfProfileOut(); |
1030 | 920 | return $s; |
1031 | 921 | } |
1032 | 922 | |