coding style tweaks
[lhc/web/wiklou.git] / includes / Math.php
index 4c24af5..09344af 100644 (file)
 <?php
+/**
+ * Contain everything related to <math> </math> parsing
+ * @file
+ * @ingroup Parser
+ */
 
-function linkToMathImage ( $tex, $outputhash )
-{
-       global $wgMathPath;
-       $url = htmlspecialchars( "$wgMathPath/$outputhash.png";
-       $alt = htmlspecialchars( $tex );
-       return "<img class='tex' src=\"$url\" alt=\"$alt\" />";
-}
-
-function renderMath( $tex )
-{
-       global $wgUser, $wgMathDirectory, $wgTmpDirectory, $wgInputEncoding;
-       global $wgTexvc;
-       $mf   = wfMsg( "math_failure" );
-       $munk = wfMsg( "math_unknown_error" );
+/**
+ * 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.
+ *
+ * @author Tomasz Wegrzanowski, with additions by Brion Vibber (2003, 2004)
+ * @ingroup Parser
+ */
+class MathRenderer {
+       var $mode = MW_MATH_MODERN;
+       var $tex = '';
+       var $inputhash = '';
+       var $hash = '';
+       var $html = '';
+       var $mathml = '';
+       var $conservativeness = 0;
 
-       $fname = "renderMath";
+       function __construct( $tex, $params=array() ) {
+               $this->tex = $tex;
+               $this->params = $params;
+       }
 
-       $math = $wgUser->getOption("math");
-       if( $math == 3 ) {
-               return ('$ '.wfEscapeHTML($tex).' $');
+       function setOutputMode( $mode ) {
+               $this->mode = $mode;
        }
 
-       $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'";
-       }
+       function render() {
+               global $wgTmpDirectory, $wgInputEncoding;
+               global $wgTexvc, $wgMathCheckFiles, $wgTexvcBackgroundColor;
 
-       $res = wfQuery( $sql, DB_READ, $fname );
+               if( $this->mode == MW_MATH_SOURCE ) {
+                       # No need to render or parse anything more!
+                       # New lines are replaced with spaces, which avoids confusing our parser (bugs 23190, 22818)
+                       return ('<span class="tex">$ ' . str_replace( "\n", " ", htmlspecialchars( $this->tex ) ) . ' $</span>');
+               }
+               if( $this->tex == '' ) {
+                       return; # bug 8372
+               }
+
+               if( !$this->_recall() ) {
+                       if( $wgMathCheckFiles ) {
+                               # Ensure that the temp and output directories are available before continuing...
+                               if( !file_exists( $wgTmpDirectory ) ) {
+                                       if( !wfMkdirParents( $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 . ' ' .
+                                       escapeshellarg( $wgTmpDirectory ).' '.
+                                       escapeshellarg( $wgTmpDirectory ).' '.
+                                       escapeshellarg( $this->tex ).' '.
+                                       escapeshellarg( $wgInputEncoding ).' '.
+                                       escapeshellarg( $wgTexvcBackgroundColor );
 
-       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 );
+                       if ( wfIsWindows() ) {
+                               # Invoke it within cygwin sh, because texvc expects sh features in its default shell
+                               $cmd = 'sh -c ' . wfEscapeShellArg( $cmd );
+                       }
+
+                       wfDebug( "TeX: $cmd\n" );
+                       $contents = wfShellExec( $cmd );
+                       wfDebug( "TeX output:\n $contents\n---\n" );
+
+                       if (strlen($contents) == 0) {
+                               return $this->_error( 'math_unknown_error' );
+                       }
+
+                       $retval = substr ($contents, 0, 1);
+                       $errmsg = '';
+                       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;
+                               }
+                               $this->mathml = null;
+                       } else if ($retval == 'X') {
+                               $this->html = null;
+                               $this->mathml = substr ($contents, 33);
+                               $this->conservativeness = 0;
+                       } else if ($retval == '+') {
+                               $this->html = null;
+                               $this->mathml = null;
+                               $this->conservativeness = 0;
                        } else {
-                               return $rpage->math_html;
+                               $errbit = htmlspecialchars( substr($contents, 1) );
+                               switch( $retval ) {
+                                       case 'E':
+                                               $errmsg = $this->_error( 'math_lexing_error', $errbit );
+                                               break;
+                                       case 'S':
+                                               $errmsg = $this->_error( 'math_syntax_error', $errbit );
+                                               break;
+                                       case 'F':
+                                               $errmsg = $this->_error( 'math_unknown_function', $errbit );
+                                               break;
+                                       default:
+                                               $errmsg = $this->_error( 'math_unknown_error', $errbit );
+                               }
+                       }
+
+                       if ( !$errmsg ) {
+                                $this->hash = substr ($contents, 1, 32);
+                       }
+
+                       wfRunHooks( 'MathAfterTexvc', array( &$this, &$errmsg ) );
+
+                       if ( $errmsg ) {
+                                return $errmsg;
+                       }
+
+                       if (!preg_match("/^[a-f0-9]{32}$/", $this->hash)) {
+                               return $this->_error( 'math_unknown_error' );
+                       }
+
+                       if( !file_exists( "$wgTmpDirectory/{$this->hash}.png" ) ) {
+                               return $this->_error( 'math_image_error' );
+                       }
+
+                       if( filesize( "$wgTmpDirectory/{$this->hash}.png" ) == 0 ) {
+                               return $this->_error( 'math_image_error' );
+                       }
+
+                       $hashpath = $this->_getHashPath();
+                       if( !file_exists( $hashpath ) ) {
+                               if( !@wfMkdirParents( $hashpath, 0755 ) ) {
+                                       return $this->_error( 'math_bad_output' );
+                               }
+                       } elseif( !is_dir( $hashpath ) || !is_writable( $hashpath ) ) {
+                               return $this->_error( 'math_bad_output' );
+                       }
+
+                       if( !rename( "$wgTmpDirectory/{$this->hash}.png", "$hashpath/{$this->hash}.png" ) ) {
+                               return $this->_error( 'math_output_error' );
+                       }
+
+                       # Now save it back to the DB:
+                       if ( !wfReadOnly() ) {
+                               $outmd5_sql = pack('H32', $this->hash);
+
+                               $md5_sql = pack('H32', $this->md5); # Binary packed, not hex
+
+                               $dbw = wfGetDB( DB_MASTER );
+                               $dbw->replace( 'math', array( 'math_inputhash' ),
+                                 array(
+                                       'math_inputhash' => $dbw->encodeBlob($md5_sql),
+                                       'math_outputhash' => $dbw->encodeBlob($outmd5_sql),
+                                       'math_html_conservativeness' => $this->conservativeness,
+                                       'math_html' => $this->html,
+                                       'math_mathml' => $this->mathml,
+                                 ), __METHOD__
+                               );
+                       }
+                       
+                       // If we're replacing an older version of the image, make sure it's current.
+                       global $wgUseSquid;
+                       if ( $wgUseSquid ) {
+                               $urls = array( $this->_mathImageUrl() );
+                               $u = new SquidUpdate( $urls );
+                               $u->doUpdate();
                        }
                }
+
+               return $this->_doRender();
        }
-       
-       # Ensure that the temp and output directories are available before continuing...
-       if( !file_exists( $wgMathDirectory ) ) {
-               if( !@mkdir( $wgMathDirectory ) ) {
-                       return "<b>$mf (" . wfMsg( "math_bad_output" ) . ")</b>";
-               }
-       } elseif( !is_dir( $wgMathDirectory ) || !is_writable( $wgMathDirectory ) ) {
-               return "<b>$mf (" . wfMsg( "math_bad_output" ) . ")</b>";
+
+       function _error( $msg, $append = '' ) {
+               $mf   = htmlspecialchars( wfMsg( 'math_failure' ) );
+               $errmsg = htmlspecialchars( wfMsg( $msg ) );
+               $source = htmlspecialchars( str_replace( "\n", ' ', $this->tex ) );
+               return "<strong class='error'>$mf ($errmsg$append): $source</strong>\n";
        }
-       if( !file_exists( $wgTmpDirectory ) ) {
-               if( !@mkdir( $wgTmpDirectory ) ) {
-                       return "<b>$mf (" . wfMsg( "math_bad_tmpdir" ) . ")</b>";
+
+       function _recall() {
+               global $wgMathDirectory, $wgMathCheckFiles;
+
+               $this->md5 = md5( $this->tex );
+               $dbr = wfGetDB( DB_SLAVE );
+               $rpage = $dbr->selectRow( 'math',
+                       array( 'math_outputhash','math_html_conservativeness','math_html','math_mathml' ),
+                       array( 'math_inputhash' => $dbr->encodeBlob(pack("H32", $this->md5))), # Binary packed, not hex
+                       __METHOD__
+               );
+
+               if( $rpage !== false ) {
+                       # Tailing 0x20s can get dropped by the database, add it back on if necessary:
+                       $xhash = unpack( 'H32md5', $dbr->decodeBlob($rpage->math_outputhash) . "                " );
+                       $this->hash = $xhash ['md5'];
+
+                       $this->conservativeness = $rpage->math_html_conservativeness;
+                       $this->html = $rpage->math_html;
+                       $this->mathml = $rpage->math_mathml;
+
+                       $filename = $this->_getHashPath() . "/{$this->hash}.png";
+                       
+                       if( !$wgMathCheckFiles ) {
+                               // Short-circuit the file existence & migration checks
+                               return true;
+                       }
+                       
+                       if( file_exists( $filename ) ) {
+                               if( filesize( $filename ) == 0 ) {
+                                       // Some horrible error corrupted stuff :(
+                                       @unlink( $filename );
+                               } else {
+                                       return true;
+                               }
+                       }
+
+                       if( file_exists( $wgMathDirectory . "/{$this->hash}.png" ) ) {
+                               $hashpath = $this->_getHashPath();
+
+                               if( !file_exists( $hashpath ) ) {
+                                       if( !@wfMkdirParents( $hashpath, 0755 ) ) {
+                                               return false;
+                                       }
+                               } elseif( !is_dir( $hashpath ) || !is_writable( $hashpath ) ) {
+                                       return false;
+                               }
+                               if ( function_exists( "link" ) ) {
+                                       return link ( $wgMathDirectory . "/{$this->hash}.png",
+                                                       $hashpath . "/{$this->hash}.png" );
+                               } else {
+                                       return rename ( $wgMathDirectory . "/{$this->hash}.png",
+                                                       $hashpath . "/{$this->hash}.png" );
+                               }
+                       }
+
                }
-       } elseif( !is_dir( $wgTmpDirectory ) || !is_writable( $wgTmpDirectory ) ) {
-               return "<b>$mf (" . wfMsg( "math_bad_tmpdir" ) . ")</b>";
-       }
-       
-       if( !is_executable( $wgTexvc ) ) {
-               return "<b>$mf (" . wfMsg( "math_notexvc" ) . ")</b>";
-       }
-       $cmd = $wgTexvc." ".
-               escapeshellarg($wgTmpDirectory)." ".
-               escapeshellarg($wgMathDirectory)." ".
-               escapeshellarg($tex)." ".
-               escapeshellarg($wgInputEncoding);
-       wfDebug( "TeX: $cmd" );
-       $contents = `$cmd`;
-
-       if (strlen($contents) == 0) {
-               return "<b>".$mf." (".$munk."): ".wfEscapeHTML($tex)."</b>";
-       }
-       
-       $retval = substr ($contents, 0, 1);
-       if (($retval == "C") || ($retval == "M") || ($retval == "L")) {
-           if ($retval == "C")
-               $conservativeness = 2;
-           else if ($retval == "M")
-               $conservativeness = 1;
-           else
-               $conservativeness = 0;
-           $outdata = substr ($contents, 33);
-
-           $i = strpos($outdata, "\000");
-
-           $outhtml = substr($outdata, 0, $i);
-           $mathml = substr($outdata, $i+1);
-
-           $sql_html = "'".mysql_escape_string($outhtml)."'";
-           $sql_mathml = "'".mysql_escape_string($mathml)."'";
-       } else if (($retval == "c") || ($retval == "m") || ($retval == "l"))  {
-           $outhtml = substr ($contents, 33);
-           if ($retval == "c")
-               $conservativeness = 2;
-           else if ($retval == "m")
-               $conservativeness = 1;
-           else
-               $conservativeness = 0;
-           $sql_html = "'".mysql_escape_string($outhtml)."'";
-           $mathml = '';
-           $sql_mathml = 'NULL';
-       } else if ($retval == "X") {
-           $outhtml = '';
-           $mathml = substr ($contents, 33);
-           $sql_html = 'NULL';
-           $sql_mathml = "'".mysql_escape_string($mathml)."'";
-           $conservativeness = 0;
-       } else if ($retval == "+") {
-           $outhtml = '';
-           $mathml = '';
-           $sql_html = 'NULL';
-           $sql_mathml = 'NULL';
-           $conservativeness = 0;
-       } else {
-           if ($retval == "E")
-               $errmsg = wfMsg( "math_lexing_error" );
-           else if ($retval == "S")
-               $errmsg = wfMsg( "math_syntax_error" );
-           else if ($retval == "F")
-               $errmsg = wfMsg( "math_unknown_function" );
-           else
-               $errmsg = $munk;
-           return "<h3>".$mf." (".$errmsg.substr($contents, 1)."): ".wfEscapeHTML($tex)."</h3>";
+
+               # Missing from the database and/or the render cache
+               return false;
        }
 
-       $outmd5 = substr ($contents, 1, 32);
-       if (!preg_match("/^[a-f0-9]{32}$/", $outmd5)) {
-               return "<b>".$mf." (".$munk."): ".wfEscapeHTML($tex)."</b>";
+       /**
+        * Select among PNG, HTML, or MathML output depending on
+        */
+       function _doRender() {
+               if( $this->mode == MW_MATH_MATHML && $this->mathml != '' ) {
+                       return Xml::tags( 'math',
+                               $this->_attribs( 'math',
+                                       array( 'xmlns' => 'http://www.w3.org/1998/Math/MathML' ) ),
+                               $this->mathml );
+               }
+               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 {
+                       return Xml::tags( 'span',
+                               $this->_attribs( 'span',
+                                       array( 'class' => 'texhtml' ) ),
+                               $this->html );
+               }
        }
 
-       if( !file_exists( "$wgMathDirectory/$outmd5.png" ) ) {
-               $errmsg = wfMsg( "math_image_error" );
-               return "<h3>$mf ($errmsg): " . wfEscapeHTML($tex) . "</h3>";
+       function _attribs( $tag, $defaults=array(), $overrides=array() ) {
+               $attribs = Sanitizer::validateTagAttributes( $this->params, $tag );
+               $attribs = Sanitizer::mergeAttributes( $defaults, $attribs );
+               $attribs = Sanitizer::mergeAttributes( $attribs, $overrides );
+               return $attribs;
        }
 
-       $outmd5_sql = mysql_escape_string(pack("H32", $outmd5));
+       function _linkToMathImage() {
+               $url = $this->_mathImageUrl();
 
-       $sql = "REPLACE INTO math VALUES ('".$md5_sql."', '".$outmd5_sql."', ".$conservativeness.", ".$sql_html.", ".$sql_mathml.")";
+               return Xml::element( 'img',
+                       $this->_attribs(
+                               'img',
+                               array(
+                                       'class' => 'tex',
+                                       'alt' => $this->tex ),
+                               array(
+                                       'src' => $url ) ) );
+       }
+
+       function _mathImageUrl() {
+               global $wgMathPath;
+               $dir = $this->_getHashSubPath();
+               return "$wgMathPath/$dir/{$this->hash}.png";
+       }
+       
+       function _getHashPath() {
+               global $wgMathDirectory;
+               $path = $wgMathDirectory .'/' . $this->_getHashSubPath();
+               wfDebug( "TeX: getHashPath, hash is: $this->hash, path is: $path\n" );
+               return $path;
+       }
        
-       $res = wfQuery( $sql, DB_READ, $fname );
-       # we don't really care if it fails
+       function _getHashSubPath() {
+               return substr($this->hash, 0, 1)
+                                       .'/'. substr($this->hash, 1, 1)
+                                       .'/'. substr($this->hash, 2, 1);
+       }
 
-       if (($math == 0) || ($rpage->math_html == '') || (($math == 1) && ($conservativeness != 2)) || (($math == 4) && ($conservativeness == 0)))
-           return linkToMathImage($tex, $outmd5);
-       else
-           return $outhtml;
+       public static function renderMath( $tex, $params=array(), ParserOptions $parserOptions = null ) {
+               $math = new MathRenderer( $tex, $params );
+               if ( $parserOptions )
+                       $math->setOutputMode( $parserOptions->getMath() );
+               return $math->render();
+       }
 }
-
-?>