2004-02-18 02:15:00 +00:00
|
|
|
<?php
|
2004-04-22 08:51:04 +00:00
|
|
|
#
|
|
|
|
|
# Takes LaTeX fragments, sends them to a helper program (texvc) for rendering
|
|
|
|
|
# to rasterized PNG and HTML and MathML approximations. An appropriate
|
|
|
|
|
# rendering form is picked and returned.
|
|
|
|
|
#
|
|
|
|
|
# by Tomasz Wegrzanowski, with additions by Brion Vibber (2003, 2004)
|
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
class MathRenderer {
|
|
|
|
|
var $mode = MW_MATH_MODERN;
|
|
|
|
|
var $tex = "";
|
|
|
|
|
var $inputhash = "";
|
|
|
|
|
var $hash = "";
|
|
|
|
|
var $html = "";
|
|
|
|
|
var $mathml = "";
|
|
|
|
|
var $conservativeness = 0;
|
|
|
|
|
|
|
|
|
|
function MathRenderer( $tex ) {
|
|
|
|
|
$this->tex = $tex;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function setOutputMode( $mode ) {
|
|
|
|
|
$this->mode = $mode;
|
2004-01-11 23:14:20 +00:00
|
|
|
}
|
2003-08-30 10:04:59 +00:00
|
|
|
|
2004-04-22 08:51:04 +00:00
|
|
|
function render() {
|
|
|
|
|
global $wgMathDirectory, $wgTmpDirectory, $wgInputEncoding;
|
|
|
|
|
global $wgTexvc;
|
2003-11-09 11:45:12 +00:00
|
|
|
|
2004-04-22 08:51:04 +00:00
|
|
|
if( $this->mode == MW_MATH_SOURCE ) {
|
|
|
|
|
# No need to render or parse anything more!
|
2004-06-07 17:19:05 +00:00
|
|
|
return ('$ '.htmlspecialchars( $this->tex ).' $');
|
2004-03-26 16:47:12 +00:00
|
|
|
}
|
2004-04-22 08:51:04 +00:00
|
|
|
|
|
|
|
|
if( !$this->_recall() ) {
|
|
|
|
|
# Ensure that the temp and output directories are available before continuing...
|
|
|
|
|
if( !file_exists( $wgMathDirectory ) ) {
|
|
|
|
|
if( !@mkdir( $wgMathDirectory ) ) {
|
|
|
|
|
return $this->_error( "math_bad_output" );
|
|
|
|
|
}
|
|
|
|
|
} elseif( !is_dir( $wgMathDirectory ) || !is_writable( $wgMathDirectory ) ) {
|
|
|
|
|
return $this->_error( "math_bad_output" );
|
|
|
|
|
}
|
|
|
|
|
if( !file_exists( $wgTmpDirectory ) ) {
|
|
|
|
|
if( !@mkdir( $wgTmpDirectory ) ) {
|
|
|
|
|
return $this->_error( "math_bad_tmpdir" );
|
|
|
|
|
}
|
|
|
|
|
} elseif( !is_dir( $wgTmpDirectory ) || !is_writable( $wgTmpDirectory ) ) {
|
|
|
|
|
return $this->_error( "math_bad_tmpdir" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if( !is_executable( $wgTexvc ) ) {
|
|
|
|
|
return $this->_error( "math_notexvc" );
|
|
|
|
|
}
|
|
|
|
|
$cmd = $wgTexvc." ".
|
2004-07-10 03:09:26 +00:00
|
|
|
wfEscapeShellArg($wgTmpDirectory)." ".
|
|
|
|
|
wfEscapeShellArg($wgMathDirectory)." ".
|
|
|
|
|
wfEscapeShellArg($this->tex)." ".
|
|
|
|
|
wfEscapeShellArg($wgInputEncoding);
|
2004-04-22 08:51:04 +00:00
|
|
|
wfDebug( "TeX: $cmd" );
|
|
|
|
|
$contents = `$cmd`;
|
|
|
|
|
|
|
|
|
|
if (strlen($contents) == 0) {
|
|
|
|
|
return $this->_error( "math_unknown_error" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$retval = substr ($contents, 0, 1);
|
|
|
|
|
if (($retval == "C") || ($retval == "M") || ($retval == "L")) {
|
|
|
|
|
if ($retval == "C")
|
|
|
|
|
$this->conservativeness = 2;
|
|
|
|
|
else if ($retval == "M")
|
|
|
|
|
$this->conservativeness = 1;
|
|
|
|
|
else
|
|
|
|
|
$this->conservativeness = 0;
|
|
|
|
|
$outdata = substr ($contents, 33);
|
|
|
|
|
|
|
|
|
|
$i = strpos($outdata, "\000");
|
|
|
|
|
|
|
|
|
|
$this->html = substr($outdata, 0, $i);
|
|
|
|
|
$this->mathml = substr($outdata, $i+1);
|
|
|
|
|
} else if (($retval == "c") || ($retval == "m") || ($retval == "l")) {
|
|
|
|
|
$this->html = substr ($contents, 33);
|
|
|
|
|
if ($retval == "c")
|
|
|
|
|
$this->conservativeness = 2;
|
|
|
|
|
else if ($retval == "m")
|
|
|
|
|
$this->conservativeness = 1;
|
|
|
|
|
else
|
|
|
|
|
$this->conservativeness = 0;
|
2004-07-10 03:09:26 +00:00
|
|
|
$this->mathml = NULL;
|
2004-04-22 08:51:04 +00:00
|
|
|
} else if ($retval == "X") {
|
2004-07-10 03:09:26 +00:00
|
|
|
$outhtml = NULL;
|
2004-04-22 08:51:04 +00:00
|
|
|
$this->mathml = substr ($contents, 33);
|
|
|
|
|
$this->conservativeness = 0;
|
|
|
|
|
} else if ($retval == "+") {
|
2004-07-10 03:09:26 +00:00
|
|
|
$this->outhtml = NULL;
|
|
|
|
|
$this->mathml = NULL;
|
2004-04-22 08:51:04 +00:00
|
|
|
$this->conservativeness = 0;
|
|
|
|
|
} else {
|
|
|
|
|
$errbit = htmlspecialchars( substr($contents, 1) );
|
|
|
|
|
switch( $retval ) {
|
|
|
|
|
case "E": return $this->_error( "math_lexing_error", $errbit );
|
|
|
|
|
case "S": return $this->_error( "math_syntax_error", $errbit );
|
|
|
|
|
case "F": return $this->_error( "math_unknown_function", $errbit );
|
|
|
|
|
default: return $this->_error( "math_unknown_error", $errbit );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->hash = substr ($contents, 1, 32);
|
|
|
|
|
if (!preg_match("/^[a-f0-9]{32}$/", $this->hash)) {
|
|
|
|
|
return $this->_error( "math_unknown_error" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if( !file_exists( "$wgMathDirectory/{$this->hash}.png" ) ) {
|
|
|
|
|
return $this->_error( "math_image_error" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Now save it back to the DB:
|
2004-07-10 03:09:26 +00:00
|
|
|
$outmd5_sql = pack("H32", $this->hash);
|
2004-04-22 08:51:04 +00:00
|
|
|
|
2004-07-10 03:09:26 +00:00
|
|
|
$md5_sql = pack("H32", $this->md5); # Binary packed, not hex
|
|
|
|
|
|
|
|
|
|
$dbw =& wfGetDB( DB_WRITE );
|
|
|
|
|
$dbw->replace( 'math', array( 'math_inputhash' ),
|
|
|
|
|
array(
|
|
|
|
|
'math_inputhash' => $md5_sql,
|
|
|
|
|
'math_outputhash' => $outmd5_sql,
|
|
|
|
|
'math_html_conservativeness' => $this->conservativeness,
|
|
|
|
|
'math_html' => $outhtml,
|
|
|
|
|
'math_mathml' => $mathml,
|
|
|
|
|
), $fname, array( 'IGNORE' )
|
|
|
|
|
);
|
2004-04-22 08:51:04 +00:00
|
|
|
|
2004-03-26 16:47:12 +00:00
|
|
|
}
|
2004-04-22 08:51:04 +00:00
|
|
|
|
|
|
|
|
return $this->_doRender();
|
2004-03-26 16:47:12 +00:00
|
|
|
}
|
|
|
|
|
|
2004-04-22 08:51:04 +00:00
|
|
|
function _error( $msg, $append = "" ) {
|
|
|
|
|
$mf = htmlspecialchars( wfMsg( "math_failure" ) );
|
|
|
|
|
$munk = htmlspecialchars( wfMsg( "math_unknown_error" ) );
|
|
|
|
|
$errmsg = htmlspecialchars( wfMsg( $msg ) );
|
|
|
|
|
$source = htmlspecialchars($this->tex);
|
|
|
|
|
return "<strong class='error'>$mf ($errmsg$append): $source</strong>\n";
|
2004-01-11 23:14:20 +00:00
|
|
|
}
|
|
|
|
|
|
2004-04-22 08:51:04 +00:00
|
|
|
function _recall() {
|
|
|
|
|
global $wgMathDirectory;
|
2004-07-10 03:09:26 +00:00
|
|
|
$fname = 'MathRenderer::_recall';
|
|
|
|
|
|
2004-04-22 08:51:04 +00:00
|
|
|
$this->md5 = md5( $this->tex );
|
2004-07-10 03:09:26 +00:00
|
|
|
$dbr =& wfGetDB( DB_READ );
|
|
|
|
|
$rpage = $dbr->getArray( 'math',
|
|
|
|
|
array( 'math_outputhash','math_html_conservativeness','math_html','math_mathml' ),
|
2004-07-12 20:59:25 +00:00
|
|
|
array( 'math_inputhash' => pack("H32", $this->md5)), # Binary packed, not hex
|
2004-07-10 03:09:26 +00:00
|
|
|
$fname
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if( $rpage !== false ) {
|
2004-04-22 08:51:04 +00:00
|
|
|
# Tailing 0x20s can get dropped by the database, add it back on if necessary:
|
|
|
|
|
$xhash = unpack( "H32md5", $rpage->math_outputhash . " " );
|
|
|
|
|
$this->hash = $xhash ['md5'];
|
|
|
|
|
|
|
|
|
|
$this->conservativeness = $rpage->math_html_conservativeness;
|
|
|
|
|
$this->html = $rpage->math_html;
|
|
|
|
|
$this->mathml = $rpage->math_mathml;
|
|
|
|
|
|
|
|
|
|
if( file_exists( "$wgMathDirectory/{$this->hash}.png" ) ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Missing from the database and/or the render cache
|
|
|
|
|
return false;
|
2003-08-30 10:04:59 +00:00
|
|
|
}
|
|
|
|
|
|
2004-04-22 08:51:04 +00:00
|
|
|
# Select among PNG, HTML, or MathML output depending on
|
|
|
|
|
function _doRender() {
|
|
|
|
|
if( $this->mode == MW_MATH_MATHML && $this->mathml != '' ) {
|
|
|
|
|
return "<math xmlns='http://www.w3.org/1998/Math/MathML'>{$this->mathml}</math>";
|
|
|
|
|
}
|
|
|
|
|
if (($this->mode == MW_MATH_PNG) || ($this->html == '') ||
|
|
|
|
|
(($this->mode == MW_MATH_SIMPLE) && ($this->conservativeness != 2)) ||
|
|
|
|
|
(($this->mode == MW_MATH_MODERN || $this->mode == MW_MATH_MATHML) && ($this->conservativeness == 0))) {
|
|
|
|
|
return $this->_linkToMathImage();
|
|
|
|
|
} else {
|
2004-06-08 13:19:21 +00:00
|
|
|
return '<span class="texhtml">'.$this->html.'</span>';
|
2004-04-22 08:51:04 +00:00
|
|
|
}
|
2004-01-11 23:14:20 +00:00
|
|
|
}
|
|
|
|
|
|
2004-04-22 08:51:04 +00:00
|
|
|
function _linkToMathImage() {
|
|
|
|
|
global $wgMathPath;
|
|
|
|
|
$url = htmlspecialchars( "$wgMathPath/{$this->hash}.png" );
|
2004-05-17 00:14:42 +00:00
|
|
|
$alt = trim(str_replace("\n", ' ', htmlspecialchars( $this->tex )));
|
2004-04-22 08:51:04 +00:00
|
|
|
return "<img class='tex' src=\"$url\" alt=\"$alt\" />";
|
2004-01-11 23:14:20 +00:00
|
|
|
}
|
2003-08-30 10:04:59 +00:00
|
|
|
|
2004-04-22 08:51:04 +00:00
|
|
|
}
|
2003-08-30 10:04:59 +00:00
|
|
|
|
2004-04-22 08:51:04 +00:00
|
|
|
function renderMath( $tex ) {
|
|
|
|
|
global $wgUser;
|
|
|
|
|
$math = new MathRenderer( $tex );
|
|
|
|
|
$math->setOutputMode( $wgUser->getOption("math"));
|
|
|
|
|
return $math->render();
|
2003-08-30 10:04:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
?>
|