ported fix for bug 2257 here
[lhc/web/wiklou.git] / includes / Parser.php
index 1ab8b03..7736b04 100644 (file)
@@ -8,6 +8,7 @@
 
 /** */
 require_once( 'Sanitizer.php' );
+require_once( 'HttpFunctions.php' );
 
 /**
  * Update this version number when the ParserOutput format
@@ -46,24 +47,23 @@ define( 'STRIP_COMMENTS', 'HTMLCommentStrip' );
 define( 'UNIQ_PREFIX', 'NaodW29');
 
 # Constants needed for external link processing
-define( 'URL_PROTOCOLS', 'http|https|ftp|irc|gopher|news|mailto' );
-define( 'HTTP_PROTOCOLS', 'http|https' );
+define( 'HTTP_PROTOCOLS', 'http:\/\/|https:\/\/' );
 # Everything except bracket, space, or control characters
 define( 'EXT_LINK_URL_CLASS', '[^]<>"\\x00-\\x20\\x7F]' );
 # Including space
 define( 'EXT_LINK_TEXT_CLASS', '[^\]\\x00-\\x1F\\x7F]' );
 define( 'EXT_IMAGE_FNAME_CLASS', '[A-Za-z0-9_.,~%\\-+&;#*?!=()@\\x80-\\xFF]' );
 define( 'EXT_IMAGE_EXTENSIONS', 'gif|png|jpg|jpeg' );
-define( 'EXT_LINK_BRACKETED',  '/\[(\b('.URL_PROTOCOLS.'):'.EXT_LINK_URL_CLASS.'+) *('.EXT_LINK_TEXT_CLASS.'*?)\]/S' );
+define( 'EXT_LINK_BRACKETED',  '/\[(\b('.$wgUrlProtocols.')'.EXT_LINK_URL_CLASS.'+) *('.EXT_LINK_TEXT_CLASS.'*?)\]/S' );
 define( 'EXT_IMAGE_REGEX',
-       '/^('.HTTP_PROTOCOLS.':)'.  # Protocol
+       '/^('.HTTP_PROTOCOLS.')'.  # Protocol
        '('.EXT_LINK_URL_CLASS.'+)\\/'.  # Hostname and path
        '('.EXT_IMAGE_FNAME_CLASS.'+)\\.((?i)'.EXT_IMAGE_EXTENSIONS.')$/S' # Filename
 );
 
 /**
  * PHP Parser
- * 
+ *
  * Processes wiki markup
  *
  * <pre>
@@ -83,7 +83,7 @@ define( 'EXT_IMAGE_REGEX',
  * settings:
  *  $wgUseTex*, $wgUseDynamicDates*, $wgInterwikiMagic*,
  *  $wgNamespacesWithSubpages, $wgAllowExternalImages*,
- *  $wgLocaltimezone
+ *  $wgLocaltimezone, $wgAllowSpecialInclusion*
  *
  *  * only within ParserOptions
  * </pre>
@@ -99,7 +99,7 @@ class Parser
        var $mTagHooks;
 
        # Cleared with clearState():
-       var $mOutput, $mAutonumber, $mDTopen, $mStripState = array();
+       var $mOutput, $mAutonumber, $mDTopen, $mStripState = array(), $mCurrentParams = array();
        var $mVariables, $mIncludeCount, $mArgStack, $mLastSection, $mInPre;
        var $mInterwikiLinkHolders, $mLinkHolders;
 
@@ -110,15 +110,16 @@ class Parser
            $mTemplatePath;     // stores an unsorted hash of all the templates already loaded
                                // in this path. Used for loop detection.
 
+       var $mIWTransData = array();
+
        /**#@-*/
 
        /**
         * Constructor
-        * 
+        *
         * @access public
         */
        function Parser() {
-               global $wgContLang;
                $this->mTemplates = array();
                $this->mTemplatePath = array();
                $this->mTagHooks = array();
@@ -140,7 +141,11 @@ class Parser
                $this->mStripState = array();
                $this->mArgStack = array();
                $this->mInPre = false;
-               $this->mInterwikiLinkHolders = array();
+               $this->mCurrentParams = array();
+               $this->mInterwikiLinkHolders = array(
+                       'texts' => array(),
+                       'titles' => array()
+               );
                $this->mLinkHolders = array(
                        'namespaces' => array(),
                        'dbkeys' => array(),
@@ -163,7 +168,7 @@ class Parser
         * @return ParserOutput a ParserOutput
         */
        function parse( $text, &$title, $options, $linestart = true, $clearState = true ) {
-               global $wgUseTidy, $wgContLang, $wgCapitalLinks;
+               global $wgUseTidy, $wgContLang;
                $fname = 'Parser::parse';
                wfProfileIn( $fname );
 
@@ -173,28 +178,22 @@ class Parser
 
                $this->mOptions = $options;
                $this->mTitle =& $title;
-               $this->mOutput->mLcfirstTitle = false;
                $this->mOutputType = OT_HTML;
 
                $this->mStripState = NULL;
-               
+
                //$text = $this->strip( $text, $this->mStripState );
                // VOODOO MAGIC FIX! Sometimes the above segfaults in PHP5.
                $x =& $this->mStripState;
+
+               wfRunHooks( 'ParserBeforeStrip', array( &$this, &$text, &$x ) );
                $text = $this->strip( $text, $x );
+               wfRunHooks( 'ParserAfterStrip', array( &$this, &$text, &$x ) );
 
                $text = $this->internalParse( $text );
 
-               // if the string __LCFIRST__ (make the first character of the title
-               // lower case) occurs in the HTML, set the mLcfirstTitle to true
-               $mw =& MagicWord::get( MAG_LCFIRST );
-               if( $mw->matchAndRemove( $text ) && $wgCapitalLinks ) {
-                       $title->lcfirst();
-                       $this->mOutput->mLcfirstTitle = true;
-               }
-
                $text = $this->unstrip( $text, $this->mStripState );
-               
+
                # Clean up special characters, only run once, next-to-last before doBlockLevels
                $fixtags = array(
                        # french spaces, last one Guillemet-left
@@ -202,39 +201,33 @@ class Parser
                        '/(.) (?=\\?|:|;|!|\\302\\273)/' => '\\1&nbsp;\\2',
                        # french spaces, Guillemet-right
                        '/(\\302\\253) /' => '\\1&nbsp;',
-                       '/<hr *>/i' => '<hr />',
-                       '/<br *>/i' => '<br />',
-                       '/<center *>/i' => '<div class="center">',
-                       '/<\\/center *>/i' => '</div>',
+                       '/<center *>(.*)<\\/center *>/i' => '<div class="center">\\1</div>',
                );
                $text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );
-               
+
                # only once and last
                $text = $this->doBlockLevels( $text, $linestart );
 
                $this->replaceLinkHolders( $text );
 
-               $dashReplace = array(
-                       '/ - /' => "&nbsp;&ndash; ", # N dash
-                       '/(?<=[\d])-(?=[\d])/' => "&ndash;", # N dash between numbers
-                       '/ -- /' => "&nbsp;&mdash; " # M dash
-               );
-               $text = preg_replace( array_keys($dashReplace), array_values($dashReplace), $text );
-
-               # the position of the convert() call should not be changed. it 
-               # assumes that the links are all replaces and the only thing left 
+               # the position of the convert() call should not be changed. it
+               # assumes that the links are all replaces and the only thing left
                # is the <nowiki> mark.
                $text = $wgContLang->convert($text);
                $this->mOutput->setTitleText($wgContLang->getParsedTitle());
 
                $text = $this->unstripNoWiki( $text, $this->mStripState );
-               
+
+               wfRunHooks( 'ParserBeforeTidy', array( &$this, &$text ) );
+
                $text = Sanitizer::normalizeCharReferences( $text );
-               global $wgUseTidy;
+               
                if ($wgUseTidy) {
                        $text = Parser::tidy($text);
                }
 
+               wfRunHooks( 'ParserAfterTidy', array( &$this, &$text ) );
+
                $this->mOutput->setText( $text );
                wfProfileOut( $fname );
                return $this->mOutput;
@@ -250,12 +243,12 @@ class Parser
                return dechex(mt_rand(0, 0x7fffffff)) . dechex(mt_rand(0, 0x7fffffff));
        }
 
-       /** 
+       /**
         * Replaces all occurrences of <$tag>content</$tag> in the text
         * with a random marker and returns the new text. the output parameter
         * $content will be an associative array filled with data on the form
         * $unique_marker => content.
-        * 
+        *
         * If $content is already set, the additional entries will be appended
         * If $tag is set to STRIP_COMMENTS, the function will extract
         * <!-- HTML comments -->
@@ -263,7 +256,7 @@ class Parser
         * @access private
         * @static
         */
-       function extractTags($tag, $text, &$content, $uniq_prefix = ''){
+       function extractTagsAndParams($tag, $text, &$content, &$tags, &$params, $uniq_prefix = ''){
                $rnd = $uniq_prefix . '-' . $tag . Parser::getRandomString();
                if ( !$content ) {
                        $content = array( );
@@ -271,30 +264,65 @@ class Parser
                $n = 1;
                $stripped = '';
 
+               if ( !$tags ) {
+                       $tags = array( );
+               }
+
+               if ( !$params ) {
+                       $params = array( );
+               }
+
+               if( $tag == STRIP_COMMENTS ) {
+                       $start = '/<!--()/';
+                       $end   = '/-->/';
+               } else {
+                       $start = "/<$tag(\\s+[^>]*|\\s*)>/i";
+                       $end   = "/<\\/$tag\\s*>/i";
+               }
+
                while ( '' != $text ) {
-                       if($tag==STRIP_COMMENTS) {
-                               $p = preg_split( '/<!--/', $text, 2 );
-                       } else {
-                               $p = preg_split( "/<\\s*$tag\\s*>/i", $text, 2 );
-                       }
+                       $p = preg_split( $start, $text, 2, PREG_SPLIT_DELIM_CAPTURE );
                        $stripped .= $p[0];
-                       if ( ( count( $p ) < 2 ) || ( '' == $p[1] ) ) {
-                               $text = '';
+                       if( count( $p ) < 3 ) {
+                               break;
+                       }
+                       $attributes = $p[1];
+                       $inside     = $p[2];
+
+                       $marker = $rnd . sprintf('%08X', $n++);
+                       $stripped .= $marker;
+
+                       $tags[$marker] = "<$tag$attributes>";
+                       $params[$marker] = Sanitizer::decodeTagAttributes( $attributes );
+
+                       $q = preg_split( $end, $inside, 2 );
+                       $content[$marker] = $q[0];
+                       if( count( $q ) < 2 ) {
+                               # No end tag -- let it run out to the end of the text.
+                               break;
                        } else {
-                               if($tag==STRIP_COMMENTS) {
-                                       $q = preg_split( '/-->/i', $p[1], 2 );
-                               } else {
-                                       $q = preg_split( "/<\\/\\s*$tag\\s*>/i", $p[1], 2 );
-                               }
-                               $marker = $rnd . sprintf('%08X', $n++);
-                               $content[$marker] = $q[0];
-                               $stripped .= $marker;
                                $text = $q[1];
                        }
                }
                return $stripped;
        }
 
+       /**
+        * Wrapper function for extractTagsAndParams
+        * for cases where $tags and $params isn't needed
+        * i.e. where tags will never have params, like <nowiki>
+        *
+        * @access private
+        * @static
+        */
+       function extractTags( $tag, $text, &$content, $uniq_prefix = '' ) {
+               $dummy_tags = array();
+               $dummy_params = array();
+
+               return Parser::extractTagsAndParams( $tag, $text, $content,
+                       $dummy_tags, $dummy_params, $uniq_prefix );
+       }
+
        /**
         * Strips and renders nowiki, pre, math, hiero
         * If $render is set, performs necessary rendering operations on plugins
@@ -316,6 +344,8 @@ class Parser
                $pre_content = array();
                $comment_content = array();
                $ext_content = array();
+               $ext_tags = array();
+               $ext_params = array();
                $gallery_content = array();
 
                # Replace any instances of the placeholders
@@ -323,8 +353,8 @@ class Parser
                #$text = str_replace( $uniq_prefix, wfHtmlEscapeFirst( $uniq_prefix ), $text );
 
                # html
-               global $wgRawHtml, $wgWhitelistEdit;
-               if( $wgRawHtml && $wgWhitelistEdit ) {
+               global $wgRawHtml;
+               if( $wgRawHtml ) {
                        $text = Parser::extractTags('html', $text, $html_content, $uniq_prefix);
                        foreach( $html_content as $marker => $content ) {
                                if ($render ) {
@@ -347,16 +377,14 @@ class Parser
                }
 
                # math
-               $text = Parser::extractTags('math', $text, $math_content, $uniq_prefix);
-               foreach( $math_content as $marker => $content ){
-                       if( $render ) {
-                               if( $this->mOptions->getUseTeX() ) {
+               if( $this->mOptions->getUseTeX() ) {
+                       $text = Parser::extractTags('math', $text, $math_content, $uniq_prefix);
+                       foreach( $math_content as $marker => $content ){
+                               if( $render ) {
                                        $math_content[$marker] = renderMath( $content );
                                } else {
-                                       $math_content[$marker] = '&lt;math&gt;'.$content.'&lt;math&gt;';
+                                       $math_content[$marker] = '<math>'.$content.'</math>';
                                }
-                       } else {
-                               $math_content[$marker] = '<math>'.$content.'</math>';
                        }
                }
 
@@ -392,12 +420,16 @@ class Parser
                # Extensions
                foreach ( $this->mTagHooks as $tag => $callback ) {
                        $ext_content[$tag] = array();
-                       $text = Parser::extractTags( $tag, $text, $ext_content[$tag], $uniq_prefix );
+                       $text = Parser::extractTagsAndParams( $tag, $text, $ext_content[$tag],
+                               $ext_tags[$tag], $ext_params[$tag], $uniq_prefix );
                        foreach( $ext_content[$tag] as $marker => $content ) {
+                               $content = $this->replaceVariables( $content, $this->mCurrentParams );
+                               $full_tag = $ext_tags[$tag][$marker];
+                               $params = $ext_params[$tag][$marker];
                                if ( $render ) {
-                                       $ext_content[$tag][$marker] = $callback( $content );
+                                       $ext_content[$tag][$marker] = $callback( $content, $params, $this );
                                } else {
-                                       $ext_content[$tag][$marker] = "<$tag>$content</$tag>";
+                                       $ext_content[$tag][$marker] = "$full_tag$content</$tag>";
                                }
                        }
                }
@@ -436,6 +468,10 @@ class Parser
         * @access private
         */
        function unstrip( $text, &$state ) {
+               if ( !is_array( $state ) ) {
+                       return $text;
+               }
+               
                # Must expand in reverse order, otherwise nested tags will be corrupted
                $contentDict = end( $state );
                for ( $contentDict = end( $state ); $contentDict !== false; $contentDict = prev( $state ) ) {
@@ -455,6 +491,10 @@ class Parser
         * @access private
         */
        function unstripNoWiki( $text, &$state ) {
+               if ( !is_array( $state ) ) {
+                       return $text;
+               }
+
                # Must expand in reverse order, otherwise nested tags will be corrupted
                for ( $content = end($state['nowiki']); $content !== false; $content = prev( $state['nowiki'] ) ) {
                        $text = str_replace( key( $state['nowiki'] ), $content, $text );
@@ -484,7 +524,9 @@ class Parser
                          'html' => array(),
                          'nowiki' => array(),
                          'math' => array(),
-                         'pre' => array()
+                         'pre' => array(),
+                         'comment' => array(),
+                         'gallery' => array(),
                        );
                }
                $state['item'][$rnd] = $text;
@@ -521,7 +563,7 @@ class Parser
                }
                return $correctedtext;
        }
-       
+
        /**
         * Spawn an external HTML tidy process and get corrected markup back from it.
         *
@@ -578,7 +620,7 @@ class Parser
                global $wgTidyConf;
                $fname = 'Parser::internalTidy';
                wfProfileIn( $fname );
-               
+
                tidy_load_config( $wgTidyConf );
                tidy_set_encoding( 'utf8' );
                tidy_parse_string( $text );
@@ -615,8 +657,11 @@ class Parser
                        $fc = substr ( $x , 0 , 1 ) ;
                        if ( preg_match( '/^(:*)\{\|(.*)$/', $x, $matches ) ) {
                                $indent_level = strlen( $matches[1] );
+                               
+                               $attributes = $this->unstripForHTML( $matches[2] );
+
                                $t[$k] = str_repeat( '<dl><dd>', $indent_level ) .
-                                       '<table' . Sanitizer::fixTagAttributes ( $matches[2], 'table' ) . '>' ;
+                                       '<table' . Sanitizer::fixTagAttributes ( $attributes, 'table' ) . '>' ;
                                array_push ( $td , false ) ;
                                array_push ( $ltd , '' ) ;
                                array_push ( $tr , false ) ;
@@ -643,7 +688,8 @@ class Parser
                                array_push ( $tr , false ) ;
                                array_push ( $td , false ) ;
                                array_push ( $ltd , '' ) ;
-                               array_push ( $ltr , Sanitizer::fixTagAttributes ( $x, 'tr' ) ) ;
+                               $attributes = $this->unstripForHTML( $x );
+                               array_push ( $ltr , Sanitizer::fixTagAttributes ( $attributes, 'tr' ) ) ;
                        }
                        else if ( '|' == $fc || '!' == $fc || '|+' == substr ( $x , 0 , 2 ) ) { # Caption
                                # $x is a table row
@@ -685,7 +731,10 @@ class Parser
                                        }
                                        if ( count ( $y ) == 1 )
                                                $y = "{$z}<{$l}>{$y[0]}" ;
-                                       else $y = $y = "{$z}<{$l}".Sanitizer::fixTagAttributes($y[0], $l).">{$y[1]}" ;
+                                       else {
+                                               $attributes = $this->unstripForHTML( $y[0] );
+                                               $y = "{$z}<{$l}".Sanitizer::fixTagAttributes($attributes, $l).">{$y[1]}" ;
+                                       }
                                        $t[$k] .= $y ;
                                        array_push ( $td , true ) ;
                                }
@@ -718,7 +767,11 @@ class Parser
                $fname = 'Parser::internalParse';
                wfProfileIn( $fname );
 
-               $text = Sanitizer::removeHTMLtags( $text );
+               # Remove <noinclude> tags and <includeonly> sections
+               $text = strtr( $text, array( '<noinclude>' => '', '</noinclude>' => '') );
+               $text = preg_replace( '/<includeonly>.*?<\/includeonly>/s', '', $text );
+
+               $text = Sanitizer::removeHTMLtags( $text, array( &$this, 'attributeStripCallback' ) );
                $text = $this->replaceVariables( $text, $args );
 
                $text = preg_replace( '/(^|\n)-----*/', '\\1<hr />', $text );
@@ -730,20 +783,28 @@ class Parser
                }
                $text = $this->doAllQuotes( $text );
                $text = $this->replaceInternalLinks( $text );
-               $text = $this->replaceExternalLinks( $text );           
-               
+               $text = $this->replaceExternalLinks( $text );
+
                # replaceInternalLinks may sometimes leave behind
                # absolute URLs, which have to be masked to hide them from replaceExternalLinks
-               $text = str_replace("http-noparse://","http://",$text);
-               
+               $text = str_replace(UNIQ_PREFIX."NOPARSE", "", $text);
+
                $text = $this->doMagicLinks( $text );
                $text = $this->doTableStuff( $text );
                $text = $this->formatHeadings( $text, $isMain );
 
+               $regex = '/<!--IW_TRANSCLUDE (\d+)-->/';
+               $text = preg_replace_callback($regex, array(&$this, 'scarySubstitution'), $text);
+
                wfProfileOut( $fname );
                return $text;
        }
 
+       function scarySubstitution($matches) {
+#              return "[[".$matches[0]."]]";
+               return $this->mIWTransData[(int)$matches[0]];
+       }
+
        /**
         * Replace special strings like "ISBN xxx" and "RFC xxx" with
         * magic external links.
@@ -765,7 +826,7 @@ class Parser
        function doExponent( $text ) {
                $fname = 'Parser::doExponent';
                wfProfileIn( $fname );
-               $text = preg_replace('/\^\^(.*)\^\^/','<small><sup>\\1</sup></small>', $text);
+               $text = preg_replace('/\^\^(.*?)\^\^/','<small><sup>\\1</sup></small>', $text);
                wfProfileOut( $fname );
                return $text;
        }
@@ -984,7 +1045,7 @@ class Parser
                wfProfileIn( $fname );
 
                $sk =& $this->mOptions->getSkin();
-               
+
                $bits = preg_split( EXT_LINK_BRACKETED, $text, -1, PREG_SPLIT_DELIM_CAPTURE );
 
                $s = $this->replaceFreeExternalLinks( array_shift( $bits ) );
@@ -1019,7 +1080,7 @@ class Parser
                        # No link text, e.g. [http://domain.tld/some.link]
                        if ( $text == '' ) {
                                # Autonumber if allowed
-                               if ( strpos( HTTP_PROTOCOLS, $protocol ) !== false ) {
+                               if ( strpos( HTTP_PROTOCOLS, str_replace('/','\/', $protocol) ) !== false ) {
                                        $text = '[' . ++$this->mAutonumber . ']';
                                        $linktype = 'autonumber';
                                } else {
@@ -1061,11 +1122,12 @@ class Parser
         * @access private
         */
        function replaceFreeExternalLinks( $text ) {
+               global $wgUrlProtocols;
                global $wgContLang;
                $fname = 'Parser::replaceFreeExternalLinks';
                wfProfileIn( $fname );
-               
-               $bits = preg_split( '/(\b(?:'.URL_PROTOCOLS.'):)/S', $text, -1, PREG_SPLIT_DELIM_CAPTURE );
+
+               $bits = preg_split( '/(\b(?:'.$wgUrlProtocols.'))/S', $text, -1, PREG_SPLIT_DELIM_CAPTURE );
                $s = array_shift( $bits );
                $i = 0;
 
@@ -1117,7 +1179,7 @@ class Parser
                                $s .= $protocol . $remainder;
                        }
                }
-               wfProfileOut();
+               wfProfileOut( $fname );
                return $s;
        }
 
@@ -1136,14 +1198,14 @@ class Parser
                }
                return $text;
        }
-       
+
        /**
         * Process [[ ]] wikilinks
         *
         * @access private
         */
        function replaceInternalLinks( $s ) {
-               global $wgContLang, $wgLinkCache;
+               global $wgContLang, $wgLinkCache, $wgUrlProtocols;
                static $fname = 'Parser::replaceInternalLinks' ;
 
                wfProfileIn( $fname );
@@ -1152,7 +1214,7 @@ class Parser
                static $tc = FALSE;
                # the % is needed to support urlencoded titles as well
                if ( !$tc ) { $tc = Title::legalChars() . '#%'; }
-               
+
                $sk =& $this->mOptions->getSkin();
 
                #split the entire text string on occurences of [[
@@ -1169,7 +1231,7 @@ class Parser
                if ( !$e1_img ) { $e1_img = "/^([{$tc}]+)\\|(.*)\$/sD"; }
                # Match the end of a line for a word that's not followed by whitespace,
                # e.g. in the case of 'The Arab al[[Razi]]', 'al' will be matched
-               static $e2 = '/^(.*?)([a-zA-Z\x80-\xff]+)$/sD';
+               $e2 = wfMsgForContent( 'linkprefix' );
 
                $useLinkPrefixExtension = $wgContLang->linkPrefixExtension();
 
@@ -1181,7 +1243,6 @@ class Parser
                if ( $useLinkPrefixExtension ) {
                        if ( preg_match( $e2, $s, $m ) ) {
                                $first_prefix = $m[2];
-                               $s = $m[1];
                        } else {
                                $first_prefix = false;
                        }
@@ -1194,7 +1255,7 @@ class Parser
 
                $checkVariantLink = sizeof($wgContLang->getVariants())>1;
                $useSubpages = $this->areSubpagesAllowed();
-               
+
                # Loop for each link
                for ($k = 0; isset( $a[$k] ); $k++) {
                        $line = $a[$k];
@@ -1215,14 +1276,18 @@ class Parser
                        }
 
                        $might_be_img = false;
-                       
+
                        if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
                                $text = $m[2];
                                # If we get a ] at the beginning of $m[3] that means we have a link that's something like:
                                # [[Image:Foo.jpg|[http://example.com desc]]] <- having three ] in a row fucks up,
                                # the real problem is with the $e1 regex
                                # See bug 1300.
-                               if (preg_match( "/^\](.*)/s", $m[3], $n ) ) {
+                               #
+                               # Still some problems for cases where the ] is meant to be outside punctuation,
+                               # and no image is in sight. See bug 2095.
+                               #
+                               if( $text !== '' && preg_match( "/^\](.*)/s", $m[3], $n ) ) {
                                        $text .= ']'; # so that replaceExternalLinks($text) works later
                                        $m[3] = $n[1];
                                }
@@ -1242,7 +1307,7 @@ class Parser
                        # Don't allow internal links to pages containing
                        # PROTO: where PROTO is a valid URL protocol; these
                        # should be external links.
-                       if (preg_match('/^(\b(?:'.URL_PROTOCOLS.'):)/', $m[1])) {
+                       if (preg_match('/^(\b(?:'.$wgUrlProtocols.'))/', $m[1])) {
                                $s .= $prefix . '[[' . $line ;
                                continue;
                        }
@@ -1259,8 +1324,8 @@ class Parser
                                # Strip off leading ':'
                                $link = substr($link, 1);
                        }
-                       
-                       $nt =& Title::newFromText( $this->unstripNoWiki($link, $this->mStripState) );
+
+                       $nt = Title::newFromText( $this->unstripNoWiki($link, $this->mStripState) );
                        if( !$nt ) {
                                $s .= $prefix . '[[' . $line;
                                continue;
@@ -1275,13 +1340,14 @@ class Parser
 
                        $ns = $nt->getNamespace();
                        $iw = $nt->getInterWiki();
-                       
+
                        if ($might_be_img) { # if this is actually an invalid link
                                if ($ns == NS_IMAGE && $noforce) { #but might be an image
                                        $found = false;
                                        while (isset ($a[$k+1]) ) {
                                                #look at the next 'line' to see if we can close it there
-                                               $next_line =  array_shift(array_splice( $a, $k + 1, 1) );
+                                               $spliced = array_splice( $a, $k + 1, 1 );
+                                               $next_line = array_shift( $spliced );
                                                if( preg_match("/^(.*?]].*?)]](.*)$/sD", $next_line, $m) ) {
                                                # the first ]] closes the inner link, the second the image
                                                        $found = true;
@@ -1315,7 +1381,7 @@ class Parser
                        $wasblank = ( '' == $text );
                        if( $wasblank ) $text = $link;
 
-                       
+
                        # Link not escaped by : , create the various objects
                        if( $noforce ) {
 
@@ -1326,7 +1392,7 @@ class Parser
                                        $s .= trim($prefix . $trail, "\n") == '' ? '': $prefix . $trail;
                                        continue;
                                }
-                               
+
                                if ( $ns == NS_IMAGE ) {
                                        wfProfileIn( "$fname-image" );
                                        if ( !wfIsBadImage( $nt->getDBkey() ) ) {
@@ -1335,21 +1401,21 @@ class Parser
                                                # but it might be hard to fix that, and it doesn't matter ATM
                                                $text = $this->replaceExternalLinks($text);
                                                $text = $this->replaceInternalLinks($text);
-                                               
+
                                                # cloak any absolute URLs inside the image markup, so replaceExternalLinks() won't touch them
-                                               $s .= $prefix . str_replace('http://', 'http-noparse://', $this->makeImage( $nt, $text ) ) . $trail;
+                                               $s .= $prefix . preg_replace("/\b($wgUrlProtocols)/", UNIQ_PREFIX."NOPARSE$1", $this->makeImage( $nt, $text) ) . $trail;
                                                $wgLinkCache->addImageLinkObj( $nt );
-                                               
+
                                                wfProfileOut( "$fname-image" );
                                                continue;
                                        }
                                        wfProfileOut( "$fname-image" );
 
                                }
-                               
+
                                if ( $ns == NS_CATEGORY ) {
                                        wfProfileIn( "$fname-category" );
-                                       $t = $wgContLang->convert($nt->getText());
+                                       $t = $wgContLang->convertHtml( $nt->getText() );
                                        $s = rtrim($s . "\n"); # bug 87
 
                                        $wgLinkCache->suspend(); # Don't save in links/brokenlinks
@@ -1368,13 +1434,13 @@ class Parser
                                        $sortkey = $wgContLang->convertCategoryKey( $sortkey );
                                        $wgLinkCache->addCategoryLinkObj( $nt, $sortkey );
                                        $this->mOutput->addCategoryLink( $t );
-                                       
+
                                        /**
                                         * Strip the whitespace Category links produce, see bug 87
                                         * @todo We might want to use trim($tmp, "\n") here.
                                         */
                                        $s .= trim($prefix . $trail, "\n") == '' ? '': $prefix . $trail;
-                                       
+
                                        wfProfileOut( "$fname-category" );
                                        continue;
                                }
@@ -1396,16 +1462,7 @@ class Parser
                                $s .= $prefix . $sk->makeKnownLinkObj( $nt, $text, '', $trail );
                                continue;
                        }
-                       if ( $nt->isAlwaysKnown() ) {
-                               $s .= $sk->makeKnownLinkObj( $nt, $text, '', $trail, $prefix );
-                       } else {
-                               /**
-                                * Add a link placeholder
-                                * Later, this will be replaced by a real link, after the existence or 
-                                * non-existence of all the links is known
-                                */
-                               $s .= $this->makeLinkHolder( $nt, $text, '', $trail, $prefix );
-                       }
+                       $s .= $this->makeLinkHolder( $nt, $text, '', $trail, $prefix );
                }
                wfProfileOut( $fname );
                return $s;
@@ -1413,8 +1470,8 @@ class Parser
 
        /**
         * Make a link placeholder. The text returned can be later resolved to a real link with
-        * replaceLinkHolders(). This is done for two reasons: firstly to avoid further 
-        * parsing of interwiki links, and secondly to allow all extistence checks and 
+        * replaceLinkHolders(). This is done for two reasons: firstly to avoid further
+        * parsing of interwiki links, and secondly to allow all extistence checks and
         * article length checks (for stub links) to be bundled into a single query.
         *
         */
@@ -1425,17 +1482,17 @@ class Parser
                } else {
                        # Separate the link trail from the rest of the link
                        list( $inside, $trail ) = Linker::splitTrail( $trail );
-                       
+
                        if ( $nt->isExternal() ) {
-                               $iwRecord = array( $nt->getPrefixedDBkey(), $prefix.$text.$inside );
-                               $nr = array_push($this->mInterwikiLinkHolders, $iwRecord);
+                               $nr = array_push( $this->mInterwikiLinkHolders['texts'], $prefix.$text.$inside );
+                               $this->mInterwikiLinkHolders['titles'][] = $nt;
                                $retVal = '<!--IWLINK '. ($nr-1) ."-->{$trail}";
                        } else {
                                $nr = array_push( $this->mLinkHolders['namespaces'], $nt->getNamespace() );
                                $this->mLinkHolders['dbkeys'][] = $nt->getDBkey();
                                $this->mLinkHolders['queries'][] = $query;
                                $this->mLinkHolders['texts'][] = $prefix.$text.$inside;
-                               $this->mLinkHolders['titles'][] =& $nt;
+                               $this->mLinkHolders['titles'][] = $nt;
 
                                $retVal = '<!--LINK '. ($nr-1) ."-->{$trail}";
                        }
@@ -1452,7 +1509,7 @@ class Parser
                global $wgNamespacesWithSubpages;
                return !empty($wgNamespacesWithSubpages[$this->mTitle->getNamespace()]);
        }
-       
+
        /**
         * Handle link to subpage if necessary
         * @param string $target the source of the link
@@ -1472,10 +1529,10 @@ class Parser
                $fname = 'Parser::maybeDoSubpageLink';
                wfProfileIn( $fname );
                $ret = $target; # default return value is no change
-                       
-               # Some namespaces don't allow subpages, 
+
+               # Some namespaces don't allow subpages,
                # so only perform processing if subpages are allowed
-               if( $this->areSubpagesAllowed() ) {             
+               if( $this->areSubpagesAllowed() ) {
                        # Look at the first character
                        if( $target != '' && $target{0} == '/' ) {
                                # / at end means we don't want the slash to be shown
@@ -1485,7 +1542,7 @@ class Parser
                                } else {
                                        $noslash = substr( $target, 1 );
                                }
-                               
+
                                $ret = $this->mTitle->getPrefixedText(). '/' . trim($noslash);
                                if( '' === $text ) {
                                        $text = $target;
@@ -1698,7 +1755,7 @@ class Parser
                                if ( $openmatch or $closematch ) {
                                        $paragraphStack = false;
                                        $output .= $this->closeParagraph();
-                                       if($preOpenMatch and !$preCloseMatch) {
+                                       if ( $preOpenMatch and !$preCloseMatch ) {
                                                $this->mInPre = true;
                                        }
                                        if ( $closematch ) {
@@ -1745,6 +1802,10 @@ class Parser
                                }
                                wfProfileOut( "$fname-paragraph" );
                        }
+                       // somewhere above we forget to get out of pre block (bug 785)
+                       if($preCloseMatch && $this->mInPre) {
+                               $this->mInPre = false;
+                       }
                        if ($paragraphStack === false) {
                                $output .= $t."\n";
                        }
@@ -1808,15 +1869,15 @@ class Parser
         * @access private
         */
        function getVariableValue( $index ) {
-               global $wgContLang, $wgSitename, $wgServer, $wgArticle;
-               
+               global $wgContLang, $wgSitename, $wgServer, $wgServerName, $wgArticle, $wgScriptPath;
+
                /**
                 * Some of these require message or data lookups and can be
                 * expensive to check many times.
                 */
                static $varCache = array();
                if( isset( $varCache[$index] ) ) return $varCache[$index];
-               
+
                switch ( $index ) {
                        case MAG_CURRENTMONTH:
                                return $varCache[$index] = $wgContLang->formatNum( date( 'm' ) );
@@ -1849,10 +1910,16 @@ class Parser
                                return $varCache[$index] = $wgContLang->formatNum( date('w') );
                        case MAG_NUMBEROFARTICLES:
                                return $varCache[$index] = $wgContLang->formatNum( wfNumberOfArticles() );
+                       case MAG_NUMBEROFFILES:
+                               return $varCache[$index] = $wgContLang->formatNum( wfNumberOfFiles() );
                        case MAG_SITENAME:
                                return $wgSitename;
                        case MAG_SERVER:
                                return $wgServer;
+                       case MAG_SERVERNAME:
+                               return $wgServerName;
+                       case MAG_SCRIPTPATH:
+                               return $wgScriptPath;
                        default:
                                return NULL;
                }
@@ -1884,7 +1951,7 @@ class Parser
         *  OT_WIKI: only {{subst:}} templates
         *  OT_MSG: only magic variables
         *  OT_HTML: all templates and magic variables
-        * 
+        *
         * @param string $tex The text to transform
         * @param array $args Key-value pairs representing template parameters to substitute
         * @access private
@@ -1906,7 +1973,7 @@ class Parser
 
                # Variable substitution
                $text = preg_replace_callback( "/{{([$titleChars]*?)}}/", array( &$this, 'variableSubstitution' ), $text );
-               
+
                if ( $this->mOutputType == OT_HTML || $this->mOutputType == OT_WIKI ) {
                        # Argument substitution
                        $text = preg_replace_callback( "/{{{([$titleChars]*?)}}}/", array( &$this, 'argSubstitution' ), $text );
@@ -1965,7 +2032,7 @@ class Parser
                # merged with the next arg because the '|' character between belongs
                # to the link syntax and not the template parameter syntax.
                $argc = count($args);
-               
+
                for ( $i = 0; $i < $argc-1; $i++ ) {
                        if ( substr_count ( $args[$i], '[[' ) != substr_count ( $args[$i], ']]' ) ) {
                                $args[$i] .= '|'.$args[$i+1];
@@ -1992,7 +2059,7 @@ class Parser
                global $wgLinkCache, $wgContLang;
                $fname = 'Parser::braceSubstitution';
                wfProfileIn( $fname );
-               
+
                $found = false;
                $nowiki = false;
                $noparse = false;
@@ -2040,6 +2107,10 @@ class Parser
                        $mwMsgnw =& MagicWord::get( MAG_MSGNW );
                        if ( $mwMsgnw->matchStartAndRemove( $part1 ) ) {
                                $nowiki = true;
+                       } else {
+                               # Remove obsolete MSG:
+                               $mwMsg =& MagicWord::get( MAG_MSG );
+                               $mwMsg->matchStartAndRemove( $part1 );
                        }
 
                        # Check if it is an internal message
@@ -2070,20 +2141,27 @@ class Parser
                        }
                }
 
-               # LOCALURL and LOCALURLE
+               # LOCALURL and FULLURL
                if ( !$found ) {
-                       $mwLocal = MagicWord::get( MAG_LOCALURL );
-                       $mwLocalE = MagicWord::get( MAG_LOCALURLE );
+                       $mwLocal =& MagicWord::get( MAG_LOCALURL );
+                       $mwLocalE =& MagicWord::get( MAG_LOCALURLE );
+                       $mwFull =& MagicWord::get( MAG_FULLURL );
+                       $mwFullE =& MagicWord::get( MAG_FULLURLE );
+                       
 
                        if ( $mwLocal->matchStartAndRemove( $part1 ) ) {
                                $func = 'getLocalURL';
                        } elseif ( $mwLocalE->matchStartAndRemove( $part1 ) ) {
                                $func = 'escapeLocalURL';
+                       } elseif ( $mwFull->matchStartAndRemove( $part1 ) ) {
+                               $func = 'getFullURL';
+                       } elseif ( $mwFullE->matchStartAndRemove( $part1 ) ) {
+                               $func = 'escapeFullURL';
                        } else {
-                               $func = '';
+                               $func = false;
                        }
 
-                       if ( $func !== '' ) {
+                       if ( $func !== false ) {
                                $title = Title::newFromText( $part1 );
                                if ( !is_null( $title ) ) {
                                        if ( $argc > 0 ) {
@@ -2105,6 +2183,16 @@ class Parser
                        }
                }
 
+               # PLURAL
+               if ( !$found && $argc >= 2 ) {
+                       $mwPluralForm =& MagicWord::get( MAG_PLURAL );
+                       if ( $mwPluralForm->matchStartAndRemove( $part1 ) ) {
+                               if ($argc==2) {$args[2]=$args[1];}
+                               $text = $linestart . $wgContLang->convertPlural( $part1, $args[0], $args[1], $args[2]);
+                               $found = true;
+                       }
+               }
+
                # Template table test
 
                # Did we encounter this template already? If yes, it is in the cache
@@ -2127,7 +2215,8 @@ class Parser
                }
 
                # Load from database
-               $itcamefromthedatabase = false;
+               $replaceHeadings = false;
+               $isHTML = false;
                $lastPathLevel = $this->mTemplatePath;
                if ( !$found ) {
                        $ns = NS_TEMPLATE;
@@ -2136,29 +2225,48 @@ class Parser
                                $ns = $this->mTitle->getNamespace();
                        }
                        $title = Title::newFromText( $part1, $ns );
+
+                       if ($title) {
+                               $interwiki = Title::getInterwikiLink($title->getInterwiki());
+                               if ($interwiki != '' && $title->isTrans()) {
+                                       return $this->scarytransclude($title, $interwiki);
+                               }
+                       }
+
                        if ( !is_null( $title ) && !$title->isExternal() ) {
                                # Check for excessive inclusion
                                $dbk = $title->getPrefixedDBkey();
                                if ( $this->incrementIncludeCount( $dbk ) ) {
-                                       # This should never be reached.
-                                       $article = new Article( $title );
-                                       $articleContent = $article->getContentWithoutUsingSoManyDamnGlobals();
-                                       if ( $articleContent !== false ) {
-                                               $found = true;
-                                               $text = $linestart . $articleContent;
-                                               $itcamefromthedatabase = true;
+                                       if ( $title->getNamespace() == NS_SPECIAL && $this->mOptions->getAllowSpecialInclusion() ) {
+                                               # Capture special page output
+                                               $text = SpecialPage::capturePath( $title );
+                                               if ( is_string( $text ) ) {
+                                                       $found = true;
+                                                       $noparse = true;
+                                                       $isHTML = true;
+                                                       $this->disableCache();
+                                               }
+                                       } else {
+                                               $article = new Article( $title );
+                                               $articleContent = $article->fetchContent(0, false);
+                                               if ( $articleContent !== false ) {
+                                                       $found = true;
+                                                       $text = $articleContent;
+                                                       $replaceHeadings = true;
+                                               }
                                        }
                                }
 
                                # If the title is valid but undisplayable, make a link to it
                                if ( $this->mOutputType == OT_HTML && !$found ) {
-                                       $text = $linestart . '[['.$title->getPrefixedText().']]';
+                                       $text = '[['.$title->getPrefixedText().']]';
                                        $found = true;
                                }
 
                                # Template cache array insertion
                                if( $found ) {
                                        $this->mTemplates[$part1] = $text;
+                                       $text = $linestart . $text;
                                }
                        }
                }
@@ -2191,8 +2299,13 @@ class Parser
                        $this->mTemplatePath[$part1] = 1;
 
                        if( $this->mOutputType == OT_HTML ) {
+                               # Remove <noinclude> sections and <includeonly> tags
+                               $text = preg_replace( '/<noinclude>.*?<\/noinclude>/s', '', $text );
+                               $text = strtr( $text, array( '<includeonly>' => '' , '</includeonly>' => '' ) );
+                               # Strip <nowiki>, <pre>, etc.
+                               $this->mCurrentParams = $assocArgs;
                                $text = $this->strip( $text, $this->mStripState );
-                               $text = Sanitizer::removeHTMLtags( $text );
+                               $text = Sanitizer::removeHTMLtags( $text, array( &$this, 'replaceVariables' ), $assocArgs );
                        }
                        $text = $this->replaceVariables( $text, $assocArgs );
 
@@ -2209,41 +2322,49 @@ class Parser
                }
                # Prune lower levels off the recursion check path
                $this->mTemplatePath = $lastPathLevel;
-               
+
                if ( !$found ) {
                        wfProfileOut( $fname );
                        return $matches[0];
                } else {
-                       # replace ==section headers==
-                       # XXX this needs to go away once we have a better parser.
-                       if ( $this->mOutputType != OT_WIKI && $itcamefromthedatabase ) {
-                               if( !is_null( $title ) )
-                                       $encodedname = base64_encode($title->getPrefixedDBkey());
-                               else
-                                       $encodedname = base64_encode("");
-                               $m = preg_split('/(^={1,6}.*?={1,6}\s*?$)/m', $text, -1,
-                                       PREG_SPLIT_DELIM_CAPTURE);
-                               $text = '';
-                               $nsec = 0;
-                               for( $i = 0; $i < count($m); $i += 2 ) {
-                                       $text .= $m[$i];
-                                       if (!isset($m[$i + 1]) || $m[$i + 1] == "") continue;
-                                       $hl = $m[$i + 1];
-                                       if( strstr($hl, "<!--MWTEMPLATESECTION") ) {
-                                               $text .= $hl;
-                                               continue;
+                       if ( $isHTML ) {
+                               # Replace raw HTML by a placeholder
+                               # Add a blank line preceding, to prevent it from mucking up
+                               # immediately preceding headings
+                               $text = "\n\n" . $this->insertStripItem( $text, $this->mStripState );
+                       } else {
+                               # replace ==section headers==
+                               # XXX this needs to go away once we have a better parser.
+                               if ( $this->mOutputType != OT_WIKI && $replaceHeadings ) {
+                                       if( !is_null( $title ) )
+                                               $encodedname = base64_encode($title->getPrefixedDBkey());
+                                       else
+                                               $encodedname = base64_encode("");
+                                       $m = preg_split('/(^={1,6}.*?={1,6}\s*?$)/m', $text, -1,
+                                               PREG_SPLIT_DELIM_CAPTURE);
+                                       $text = '';
+                                       $nsec = 0;
+                                       for( $i = 0; $i < count($m); $i += 2 ) {
+                                               $text .= $m[$i];
+                                               if (!isset($m[$i + 1]) || $m[$i + 1] == "") continue;
+                                               $hl = $m[$i + 1];
+                                               if( strstr($hl, "<!--MWTEMPLATESECTION") ) {
+                                                       $text .= $hl;
+                                                       continue;
+                                               }
+                                               preg_match('/^(={1,6})(.*?)(={1,6})\s*?$/m', $hl, $m2);
+                                               $text .= $m2[1] . $m2[2] . "<!--MWTEMPLATESECTION="
+                                                       . $encodedname . "&" . base64_encode("$nsec") . "-->" . $m2[3];
+
+                                               $nsec++;
                                        }
-                                       preg_match('/^(={1,6})(.*?)(={1,6})\s*?$/m', $hl, $m2);
-                                       $text .= $m2[1] . $m2[2] . "<!--MWTEMPLATESECTION="
-                                               . $encodedname . "&" . base64_encode("$nsec") . "-->" . $m2[3];
-                                       
-                                       $nsec++;
                                }
                        }
                }
+
                # Prune lower levels off the recursion check path
                $this->mTemplatePath = $lastPathLevel;
-               
+
                if ( !$found ) {
                        wfProfileOut( $fname );
                        return $matches[0];
@@ -2253,6 +2374,49 @@ class Parser
                }
        }
 
+       /**
+        * Translude an interwiki link.
+        */
+       function scarytransclude($title, $interwiki) {
+               global $wgEnableScaryTranscluding;
+
+               if (!$wgEnableScaryTranscluding)
+                       return wfMsg('scarytranscludedisabled');
+
+               $articlename = "Template:" . $title->getDBkey();
+               $url = str_replace('$1', urlencode($articlename), $interwiki);
+               if (strlen($url) > 255)
+                       return wfMsg('scarytranscludetoolong');
+               $text = $this->fetchScaryTemplateMaybeFromCache($url);
+               $this->mIWTransData[] = $text;
+               return "<!--IW_TRANSCLUDE ".(count($this->mIWTransData) - 1)."-->";
+       }
+
+       function fetchScaryTemplateMaybeFromCache($url) {
+               $dbr =& wfGetDB(DB_SLAVE);
+               $obj = $dbr->selectRow('transcache', array('tc_time', 'tc_contents'),
+                               array('tc_url' => $url));
+               if ($obj) {
+                       $time = $obj->tc_time;
+                       $text = $obj->tc_contents;
+                       if ($time && $time < (time() + (60*60))) {
+                               return $text;
+                       }
+               }
+
+               $text = wfGetHTTP($url . '?action=render');
+               if (!$text)
+                       return wfMsg('scarytranscludefailed', $url);
+
+               $dbw =& wfGetDB(DB_MASTER);
+               $dbw->replace('transcache', array(), array(
+                       'tc_url' => $url,
+                       'tc_time' => time(),
+                       'tc_contents' => $text));
+               return $text;
+       }
+
+
        /**
         * Triple brace replacement -- used for template arguments
         * @access private
@@ -2290,16 +2454,16 @@ class Parser
         * 2) Add an [edit] link to sections for logged in users who have enabled the option
         * 3) Add a Table of contents on the top for users who have enabled the option
         * 4) Auto-anchor headings
-        *      
+        *
         * It loops through all headlines, collects the necessary data, then splits up the
         * string and re-inserts the newly formatted headlines.
-        * 
+        *
         * @param string $text
         * @param boolean $isMain
         * @access private
         */
        function formatHeadings( $text, $isMain=true ) {
-               global $wgInputEncoding, $wgMaxTocLevel, $wgContLang, $wgLinkHolders, $wgInterwikiLinkHolders;
+               global $wgMaxTocLevel, $wgContLang, $wgLinkHolders, $wgInterwikiLinkHolders;
 
                $doNumberHeadings = $this->mOptions->getNumberHeadings();
                $doShowToc = true;
@@ -2390,9 +2554,9 @@ class Parser
                                $prevtoclevel = $toclevel;
                        }
                        $level = $matches[1][$headlineCount];
-                       
+
                        if( $doNumberHeadings || $doShowToc ) {
-                               
+
                                if ( $level > $prevlevel ) {
                                        # Increase TOC level
                                        $toclevel++;
@@ -2426,7 +2590,7 @@ class Parser
                                        # No change in level, end TOC line
                                        $toc .= $sk->tocLineEnd();
                                }
-                               
+
                                $levelCount[$toclevel] = $level;
 
                                # count number of headlines for each level
@@ -2450,19 +2614,19 @@ class Parser
 
                        # Remove link placeholders by the link text.
                        #     <!--LINK number-->
-                       # turns into 
+                       # turns into
                        #     link text with suffix
                        $canonized_headline = preg_replace( '/<!--LINK ([0-9]*)-->/e',
                                                            "\$this->mLinkHolders['texts'][\$1]",
                                                            $canonized_headline );
                        $canonized_headline = preg_replace( '/<!--IWLINK ([0-9]*)-->/e',
-                                                           "\$this->mInterwikiLinkHolders[\$1][1]",
+                                                           "\$this->mInterwikiLinkHolders['texts'][\$1]",
                                                            $canonized_headline );
 
                        # strip out HTML
                        $canonized_headline = preg_replace( '/<.*?' . '>/','',$canonized_headline );
                        $tocline = trim( $canonized_headline );
-                       $canonized_headline = urlencode( do_html_entity_decode( str_replace(' ', '_', $tocline), ENT_COMPAT, $wgInputEncoding ) );
+                       $canonized_headline = urlencode( Sanitizer::decodeCharReferences( str_replace(' ', '_', $tocline) ) );
                        $replacearray = array(
                                '%3A' => ':',
                                '%' => '.'
@@ -2602,7 +2766,7 @@ class Parser
         * @return string
         */
        function magicRFC( $text, $keyword='RFC ', $urlmsg='rfcurl'  ) {
-               
+
                $valid = '0123456789';
                $internal = false;
 
@@ -2611,7 +2775,7 @@ class Parser
                        return $text;
                }
                $text = substr( array_shift( $a ), 1);
-               
+
                /* Check if keyword is preceed by [[.
                 * This test is made here cause of the array_shift above
                 * that prevent the test to be done in the foreach.
@@ -2654,7 +2818,7 @@ class Parser
                                $la = $sk->getExternalLinkAttributes( $url, $keyword.$id );
                                $text .= "<a href='{$url}'{$la}>{$keyword}{$id}</a>{$x}";
                        }
-                       
+
                        /* Check if the next RFC keyword is preceed by [[ */
                        $internal = ( substr($x,-2) == '[[' );
                }
@@ -2720,7 +2884,7 @@ class Parser
                 * everyone the same signiture and use the default one rather
                 * than the one selected in each users preferences.
                 */
-               $d = $wgContLang->timeanddate( wfTimestampNow(), false, false) .
+               $d = $wgContLang->timeanddate( date( 'YmdHis' ), false, false) .
                  ' (' . date( 'T' ) . ')';
                if ( isset( $wgLocaltimezone ) ) {
                        putenv( 'TZ='.$oldtz );
@@ -2787,7 +2951,7 @@ class Parser
 
        /**
         * Transform a MediaWiki message by replacing magic variables.
-        * 
+        *
         * @param string $text the text to transform
         * @param ParserOptions $options  options
         * @return string the text with variables substituted
@@ -2844,16 +3008,16 @@ class Parser
                $pdbks = array();
                $colours = array();
                $sk = $this->mOptions->getSkin();
-               
+
                if ( !empty( $this->mLinkHolders['namespaces'] ) ) {
                        wfProfileIn( $fname.'-check' );
                        $dbr =& wfGetDB( DB_SLAVE );
                        $page = $dbr->tableName( 'page' );
                        $threshold = $wgUser->getOption('stubthreshold');
-                       
+
                        # Sort by namespace
                        asort( $this->mLinkHolders['namespaces'] );
-       
+
                        # Generate query
                        $query = false;
                        foreach ( $this->mLinkHolders['namespaces'] as $key => $val ) {
@@ -2868,7 +3032,7 @@ class Parser
                                $pdbk = $pdbks[$key] = $title->getPrefixedDBkey();
 
                                # Check if it's in the link cache already
-                               if ( $wgLinkCache->getGoodLinkID( $pdbk ) ) {
+                               if ( $title->isAlwaysKnown() || $wgLinkCache->getGoodLinkID( $pdbk ) ) {
                                        $colours[$pdbk] = 1;
                                } elseif ( $wgLinkCache->isBadLink( $pdbk ) ) {
                                        $colours[$pdbk] = 0;
@@ -2887,7 +3051,7 @@ class Parser
                                        } else {
                                                $query .= ', ';
                                        }
-                               
+
                                        $query .= $dbr->addQuotes( $this->mLinkHolders['dbkeys'][$key] );
                                }
                        }
@@ -2896,9 +3060,9 @@ class Parser
                                if ( $options & RLH_FOR_UPDATE ) {
                                        $query .= ' FOR UPDATE';
                                }
-                       
+
                                $res = $dbr->query( $query, $fname );
-                               
+
                                # Fetch data and form into an associative array
                                # non-existent = broken
                                # 1 = known
@@ -2906,8 +3070,8 @@ class Parser
                                while ( $s = $dbr->fetchObject($res) ) {
                                        $title = Title::makeTitle( $s->page_namespace, $s->page_title );
                                        $pdbk = $title->getPrefixedDBkey();
-                                       $wgLinkCache->addGoodLink( $s->page_id, $pdbk );
-                                       
+                                       $wgLinkCache->addGoodLinkObj( $s->page_id, $title );
+
                                        if ( $threshold >  0 ) {
                                                $size = $s->page_len;
                                                if ( $s->page_is_redirect || $s->page_namespace != 0 || $size >= $threshold ) {
@@ -2921,7 +3085,7 @@ class Parser
                                }
                        }
                        wfProfileOut( $fname.'-check' );
-                       
+
                        # Construct search and replace arrays
                        wfProfileIn( $fname.'-construct' );
                        $wgOutputReplace = array();
@@ -2930,7 +3094,7 @@ class Parser
                                $searchkey = "<!--LINK $key-->";
                                $title = $this->mLinkHolders['titles'][$key];
                                if ( empty( $colours[$pdbk] ) ) {
-                                       $wgLinkCache->addBadLink( $pdbk );
+                                       $wgLinkCache->addBadLinkObj( $title );
                                        $colours[$pdbk] = 0;
                                        $wgOutputReplace[$searchkey] = $sk->makeBrokenLinkObj( $title,
                                                                        $this->mLinkHolders['texts'][$key],
@@ -2949,7 +3113,7 @@ class Parser
 
                        # Do the thing
                        wfProfileIn( $fname.'-replace' );
-                       
+
                        $text = preg_replace_callback(
                                '/(<!--LINK .*?-->)/',
                                "wfOutputReplaceMatches",
@@ -2960,15 +3124,15 @@ class Parser
 
                # Now process interwiki link holders
                # This is quite a bit simpler than internal links
-               if ( !empty( $this->mInterwikiLinkHolders ) ) {
+               if ( !empty( $this->mInterwikiLinkHolders['texts'] ) ) {
                        wfProfileIn( $fname.'-interwiki' );
                        # Make interwiki link HTML
                        $wgOutputReplace = array();
-                       foreach( $this->mInterwikiLinkHolders as $i => $lh ) {
-                               $s = $sk->makeLink( $lh[0], $lh[1] );
-                               $wgOutputReplace[] = $s;
+                       foreach( $this->mInterwikiLinkHolders['texts'] as $key => $link ) {
+                               $title = $this->mInterwikiLinkHolders['titles'][$key];
+                               $wgOutputReplace[$key] = $sk->makeLinkObj( $title, $link );
                        }
-                       
+
                        $text = preg_replace_callback(
                                '/<!--IWLINK (.*?)-->/',
                                "wfOutputReplaceMatches",
@@ -2980,6 +3144,48 @@ class Parser
                return $colours;
        }
 
+       /**
+        * Replace <!--LINK--> link placeholders with plain text of links
+        * (not HTML-formatted).
+        * @param string $text
+        * @return string
+        */
+       function replaceLinkHoldersText( $text ) {
+               global $wgUser, $wgLinkCache;
+               global $wgOutputReplace;
+
+               $fname = 'Parser::replaceLinkHoldersText';
+               wfProfileIn( $fname );
+
+               $text = preg_replace_callback(
+                       '/<!--(LINK|IWLINK) (.*?)-->/',
+                       array( &$this, 'replaceLinkHoldersTextCallback' ),
+                       $text );
+
+               wfProfileOut( $fname );
+               return $text;
+       }
+
+       /**
+        * @param array $matches
+        * @return string
+        * @access private
+        */
+       function replaceLinkHoldersTextCallback( $matches ) {
+               $type = $matches[1];
+               $key  = $matches[2];
+               if( $type == 'LINK' ) {
+                       if( isset( $this->mLinkHolders['texts'][$key] ) ) {
+                               return $this->mLinkHolders['texts'][$key];
+                       }
+               } elseif( $type == 'IWLINK' ) {
+                       if( isset( $this->mInterwikiLinkHolders['texts'][$key] ) ) {
+                               return $this->mInterwikiLinkHolders['texts'][$key];
+                       }
+               }
+               return $matches[0];
+       }
+
        /**
         * Renders an image gallery from a text with one line per image.
         * text labels may be given by using |-style alternative text. E.g.
@@ -2993,9 +3199,10 @@ class Parser
         */
        function renderImageGallery( $text ) {
                # Setup the parser
-               global $wgUser, $wgParser, $wgTitle;
+               global $wgUser, $wgTitle;
                $parserOptions = ParserOptions::newFromUser( $wgUser );
-       
+               $localParser = new Parser();
+
                global $wgLinkCache;
                $ig = new ImageGallery();
                $ig->setShowBytes( false );
@@ -3020,10 +3227,10 @@ class Parser
                        } else {
                                $label = '';
                        }
-                       
-                       $html = $wgParser->parse( $label , $wgTitle, $parserOptions );
+
+                       $html = $localParser->parse( $label , $wgTitle, $parserOptions );
                        $html = $html->mText;
-                       
+
                        $ig->add( new Image( $nt ), $html );
                        $wgLinkCache->addImageLinkObj( $nt );
                }
@@ -3036,7 +3243,7 @@ class Parser
        function makeImage( &$nt, $options ) {
                global $wgContLang, $wgUseImageResize;
                global $wgUser, $wgThumbLimits;
-               
+
                $align = '';
 
                # Check if the options text is of the form "options|alt text"
@@ -3052,6 +3259,7 @@ class Parser
                $part = explode( '|', $options);
 
                $mwThumb  =& MagicWord::get( MAG_IMG_THUMBNAIL );
+               $mwManualThumb =& MagicWord::get( MAG_IMG_MANUALTHUMB );
                $mwLeft   =& MagicWord::get( MAG_IMG_LEFT );
                $mwRight  =& MagicWord::get( MAG_IMG_RIGHT );
                $mwNone   =& MagicWord::get( MAG_IMG_NONE );
@@ -3061,17 +3269,15 @@ class Parser
                $caption = '';
 
                $width = $height = $framed = $thumb = false;
-               $manual_thumb = "" ;
+               $manual_thumb = '' ;
 
                foreach( $part as $key => $val ) {
-                       $val_parts = explode ( "=" , $val , 2 ) ;
-                       $left_part = array_shift ( $val_parts ) ;
                        if ( $wgUseImageResize && ! is_null( $mwThumb->matchVariableStartToEnd($val) ) ) {
                                $thumb=true;
-                       } elseif ( $wgUseImageResize && count ( $val_parts ) == 1 && ! is_null( $mwThumb->matchVariableStartToEnd($left_part) ) ) {
+                       } elseif ( ! is_null( $match = $mwManualThumb->matchVariableStartToEnd($val) ) ) {
                                # use manually specified thumbnail
                                $thumb=true;
-                               $manual_thumb = array_shift ( $val_parts ) ;
+                               $manual_thumb = $match;
                        } elseif ( ! is_null( $mwRight->matchVariableStartToEnd($val) ) ) {
                                # remember to set an alignment, don't render immediately
                                $align = 'right';
@@ -3100,14 +3306,41 @@ class Parser
                        }
                }
                # Strip bad stuff out of the alt text
-               $alt = $caption;
-               $this->replaceLinkHolders( $alt );
+               $alt = $this->replaceLinkHoldersText( $caption );
                $alt = Sanitizer::stripAllTags( $alt );
 
                # Linker does the rest
                $sk =& $this->mOptions->getSkin();
                return $sk->makeImageLinkObj( $nt, $caption, $alt, $align, $width, $height, $framed, $thumb, $manual_thumb );
        }
+
+       /**
+        * Set a flag in the output object indicating that the content is dynamic and 
+        * shouldn't be cached.
+        */
+       function disableCache() {
+               $this->mOutput->mCacheTime = -1;
+       }
+       
+       /**
+        * Callback from the Sanitizer for expanding items found in HTML attribute
+        * values, so they can be safely tested and escaped.
+        * @param string $text
+        * @param array $args
+        * @return string
+        * @access private
+        */
+       function attributeStripCallback( &$text, $args ) {
+               $text = $this->replaceVariables( $text, $args );
+               $text = $this->unstripForHTML( $text );
+               return $text;
+       }
+       
+       function unstripForHTML( $text ) {
+               $text = $this->unstrip( $text, $this->mStripState );
+               $text = $this->unstripNoWiki( $text, $this->mStripState );
+               return $text;
+       }
 }
 
 /**
@@ -3117,10 +3350,9 @@ class Parser
 class ParserOutput
 {
        var $mText, $mLanguageLinks, $mCategoryLinks, $mContainsOldMagic;
-       var $mCacheTime; # Used in ParserCache
+       var $mCacheTime; # Timestamp on this article, or -1 for uncacheable. Used in ParserCache.
        var $mVersion;   # Compatibility check
        var $mTitleText; # title text of the chosen language variant
-       var $mLcfirstTitle; # This is true if the first letter in the title has to be lowercase
 
        function ParserOutput( $text = '', $languageLinks = array(), $categoryLinks = array(),
                $containsOldMagic = false, $titletext = '' )
@@ -3139,7 +3371,6 @@ class ParserOutput
        function getCategoryLinks()          { return array_keys( $this->mCategoryLinks ); }
        function getCacheTime()              { return $this->mCacheTime; }
        function getTitleText()              { return $this->mTitleText; }
-       function getLcfirstTitle()             { return $this->mLcfirstTitle; }
        function containsOldMagic()          { return $this->mContainsOldMagic; }
        function setText( $text )            { return wfSetVar( $this->mText, $text ); }
        function setLanguageLinks( $ll )     { return wfSetVar( $this->mLanguageLinks, $ll ); }
@@ -3167,7 +3398,8 @@ class ParserOutput
         */
        function expired( $touched ) {
                global $wgCacheEpoch;
-               return $this->getCacheTime() <= $touched ||
+               return $this->getCacheTime() == -1 || // parser says it's uncacheable
+                      $this->getCacheTime() <= $touched ||
                       $this->getCacheTime() <= $wgCacheEpoch ||
                       !isset( $this->mVersion ) ||
                       version_compare( $this->mVersion, MW_PARSER_VERSION, "lt" );
@@ -3190,15 +3422,18 @@ class ParserOptions
        var $mDateFormat;                # Date format index
        var $mEditSection;               # Create "edit section" links
        var $mNumberHeadings;            # Automatically number headings
+       var $mAllowSpecialInclusion;     # Allow inclusion of special pages
 
        function getUseTeX()                        { return $this->mUseTeX; }
        function getUseDynamicDates()               { return $this->mUseDynamicDates; }
        function getInterwikiMagic()                { return $this->mInterwikiMagic; }
        function getAllowExternalImages()           { return $this->mAllowExternalImages; }
-       function getSkin()                          { return $this->mSkin; }
+       function &getSkin()                         { return $this->mSkin; }
        function getDateFormat()                    { return $this->mDateFormat; }
        function getEditSection()                   { return $this->mEditSection; }
        function getNumberHeadings()                { return $this->mNumberHeadings; }
+       function getAllowSpecialInclusion()         { return $this->mAllowSpecialInclusion; }
+
 
        function setUseTeX( $x )                    { return wfSetVar( $this->mUseTeX, $x ); }
        function setUseDynamicDates( $x )           { return wfSetVar( $this->mUseDynamicDates, $x ); }
@@ -3207,9 +3442,15 @@ class ParserOptions
        function setDateFormat( $x )                { return wfSetVar( $this->mDateFormat, $x ); }
        function setEditSection( $x )               { return wfSetVar( $this->mEditSection, $x ); }
        function setNumberHeadings( $x )            { return wfSetVar( $this->mNumberHeadings, $x ); }
+       function setAllowSpecialInclusion( $x )     { return wfSetVar( $this->mAllowSpecialInclusion, $x ); }
 
        function setSkin( &$x ) { $this->mSkin =& $x; }
 
+       function ParserOptions() {
+               global $wgUser;
+               $this->initialiseFromUser( $wgUser );
+       }
+
        /**
         * Get parser options
         * @static
@@ -3222,7 +3463,8 @@ class ParserOptions
 
        /** Get user options */
        function initialiseFromUser( &$userInput ) {
-               global $wgUseTeX, $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
+               global $wgUseTeX, $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages,
+                      $wgAllowSpecialInclusion;
                $fname = 'ParserOptions::initialiseFromUser';
                wfProfileIn( $fname );
                if ( !$userInput ) {
@@ -3240,8 +3482,9 @@ class ParserOptions
                $this->mSkin =& $user->getSkin();
                wfProfileOut( $fname.'-skin' );
                $this->mDateFormat = $user->getOption( 'date' );
-               $this->mEditSection = $user->getOption( 'editsection' );
+               $this->mEditSection = true;
                $this->mNumberHeadings = $user->getOption( 'numberheadings' );
+               $this->mAllowSpecialInclusion = $wgAllowSpecialInclusion;
                wfProfileOut( $fname );
        }
 }
@@ -3265,6 +3508,20 @@ function wfNumberOfArticles() {
        return $wgNumberOfArticles;
 }
 
+/**
+ * Return the number of files
+ */
+function wfNumberOfFiles() {
+       $fname = 'Parser::wfNumberOfFiles';
+
+       wfProfileIn( $fname );
+       $dbr =& wfGetDB( DB_SLAVE );
+       $res = $dbr->selectField('image', 'COUNT(*)', array(), $fname );
+       wfProfileOut( $fname );
+
+       return $res;
+}
+
 /**
  * Get various statistics from the database
  * @private
@@ -3292,7 +3549,7 @@ function wfLoadSiteStats() {
 /**
  * Escape html tags
  * Basicly replacing " > and < with HTML entities ( &quot;, &gt;, &lt;)
- *  
+ *
  * @param string $in Text that might contain HTML tags
  * @return string Escaped string
  */