From 08395634faf9a52a3d7002af30135f25d778b466 Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Sun, 11 Jan 2004 23:14:20 +0000 Subject: [PATCH] Configurable location of texvc; detect failure to produce PNG output when texvc returns otherwise correctly; clean up whitespace. --- includes/DefaultSettings.php | 6 ++++ includes/Math.php | 64 ++++++++++++++++++++++-------------- languages/Language.php | 1 + 3 files changed, 47 insertions(+), 24 deletions(-) diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index cfc67fb11c..4d8a7a9645 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -132,7 +132,13 @@ $wgDisableCookieCheck = false; $wgAllowExternalImages = true; $wgMiserMode = false; # Disable database-intensive features + +# To use inline TeX, you need to compile 'texvc' (in the 'math' subdirectory +# of the MediaWiki package and have latex, dvips, gs (ghostscript), and +# convert (ImageMagick) installed and available in the PATH. +# Please see math/README for more information. $wgUseTeX = false; +$wgTexvc = "./math/texvc"; # Location of the texvc binary # Profiling / debugging $wgProfiling = false; # Enable for more detailed by-function times in debug log diff --git a/includes/Math.php b/includes/Math.php index 8554ac194f..90f20bd14c 100644 --- a/includes/Math.php +++ b/includes/Math.php @@ -8,42 +8,52 @@ function linkToMathImage ( $tex, $outputhash ) function renderMath( $tex ) { - global $wgUser, $wgMathDirectory, $wgTmpDirectory, $wgInputEncoding; - $mf = wfMsg( "math_failure" ); - $munk = wfMsg( "math_unknown_error" ); + global $wgUser, $wgMathDirectory, $wgTmpDirectory, $wgInputEncoding; + global $wgTexvc; + $mf = wfMsg( "math_failure" ); + $munk = wfMsg( "math_unknown_error" ); - $fname = "renderMath"; + $fname = "renderMath"; - $math = $wgUser->getOption("math"); - if ($math == 3) - return ('$ '.wfEscapeHTML($tex).' $'); + $math = $wgUser->getOption("math"); + if( $math == 3 ) { + return ('$ '.wfEscapeHTML($tex).' $'); + } - $md5 = md5($tex); - $md5_sql = mysql_escape_string(pack("H32", $md5)); - if ($math == 0) - $sql = "SELECT math_outputhash FROM math WHERE math_inputhash = '".$md5_sql."'"; - else - $sql = "SELECT math_outputhash,math_html_conservativeness,math_html FROM math WHERE math_inputhash = '".$md5_sql."'"; + $md5 = md5($tex); + $md5_sql = mysql_escape_string(pack("H32", $md5)); + if ($math == 0) { + $sql = "SELECT math_outputhash FROM math WHERE math_inputhash = '$md5_sql'"; + } else { + $sql = "SELECT math_outputhash,math_html_conservativeness,math_html FROM math WHERE math_inputhash = '$md5_sql'"; + } - $res = wfQuery( $sql, DB_READ, $fname ); + $res = wfQuery( $sql, DB_READ, $fname ); if( $rpage = wfFetchObject ( $res ) ) { $outputhash = unpack( "H32md5", $rpage->math_outputhash . " " ); $outputhash = $outputhash ['md5']; if( file_exists( "$wgMathDirectory/$outputhash.png" ) ) { - if (($math == 0) || ($rpage->math_html == '') || (($math == 1) && ($rpage->math_html_conservativeness != 2)) || (($math == 4) && ($rpage->math_html_conservativeness == 0))) - return linkToMathImage ( $tex, $outputhash ); - else - return $rpage->math_html; + if (($math == 0) || ($rpage->math_html == '') || (($math == 1) && ($rpage->math_html_conservativeness != 2)) || (($math == 4) && ($rpage->math_html_conservativeness == 0))) { + return linkToMathImage ( $tex, $outputhash ); + } else { + return $rpage->math_html; + } } } - $cmd = "./math/texvc ".escapeshellarg($wgTmpDirectory)." ". - escapeshellarg($wgMathDirectory)." ".escapeshellarg($tex)." ".escapeshellarg($wgInputEncoding); + $cmd = $wgTexvc." ". + escapeshellarg($wgTmpDirectory)." ". + escapeshellarg($wgMathDirectory)." ". + escapeshellarg($tex)." ". + escapeshellarg($wgInputEncoding); + wfDebug( "TeX: $cmd" ); $contents = `$cmd`; - if (strlen($contents) == 0) - return "".$mf." (".$munk."): ".wfEscapeHTML($tex).""; + if (strlen($contents) == 0) { + return "".$mf." (".$munk."): ".wfEscapeHTML($tex).""; + } + $retval = substr ($contents, 0, 1); if (($retval == "C") || ($retval == "M") || ($retval == "L")) { if ($retval == "C") @@ -97,8 +107,14 @@ function renderMath( $tex ) } $outmd5 = substr ($contents, 1, 32); - if (!preg_match("/^[a-f0-9]{32}$/", $outmd5)) - return "".$mf." (".$munk."): ".wfEscapeHTML($tex).""; + if (!preg_match("/^[a-f0-9]{32}$/", $outmd5)) { + return "".$mf." (".$munk."): ".wfEscapeHTML($tex).""; + } + + if( !file_exists( "$wgMathDirectory/$outmd5.png" ) ) { + $errmsg = wfMsg( "math_image_error" ); + return "

$mf ($errmsg): " . wfEscapeHTML($tex) . "

"; + } $outmd5_sql = mysql_escape_string(pack("H32", $outmd5)); diff --git a/languages/Language.php b/languages/Language.php index 37b6f4ebf0..9e7c1bcfce 100644 --- a/languages/Language.php +++ b/languages/Language.php @@ -788,6 +788,7 @@ See [[$wgMetaNamespace:User preferences help]] for help deciphering the options. "math_unknown_function" => "unknown function ", "math_lexing_error" => "lexing error", "math_syntax_error" => "syntax error", +"math_image_error" => "PNG conversion failed", "saveprefs" => "Save preferences", "resetprefs" => "Reset preferences", "oldpassword" => "Old password", -- 2.20.1