00001 <?php
00016 class MathRenderer {
00017 var $mode = MW_MATH_MODERN;
00018 var $tex = '';
00019 var $inputhash = '';
00020 var $hash = '';
00021 var $html = '';
00022 var $mathml = '';
00023 var $conservativeness = 0;
00024
00025 function __construct( $tex, $params=array() ) {
00026 $this->tex = $tex;
00027 $this->params = $params;
00028 }
00029
00030 function setOutputMode( $mode ) {
00031 $this->mode = $mode;
00032 }
00033
00034 function render() {
00035 global $wgTmpDirectory, $wgInputEncoding;
00036 global $wgTexvc, $wgMathCheckFiles, $wgTexvcBackgroundColor;
00037
00038 if( $this->mode == MW_MATH_SOURCE ) {
00039 # No need to render or parse anything more!
00040 return ('$ '.htmlspecialchars( $this->tex ).' $');
00041 }
00042 if( $this->tex == '' ) {
00043 return; # bug 8372
00044 }
00045
00046 if( !$this->_recall() ) {
00047 if( $wgMathCheckFiles ) {
00048 # Ensure that the temp and output directories are available before continuing...
00049 if( !file_exists( $wgTmpDirectory ) ) {
00050 if( !wfMkdirParents( $wgTmpDirectory ) ) {
00051 return $this->_error( 'math_bad_tmpdir' );
00052 }
00053 } elseif( !is_dir( $wgTmpDirectory ) || !is_writable( $wgTmpDirectory ) ) {
00054 return $this->_error( 'math_bad_tmpdir' );
00055 }
00056 }
00057
00058 if( function_exists( 'is_executable' ) && !is_executable( $wgTexvc ) ) {
00059 return $this->_error( 'math_notexvc' );
00060 }
00061 $cmd = $wgTexvc . ' ' .
00062 escapeshellarg( $wgTmpDirectory ).' '.
00063 escapeshellarg( $wgTmpDirectory ).' '.
00064 escapeshellarg( $this->tex ).' '.
00065 escapeshellarg( $wgInputEncoding ).' '.
00066 escapeshellarg( $wgTexvcBackgroundColor );
00067
00068 if ( wfIsWindows() ) {
00069 # Invoke it within cygwin sh, because texvc expects sh features in its default shell
00070 $cmd = 'sh -c ' . wfEscapeShellArg( $cmd );
00071 }
00072
00073 wfDebug( "TeX: $cmd\n" );
00074 $contents = `$cmd`;
00075 wfDebug( "TeX output:\n $contents\n---\n" );
00076
00077 if (strlen($contents) == 0) {
00078 return $this->_error( 'math_unknown_error' );
00079 }
00080
00081 $retval = substr ($contents, 0, 1);
00082 $errmsg = '';
00083 if (($retval == 'C') || ($retval == 'M') || ($retval == 'L')) {
00084 if ($retval == 'C') {
00085 $this->conservativeness = 2;
00086 } else if ($retval == 'M') {
00087 $this->conservativeness = 1;
00088 } else {
00089 $this->conservativeness = 0;
00090 }
00091 $outdata = substr ($contents, 33);
00092
00093 $i = strpos($outdata, "\000");
00094
00095 $this->html = substr($outdata, 0, $i);
00096 $this->mathml = substr($outdata, $i+1);
00097 } else if (($retval == 'c') || ($retval == 'm') || ($retval == 'l')) {
00098 $this->html = substr ($contents, 33);
00099 if ($retval == 'c') {
00100 $this->conservativeness = 2;
00101 } else if ($retval == 'm') {
00102 $this->conservativeness = 1;
00103 } else {
00104 $this->conservativeness = 0;
00105 }
00106 $this->mathml = null;
00107 } else if ($retval == 'X') {
00108 $this->html = null;
00109 $this->mathml = substr ($contents, 33);
00110 $this->conservativeness = 0;
00111 } else if ($retval == '+') {
00112 $this->html = null;
00113 $this->mathml = null;
00114 $this->conservativeness = 0;
00115 } else {
00116 $errbit = htmlspecialchars( substr($contents, 1) );
00117 switch( $retval ) {
00118 case 'E':
00119 $errmsg = $this->_error( 'math_lexing_error', $errbit );
00120 break;
00121 case 'S':
00122 $errmsg = $this->_error( 'math_syntax_error', $errbit );
00123 break;
00124 case 'F':
00125 $errmsg = $this->_error( 'math_unknown_function', $errbit );
00126 break;
00127 default:
00128 $errmsg = $this->_error( 'math_unknown_error', $errbit );
00129 }
00130 }
00131
00132 if ( !$errmsg ) {
00133 $this->hash = substr ($contents, 1, 32);
00134 }
00135
00136 wfRunHooks( 'MathAfterTexvc', array( &$this, &$errmsg ) );
00137
00138 if ( $errmsg ) {
00139 return $errmsg;
00140 }
00141
00142 if (!preg_match("/^[a-f0-9]{32}$/", $this->hash)) {
00143 return $this->_error( 'math_unknown_error' );
00144 }
00145
00146 if( !file_exists( "$wgTmpDirectory/{$this->hash}.png" ) ) {
00147 return $this->_error( 'math_image_error' );
00148 }
00149
00150 if( filesize( "$wgTmpDirectory/{$this->hash}.png" ) == 0 ) {
00151 return $this->_error( 'math_image_error' );
00152 }
00153
00154 $hashpath = $this->_getHashPath();
00155 if( !file_exists( $hashpath ) ) {
00156 if( !@wfMkdirParents( $hashpath, 0755 ) ) {
00157 return $this->_error( 'math_bad_output' );
00158 }
00159 } elseif( !is_dir( $hashpath ) || !is_writable( $hashpath ) ) {
00160 return $this->_error( 'math_bad_output' );
00161 }
00162
00163 if( !rename( "$wgTmpDirectory/{$this->hash}.png", "$hashpath/{$this->hash}.png" ) ) {
00164 return $this->_error( 'math_output_error' );
00165 }
00166
00167 # Now save it back to the DB:
00168 if ( !wfReadOnly() ) {
00169 $outmd5_sql = pack('H32', $this->hash);
00170
00171 $md5_sql = pack('H32', $this->md5); # Binary packed, not hex
00172
00173 $dbw = wfGetDB( DB_MASTER );
00174 $dbw->replace( 'math', array( 'math_inputhash' ),
00175 array(
00176 'math_inputhash' => $dbw->encodeBlob($md5_sql),
00177 'math_outputhash' => $dbw->encodeBlob($outmd5_sql),
00178 'math_html_conservativeness' => $this->conservativeness,
00179 'math_html' => $this->html,
00180 'math_mathml' => $this->mathml,
00181 ), __METHOD__
00182 );
00183 }
00184
00185
00186 global $wgUseSquid;
00187 if ( $wgUseSquid ) {
00188 $urls = array( $this->_mathImageUrl() );
00189 $u = new SquidUpdate( $urls );
00190 $u->doUpdate();
00191 }
00192 }
00193
00194 return $this->_doRender();
00195 }
00196
00197 function _error( $msg, $append = '' ) {
00198 $mf = htmlspecialchars( wfMsg( 'math_failure' ) );
00199 $errmsg = htmlspecialchars( wfMsg( $msg ) );
00200 $source = htmlspecialchars( str_replace( "\n", ' ', $this->tex ) );
00201 return "<strong class='error'>$mf ($errmsg$append): $source</strong>\n";
00202 }
00203
00204 function _recall() {
00205 global $wgMathDirectory, $wgMathCheckFiles;
00206
00207 $this->md5 = md5( $this->tex );
00208 $dbr = wfGetDB( DB_SLAVE );
00209 $rpage = $dbr->selectRow( 'math',
00210 array( 'math_outputhash','math_html_conservativeness','math_html','math_mathml' ),
00211 array( 'math_inputhash' => $dbr->encodeBlob(pack("H32", $this->md5))), # Binary packed, not hex
00212 __METHOD__
00213 );
00214
00215 if( $rpage !== false ) {
00216 # Tailing 0x20s can get dropped by the database, add it back on if necessary:
00217 $xhash = unpack( 'H32md5', $dbr->decodeBlob($rpage->math_outputhash) . " " );
00218 $this->hash = $xhash ['md5'];
00219
00220 $this->conservativeness = $rpage->math_html_conservativeness;
00221 $this->html = $rpage->math_html;
00222 $this->mathml = $rpage->math_mathml;
00223
00224 $filename = $this->_getHashPath() . "/{$this->hash}.png";
00225
00226 if( !$wgMathCheckFiles ) {
00227
00228 return true;
00229 }
00230
00231 if( file_exists( $filename ) ) {
00232 if( filesize( $filename ) == 0 ) {
00233
00234 @unlink( $filename );
00235 } else {
00236 return true;
00237 }
00238 }
00239
00240 if( file_exists( $wgMathDirectory . "/{$this->hash}.png" ) ) {
00241 $hashpath = $this->_getHashPath();
00242
00243 if( !file_exists( $hashpath ) ) {
00244 if( !@wfMkdirParents( $hashpath, 0755 ) ) {
00245 return false;
00246 }
00247 } elseif( !is_dir( $hashpath ) || !is_writable( $hashpath ) ) {
00248 return false;
00249 }
00250 if ( function_exists( "link" ) ) {
00251 return link ( $wgMathDirectory . "/{$this->hash}.png",
00252 $hashpath . "/{$this->hash}.png" );
00253 } else {
00254 return rename ( $wgMathDirectory . "/{$this->hash}.png",
00255 $hashpath . "/{$this->hash}.png" );
00256 }
00257 }
00258
00259 }
00260
00261 # Missing from the database and/or the render cache
00262 return false;
00263 }
00264
00268 function _doRender() {
00269 if( $this->mode == MW_MATH_MATHML && $this->mathml != '' ) {
00270 return Xml::tags( 'math',
00271 $this->_attribs( 'math',
00272 array( 'xmlns' => 'http://www.w3.org/1998/Math/MathML' ) ),
00273 $this->mathml );
00274 }
00275 if (($this->mode == MW_MATH_PNG) || ($this->html == '') ||
00276 (($this->mode == MW_MATH_SIMPLE) && ($this->conservativeness != 2)) ||
00277 (($this->mode == MW_MATH_MODERN || $this->mode == MW_MATH_MATHML) && ($this->conservativeness == 0))) {
00278 return $this->_linkToMathImage();
00279 } else {
00280 return Xml::tags( 'span',
00281 $this->_attribs( 'span',
00282 array( 'class' => 'texhtml' ) ),
00283 $this->html );
00284 }
00285 }
00286
00287 function _attribs( $tag, $defaults=array(), $overrides=array() ) {
00288 $attribs = Sanitizer::validateTagAttributes( $this->params, $tag );
00289 $attribs = Sanitizer::mergeAttributes( $defaults, $attribs );
00290 $attribs = Sanitizer::mergeAttributes( $attribs, $overrides );
00291 return $attribs;
00292 }
00293
00294 function _linkToMathImage() {
00295 $url = $this->_mathImageUrl();
00296
00297 return Xml::element( 'img',
00298 $this->_attribs(
00299 'img',
00300 array(
00301 'class' => 'tex',
00302 'alt' => $this->tex ),
00303 array(
00304 'src' => $url ) ) );
00305 }
00306
00307 function _mathImageUrl() {
00308 global $wgMathPath;
00309 $dir = $this->_getHashSubPath();
00310 return "$wgMathPath/$dir/{$this->hash}.png";
00311 }
00312
00313 function _getHashPath() {
00314 global $wgMathDirectory;
00315 $path = $wgMathDirectory .'/' . $this->_getHashSubPath();
00316 wfDebug( "TeX: getHashPath, hash is: $this->hash, path is: $path\n" );
00317 return $path;
00318 }
00319
00320 function _getHashSubPath() {
00321 return substr($this->hash, 0, 1)
00322 .'/'. substr($this->hash, 1, 1)
00323 .'/'. substr($this->hash, 2, 1);
00324 }
00325
00326 public static function renderMath( $tex, $params=array() ) {
00327 global $wgUser;
00328 $math = new MathRenderer( $tex, $params );
00329 $math->setOutputMode( $wgUser->getOption('math'));
00330 return $math->render();
00331 }
00332 }