* (bug 4167) Fix regression caused by patch for bug 153
[lhc/web/wiklou.git] / includes / Math.php
index f22226c..ef6c603 100644 (file)
@@ -1,12 +1,18 @@
 <?php
-#
-# 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)
-#
+/**
+ * Contain everything related to <math> </math> parsing
+ * @package MediaWiki
+ */
 
+/**
+ * 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)
+ *
+ * @package MediaWiki
+ */
 class MathRenderer {
        var $mode = MW_MATH_MODERN;
        var $tex = '';
@@ -25,8 +31,9 @@ class MathRenderer {
        }
 
        function render() {
-               global $wgMathDirectory, $wgTmpDirectory, $wgInputEncoding;
+               global $wgTmpDirectory, $wgInputEncoding;
                global $wgTexvc;
+               $fname = 'MathRenderer::render';
        
                if( $this->mode == MW_MATH_SOURCE ) {
                        # No need to render or parse anything more!
@@ -35,13 +42,6 @@ class MathRenderer {
                
                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' );
@@ -50,16 +50,23 @@ class MathRenderer {
                                return $this->_error( 'math_bad_tmpdir' );
                        }
                        
-                       if( !is_executable( $wgTexvc ) ) {
+                       if( function_exists( 'is_executable' ) && !is_executable( $wgTexvc ) ) {
                                return $this->_error( 'math_notexvc' );
                        }
-                       $cmd = $wgTexvc.' '.
-                               wfEscapeShellArg($wgTmpDirectory).' '.
-                               wfEscapeShellArg($wgMathDirectory).' '.
-                               wfEscapeShellArg($this->tex).' '.
-                               wfEscapeShellArg($wgInputEncoding);
-                       wfDebug( 'TeX: '.$cmd );
+                       $cmd = $wgTexvc . ' ' . 
+                                       escapeshellarg( $wgTmpDirectory ).' '.
+                                       escapeshellarg( $wgTmpDirectory ).' '.
+                                       escapeshellarg( $this->tex ).' '.
+                                       escapeshellarg( $wgInputEncoding );
+                                       
+                       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 = `$cmd`;
+                       wfDebug( "TeX output:\n $contents\n---\n" );
                
                        if (strlen($contents) == 0) {
                                return $this->_error( 'math_unknown_error' );
@@ -89,11 +96,11 @@ class MathRenderer {
                                        $this->conservativeness = 0;
                                $this->mathml = NULL;
                        } else if ($retval == 'X') {
-                               $outhtml = NULL;
+                               $this->html = NULL;
                                $this->mathml = substr ($contents, 33);
                                $this->conservativeness = 0;
                        } else if ($retval == '+') {
-                               $this->outhtml = NULL;
+                               $this->html = NULL;
                                $this->mathml = NULL;
                                $this->conservativeness = 0;
                        } else {
@@ -111,25 +118,40 @@ class MathRenderer {
                                return $this->_error( 'math_unknown_error' );
                        }
                
-                       if( !file_exists( "$wgMathDirectory/{$this->hash}.png" ) ) {
+                       if( !file_exists( "$wgTmpDirectory/{$this->hash}.png" ) ) {
                                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:
-                       $outmd5_sql = pack('H32', $this->hash);
-               
-                       $md5_sql = pack('H32', $this->md5); # Binary packed, not hex
+                       if ( !wfReadOnly() ) {
+                               $outmd5_sql = pack('H32', $this->hash);
                        
-                       $dbw =& wfGetDB( DB_MASTER );
-                       $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' ) 
-                       );
+                               $md5_sql = pack('H32', $this->md5); # Binary packed, not hex
+                               
+                               $dbw =& wfGetDB( DB_MASTER );
+                               $dbw->replace( 'math', array( 'math_inputhash' ),
+                                 array( 
+                                       'math_inputhash' => $md5_sql, 
+                                       'math_outputhash' => $outmd5_sql,
+                                       'math_html_conservativeness' => $this->conservativeness,
+                                       'math_html' => $this->html,
+                                       'math_mathml' => $this->mathml,
+                                 ), $fname, array( 'IGNORE' ) 
+                               );
+                       }
                        
                }
                
@@ -150,7 +172,7 @@ class MathRenderer {
 
                $this->md5 = md5( $this->tex );
                $dbr =& wfGetDB( DB_SLAVE );
-               $rpage = $dbr->getArray( 'math', 
+               $rpage = $dbr->selectRow( 'math', 
                        array( 'math_outputhash','math_html_conservativeness','math_html','math_mathml' ),
                        array( 'math_inputhash' => pack("H32", $this->md5)), # Binary packed, not hex
                        $fname
@@ -165,16 +187,38 @@ class MathRenderer {
                        $this->html = $rpage->math_html;
                        $this->mathml = $rpage->math_mathml;
                        
-                       if( file_exists( "$wgMathDirectory/{$this->hash}.png" ) ) {
+                       if( file_exists( $this->_getHashPath() . "/{$this->hash}.png" ) ) {
                                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" );
+                               }
+                       }
+                               
                }
                
                # Missing from the database and/or the render cache
                return false;
        }
 
-       # Select among PNG, HTML, or MathML output depending on 
+       /**
+        * 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>";
@@ -190,11 +234,23 @@ class MathRenderer {
 
        function _linkToMathImage() {
                global $wgMathPath;
-               $url = htmlspecialchars( "$wgMathPath/{$this->hash}.png" );
+               $url = htmlspecialchars( "$wgMathPath/" . substr($this->hash, 0, 1)
+                                       .'/'. substr($this->hash, 1, 1) .'/'. substr($this->hash, 2, 1)
+                                       . "/{$this->hash}.png" );
                $alt = trim(str_replace("\n", ' ', htmlspecialchars( $this->tex )));
                return "<img class='tex' src=\"$url\" alt=\"$alt\" />";
        }
 
+       function _getHashPath() {
+               global $wgMathDirectory;
+               $path = $wgMathDirectory .'/'. substr($this->hash, 0, 1)
+                                       .'/'. substr($this->hash, 1, 1)
+                                       .'/'. substr($this->hash, 2, 1);
+               wfDebug( "TeX: getHashPath, hash is: $this->hash, path is: $path\n" );
+               return $path;
+       }
+
+
 }
 
 function renderMath( $tex ) {