]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blobdiff - includes/Math.php
MediaWiki 1.17.0
[autoinstallsdev/mediawiki.git] / includes / Math.php
index 2771d04cbb89145ee7ca2e01e951c2be873fe2cc..0e136d5f9543e1b00c121725f201405820905bf7 100644 (file)
@@ -1,6 +1,8 @@
 <?php
 /**
  * Contain everything related to <math> </math> parsing
+ * @file
+ * @ingroup Parser
  */
 
 /**
@@ -8,8 +10,8 @@
  * 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)
- *
+ * @author Tomasz Wegrzanowski, with additions by Brion Vibber (2003, 2004)
+ * @ingroup Parser
  */
 class MathRenderer {
        var $mode = MW_MATH_MODERN;
@@ -31,35 +33,38 @@ class MathRenderer {
 
        function render() {
                global $wgTmpDirectory, $wgInputEncoding;
-               global $wgTexvc;
-               $fname = 'MathRenderer::render';
+               global $wgTexvc, $wgMathCheckFiles, $wgTexvcBackgroundColor;
 
                if( $this->mode == MW_MATH_SOURCE ) {
                        # No need to render or parse anything more!
-                       return ('$ '.htmlspecialchars( $this->tex ).' $');
+                       # 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() ) {
-                       # Ensure that the temp and output directories are available before continuing...
-                       if( !file_exists( $wgTmpDirectory ) ) {
-                               if( !@mkdir( $wgTmpDirectory ) ) {
+                       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' );
                                }
-                       } elseif( !is_dir( $wgTmpDirectory ) || !is_writable( $wgTmpDirectory ) ) {
-                               return $this->_error( 'math_bad_tmpdir' );
                        }
 
-                       if( function_exists( 'is_executable' ) && !is_executable( $wgTexvc ) ) {
+                       if( !is_executable( $wgTexvc ) ) {
                                return $this->_error( 'math_notexvc' );
                        }
                        $cmd = $wgTexvc . ' ' .
                                        escapeshellarg( $wgTmpDirectory ).' '.
                                        escapeshellarg( $wgTmpDirectory ).' '.
                                        escapeshellarg( $this->tex ).' '.
-                                       escapeshellarg( $wgInputEncoding );
+                                       escapeshellarg( $wgInputEncoding ).' '.
+                                       escapeshellarg( $wgTexvcBackgroundColor );
 
                        if ( wfIsWindows() ) {
                                # Invoke it within cygwin sh, because texvc expects sh features in its default shell
@@ -67,7 +72,7 @@ class MathRenderer {
                        }
 
                        wfDebug( "TeX: $cmd\n" );
-                       $contents = `$cmd`;
+                       $contents = wfShellExec( $cmd );
                        wfDebug( "TeX output:\n $contents\n---\n" );
 
                        if (strlen($contents) == 0) {
@@ -99,22 +104,29 @@ class MathRenderer {
                                } else {
                                        $this->conservativeness = 0;
                                }
-                               $this->mathml = NULL;
+                               $this->mathml = null;
                        } else if ($retval == 'X') {
-                               $this->html = NULL;
+                               $this->html = null;
                                $this->mathml = substr ($contents, 33);
                                $this->conservativeness = 0;
                        } else if ($retval == '+') {
-                               $this->html = NULL;
-                               $this->mathml = NULL;
+                               $this->html = null;
+                               $this->mathml = null;
                                $this->conservativeness = 0;
                        } else {
                                $errbit = htmlspecialchars( substr($contents, 1) );
                                switch( $retval ) {
-                                       case 'E': $errmsg = $this->_error( 'math_lexing_error', $errbit );
-                                       case 'S': $errmsg = $this->_error( 'math_syntax_error', $errbit );
-                                       case 'F': $errmsg = $this->_error( 'math_unknown_function', $errbit );
-                                       default:  $errmsg = $this->_error( 'math_unknown_error', $errbit );
+                                       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 );
                                }
                        }
 
@@ -136,9 +148,16 @@ class MathRenderer {
                                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 ) ) {
+                               wfSuppressWarnings();
+                               $ret = wfMkdirParents( $hashpath, 0755 );
+                               wfRestoreWarnings();
+                               if( !$ret ) {
                                        return $this->_error( 'math_bad_output' );
                                }
                        } elseif( !is_dir( $hashpath ) || !is_writable( $hashpath ) ) {
@@ -163,10 +182,17 @@ class MathRenderer {
                                        'math_html_conservativeness' => $this->conservativeness,
                                        'math_html' => $this->html,
                                        'math_mathml' => $this->mathml,
-                                 ), $fname, array( 'IGNORE' )
+                                 ), __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();
@@ -180,15 +206,14 @@ class MathRenderer {
        }
 
        function _recall() {
-               global $wgMathDirectory;
-               $fname = 'MathRenderer::_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
-                       $fname
+                       __METHOD__
                );
 
                if( $rpage !== false ) {
@@ -200,15 +225,32 @@ class MathRenderer {
                        $this->html = $rpage->math_html;
                        $this->mathml = $rpage->math_mathml;
 
-                       if( file_exists( $this->_getHashPath() . "/{$this->hash}.png" ) ) {
+                       $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 :(
+                                       wfSuppressWarnings();
+                                       unlink( $filename );
+                                       wfRestoreWarnings();
+                               } else {
+                                       return true;
+                               }
+                       }
 
                        if( file_exists( $wgMathDirectory . "/{$this->hash}.png" ) ) {
                                $hashpath = $this->_getHashPath();
 
                                if( !file_exists( $hashpath ) ) {
-                                       if( !@wfMkdirParents( $hashpath, 0755 ) ) {
+                                       wfSuppressWarnings();
+                                       $ret = wfMkdirParents( $hashpath, 0755 );
+                                       wfRestoreWarnings();
+                                       if( !$ret ) {
                                                return false;
                                        }
                                } elseif( !is_dir( $hashpath ) || !is_writable( $hashpath ) ) {
@@ -250,7 +292,7 @@ class MathRenderer {
                                $this->html );
                }
        }
-       
+
        function _attribs( $tag, $defaults=array(), $overrides=array() ) {
                $attribs = Sanitizer::validateTagAttributes( $this->params, $tag );
                $attribs = Sanitizer::mergeAttributes( $defaults, $attribs );
@@ -259,10 +301,7 @@ class MathRenderer {
        }
 
        function _linkToMathImage() {
-               global $wgMathPath;
-               $url = "$wgMathPath/" . substr($this->hash, 0, 1)
-                                       .'/'. substr($this->hash, 1, 1) .'/'. substr($this->hash, 2, 1)
-                                       . "/{$this->hash}.png";
+               $url = $this->_mathImageUrl();
 
                return Xml::element( 'img',
                        $this->_attribs(
@@ -274,20 +313,29 @@ class MathRenderer {
                                        'src' => $url ) ) );
        }
 
+       function _mathImageUrl() {
+               global $wgMathPath;
+               $dir = $this->_getHashSubPath();
+               return "$wgMathPath/$dir/{$this->hash}.png";
+       }
+       
        function _getHashPath() {
                global $wgMathDirectory;
-               $path = $wgMathDirectory .'/'. substr($this->hash, 0, 1)
-                                       .'/'. substr($this->hash, 1, 1)
-                                       .'/'. substr($this->hash, 2, 1);
+               $path = $wgMathDirectory .'/' . $this->_getHashSubPath();
                wfDebug( "TeX: getHashPath, hash is: $this->hash, path is: $path\n" );
                return $path;
        }
+       
+       function _getHashSubPath() {
+               return substr($this->hash, 0, 1)
+                                       .'/'. substr($this->hash, 1, 1)
+                                       .'/'. substr($this->hash, 2, 1);
+       }
 
-       public static function renderMath( $tex, $params=array() ) {
-               global $wgUser;
+       public static function renderMath( $tex, $params=array(), ParserOptions $parserOptions = null ) {
                $math = new MathRenderer( $tex, $params );
-               $math->setOutputMode( $wgUser->getOption('math'));
+               if ( $parserOptions )
+                       $math->setOutputMode( $parserOptions->getMath() );
                return $math->render();
        }
 }
-