r1613 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r1612‎ | r1613 | r1614 >
Date:10:04, 30 August 2003
Author:vibber
Status:old
Tags:
Comment:
Trying to cut these monster files to manageable size... moving math support functions from OutputPage.php into Math.php
Modified paths:
  • /trunk/phase3/includes/Math.php (added) (history)
  • /trunk/phase3/includes/OutputPage.php (modified) (history)

Diff [purge]

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
1118 + native
Added: svn:keywords
2119 + Author Date Id Revision
Index: trunk/phase3/includes/OutputPage.php
@@ -1,119 +1,8 @@
22 <?
33 # See design.doc
44
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" );
106
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 -
1187 class OutputPage {
1198 var $mHeaders, $mCookies, $mMetatags, $mKeywords;
1209 var $mLinktags, $mPagetitle, $mBodytext, $mDebugtext;
@@ -942,8 +831,8 @@
943832 $e2 = "/^([{$tc}]+)]](.*)\$/sD";
944833 wfProfileOut();
945834
946 - wfProfileIn( "$fname-loop" );
947835 foreach ( $a as $line ) {
 836+ wfProfileIn( "$fname-loop" );
948837 if ( preg_match( $e1, $line, $m ) ) { # page with alternate text
949838
950839 $text = $m[2];
@@ -957,6 +846,7 @@
958847
959848 else { # Invalid form; output directly
960849 $s .= "[[" . $line ;
 850+ wfProfileOut();
961851 continue;
962852 }
963853 if(substr($m[1],0,1)=="/") { # subpage
@@ -1023,9 +913,9 @@
1024914 if ( "" == $text ) { $text = $link; }
1025915 $s .= $sk->makeLink( $link, $text, "", $trail );
1026916 }
 917+ wfProfileOut();
1027918 }
1028919 wfProfileOut();
1029 - wfProfileOut();
1030920 return $s;
1031921 }
1032922

Status & tagging log