Fixes and improvements to interwiki transclusion:
[lhc/web/wiklou.git] / includes / Parser.php
index f40d3ea..5d5c4b4 100644 (file)
@@ -15,7 +15,7 @@ require_once( 'HttpFunctions.php' );
  * changes in an incompatible way, so the parser cache
  * can automatically discard old data.
  */
-define( 'MW_PARSER_VERSION', '1.5.0' );
+define( 'MW_PARSER_VERSION', '1.6.0' );
 
 /**
  * Variable substitution O(N^2) attack
@@ -43,13 +43,10 @@ define( 'OT_MSG' , 3 );
 # may want to use in wikisyntax
 define( 'STRIP_COMMENTS', 'HTMLCommentStrip' );
 
-# prefix for escaping, used in two functions at least
-define( 'UNIQ_PREFIX', 'NaodW29');
-
 # Constants needed for external link processing
 define( 'HTTP_PROTOCOLS', 'http:\/\/|https:\/\/' );
 # Everything except bracket, space, or control characters
-define( 'EXT_LINK_URL_CLASS', '[^]<>"\\x00-\\x20\\x7F]' );
+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]' );
@@ -76,7 +73,7 @@ define( 'EXT_IMAGE_REGEX',
  *   performs brace substitution on MediaWiki messages
  *
  * Globals used:
- *    objects:   $wgLang, $wgLinkCache
+ *    objects:   $wgLang
  *
  * NOT $wgArticle, $wgUser or $wgTitle. Keep them away!
  *
@@ -101,16 +98,18 @@ class Parser
        # Cleared with clearState():
        var $mOutput, $mAutonumber, $mDTopen, $mStripState = array();
        var $mVariables, $mIncludeCount, $mArgStack, $mLastSection, $mInPre;
-       var $mInterwikiLinkHolders, $mLinkHolders;
+       var $mInterwikiLinkHolders, $mLinkHolders, $mUniqPrefix;
 
        # Temporary:
-       var $mOptions, $mTitle, $mOutputType,
+       var $mOptions,      // ParserOptions object
+               $mTitle,        // Title context, used for self-link rendering and similar things
+               $mOutputType,   // Output type, one of the OT_xxx constants
            $mTemplates,        // cache of already loaded templates, avoids
                                // multiple SQL queries for the same string
-           $mTemplatePath;     // stores an unsorted hash of all the templates already loaded
+           $mTemplatePath,     // stores an unsorted hash of all the templates already loaded
                                // in this path. Used for loop detection.
-
-       var $mIWTransData = array();
+               $mIWTransData = array(),
+               $mRevisionId;   // ID to display in {{REVISIONID}} tags
 
        /**#@-*/
 
@@ -153,11 +152,23 @@ class Parser
                        'titles' => array()
                );
                $this->mRevisionId = null;
+               $this->mUniqPrefix = 'UNIQ' . Parser::getRandomString();
+
+               wfRunHooks( 'ParserClearState', array( &$this ) );
+       }
+
+       /**
+        * Accessor for mUniqPrefix.
+        *
+        * @access public
+        */
+       function UniqPrefix() {
+               return $this->mUniqPrefix;
        }
 
        /**
-        * First pass--just handle <nowiki> sections, pass the rest off
-        * to internalParse() which does all the real work.
+        * Convert wikitext to HTML
+        * Do not call this function recursively.
         *
         * @access private
         * @param string $text Text we want to parse
@@ -169,7 +180,12 @@ class Parser
         * @return ParserOutput a ParserOutput
         */
        function parse( $text, &$title, $options, $linestart = true, $clearState = true, $revid = null ) {
-               global $wgUseTidy, $wgContLang;
+               /**
+                * First pass--just handle <nowiki> sections, pass the rest off
+                * to internalParse() which does all the real work.
+                */
+
+               global $wgUseTidy, $wgAlwaysUseTidy, $wgContLang;
                $fname = 'Parser::parse';
                wfProfileIn( $fname );
 
@@ -192,6 +208,12 @@ class Parser
                $text = $this->strip( $text, $x );
                wfRunHooks( 'ParserAfterStrip', array( &$this, &$text, &$x ) );
 
+               # Hook to suspend the parser in this state
+               if ( !wfRunHooks( 'ParserBeforeInternalParse', array( &$this, &$text, &$x ) ) ) {
+                       wfProfileOut( $fname );
+                       return $text ;
+               }
+
                $text = $this->internalParse( $text );
 
                $text = $this->unstrip( $text, $this->mStripState );
@@ -223,8 +245,8 @@ class Parser
                wfRunHooks( 'ParserBeforeTidy', array( &$this, &$text ) );
 
                $text = Sanitizer::normalizeCharReferences( $text );
-               
-               if ($wgUseTidy) {
+
+               if (($wgUseTidy and $this->mOptions->mTidy) or $wgAlwaysUseTidy) {
                        $text = Parser::tidy($text);
                }
 
@@ -232,6 +254,7 @@ class Parser
 
                $this->mOutput->setText( $text );
                wfProfileOut( $fname );
+
                return $this->mOutput;
        }
 
@@ -358,7 +381,7 @@ class Parser
                $gallery_content = array();
 
                # Replace any instances of the placeholders
-               $uniq_prefix = UNIQ_PREFIX;
+               $uniq_prefix = $this->mUniqPrefix;
                #$text = str_replace( $uniq_prefix, wfHtmlEscapeFirst( $uniq_prefix ), $text );
 
                # html
@@ -412,7 +435,7 @@ class Parser
                foreach( $gallery_content as $marker => $content ) {
                        require_once( 'ImageGallery.php' );
                        if ( $render ) {
-                               $gallery_content[$marker] = Parser::renderImageGallery( $content );
+                               $gallery_content[$marker] = $this->renderImageGallery( $content );
                        } else {
                                $gallery_content[$marker] = '<gallery>'.$content.'</gallery>';
                        }
@@ -435,7 +458,7 @@ class Parser
                                $full_tag = $ext_tags[$tag][$marker];
                                $params = $ext_params[$tag][$marker];
                                if ( $render )
-                                       $ext_content[$tag][$marker] = call_user_func_array( $callback, array( $content, $params, $this ) );
+                                       $ext_content[$tag][$marker] = call_user_func_array( $callback, array( $content, $params, &$this ) );
                                else {
                                        if ( is_null( $content ) ) {
                                                // Empty element tag
@@ -484,7 +507,7 @@ class Parser
                if ( !is_array( $state ) ) {
                        return $text;
                }
-               
+
                # Must expand in reverse order, otherwise nested tags will be corrupted
                foreach( array_reverse( $state, true ) as $tag => $contentDict ) {
                        if( $tag != 'nowiki' && $tag != 'html' ) {
@@ -530,7 +553,7 @@ class Parser
         * @access private
         */
        function insertStripItem( $text, &$state ) {
-               $rnd = UNIQ_PREFIX . '-item' . Parser::getRandomString();
+               $rnd = $this->mUniqPrefix . '-item' . Parser::getRandomString();
                if ( !$state ) {
                        $state = array(
                          'html' => array(),
@@ -669,7 +692,7 @@ 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 ) .
@@ -800,7 +823,7 @@ class Parser
 
                # replaceInternalLinks may sometimes leave behind
                # absolute URLs, which have to be masked to hide them from replaceExternalLinks
-               $text = str_replace(UNIQ_PREFIX."NOPARSE", "", $text);
+               $text = str_replace($this->mUniqPrefix."NOPARSE", "", $text);
 
                $text = $this->doMagicLinks( $text );
                $text = $this->doTableStuff( $text );
@@ -831,19 +854,6 @@ class Parser
                return $text;
        }
 
-       /**
-        * Parse ^^ tokens and return html
-        *
-        * @access private
-        */
-       function doExponent( $text ) {
-               $fname = 'Parser::doExponent';
-               wfProfileIn( $fname );
-               $text = preg_replace('/\^\^(.*?)\^\^/','<small><sup>\\1</sup></small>', $text);
-               wfProfileOut( $fname );
-               return $text;
-       }
-
        /**
         * Parse headers and return html
         *
@@ -853,7 +863,7 @@ class Parser
                $fname = 'Parser::doHeadings';
                wfProfileIn( $fname );
                for ( $i = 6; $i >= 1; --$i ) {
-                       $h = substr( '======', 0, $i );
+                       $h = str_repeat( '=', $i );
                        $text = preg_replace( "/^{$h}(.+){$h}(\\s|$)/m",
                          "<h{$i}>\\1</h{$i}>\\2", $text );
                }
@@ -1111,19 +1121,23 @@ class Parser
 
                        # Replace &amp; from obsolete syntax with &.
                        # All HTML entities will be escaped by makeExternalLink()
-                       # or maybeMakeExternalImage()
                        $url = str_replace( '&amp;', '&', $url );
+                       # Replace unnecessary URL escape codes with the referenced character
+                       # This prevents spammers from hiding links from the filters
+                       $url = Parser::replaceUnusualEscapes( $url );
 
                        # Process the trail (i.e. everything after this link up until start of the next link),
                        # replacing any non-bracketed links
                        $trail = $this->replaceFreeExternalLinks( $trail );
 
-
                        # Use the encoded URL
                        # This means that users can paste URLs directly into the text
                        # Funny characters like &ouml; aren't valid in URLs anyway
                        # This was changed in August 2004
                        $s .= $sk->makeExternalLink( $url, $text, false, $linktype ) . $dtrail . $trail;
+
+                       # Register link in the output object
+                       $this->mOutput->addExternalLink( $url );
                }
 
                wfProfileOut( $fname );
@@ -1179,12 +1193,16 @@ class Parser
                                # All HTML entities will be escaped by makeExternalLink()
                                # or maybeMakeExternalImage()
                                $url = str_replace( '&amp;', '&', $url );
+                               # Replace unnecessary URL escape codes with their equivalent characters
+                               $url = Parser::replaceUnusualEscapes( $url );
 
                                # Is this an external image?
                                $text = $this->maybeMakeExternalImage( $url );
                                if ( $text === false ) {
                                        # Not an image, make a link
                                        $text = $sk->makeExternalLink( $url, $wgContLang->markNoConversion($url), true, 'free' );
+                                       # Register it in the output object
+                                       $this->mOutput->addExternalLink( $url );
                                }
                                $s .= $text . $trail;
                        } else {
@@ -1195,6 +1213,36 @@ class Parser
                return $s;
        }
 
+       /**
+        * Replace unusual URL escape codes with their equivalent characters
+        * @param string 
+        * @return string
+        * @static
+        */
+       function replaceUnusualEscapes( $url ) {
+               return preg_replace_callback( '/%[0-9A-Fa-f]{2}/', 
+                       array( 'Parser', 'replaceUnusualEscapesCallback' ), $url );
+       }
+
+       /**
+        * Callback function used in replaceUnusualEscapes().
+        * Replaces unusual URL escape codes with their equivalent character
+        * @static
+        * @access private
+        */
+       function replaceUnusualEscapesCallback( $matches ) {
+               $char = urldecode( $matches[0] );
+               $ord = ord( $char );
+               // Is it an unsafe or HTTP reserved character according to RFC 1738?
+               if ( $ord > 32 && $ord < 127 && strpos( '<>"#{}|\^~[]`;/?', $char ) === false ) {
+                       // No, shouldn't be escaped
+                       return $char;
+               } else {
+                       // Yes, leave it escaped
+                       return $matches[0];
+               }
+       }
+
        /**
         * make an image if it's allowed, either through the global
         * option or through the exception
@@ -1205,7 +1253,7 @@ class Parser
                $imagesfrom = $this->mOptions->getAllowExternalImagesFrom();
                $imagesexception = !empty($imagesfrom);
                $text = false;
-               if ( $this->mOptions->getAllowExternalImages() 
+               if ( $this->mOptions->getAllowExternalImages()
                     || ( $imagesexception && strpos( $url, $imagesfrom ) === 0 ) ) {
                        if ( preg_match( EXT_IMAGE_REGEX, $url ) ) {
                                # Image found
@@ -1221,7 +1269,7 @@ class Parser
         * @access private
         */
        function replaceInternalLinks( $s ) {
-               global $wgContLang, $wgLinkCache;
+               global $wgContLang;
                static $fname = 'Parser::replaceInternalLinks' ;
 
                wfProfileIn( $fname );
@@ -1403,7 +1451,7 @@ class Parser
 
                                # Interwikis
                                if( $iw && $this->mOptions->getInterwikiMagic() && $nottalk && $wgContLang->getLanguageName( $iw ) ) {
-                                       array_push( $this->mOutput->mLanguageLinks, $nt->getFullText() );
+                                       $this->mOutput->addLanguageLink( $nt->getFullText() );
                                        $s = rtrim($s . "\n");
                                        $s .= trim($prefix . $trail, "\n") == '' ? '': $prefix . $trail;
                                        continue;
@@ -1419,8 +1467,8 @@ class Parser
                                                $text = $this->replaceInternalLinks($text);
 
                                                # cloak any absolute URLs inside the image markup, so replaceExternalLinks() won't touch them
-                                               $s .= $prefix . preg_replace( "/\b(" . wfUrlProtocols() . ')/', UNIQ_PREFIX."NOPARSE$1", $this->makeImage( $nt, $text) ) . $trail;
-                                               $wgLinkCache->addImageLinkObj( $nt );
+                                               $s .= $prefix . $this->armorLinks( $this->makeImage( $nt, $text ) ) . $trail;
+                                               $this->mOutput->addImage( $nt->getDBkey() );
 
                                                wfProfileOut( "$fname-image" );
                                                continue;
@@ -1431,13 +1479,8 @@ class Parser
 
                                if ( $ns == NS_CATEGORY ) {
                                        wfProfileIn( "$fname-category" );
-                                       $t = $wgContLang->convertHtml( $nt->getText() );
                                        $s = rtrim($s . "\n"); # bug 87
 
-                                       $wgLinkCache->suspend(); # Don't save in links/brokenlinks
-                                       $t = $sk->makeLinkObj( $nt, $t, '', '' , $prefix );
-                                       $wgLinkCache->resume();
-
                                        if ( $wasblank ) {
                                                if ( $this->mTitle->getNamespace() == NS_CATEGORY ) {
                                                        $sortkey = $this->mTitle->getText();
@@ -1447,9 +1490,9 @@ class Parser
                                        } else {
                                                $sortkey = $text;
                                        }
+                                       $sortkey = Sanitizer::decodeCharReferences( $sortkey );
                                        $sortkey = $wgContLang->convertCategoryKey( $sortkey );
-                                       $wgLinkCache->addCategoryLinkObj( $nt, $sortkey );
-                                       $this->mOutput->addCategoryLink( $t );
+                                       $this->mOutput->addCategory( $nt->getDBkey(), $sortkey );
 
                                        /**
                                         * Strip the whitespace Category links produce, see bug 87
@@ -1471,12 +1514,23 @@ class Parser
 
                        # Special and Media are pseudo-namespaces; no pages actually exist in them
                        if( $ns == NS_MEDIA ) {
-                               $s .= $prefix . $sk->makeMediaLinkObj( $nt, $text, true ) . $trail;
-                               $wgLinkCache->addImageLinkObj( $nt );
+                               $link = $sk->makeMediaLinkObj( $nt, $text );
+                               # Cloak with NOPARSE to avoid replacement in replaceExternalLinks
+                               $s .= $prefix . $this->armorLinks( $link ) . $trail;
+                               $this->mOutput->addImage( $nt->getDBkey() );
                                continue;
                        } elseif( $ns == NS_SPECIAL ) {
-                               $s .= $prefix . $sk->makeKnownLinkObj( $nt, $text, '', $trail );
+                               $s .= $this->makeKnownLinkHolder( $nt, $text, '', $trail, $prefix );
                                continue;
+                       } elseif( $ns == NS_IMAGE ) {
+                               $img = Image::newFromTitle( $nt );
+                               if( $img->exists() ) {
+                                       // Force a blue link if the file exists; may be a remote
+                                       // upload on the shared repository, and we want to see its
+                                       // auto-generated page.
+                                       $s .= $this->makeKnownLinkHolder( $nt, $text, '', $trail, $prefix );
+                                       continue;
+                               }
                        }
                        $s .= $this->makeLinkHolder( $nt, $text, '', $trail, $prefix );
                }
@@ -1516,6 +1570,44 @@ class Parser
                return $retVal;
        }
 
+       /**
+        * Render a forced-blue link inline; protect against double expansion of
+        * URLs if we're in a mode that prepends full URL prefixes to internal links.
+        * Since this little disaster has to split off the trail text to avoid
+        * breaking URLs in the following text without breaking trails on the
+        * wiki links, it's been made into a horrible function.
+        *
+        * @param Title $nt
+        * @param string $text
+        * @param string $query
+        * @param string $trail
+        * @param string $prefix
+        * @return string HTML-wikitext mix oh yuck
+        */
+       function makeKnownLinkHolder( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
+               list( $inside, $trail ) = Linker::splitTrail( $trail );
+               $sk =& $this->mOptions->getSkin();
+               $link = $sk->makeKnownLinkObj( $nt, $text, $query, $inside, $prefix );
+               return $this->armorLinks( $link ) . $trail;
+       }
+
+       /**
+        * Insert a NOPARSE hacky thing into any inline links in a chunk that's
+        * going to go through further parsing steps before inline URL expansion.
+        *
+        * In particular this is important when using action=render, which causes
+        * full URLs to be included.
+        *
+        * Oh man I hate our multi-layer parser!
+        *
+        * @param string more-or-less HTML
+        * @return string less-or-more HTML with NOPARSE bits
+        */
+       function armorLinks( $text ) {
+               return preg_replace( "/\b(" . wfUrlProtocols() . ')/',
+                       "{$this->mUniqPrefix}NOPARSE$1", $text );
+       }
+
        /**
         * Return true if subpage links should be expanded on this page.
         * @return bool
@@ -1762,12 +1854,11 @@ class Parser
                        if( 0 == $prefixLength ) {
                                wfProfileIn( "$fname-paragraph" );
                                # No prefix (not in list)--go to paragraph mode
-                               $uniq_prefix = UNIQ_PREFIX;
                                // XXX: use a stack for nestable elements like span, table and div
                                $openmatch = preg_match('/(<table|<blockquote|<h1|<h2|<h3|<h4|<h5|<h6|<pre|<tr|<p|<ul|<li|<\\/tr|<\\/td|<\\/th)/iS', $t );
                                $closematch = preg_match(
                                        '/(<\\/table|<\\/blockquote|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6|'.
-                                       '<td|<th|<div|<\\/div|<hr|<\\/pre|<\\/p|'.$uniq_prefix.'-pre|<\\/li|<\\/ul)/iS', $t );
+                                       '<td|<th|<div|<\\/div|<hr|<\\/pre|<\\/p|'.$this->mUniqPrefix.'-pre|<\\/li|<\\/ul)/iS', $t );
                                if ( $openmatch or $closematch ) {
                                        $paragraphStack = false;
                                        $output .= $this->closeParagraph();
@@ -1910,6 +2001,8 @@ class Parser
                                return $varCache[$index] = $wgContLang->getMonthAbbreviation( date( 'n', $ts ) );
                        case MAG_CURRENTDAY:
                                return $varCache[$index] = $wgContLang->formatNum( date( 'j', $ts ) );
+                       case MAG_CURRENTDAY2:
+                               return $varCache[$index] = $wgContLang->formatNum( date( 'd', $ts ) );
                        case MAG_PAGENAME:
                                return $this->mTitle->getText();
                        case MAG_PAGENAMEE:
@@ -1917,7 +2010,7 @@ class Parser
                        case MAG_FULLPAGENAME:
                                return $this->mTitle->getPrefixedText();
                        case MAG_FULLPAGENAMEE:
-                               return wfUrlencode( $this->mTitle->getPrefixedText() );
+                               return $this->mTitle->getPrefixedURL();
                        case MAG_REVISIONID:
                                return $this->mRevisionId;
                        case MAG_NAMESPACE:
@@ -1931,7 +2024,9 @@ class Parser
                        case MAG_CURRENTTIME:
                                return $varCache[$index] = $wgContLang->time( wfTimestamp( TS_MW, $ts ), false, false );
                        case MAG_CURRENTWEEK:
-                               return $varCache[$index] = $wgContLang->formatNum( date( 'W', $ts ) );
+                               // @bug 4594 PHP5 has it zero padded, PHP4 does not, cast to
+                               // int to remove the padding
+                               return $varCache[$index] = $wgContLang->formatNum( (int)date( 'W', $ts ) );
                        case MAG_CURRENTDOW:
                                return $varCache[$index] = $wgContLang->formatNum( date( 'w', $ts ) );
                        case MAG_NUMBEROFARTICLES:
@@ -2004,14 +2099,14 @@ class Parser
 
                        if ($lastOpeningBrace >= 0) {
                                $pos = strpos ($text, $openingBraceStack[$lastOpeningBrace]['braceEnd'], $i);
-                       
+
                                if (false !== $pos && (-1 == $nextPos || $pos < $nextPos)){
                                        $rule = null;
                                        $nextPos = $pos;
                                }
 
                                $pos = strpos ($text, '|', $i);
-                               
+
                                if (false !== $pos && (-1 == $nextPos || $pos < $nextPos)){
                                        $rule = null;
                                        $nextPos = $pos;
@@ -2031,14 +2126,14 @@ class Parser
                                                           'title' => '',
                                                           'parts' => null);
 
-                               # count openning brace characters 
+                               # count openning brace characters
                                while ($i+1 < strlen($text) && $text[$i+1] == $piece['brace']) {
                                        $piece['count']++;
                                        $i++;
                                }
 
-                               $piece['startAt'] = $i+1; 
-                               $piece['partStart'] = $i+1; 
+                               $piece['startAt'] = $i+1;
+                               $piece['partStart'] = $i+1;
 
                                # we need to add to stack only if openning brace count is enough for any given rule
                                foreach ($rule['cb'] as $cnt => $fn) {
@@ -2072,7 +2167,7 @@ class Parser
                                                        $matchingCallback = $fn;
                                                }
                                        }
-                                       
+
                                        if ($matchingCount == 0) {
                                                $i += $count - 1;
                                                continue;
@@ -2086,7 +2181,7 @@ class Parser
 
                                        $pieceStart = $openingBraceStack[$lastOpeningBrace]['startAt'] - $matchingCount;
                                        $pieceEnd = $i + $matchingCount;
-                                       
+
                                        if( is_callable( $matchingCallback ) ) {
                                                $cbArgs = array (
                                                                                 'text' => substr($text, $pieceStart, $pieceEnd - $pieceStart),
@@ -2137,7 +2232,7 @@ class Parser
                                        }
                                        else
                                                $openingBraceStack[$lastOpeningBrace]['parts'][] = substr($text, $openingBraceStack[$lastOpeningBrace]['partStart'], $i - $openingBraceStack[$lastOpeningBrace]['partStart']);
-                                       
+
                                        $openingBraceStack[$lastOpeningBrace]['partStart'] = $i + 1;
                                }
                        }
@@ -2193,7 +2288,7 @@ class Parser
         * @access private
         */
        function variableSubstitution( $matches ) {
-               $fname = 'parser::variableSubstitution';
+               $fname = 'Parser::variableSubstitution';
                $varname = $matches[1];
                wfProfileIn( $fname );
                if ( !$this->mVariables ) {
@@ -2257,13 +2352,15 @@ class Parser
         * @access private
         */
        function braceSubstitution( $piece ) {
-               global $wgLinkCache, $wgContLang;
+               global $wgContLang;
                $fname = 'Parser::braceSubstitution';
                wfProfileIn( $fname );
 
                $found = false;
                $nowiki = false;
                $noparse = false;
+               $replaceHeadings = false;
+               $isHTML = false;
 
                $title = NULL;
 
@@ -2328,7 +2425,7 @@ class Parser
                        # Check for NS: (namespace expansion)
                        $mwNs = MagicWord::get( MAG_NS );
                        if ( $mwNs->matchStartAndRemove( $part1 ) ) {
-                               if ( intval( $part1 ) ) {
+                               if ( intval( $part1 ) || $part1 == "0" ) {
                                        $text = $linestart . $wgContLang->getNsText( intval( $part1 ) );
                                        $found = true;
                                } else {
@@ -2368,7 +2465,7 @@ class Parser
                        $mwLocalE =& MagicWord::get( MAG_LOCALURLE );
                        $mwFull =& MagicWord::get( MAG_FULLURL );
                        $mwFullE =& MagicWord::get( MAG_FULLURLE );
-                       
+
 
                        if ( $mwLocal->matchStartAndRemove( $part1 ) ) {
                                $func = 'getLocalURL';
@@ -2426,7 +2523,7 @@ class Parser
                                $noparse = true;
                                $found = true;
                                $text = $linestart .
-                                       "\{\{$part1}}" .
+                                       '{{' . $part1 . '}}' .
                                        '<!-- WARNING: template loop detected -->';
                                wfDebug( "$fname: template loop broken at '$part1'\n" );
                        } else {
@@ -2436,8 +2533,6 @@ class Parser
                }
 
                # Load from database
-               $replaceHeadings = false;
-               $isHTML = false;
                $lastPathLevel = $this->mTemplatePath;
                if ( !$found ) {
                        $ns = NS_TEMPLATE;
@@ -2447,47 +2542,51 @@ class Parser
                        }
                        $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 ) ) {
-                                       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 ( !is_null( $title ) ) {
+                               if ( !$title->isExternal() ) {
+                                       # Check for excessive inclusion
+                                       $dbk = $title->getPrefixedDBkey();
+                                       if ( $this->incrementIncludeCount( $dbk ) ) {
+                                               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 {
+                                                       $articleContent = $this->fetchTemplate( $title );
+                                                       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 = '[['.$title->getPrefixedText().']]';
-                                       $found = true;
-                               }
+                                       # If the title is valid but undisplayable, make a link to it
+                                       if ( $this->mOutputType == OT_HTML && !$found ) {
+                                               $text = '[['.$title->getPrefixedText().']]';
+                                               $found = true;
+                                       }
 
-                               # Template cache array insertion
-                               if( $found ) {
-                                       $this->mTemplates[$part1] = $text;
-                                       $text = $linestart . $text;
+                                       # Template cache array insertion
+                                       if( $found ) {
+                                               $this->mTemplates[$part1] = $text;
+                                               $text = $linestart . $text;
+                                       }
+                               } elseif ( $title->isTrans() ) {
+                                       // Interwiki transclusion
+                                       if ( $this->mOutputType == OT_HTML ) {
+                                               $text = $this->interwikiTransclude( $title, 'render' );
+                                               $isHTML = true;
+                                               $noparse = true;
+                                       } else {
+                                               $text = $this->interwikiTransclude( $title, 'raw' );
+                                       }
+                                       $found = true;
                                }
                        }
                }
@@ -2521,15 +2620,15 @@ class Parser
 
                        # If there are any <onlyinclude> tags, only include them
                        if ( in_string( '<onlyinclude>', $text ) && in_string( '</onlyinclude>', $text ) ) {
-                               preg_match_all( '/<onlyinclude>(.*?)<\/onlyinclude>/s', $text, $m );
+                               preg_match_all( '/<onlyinclude>(.*?)\n?<\/onlyinclude>/s', $text, $m );
                                $text = '';
                                foreach ($m[1] as $piece)
-                                       $text .= $this->trimOnlyinclude( $piece );
+                                       $text .= $piece;
                        }
                        # Remove <noinclude> sections and <includeonly> tags
                        $text = preg_replace( '/<noinclude>.*?<\/noinclude>/s', '', $text );
                        $text = strtr( $text, array( '<includeonly>' => '' , '</includeonly>' => '' ) );
-                       
+
                        if( $this->mOutputType == OT_HTML ) {
                                # Strip <nowiki>, <pre>, etc.
                                $text = $this->strip( $text, $this->mStripState );
@@ -2537,11 +2636,6 @@ class Parser
                        }
                        $text = $this->replaceVariables( $text, $assocArgs );
 
-                       # Resume the link cache and register the inclusion as a link
-                       if ( $this->mOutputType == OT_HTML && !is_null( $title ) ) {
-                               $wgLinkCache->addLinkObj( $title );
-                       }
-
                        # If the template begins with a table or block-level
                        # element, it should be treated as beginning a new line.
                        if (!$piece['lineStart'] && preg_match('/^({\\||:|;|#|\*)/', $text)) {
@@ -2601,56 +2695,73 @@ class Parser
                        return $text;
                }
        }
-       
+
        /**
-        * Trim the first and last newlines of a string, this is not equivalent
-        * to trim( $str, "\n" ) which would trim them all.
-        *
-        * @param string $str The string to trim
-        * @return string
+        * Fetch the unparsed text of a template and register a reference to it. 
         */
-       function trimOnlyinclude( $str ) {
-               $str = preg_replace( "/^\n/", '', $str );
-               $str = preg_replace( "/\n$/", '', $str );
-               return $str;
+       function fetchTemplate( $title ) {
+               $text = false;
+               // Loop to fetch the article, with up to 1 redirect
+               for ( $i = 0; $i < 2 && is_object( $title ); $i++ ) {
+                       $rev = Revision::newFromTitle( $title );
+                       $this->mOutput->addTemplate( $title, $title->getArticleID() );
+                       if ( !$rev ) {
+                               break;
+                       }
+                       $text = $rev->getText();
+                       if ( $text === false ) {
+                               break;
+                       }
+                       // Redirect?
+                       $title = Title::newFromRedirect( $text );
+               }
+               return $text;
        }
 
        /**
-        * Translude an interwiki link.
+        * Transclude an interwiki link.
         */
-       function scarytransclude($title, $interwiki) {
-               global $wgEnableScaryTranscluding;
+       function interwikiTransclude( $title, $action ) {
+               global $wgEnableScaryTranscluding, $wgCanonicalNamespaceNames;
 
                if (!$wgEnableScaryTranscluding)
                        return wfMsg('scarytranscludedisabled');
 
-               $articlename = "Template:" . $title->getDBkey();
-               $url = str_replace('$1', urlencode($articlename), $interwiki);
+               // The namespace will actually only be 0 or 10, depending on whether there was a leading :
+               // But we'll handle it generally anyway
+               if ( $title->getNamespace() ) {
+                       // Use the canonical namespace, which should work anywhere
+                       $articleName = $wgCanonicalNamespaceNames[$title->getNamespace()] . ':' . $title->getDBkey();
+               } else {
+                       $articleName = $title->getDBkey();
+               }
+
+               $url = str_replace('$1', urlencode($articleName), Title::getInterwikiLink($title->getInterwiki()));
+               $url .= "?action=$action";
                if (strlen($url) > 255)
                        return wfMsg('scarytranscludetoolong');
-               $text = $this->fetchScaryTemplateMaybeFromCache($url);
-               $this->mIWTransData[] = $text;
-               return "<!--IW_TRANSCLUDE ".(count($this->mIWTransData) - 1)."-->";
+               return $this->fetchScaryTemplateMaybeFromCache($url);
        }
 
        function fetchScaryTemplateMaybeFromCache($url) {
+               global $wgTranscludeCacheExpiry;
                $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))) {
+                       if ($time && time() < $time + $wgTranscludeCacheExpiry ) {
                                return $text;
                        }
                }
 
-               $text = wfGetHTTP($url . '?action=render');
+               $text = wfGetHTTP($url);
                if (!$text)
                        return wfMsg('scarytranscludefailed', $url);
 
                $dbw =& wfGetDB(DB_MASTER);
-               $dbw->replace('transcache', array(), array(
+               $dbw->replace('transcache', array('tc_url'), array(
                        'tc_url' => $url,
                        'tc_time' => time(),
                        'tc_contents' => $text));
@@ -2706,7 +2817,7 @@ class Parser
         * @access private
         */
        function formatHeadings( $text, $isMain=true ) {
-               global $wgMaxTocLevel, $wgContLang, $wgLinkHolders, $wgInterwikiLinkHolders;
+               global $wgMaxTocLevel, $wgContLang;
 
                $doNumberHeadings = $this->mOptions->getNumberHeadings();
                $doShowToc = true;
@@ -2869,12 +2980,7 @@ class Parser
                        # strip out HTML
                        $canonized_headline = preg_replace( '/<.*?' . '>/','',$canonized_headline );
                        $tocline = trim( $canonized_headline );
-                       $canonized_headline = urlencode( Sanitizer::decodeCharReferences( str_replace(' ', '_', $tocline) ) );
-                       $replacearray = array(
-                               '%3A' => ':',
-                               '%' => '.'
-                       );
-                       $canonized_headline = str_replace(array_keys($replacearray),array_values($replacearray),$canonized_headline);
+                       $canonized_headline = Sanitizer::escapeId( $tocline );
                        $refers[$headlineCount] = $canonized_headline;
 
                        # count how many in assoc. array so we can track dupes in anchors
@@ -3108,14 +3214,6 @@ class Parser
        function pstPass2( $text, &$user ) {
                global $wgContLang, $wgLocaltimezone;
 
-               # Variable replacement
-               # Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
-               $text = $this->replaceVariables( $text );
-
-               # Signatures
-               #
-               $sigText = $this->getUserSig( $user );
-
                /* Note: This is the timestamp saved as hardcoded wikitext to
                 * the database, we use $wgContLang here in order to give
                 * everyone the same signiture and use the default one rather
@@ -3131,14 +3229,24 @@ class Parser
                        putenv( 'TZ='.$oldtz );
                }
 
-               $text = preg_replace( '/~~~~~/', $d, $text );
-               $text = preg_replace( '/~~~~/', "$sigText $d", $text );
-               $text = preg_replace( '/~~~/', $sigText, $text );
+               # Variable replacement
+               # Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
+               $text = $this->replaceVariables( $text );
+               
+               # Signatures
+               $sigText = $this->getUserSig( $user );
+               $text = strtr( $text, array( 
+                       '~~~~~' => $d,
+                       '~~~~' => "$sigText $d",
+                       '~~~' => $sigText
+               ) );
 
                # Context links: [[|name]] and [[name (context)|]]
                #
-               $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
-               $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
+               global $wgLegalTitleChars;
+               $tc = "[$wgLegalTitleChars]";
+               $np = str_replace( array( '(', ')' ), array( '', '' ), $tc ); # No parens
+
                $namespacechar = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
                $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
 
@@ -3170,7 +3278,7 @@ class Parser
 
                return $text;
        }
-       
+
        /**
         * Fetch the user's signature text, if any, and normalize to
         * validated, ready-to-insert wikitext.
@@ -3180,53 +3288,60 @@ class Parser
         * @access private
         */
        function getUserSig( &$user ) {
-               $name = $user->getName();
-               $nick = trim( $user->getOption( 'nickname' ) );
-               if ( '' == $nick ) {
-                       $nick = $name;
-               }
-               
-               if( $user->getOption( 'fancysig' ) ) {
-                       // A wikitext signature.
-                       $valid = $this->validateSig( $nick );
-                       if( $valid === false ) {
-                               // Fall back to default sig
-                               $nick = $name;
-                               wfDebug( "Parser::getUserSig: $name has bad XML tags in signature.\n" );
+               global $wgContLang;
+
+               $username = $user->getName();
+               $nickname = $user->getOption( 'nickname' );
+               $nickname = $nickname === '' ? $username : $nickname;
+       
+               if( $user->getBoolOption( 'fancysig' ) !== false ) {
+                       # Sig. might contain markup; validate this
+                       if( $this->validateSig( $nickname ) !== false ) {
+                               # Validated; clean up (if needed) and return it
+                               return( $this->cleanSig( $nickname ) );
                        } else {
-                               return $nick;
+                               # Failed to validate; fall back to the default
+                               $nickname = $username;
+                               wfDebug( "Parser::getUserSig: $username has bad XML tags in signature.\n" );
                        }
                }
-               
-               // Plain text linking to the user's homepage
-               global $wgContLang;
-               $page = $user->getUserPage();
-               return '[[' .
-                       $page->getPrefixedText() .
-                       "|" .
-                       wfEscapeWikIText( $nick ) .
-                       "]]";
+
+               # If we're still here, make it a link to the user page
+               $userpage = $user->getUserPage();
+               return( '[[' . $userpage->getPrefixedText() . '|' . wfEscapeWikiText( $nickname ) . ']]' );
        }
-       
+
        /**
-        * We want to enforce two rules on wikitext sigs here:
-        * 1) Expand any templates at save time (forced subst:)
-        * 2) Check for unbalanced XML tags, and reject if so.
+        * Check that the user's signature contains no bad XML
         *
         * @param string $text
         * @return mixed An expanded string, or false if invalid.
-        *
-        * @todo Run brace substitutions
-        * @todo ?? Check for unbalanced '' and ''' quotes, etc
         */
        function validateSig( $text ) {
-               if( wfIsWellFormedXmlFragment( $text ) ) {
-                       return $text;
-               } else {
-                       return false;
-               }
+               return( wfIsWellFormedXmlFragment( $text ) ? $text : false );
        }
+       
+       /**
+        * Clean up signature text
+        *
+        * 1) Strip ~~~, ~~~~ and ~~~~~ out of signatures
+        * 2) Substitute all transclusions
+        *
+        * @param string $text
+        * @return string Signature text
+        */
+       function cleanSig( $text ) {
+               $substWord = MagicWord::get( MAG_SUBST );
+               $substRegex = '/\{\{(?!(?:' . $substWord->getBaseRegex() . '))/x' . $substWord->getRegexCase();
+               $substText = '{{' . $substWord->getSynonym( 0 );
 
+               $text = preg_replace( $substRegex, $substText, $text );
+               $text = preg_replace( '/~{3,5}/', '', $text );
+               $text = $this->replaceVariables( $text );
+       
+               return $text;
+       }
+       
        /**
         * Set up some variables which are usually set up in parse()
         * so that an external function can call some class members with confidence
@@ -3253,12 +3368,16 @@ class Parser
                global $wgTitle;
                static $executing = false;
 
+               $fname = "Parser::transformMsg";
+
                # Guard against infinite recursion
                if ( $executing ) {
                        return $text;
                }
                $executing = true;
 
+               wfProfileIn($fname);
+
                $this->mTitle = $wgTitle;
                $this->mOptions = $options;
                $this->mOutputType = OT_MSG;
@@ -3266,6 +3385,7 @@ class Parser
                $text = $this->replaceVariables( $text );
 
                $executing = false;
+               wfProfileOut($fname);
                return $text;
        }
 
@@ -3284,7 +3404,7 @@ class Parser
        function setHook( $tag, $callback ) {
                $oldVal = @$this->mTagHooks[$tag];
                $this->mTagHooks[$tag] = $callback;
-               
+
                return $oldVal;
        }
 
@@ -3298,7 +3418,7 @@ class Parser
         * $options is a bit field, RLH_FOR_UPDATE to select for update
         */
        function replaceLinkHolders( &$text, $options = 0 ) {
-               global $wgUser, $wgLinkCache;
+               global $wgUser;
                global $wgOutputReplace;
 
                $fname = 'Parser::replaceLinkHolders';
@@ -3306,7 +3426,8 @@ class Parser
 
                $pdbks = array();
                $colours = array();
-               $sk = $this->mOptions->getSkin();
+               $sk =& $this->mOptions->getSkin();
+               $linkCache =& LinkCache::singleton();
 
                if ( !empty( $this->mLinkHolders['namespaces'] ) ) {
                        wfProfileIn( $fname.'-check' );
@@ -3319,7 +3440,7 @@ class Parser
 
                        # Generate query
                        $query = false;
-                       foreach ( $this->mLinkHolders['namespaces'] as $key => $val ) {
+                       foreach ( $this->mLinkHolders['namespaces'] as $key => $ns ) {
                                # Make title object
                                $title = $this->mLinkHolders['titles'][$key];
 
@@ -3330,23 +3451,26 @@ class Parser
                                }
                                $pdbk = $pdbks[$key] = $title->getPrefixedDBkey();
 
-                               # Check if it's in the link cache already
-                               if ( $title->isAlwaysKnown() || $wgLinkCache->getGoodLinkID( $pdbk ) ) {
+                               # Check if it's a static known link, e.g. interwiki
+                               if ( $title->isAlwaysKnown() ) {
                                        $colours[$pdbk] = 1;
-                               } elseif ( $wgLinkCache->isBadLink( $pdbk ) ) {
+                               } elseif ( ( $id = $linkCache->getGoodLinkID( $pdbk ) ) != 0 ) {
+                                       $colours[$pdbk] = 1;
+                                       $this->mOutput->addLink( $title, $id );
+                               } elseif ( $linkCache->isBadLink( $pdbk ) ) {
                                        $colours[$pdbk] = 0;
                                } else {
                                        # Not in the link cache, add it to the query
                                        if ( !isset( $current ) ) {
-                                               $current = $val;
+                                               $current = $ns;
                                                $query =  "SELECT page_id, page_namespace, page_title";
                                                if ( $threshold > 0 ) {
                                                        $query .= ', page_len, page_is_redirect';
                                                }
-                                               $query .= " FROM $page WHERE (page_namespace=$val AND page_title IN(";
-                                       } elseif ( $current != $val ) {
-                                               $current = $val;
-                                               $query .= ")) OR (page_namespace=$val AND page_title IN(";
+                                               $query .= " FROM $page WHERE (page_namespace=$ns AND page_title IN(";
+                                       } elseif ( $current != $ns ) {
+                                               $current = $ns;
+                                               $query .= ")) OR (page_namespace=$ns AND page_title IN(";
                                        } else {
                                                $query .= ', ';
                                        }
@@ -3369,7 +3493,8 @@ class Parser
                                while ( $s = $dbr->fetchObject($res) ) {
                                        $title = Title::makeTitle( $s->page_namespace, $s->page_title );
                                        $pdbk = $title->getPrefixedDBkey();
-                                       $wgLinkCache->addGoodLinkObj( $s->page_id, $title );
+                                       $linkCache->addGoodLinkObj( $s->page_id, $title );
+                                       $this->mOutput->addLink( $title, $s->page_id );
 
                                        if ( $threshold >  0 ) {
                                                $size = $s->page_len;
@@ -3393,8 +3518,9 @@ class Parser
                                $searchkey = "<!--LINK $key-->";
                                $title = $this->mLinkHolders['titles'][$key];
                                if ( empty( $colours[$pdbk] ) ) {
-                                       $wgLinkCache->addBadLinkObj( $title );
+                                       $linkCache->addBadLinkObj( $title );
                                        $colours[$pdbk] = 0;
+                                       $this->mOutput->addLink( $title, 0 );
                                        $wgOutputReplace[$searchkey] = $sk->makeBrokenLinkObj( $title,
                                                                        $this->mLinkHolders['texts'][$key],
                                                                        $this->mLinkHolders['queries'][$key] );
@@ -3450,7 +3576,7 @@ class Parser
         * @return string
         */
        function replaceLinkHoldersText( $text ) {
-               global $wgUser, $wgLinkCache;
+               global $wgUser;
                global $wgOutputReplace;
 
                $fname = 'Parser::replaceLinkHoldersText';
@@ -3493,16 +3619,12 @@ class Parser
         * given as text will return the HTML of a gallery with two images,
         * labeled 'The number "1"' and
         * 'A tree'.
-        *
-        * @static
         */
        function renderImageGallery( $text ) {
                # Setup the parser
-               global $wgUser, $wgTitle;
-               $parserOptions = ParserOptions::newFromUser( $wgUser );
+               $parserOptions = new ParserOptions;
                $localParser = new Parser();
 
-               global $wgLinkCache;
                $ig = new ImageGallery();
                $ig->setShowBytes( false );
                $ig->setShowFilename( false );
@@ -3516,7 +3638,7 @@ class Parser
                        if ( count( $matches ) == 0 ) {
                                continue;
                        }
-                       $nt = Title::newFromURL( $matches[1] );
+                       $nt =& Title::newFromText( $matches[1] );
                        if( is_null( $nt ) ) {
                                # Bogus title. Ignore these so we don't bomb out later.
                                continue;
@@ -3527,11 +3649,11 @@ class Parser
                                $label = '';
                        }
 
-                       $html = $localParser->parse( $label , $wgTitle, $parserOptions );
-                       $html = $html->mText;
+                       $pout = $localParser->parse( $label , $this->mTitle, $parserOptions );
+                       $html = $pout->getText();
 
                        $ig->add( new Image( $nt ), $html );
-                       $wgLinkCache->addImageLinkObj( $nt );
+                       $this->mOutput->addImage( $nt->getDBkey() );
                }
                return $ig->toHTML();
        }
@@ -3540,8 +3662,7 @@ class Parser
         * Parse image options text and use it to make an image
         */
        function makeImage( &$nt, $options ) {
-               global $wgContLang, $wgUseImageResize;
-               global $wgUser, $wgThumbLimits;
+               global $wgContLang, $wgUseImageResize, $wgUser;
 
                $align = '';
 
@@ -3614,14 +3735,14 @@ class Parser
        }
 
        /**
-        * Set a flag in the output object indicating that the content is dynamic and 
+        * 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
@@ -3634,12 +3755,27 @@ class Parser
                $text = $this->unstripForHTML( $text );
                return $text;
        }
-       
+
        function unstripForHTML( $text ) {
                $text = $this->unstrip( $text, $this->mStripState );
                $text = $this->unstripNoWiki( $text, $this->mStripState );
                return $text;
        }
+       /**#@-*/
+
+       /**#@+
+        * Accessor/mutator
+        */
+       function Title( $x = NULL ) { return wfSetVar( $this->mTitle, $x ); }
+       function Options( $x = NULL ) { return wfSetVar( $this->mOptions, $x ); }
+       function OutputType( $x = NULL ) { return wfSetVar( $this->mOutputType, $x ); }
+       /**#@-*/
+
+       /**#@+
+        * Accessor
+        */
+       function getTags() { return array_keys( $this->mTagHooks ); }
+       /**#@-*/
 }
 
 /**
@@ -3648,43 +3784,85 @@ class Parser
  */
 class ParserOutput
 {
-       var $mText, $mLanguageLinks, $mCategoryLinks, $mContainsOldMagic;
-       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 $mText,             # The output text
+               $mLanguageLinks,    # List of the full text of language links, in the order they appear
+               $mCategories,       # Map of category names to sort keys
+               $mContainsOldMagic, # Boolean variable indicating if the input contained variables like {{CURRENTDAY}}
+               $mCacheTime,        # Timestamp on this article, or -1 for uncacheable. Used in ParserCache.
+               $mVersion,          # Compatibility check
+               $mTitleText,        # title text of the chosen language variant
+               $mLinks,            # 2-D map of NS/DBK to ID for the links in the document. ID=zero for broken.
+               $mTemplates,        # 2-D map of NS/DBK to ID for the template references. ID=zero for broken.
+               $mImages,           # DB keys of the images used, in the array key only
+               $mExternalLinks;    # External link URLs, in the key only
 
        function ParserOutput( $text = '', $languageLinks = array(), $categoryLinks = array(),
                $containsOldMagic = false, $titletext = '' )
        {
                $this->mText = $text;
                $this->mLanguageLinks = $languageLinks;
-               $this->mCategoryLinks = $categoryLinks;
+               $this->mCategories = $categoryLinks;
                $this->mContainsOldMagic = $containsOldMagic;
                $this->mCacheTime = '';
                $this->mVersion = MW_PARSER_VERSION;
                $this->mTitleText = $titletext;
+               $this->mLinks = array();
+               $this->mTemplates = array();
+               $this->mImages = array();
+               $this->mExternalLinks = array();
        }
 
        function getText()                   { return $this->mText; }
        function getLanguageLinks()          { return $this->mLanguageLinks; }
-       function getCategoryLinks()          { return array_keys( $this->mCategoryLinks ); }
+       function getCategoryLinks()          { return array_keys( $this->mCategories ); }
+       function &getCategories()            { return $this->mCategories; }
        function getCacheTime()              { return $this->mCacheTime; }
        function getTitleText()              { return $this->mTitleText; }
+       function &getLinks()                 { return $this->mLinks; }
+       function &getTemplates()             { return $this->mTemplates; }
+       function &getImages()                { return $this->mImages; }
+       function &getExternalLinks()         { return $this->mExternalLinks; }
+
        function containsOldMagic()          { return $this->mContainsOldMagic; }
        function setText( $text )            { return wfSetVar( $this->mText, $text ); }
        function setLanguageLinks( $ll )     { return wfSetVar( $this->mLanguageLinks, $ll ); }
-       function setCategoryLinks( $cl )     { return wfSetVar( $this->mCategoryLinks, $cl ); }
+       function setCategoryLinks( $cl )     { return wfSetVar( $this->mCategories, $cl ); }
        function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic, $com ); }
        function setCacheTime( $t )          { return wfSetVar( $this->mCacheTime, $t ); }
        function setTitleText( $t )          { return wfSetVar ($this->mTitleText, $t); }
 
-       function addCategoryLink( $c )       { $this->mCategoryLinks[$c] = 1; }
+       function addCategory( $c, $sort )    { $this->mCategories[$c] = $sort; }
+       function addImage( $name )           { $this->mImages[$name] = 1; }
+       function addLanguageLink( $t )       { $this->mLanguageLinks[] = $t; }
+       function addExternalLink( $url )     { $this->mExternalLinks[$url] = 1; }
+
+       function addLink( $title, $id ) {
+               $ns = $title->getNamespace();
+               $dbk = $title->getDBkey();
+               if ( !isset( $this->mLinks[$ns] ) ) {
+                       $this->mLinks[$ns] = array();
+               }
+               $this->mLinks[$ns][$dbk] = $id;
+       }
+
+       function addTemplate( $title, $id ) {
+               $ns = $title->getNamespace();
+               $dbk = $title->getDBkey();
+               if ( !isset( $this->mTemplates[$ns] ) ) {
+                       $this->mTemplates[$ns] = array();
+               }
+               $this->mTemplates[$ns][$dbk] = $id;
+       }
 
+       /**
+        * @deprecated
+        */
+       /*
        function merge( $other ) {
                $this->mLanguageLinks = array_merge( $this->mLanguageLinks, $other->mLanguageLinks );
-               $this->mCategoryLinks = array_merge( $this->mCategoryLinks, $this->mLanguageLinks );
+               $this->mCategories = array_merge( $this->mCategories, $this->mLanguageLinks );
                $this->mContainsOldMagic = $this->mContainsOldMagic || $other->mContainsOldMagic;
-       }
+       }*/
 
        /**
         * Return true if this cached output object predates the global or
@@ -3723,6 +3901,7 @@ class ParserOptions
        var $mEditSection;               # Create "edit section" links
        var $mNumberHeadings;            # Automatically number headings
        var $mAllowSpecialInclusion;     # Allow inclusion of special pages
+       var $mTidy;                      # Ask for tidy cleanup
 
        function getUseTeX()                        { return $this->mUseTeX; }
        function getUseDynamicDates()               { return $this->mUseDynamicDates; }
@@ -3734,7 +3913,7 @@ class ParserOptions
        function getEditSection()                   { return $this->mEditSection; }
        function getNumberHeadings()                { return $this->mNumberHeadings; }
        function getAllowSpecialInclusion()         { return $this->mAllowSpecialInclusion; }
-
+       function getTidy()                          { return $this->mTidy; }
 
        function setUseTeX( $x )                    { return wfSetVar( $this->mUseTeX, $x ); }
        function setUseDynamicDates( $x )           { return wfSetVar( $this->mUseDynamicDates, $x ); }
@@ -3745,7 +3924,7 @@ class ParserOptions
        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 setTidy( $x )                      { return wfSetVar( $this->mTidy, $x); }
        function setSkin( &$x ) { $this->mSkin =& $x; }
 
        function ParserOptions() {
@@ -3788,6 +3967,7 @@ class ParserOptions
                $this->mEditSection = true;
                $this->mNumberHeadings = $user->getOption( 'numberheadings' );
                $this->mAllowSpecialInclusion = $wgAllowSpecialInclusion;
+               $this->mTidy = false;
                wfProfileOut( $fname );
        }
 }
@@ -3827,7 +4007,7 @@ function wfNumberOfFiles() {
 
 /**
  * Get various statistics from the database
- * @private
+ * @access private
  */
 function wfLoadSiteStats() {
        global $wgNumberOfArticles, $wgTotalViews, $wgTotalEdits;
@@ -3851,7 +4031,7 @@ function wfLoadSiteStats() {
 
 /**
  * Escape html tags
- * Basicly replacing " > and < with HTML entities ( &quot;, &gt;, &lt;)
+ * Basically replacing " > and < with HTML entities ( &quot;, &gt;, &lt;)
  *
  * @param string $in Text that might contain HTML tags
  * @return string Escaped string