check that $wgArticle is an instance of the Article class in Skin::pageStats() per...
[lhc/web/wiklou.git] / includes / Math.php
index c2dd041..2ed1603 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;
@@ -20,8 +22,9 @@ class MathRenderer {
        var $mathml = '';
        var $conservativeness = 0;
 
-       function __construct( $tex ) {
+       function __construct( $tex, $params=array() ) {
                $this->tex = $tex;
+               $this->params = $params;
        }
 
        function setOutputMode( $mode ) {
@@ -44,7 +47,7 @@ class MathRenderer {
                if( !$this->_recall() ) {
                        # Ensure that the temp and output directories are available before continuing...
                        if( !file_exists( $wgTmpDirectory ) ) {
-                               if( !@mkdir( $wgTmpDirectory ) ) {
+                               if( !wfMkdirParents( $wgTmpDirectory ) ) {
                                        return $this->_error( 'math_bad_tmpdir' );
                                }
                        } elseif( !is_dir( $wgTmpDirectory ) || !is_writable( $wgTmpDirectory ) ) {
@@ -110,10 +113,17 @@ class MathRenderer {
                        } 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 );
                                }
                        }
 
@@ -135,6 +145,10 @@ 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 ) ) {
@@ -162,10 +176,17 @@ class MathRenderer {
                                        'math_html_conservativeness' => $this->conservativeness,
                                        'math_html' => $this->html,
                                        'math_mathml' => $this->mathml,
-                                 ), $fname, array( 'IGNORE' )
+                                 ), $fname
                                );
                        }
-
+                       
+                       // 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();
@@ -199,8 +220,14 @@ class MathRenderer {
                        $this->html = $rpage->math_html;
                        $this->mathml = $rpage->math_mathml;
 
-                       if( file_exists( $this->_getHashPath() . "/{$this->hash}.png" ) ) {
-                               return true;
+                       $filename = $this->_getHashPath() . "/{$this->hash}.png";
+                       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" ) ) {
@@ -233,40 +260,66 @@ class MathRenderer {
         */
        function _doRender() {
                if( $this->mode == MW_MATH_MATHML && $this->mathml != '' ) {
-                       return "<math xmlns='http://www.w3.org/1998/Math/MathML'>{$this->mathml}</math>";
+                       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 '<span class="texhtml">'.$this->html.'</span>';
+                       return Xml::tags( 'span',
+                               $this->_attribs( 'span',
+                                       array( 'class' => 'texhtml' ) ),
+                               $this->html );
                }
        }
 
+       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;
+       }
+
        function _linkToMathImage() {
-               global $wgMathPath;
-               $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\" />";
+               $url = $this->_mathImageUrl();
+
+               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 .'/'. 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 ) {
+       public static function renderMath( $tex, $params=array() ) {
                global $wgUser;
-               $math = new MathRenderer( $tex );
+               $math = new MathRenderer( $tex, $params );
                $math->setOutputMode( $wgUser->getOption('math'));
                return $math->render();
        }
 }
-