Don't use public-audience-only function
[lhc/web/wiklou.git] / includes / Math.php
index e40abdf..ca225d6 100644 (file)
@@ -1,7 +1,8 @@
 <?php
 /**
  * Contain everything related to <math> </math> parsing
- * @package MediaWiki
+ * @file
+ * @ingroup Parser
  */
 
 /**
@@ -9,9 +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)
- *
- * @package MediaWiki
+ * @author Tomasz Wegrzanowski, with additions by Brion Vibber (2003, 2004)
+ * @ingroup Parser
  */
 class MathRenderer {
        var $mode = MW_MATH_MODERN;
@@ -22,8 +22,9 @@ class MathRenderer {
        var $mathml = '';
        var $conservativeness = 0;
 
-       function MathRenderer( $tex ) {
+       function __construct( $tex, $params=array() ) {
                $this->tex = $tex;
+               $this->params = $params;
        }
 
        function setOutputMode( $mode ) {
@@ -39,11 +40,14 @@ class MathRenderer {
                        # No need to render or parse anything more!
                        return ('$ '.htmlspecialchars( $this->tex ).' $');
                }
+               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( !wfMkdirParents( $wgTmpDirectory ) ) {
                                        return $this->_error( 'math_bad_tmpdir' );
                                }
                        } elseif( !is_dir( $wgTmpDirectory ) || !is_writable( $wgTmpDirectory ) ) {
@@ -73,13 +77,15 @@ class MathRenderer {
                        }
 
                        $retval = substr ($contents, 0, 1);
+                       $errmsg = '';
                        if (($retval == 'C') || ($retval == 'M') || ($retval == 'L')) {
-                               if ($retval == 'C')
+                               if ($retval == 'C') {
                                        $this->conservativeness = 2;
-                               else if ($retval == 'M')
+                               } else if ($retval == 'M') {
                                        $this->conservativeness = 1;
-                               else
+                               } else {
                                        $this->conservativeness = 0;
+                               }
                                $outdata = substr ($contents, 33);
 
                                $i = strpos($outdata, "\000");
@@ -88,12 +94,13 @@ class MathRenderer {
                                $this->mathml = substr($outdata, $i+1);
                        } else if (($retval == 'c') || ($retval == 'm') || ($retval == 'l'))  {
                                $this->html = substr ($contents, 33);
-                               if ($retval == 'c')
+                               if ($retval == 'c') {
                                        $this->conservativeness = 2;
-                               else if ($retval == 'm')
+                               } else if ($retval == 'm') {
                                        $this->conservativeness = 1;
-                               else
+                               } else {
                                        $this->conservativeness = 0;
+                               }
                                $this->mathml = NULL;
                        } else if ($retval == 'X') {
                                $this->html = NULL;
@@ -106,14 +113,30 @@ class MathRenderer {
                        } else {
                                $errbit = htmlspecialchars( substr($contents, 1) );
                                switch( $retval ) {
-                                       case 'E': return $this->_error( 'math_lexing_error', $errbit );
-                                       case 'S': return $this->_error( 'math_syntax_error', $errbit );
-                                       case 'F': return $this->_error( 'math_unknown_function', $errbit );
-                                       default:  return $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 );
                                }
                        }
 
-                       $this->hash = substr ($contents, 1, 32);
+                       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' );
                        }
@@ -141,15 +164,15 @@ class MathRenderer {
 
                                $md5_sql = pack('H32', $this->md5); # Binary packed, not hex
 
-                               $dbw =& wfGetDB( DB_MASTER );
+                               $dbw = wfGetDB( DB_MASTER );
                                $dbw->replace( 'math', array( 'math_inputhash' ),
                                  array(
-                                       'math_inputhash' => $md5_sql,
-                                       'math_outputhash' => $outmd5_sql,
+                                       '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,
-                                 ), $fname, array( 'IGNORE' )
+                                 ), $fname
                                );
                        }
 
@@ -170,16 +193,16 @@ class MathRenderer {
                $fname = 'MathRenderer::_recall';
 
                $this->md5 = md5( $this->tex );
-               $dbr =& wfGetDB( DB_SLAVE );
+               $dbr = wfGetDB( DB_SLAVE );
                $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
+                       array( 'math_inputhash' => $dbr->encodeBlob(pack("H32", $this->md5))), # Binary packed, not hex
                        $fname
                );
 
                if( $rpage !== false ) {
                        # Tailing 0x20s can get dropped by the database, add it back on if necessary:
-                       $xhash = unpack( 'H32md5', $rpage->math_outputhash . "                " );
+                       $xhash = unpack( 'H32md5', $dbr->decodeBlob($rpage->math_outputhash) . "                " );
                        $this->hash = $xhash ['md5'];
 
                        $this->conservativeness = $rpage->math_html_conservativeness;
@@ -220,24 +243,44 @@ 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)
+               $url = "$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\" />";
+                                       . "/{$this->hash}.png";
+
+               return Xml::element( 'img',
+                       $this->_attribs(
+                               'img',
+                               array(
+                                       'class' => 'tex',
+                                       'alt' => $this->tex ),
+                               array(
+                                       'src' => $url ) ) );
        }
 
        function _getHashPath() {
@@ -249,14 +292,10 @@ class MathRenderer {
                return $path;
        }
 
-
-}
-
-function renderMath( $tex ) {
-       global $wgUser;
-       $math = new MathRenderer( $tex );
-       $math->setOutputMode( $wgUser->getOption('math'));
-       return $math->render();
+       public static function renderMath( $tex, $params=array() ) {
+               global $wgUser;
+               $math = new MathRenderer( $tex, $params );
+               $math->setOutputMode( $wgUser->getOption('math'));
+               return $math->render();
+       }
 }
-
-?>