(bug 4876) Add __NEWSECTIONLINK__ magic word to force the "new section" link/tab...
[lhc/web/wiklou.git] / includes / Parser.php
index 580d63b..b4eeb0d 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.6.0' );
+define( 'MW_PARSER_VERSION', '1.6.1' );
 
 /**
  * Variable substitution O(N^2) attack
@@ -73,7 +73,7 @@ define( 'EXT_IMAGE_REGEX',
  *   performs brace substitution on MediaWiki messages
  *
  * Globals used:
- *    objects:   $wgLang
+ *    objects:   $wgLang, $wgContLang
  *
  * NOT $wgArticle, $wgUser or $wgTitle. Keep them away!
  *
@@ -90,25 +90,25 @@ define( 'EXT_IMAGE_REGEX',
 class Parser
 {
        /**#@+
-        * @access private
+        * @private
         */
        # Persistent:
-       var $mTagHooks;
+       var $mTagHooks, $mFunctionHooks;
 
        # Cleared with clearState():
        var $mOutput, $mAutonumber, $mDTopen, $mStripState = array();
        var $mVariables, $mIncludeCount, $mArgStack, $mLastSection, $mInPre;
        var $mInterwikiLinkHolders, $mLinkHolders, $mUniqPrefix;
+       var $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
+                               // in this path. Used for loop detection.
 
-       # Temporary:
+       # Temporary
+       # These are variables reset at least once per parse regardless of $clearState
        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
-                               // in this path. Used for loop detection.
-               $mIWTransData = array(),
                $mRevisionId;   // ID to display in {{REVISIONID}} tags
 
        /**#@-*/
@@ -116,19 +116,18 @@ class Parser
        /**
         * Constructor
         *
-        * @access public
+        * @public
         */
        function Parser() {
-               $this->mTemplates = array();
-               $this->mTemplatePath = array();
                $this->mTagHooks = array();
+               $this->mFunctionHooks = array();
                $this->clearState();
        }
 
        /**
         * Clear Parser state
         *
-        * @access private
+        * @private
         */
        function clearState() {
                $this->mOutput = new ParserOutput;
@@ -154,13 +153,17 @@ class Parser
                $this->mRevisionId = null;
                $this->mUniqPrefix = 'UNIQ' . Parser::getRandomString();
 
+               # Clear these on every parse, bug 4549
+               $this->mTemplates = array();
+               $this->mTemplatePath = array();
+
                wfRunHooks( 'ParserClearState', array( &$this ) );
        }
 
        /**
         * Accessor for mUniqPrefix.
         *
-        * @access public
+        * @public
         */
        function UniqPrefix() {
                return $this->mUniqPrefix;
@@ -170,7 +173,7 @@ class Parser
         * Convert wikitext to HTML
         * Do not call this function recursively.
         *
-        * @access private
+        * @private
         * @param string $text Text we want to parse
         * @param Title &$title A title object
         * @param array $options
@@ -198,8 +201,6 @@ class Parser
                $this->mRevisionId = $revid;
                $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;
@@ -234,11 +235,11 @@ class Parser
 
                $this->replaceLinkHolders( $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 parserConvert() call should not be changed. it
+               # assumes that the links are all replaced and the only thing left
                # is the <nowiki> mark.
-               $text = $wgContLang->convert($text);
-               $this->mOutput->setTitleText($wgContLang->getParsedTitle());
+               # Side-effects: this calls $this->mOutput->setTitleText()
+               $text = $wgContLang->parserConvert( $text, $this );
 
                $text = $this->unstripNoWiki( $text, $this->mStripState );
 
@@ -248,6 +249,32 @@ class Parser
 
                if (($wgUseTidy and $this->mOptions->mTidy) or $wgAlwaysUseTidy) {
                        $text = Parser::tidy($text);
+               } else {
+                       # attempt to sanitize at least some nesting problems
+                       # (bug #2702 and quite a few others)
+                       $tidyregs = array(      
+                               # ''Something [http://www.cool.com cool''] --> 
+                               # <i>Something</i><a href="http://www.cool.com"..><i>cool></i></a>
+                               '/(<([bi])>)(<([bi])>)?([^<]*)(<\/?a[^<]*>)([^<]*)(<\/\\4>)?(<\/\\2>)/' =>
+                               '\\1\\3\\5\\8\\9\\6\\1\\3\\7\\8\\9',
+                               # fix up an anchor inside another anchor, only
+                               # at least for a single single nested link (bug 3695)
+                               '/(<a[^>]+>)([^<]*)(<a[^>]+>[^<]*)<\/a>(.*)<\/a>/' =>
+                               '\\1\\2</a>\\3</a>\\1\\4</a>',
+                               # fix div inside inline elements- doBlockLevels won't wrap a line which
+                               # contains a div, so fix it up here; replace
+                               # div with escaped text
+                               '/(<([aib]) [^>]+>)([^<]*)(<div([^>]*)>)(.*)(<\/div>)([^<]*)(<\/\\2>)/' =>
+                               '\\1\\3&lt;div\\5&gt;\\6&lt;/div&gt;\\8\\9',
+                               # remove empty italic or bold tag pairs, some
+                               # introduced by rules above
+                               '/<([bi])><\/\\1>/' => '' 
+                       );
+
+                       $text = preg_replace( 
+                               array_keys( $tidyregs ),
+                               array_values( $tidyregs ),
+                               $text );
                }
 
                wfRunHooks( 'ParserAfterTidy', array( &$this, &$text ) );
@@ -261,13 +288,16 @@ class Parser
        /**
         * Get a random string
         *
-        * @access private
+        * @private
         * @static
         */
        function getRandomString() {
                return dechex(mt_rand(0, 0x7fffffff)) . dechex(mt_rand(0, 0x7fffffff));
        }
 
+       function &getTitle() { return $this->mTitle; }
+       function getOptions() { return $this->mOptions; }
+
        /**
         * Replaces all occurrences of <$tag>content</$tag> in the text
         * with a random marker and returns the new text. the output parameter
@@ -278,7 +308,7 @@ class Parser
         * If $tag is set to STRIP_COMMENTS, the function will extract
         * <!-- HTML comments -->
         *
-        * @access private
+        * @private
         * @static
         */
        function extractTagsAndParams($tag, $text, &$content, &$tags, &$params, $uniq_prefix = ''){
@@ -298,22 +328,29 @@ class Parser
                }
 
                if( $tag == STRIP_COMMENTS ) {
-                       $start = '/<!--()()/';
+                       $start = '/<!--()/';
                        $end   = '/-->/';
                } else {
-                       $start = "/<$tag(\\s+[^\\/>]*|\\s*)(\\/?)>/i";
+                       $start = "/<$tag(\\s+[^>]*|\\s*\/?)>/i";
                        $end   = "/<\\/$tag\\s*>/i";
                }
 
                while ( '' != $text ) {
                        $p = preg_split( $start, $text, 2, PREG_SPLIT_DELIM_CAPTURE );
                        $stripped .= $p[0];
-                       if( count( $p ) < 4 ) {
+                       if( count( $p ) < 3 ) {
                                break;
                        }
                        $attributes = $p[1];
-                       $empty      = $p[2];
-                       $inside     = $p[3];
+                       $inside     = $p[2];
+
+                       // If $attributes ends with '/', we have an empty element tag, <tag />
+                       if( $tag != STRIP_COMMENTS && substr( $attributes, -1 ) == '/' ) {
+                               $attributes = substr( $attributes, 0, -1);
+                               $empty = '/';
+                       } else {
+                               $empty = '';
+                       }
 
                        $marker = $rnd . sprintf('%08X', $n++);
                        $stripped .= $marker;
@@ -344,7 +381,7 @@ class Parser
         * for cases where $tags and $params isn't needed
         * i.e. where tags will never have params, like <nowiki>
         *
-        * @access private
+        * @private
         * @static
         */
        function extractTags( $tag, $text, &$content, $uniq_prefix = '' ) {
@@ -366,7 +403,7 @@ class Parser
         *  for section editing, where these comments cause confusion when
         *  counting the sections in the wikisource
         *
-        * @access private
+        * @private
         */
        function strip( $text, &$state, $stripcomments = false ) {
                $render = ($this->mOutputType == OT_HTML);
@@ -442,11 +479,9 @@ class Parser
                }
 
                # Comments
-               if($stripcomments) {
-                       $text = Parser::extractTags(STRIP_COMMENTS, $text, $comment_content, $uniq_prefix);
-                       foreach( $comment_content as $marker => $content ){
-                               $comment_content[$marker] = '<!--'.$content.'-->';
-                       }
+               $text = Parser::extractTags(STRIP_COMMENTS, $text, $comment_content, $uniq_prefix);
+               foreach( $comment_content as $marker => $content ){
+                       $comment_content[$marker] = '<!--'.$content.'-->';
                }
 
                # Extensions
@@ -470,14 +505,24 @@ class Parser
                        }
                }
 
+               # Unstrip comments unless explicitly told otherwise.
+               # (The comments are always stripped prior to this point, so as to
+               # not invoke any extension tags / parser hooks contained within
+               # a comment.)
+               if ( !$stripcomments ) {
+                       $tempstate = array( 'comment' => $comment_content );
+                       $text = $this->unstrip( $text, $tempstate );
+                       $comment_content = array();
+               }
+
                # Merge state with the pre-existing state, if there is one
                if ( $state ) {
                        $state['html'] = $state['html'] + $html_content;
                        $state['nowiki'] = $state['nowiki'] + $nowiki_content;
                        $state['math'] = $state['math'] + $math_content;
                        $state['pre'] = $state['pre'] + $pre_content;
-                       $state['comment'] = $state['comment'] + $comment_content;
                        $state['gallery'] = $state['gallery'] + $gallery_content;
+                       $state['comment'] = $state['comment'] + $comment_content;
 
                        foreach( $ext_content as $tag => $array ) {
                                if ( array_key_exists( $tag, $state ) ) {
@@ -490,8 +535,8 @@ class Parser
                          'nowiki' => $nowiki_content,
                          'math' => $math_content,
                          'pre' => $pre_content,
-                         'comment' => $comment_content,
                          'gallery' => $gallery_content,
+                         'comment' => $comment_content,
                        ) + $ext_content;
                }
                return $text;
@@ -501,7 +546,7 @@ class Parser
         * restores pre, math, and hiero removed by strip()
         *
         * always call unstripNoWiki() after this one
-        * @access private
+        * @private
         */
        function unstrip( $text, &$state ) {
                if ( !is_array( $state ) ) {
@@ -523,7 +568,7 @@ class Parser
        /**
         * always call this after unstrip() to preserve the order
         *
-        * @access private
+        * @private
         */
        function unstripNoWiki( $text, &$state ) {
                if ( !is_array( $state ) ) {
@@ -550,7 +595,7 @@ class Parser
         * Returns the unique tag which must be inserted into the stripped text
         * The tag will be replaced with the original text in unstrip()
         *
-        * @access private
+        * @private
         */
        function insertStripItem( $text, &$state ) {
                $rnd = $this->mUniqPrefix . '-item' . Parser::getRandomString();
@@ -579,7 +624,7 @@ class Parser
         *
         * @param string $text Hideous HTML input
         * @return string Corrected HTML output
-        * @access public
+        * @public
         * @static
         */
        function tidy( $text ) {
@@ -602,7 +647,7 @@ class Parser
        /**
         * Spawn an external HTML tidy process and get corrected markup back from it.
         *
-        * @access private
+        * @private
         * @static
         */
        function externalTidy( $text ) {
@@ -621,6 +666,11 @@ class Parser
                $pipes = array();
                $process = proc_open("$wgTidyBin -config $wgTidyConf $wgTidyOpts$opts", $descriptorspec, $pipes);
                if (is_resource($process)) {
+                       // Theoretically, this style of communication could cause a deadlock
+                       // here. If the stdout buffer fills up, then writes to stdin could
+                       // block. This doesn't appear to happen with tidy, because tidy only
+                       // writes to stdout after it's finished reading from stdin. Search
+                       // for tidyParseStdin and tidySaveStdout in console/tidy.c
                        fwrite($pipes[0], $text);
                        fclose($pipes[0]);
                        while (!feof($pipes[1])) {
@@ -648,7 +698,7 @@ class Parser
         *
         * 'pear install tidy' should be able to compile the extension module.
         *
-        * @access private
+        * @private
         * @static
         */
        function internalTidy( $text ) {
@@ -674,7 +724,7 @@ class Parser
        /**
         * parse the wiki syntax used to render tables
         *
-        * @access private
+        * @private
         */
        function doTableStuff ( $t ) {
                $fname = 'Parser::doTableStuff';
@@ -685,6 +735,7 @@ class Parser
                $ltd = array () ; # Was it TD or TH?
                $tr = array () ; # Is currently a tr tag open?
                $ltr = array () ; # tr attributes
+               $has_opened_tr = array(); # Did this table open a <tr> element?
                $indent_level = 0; # indent level of the table
                foreach ( $t AS $k => $x )
                {
@@ -701,11 +752,13 @@ class Parser
                                array_push ( $ltd , '' ) ;
                                array_push ( $tr , false ) ;
                                array_push ( $ltr , '' ) ;
+                               array_push ( $has_opened_tr, false );
                        }
                        else if ( count ( $td ) == 0 ) { } # Don't do any of the following
                        else if ( '|}' == substr ( $x , 0 , 2 ) ) {
                                $z = "</table>" . substr ( $x , 2);
                                $l = array_pop ( $ltd ) ;
+                               if ( !array_pop ( $has_opened_tr ) ) $z = "<tr><td></td></tr>" . $z ;
                                if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
                                if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
                                array_pop ( $ltr ) ;
@@ -716,6 +769,8 @@ class Parser
                                while ( $x != '' && substr ( $x , 0 , 1 ) == '-' ) $x = substr ( $x , 1 ) ;
                                $z = '' ;
                                $l = array_pop ( $ltd ) ;
+                               array_pop ( $has_opened_tr );
+                               array_push ( $has_opened_tr , true ) ;
                                if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
                                if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
                                array_pop ( $ltr ) ;
@@ -734,7 +789,13 @@ class Parser
                                }
                                $after = substr ( $x , 1 ) ;
                                if ( $fc == '!' ) $after = str_replace ( '!!' , '||' , $after ) ;
-                               $after = explode ( '||' , $after ) ;
+                               
+                               // Split up multiple cells on the same line.
+                               // FIXME: This can result in improper nesting of tags processed
+                               // by earlier parser steps, but should avoid splitting up eg
+                               // attribute values containing literal "||".
+                               $after = wfExplodeMarkup( '||', $after );
+                               
                                $t[$k] = '' ;
 
                                # Loop through each table cell
@@ -747,6 +808,8 @@ class Parser
                                                if ( !array_pop ( $tr ) ) $z = '<tr'.$tra.">\n" ;
                                                array_push ( $tr , true ) ;
                                                array_push ( $ltr , '' ) ;
+                                               array_pop ( $has_opened_tr );
+                                               array_push ( $has_opened_tr , true ) ;
                                        }
 
                                        $l = array_pop ( $ltd ) ;
@@ -779,12 +842,17 @@ class Parser
                # Closing open td, tr && table
                while ( count ( $td ) > 0 )
                {
+                       $l = array_pop ( $ltd ) ;
                        if ( array_pop ( $td ) ) $t[] = '</td>' ;
                        if ( array_pop ( $tr ) ) $t[] = '</tr>' ;
+                       if ( !array_pop ( $has_opened_tr ) ) $t[] = "<tr><td></td></tr>" ;
                        $t[] = '</table>' ;
                }
 
                $t = implode ( "\n" , $t ) ;
+               # special case: don't return empty table
+               if($t == "<table>\n<tr><td></td></tr>\n</table>")
+                       $t = '';
                wfProfileOut( $fname );
                return $t ;
        }
@@ -793,10 +861,9 @@ class Parser
         * Helper function for parse() that transforms wiki markup into
         * HTML. Only called for $mOutputType == OT_HTML.
         *
-        * @access private
+        * @private
         */
        function internalParse( $text ) {
-               global $wgContLang;
                $args = array();
                $isMain = true;
                $fname = 'Parser::internalParse';
@@ -829,23 +896,15 @@ class Parser
                $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.
         *
-        * @access private
+        * @private
         */
        function &doMagicLinks( &$text ) {
                $text = $this->magicISBN( $text );
@@ -857,7 +916,7 @@ class Parser
        /**
         * Parse headers and return html
         *
-        * @access private
+        * @private
         */
        function doHeadings( $text ) {
                $fname = 'Parser::doHeadings';
@@ -873,7 +932,7 @@ class Parser
 
        /**
         * Replace single quotes with HTML markup
-        * @access private
+        * @private
         * @return string the altered text
         */
        function doAllQuotes( $text ) {
@@ -891,7 +950,7 @@ class Parser
 
        /**
         * Helper function for doAllQuotes()
-        * @access private
+        * @private
         */
        function doQuotes( $text ) {
                $arr = preg_split( "/(''+)/", $text, -1, PREG_SPLIT_DELIM_CAPTURE );
@@ -1060,7 +1119,7 @@ class Parser
         * Note: this is all very hackish and the order of execution matters a lot.
         * Make sure to run maintenance/parserTests.php if you change this code.
         *
-        * @access private
+        * @private
         */
        function replaceExternalLinks( $text ) {
                global $wgContLang;
@@ -1122,9 +1181,6 @@ class Parser
                        # Replace &amp; from obsolete syntax with &.
                        # All HTML entities will be escaped by makeExternalLink()
                        $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
@@ -1136,8 +1192,11 @@ class Parser
                        # 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 );
+                       # Register link in the output object.
+                       # Replace unnecessary URL escape codes with the referenced character
+                       # This prevents spammers from hiding links from the filters
+                       $pasteurized = Parser::replaceUnusualEscapes( $url );
+                       $this->mOutput->addExternalLink( $pasteurized );
                }
 
                wfProfileOut( $fname );
@@ -1146,7 +1205,7 @@ class Parser
 
        /**
         * Replace anything that looks like a URL with a link
-        * @access private
+        * @private
         */
        function replaceFreeExternalLinks( $text ) {
                global $wgContLang;
@@ -1168,6 +1227,19 @@ class Parser
                                $url = $protocol . $m[1];
                                $trail = $m[2];
 
+                               # special case: handle urls as url args:
+                               # http://www.example.com/foo?=http://www.example.com/bar
+                               if(strlen($trail) == 0 && 
+                                       isset($bits[$i]) &&
+                                       preg_match('/^'. wfUrlProtocols() . '$/S', $bits[$i]) &&
+                                       preg_match( '/^('.EXT_LINK_URL_CLASS.'+)(.*)$/s', $bits[$i + 1], $m )) 
+                               {
+                                       # add protocol, arg
+                                       $url .= $bits[$i] . $m[1]; # protocol, url as arg to previous link
+                                       $i += 2;
+                                       $trail = $m[2];
+                               }
+
                                # The characters '<' and '>' (which were escaped by
                                # removeHTMLtags()) should not be included in
                                # URLs, per RFC 2396.
@@ -1193,16 +1265,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 );
+                                       # Register it in the output object...
+                                       # Replace unnecessary URL escape codes with their equivalent characters
+                                       $pasteurized = Parser::replaceUnusualEscapes( $url );
+                                       $this->mOutput->addExternalLink( $pasteurized );
                                }
                                $s .= $text . $trail;
                        } else {
@@ -1215,12 +1287,16 @@ class Parser
 
        /**
         * Replace unusual URL escape codes with their equivalent characters
-        * @param string 
+        * @param string
         * @return string
         * @static
+        * @fixme This can merge genuinely required bits in the path or query string,
+        *        breaking legit URLs. A proper fix would treat the various parts of
+        *        the URL differently; as a workaround, just use the output for
+        *        statistical records, not for actual linking/output.
         */
        function replaceUnusualEscapes( $url ) {
-               return preg_replace_callback( '/%[0-9A-Fa-f]{2}/', 
+               return preg_replace_callback( '/%[0-9A-Fa-f]{2}/',
                        array( 'Parser', 'replaceUnusualEscapesCallback' ), $url );
        }
 
@@ -1228,7 +1304,7 @@ class Parser
         * Callback function used in replaceUnusualEscapes().
         * Replaces unusual URL escape codes with their equivalent character
         * @static
-        * @access private
+        * @private
         */
        function replaceUnusualEscapesCallback( $matches ) {
                $char = urldecode( $matches[0] );
@@ -1246,7 +1322,7 @@ class Parser
        /**
         * make an image if it's allowed, either through the global
         * option or through the exception
-        * @access private
+        * @private
         */
        function maybeMakeExternalImage( $url ) {
                $sk =& $this->mOptions->getSkin();
@@ -1266,7 +1342,7 @@ class Parser
        /**
         * Process [[ ]] wikilinks
         *
-        * @access private
+        * @private
         */
        function replaceInternalLinks( $s ) {
                global $wgContLang;
@@ -1351,12 +1427,18 @@ class Parser
                                # 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 ) ) {
+                               if( $text !== '' && 
+                                       preg_match( "/^\](.*)/s", $m[3], $n ) && 
+                                       strpos($text, '[') !== false 
+                               ) 
+                               {
                                        $text .= ']'; # so that replaceExternalLinks($text) works later
                                        $m[3] = $n[1];
                                }
                                # fix up urlencoded title texts
-                               if(preg_match('/%/', $m[1] )) $m[1] = urldecode($m[1]);
+                               if(preg_match('/%/', $m[1] )) 
+                                       # Should anchors '#' also be rejected?
+                                       $m[1] = str_replace( array('<', '>'), array('&lt;', '&gt;'), urldecode($m[1]) );
                                $trail = $m[3];
                        } elseif( preg_match($e1_img, $line, $m) ) { # Invalid, but might be an image with a link in its caption
                                $might_be_img = true;
@@ -1623,7 +1705,7 @@ class Parser
         * @param string $target the source of the link
         * @param string &$text the link text, modified as necessary
         * @return string the full name of the link
-        * @access private
+        * @private
         */
        function maybeDoSubpageLink($target, &$text) {
                # Valid link forms:
@@ -1689,7 +1771,7 @@ class Parser
 
        /**#@+
         * Used by doBlockLevels()
-        * @access private
+        * @private
         */
        /* private */ function closeParagraph() {
                $result = '';
@@ -1766,7 +1848,7 @@ class Parser
        /**
         * Make lists from lines starting with ':', '*', '#', etc.
         *
-        * @access private
+        * @private
         * @return string the lists rendered as HTML
         */
        function doBlockLevels( $text, $linestart ) {
@@ -1861,6 +1943,7 @@ class Parser
                                        '<td|<th|<div|<\\/div|<hr|<\\/pre|<\\/p|'.$this->mUniqPrefix.'-pre|<\\/li|<\\/ul)/iS', $t );
                                if ( $openmatch or $closematch ) {
                                        $paragraphStack = false;
+                                       # TODO bug 5718: paragraph closed
                                        $output .= $this->closeParagraph();
                                        if ( $preOpenMatch and !$preCloseMatch ) {
                                                $this->mInPre = true;
@@ -1973,7 +2056,7 @@ class Parser
        /**
         * Return value of a magic variable (like PAGENAME)
         *
-        * @access private
+        * @private
         */
        function getVariableValue( $index ) {
                global $wgContLang, $wgSitename, $wgServer, $wgServerName, $wgScriptPath;
@@ -2011,12 +2094,44 @@ class Parser
                                return $this->mTitle->getPrefixedText();
                        case MAG_FULLPAGENAMEE:
                                return $this->mTitle->getPrefixedURL();
+                       case MAG_SUBPAGENAME:
+                               return $this->mTitle->getSubpageText();
+                       case MAG_SUBPAGENAMEE:
+                               return $this->mTitle->getSubpageUrlForm();
+                       case MAG_TALKPAGENAME:
+                               if( $this->mTitle->canTalk() ) {
+                                       $talkPage = $this->mTitle->getTalkPage();
+                                       return $talkPage->getPrefixedText();
+                               } else {
+                                       return '';
+                               }
+                       case MAG_TALKPAGENAMEE:
+                               if( $this->mTitle->canTalk() ) {
+                                       $talkPage = $this->mTitle->getTalkPage();
+                                       return $talkPage->getPrefixedUrl();
+                               } else {
+                                       return '';
+                               }
+                       case MAG_SUBJECTPAGENAME:
+                               $subjPage = $this->mTitle->getSubjectPage();
+                               return $subjPage->getPrefixedText();
+                       case MAG_SUBJECTPAGENAMEE:
+                               $subjPage = $this->mTitle->getSubjectPage();
+                               return $subjPage->getPrefixedUrl();
                        case MAG_REVISIONID:
                                return $this->mRevisionId;
                        case MAG_NAMESPACE:
                                return $wgContLang->getNsText( $this->mTitle->getNamespace() );
                        case MAG_NAMESPACEE:
                                return wfUrlencode( $wgContLang->getNsText( $this->mTitle->getNamespace() ) );
+                       case MAG_TALKSPACE:
+                               return $this->mTitle->canTalk() ? $this->mTitle->getTalkNsText() : '';
+                       case MAG_TALKSPACEE:
+                               return $this->mTitle->canTalk() ? wfUrlencode( $this->mTitle->getTalkNsText() ) : '';
+                       case MAG_SUBJECTSPACE:
+                               return $this->mTitle->getSubjectNsText();
+                       case MAG_SUBJECTSPACEE:
+                               return( wfUrlencode( $this->mTitle->getSubjectNsText() ) );
                        case MAG_CURRENTDAYNAME:
                                return $varCache[$index] = $wgContLang->getWeekdayName( date( 'w', $ts ) + 1 );
                        case MAG_CURRENTYEAR:
@@ -2033,6 +2148,8 @@ class Parser
                                return $varCache[$index] = $wgContLang->formatNum( wfNumberOfArticles() );
                        case MAG_NUMBEROFFILES:
                                return $varCache[$index] = $wgContLang->formatNum( wfNumberOfFiles() );
+                       case MAG_NUMBEROFUSERS:
+                               return $varCache[$index] = $wgContLang->formatNum( wfNumberOfUsers() );
                        case MAG_SITENAME:
                                return $wgSitename;
                        case MAG_SERVER:
@@ -2053,7 +2170,7 @@ class Parser
        /**
         * initialise the magic variables (like CURRENTMONTHNAME)
         *
-        * @access private
+        * @private
         */
        function initialiseVariables() {
                $fname = 'Parser::initialiseVariables';
@@ -2079,7 +2196,7 @@ class Parser
         *                                                                4 => callback         # replacement callback to call if {{{{..}}}} is found
         *                                                                )
         *                                      )
-        * @access private
+        * @private
         */
        function replace_callback ($text, $callbacks) {
                $openingBraceStack = array();   # this array will hold a stack of parentheses which are not closed yet
@@ -2253,9 +2370,10 @@ class Parser
         *
         * @param string $tex The text to transform
         * @param array $args Key-value pairs representing template parameters to substitute
-        * @access private
+        * @param bool $argsOnly Only do argument (triple-brace) expansion, not double-brace expansion
+        * @private
         */
-       function replaceVariables( $text, $args = array() ) {
+       function replaceVariables( $text, $args = array(), $argsOnly = false ) {
                # Prevent too big inclusions
                if( strlen( $text ) > MAX_INCLUDE_SIZE ) {
                        return $text;
@@ -2268,7 +2386,9 @@ class Parser
                array_push( $this->mArgStack, $args );
 
                $braceCallbacks = array();
-               $braceCallbacks[2] = array( &$this, 'braceSubstitution' );
+               if ( !$argsOnly ) {
+                       $braceCallbacks[2] = array( &$this, 'braceSubstitution' );
+               }
                if ( $this->mOutputType == OT_HTML || $this->mOutputType == OT_WIKI ) {
                        $braceCallbacks[3] = array( &$this, 'argSubstitution' );
                }
@@ -2285,7 +2405,7 @@ class Parser
 
        /**
         * Replace magic variables
-        * @access private
+        * @private
         */
        function variableSubstitution( $matches ) {
                $fname = 'Parser::variableSubstitution';
@@ -2349,17 +2469,23 @@ class Parser
         *  $piece['title']: the title, i.e. the part before the |
         *  $piece['parts']: the parameter array
         * @return string the text of the template
-        * @access private
+        * @private
         */
        function braceSubstitution( $piece ) {
-               global $wgContLang;
+               global $wgContLang, $wgLang, $wgAllowDisplayTitle, $action;
                $fname = 'Parser::braceSubstitution';
                wfProfileIn( $fname );
 
-               $found = false;
-               $nowiki = false;
-               $noparse = false;
+               # Flags
+               $found = false;             # $text has been filled
+               $nowiki = false;            # wiki markup in $text should be escaped
+               $noparse = false;           # Unsafe HTML tags should not be stripped, etc.
+               $noargs = false;            # Don't replace triple-brace arguments in $text
+               $replaceHeadings = false;   # Make the edit section links go to the template not the article
+               $isHTML = false;            # $text is HTML, armour it against wikitext transformation
+               $forceRawInterwiki = false; # Force interwiki transclusion to be done in raw mode not rendered
 
+               # Title object, where $text came from
                $title = NULL;
 
                $linestart = '';
@@ -2376,6 +2502,7 @@ class Parser
                                $text = $replaceWith;
                                $found = true;
                                $noparse = true;
+                               $noargs = true;
                        }
                }
 
@@ -2393,10 +2520,11 @@ class Parser
                                $text = $piece['text'];
                                $found = true;
                                $noparse = true;
+                               $noargs = true;
                        }
                }
 
-               # MSG, MSGNW and INT
+               # MSG, MSGNW, INT and RAW
                if ( !$found ) {
                        # Check for MSGNW:
                        $mwMsgnw =& MagicWord::get( MAG_MSGNW );
@@ -2407,7 +2535,13 @@ class Parser
                                $mwMsg =& MagicWord::get( MAG_MSG );
                                $mwMsg->matchStartAndRemove( $part1 );
                        }
-
+                       
+                       # Check for RAW:
+                       $mwRaw =& MagicWord::get( MAG_RAW );
+                       if ( $mwRaw->matchStartAndRemove( $part1 ) ) {
+                               $forceRawInterwiki = true;
+                       }
+                       
                        # Check if it is an internal message
                        $mwInt =& MagicWord::get( MAG_INT );
                        if ( $mwInt->matchStartAndRemove( $part1 ) ) {
@@ -2490,11 +2624,12 @@ class Parser
                        }
                }
 
+               $lang = $this->mOptions->getInterfaceMessage() ? $wgLang : $wgContLang;
                # GRAMMAR
                if ( !$found && $argc == 1 ) {
                        $mwGrammar =& MagicWord::get( MAG_GRAMMAR );
                        if ( $mwGrammar->matchStartAndRemove( $part1 ) ) {
-                               $text = $linestart . $wgContLang->convertGrammar( $args[0], $part1 );
+                               $text = $linestart . $lang->convertGrammar( $args[0], $part1 );
                                $found = true;
                        }
                }
@@ -2504,21 +2639,104 @@ class Parser
                        $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]);
+                               $text = $linestart . $lang->convertPlural( $part1, $args[0], $args[1], $args[2]);
+                               $found = true;
+                       }
+               }
+               
+               # DISPLAYTITLE
+               if ( !$found && $argc == 1 && $wgAllowDisplayTitle ) {
+                       $mwDT =& MagicWord::get( MAG_DISPLAYTITLE );
+                       if ( $mwDT->matchStartAndRemove( $part1 ) ) {
+                               
+                               # Set title in parser output object
+                               $param = $args[0];
+                               $parserOptions = new ParserOptions;
+                               $local_parser = new Parser ();
+                               $t2 = $local_parser->parse ( $param, $this->mTitle, $parserOptions, false );
+                               $this->mOutput->mHTMLtitle = $t2->GetText();
+
+                               # Add subtitle
+                               $t = $this->mTitle->getPrefixedText();
+                               $this->mOutput->mSubtitle .= wfMsg('displaytitle', $t);
+                               $text = "" ;
+                               $found = true ;
+                       }
+               }               
+
+               # NUMBEROFUSERS, NUMBEROFARTICLES, and NUMBEROFFILES
+               if( !$found ) {
+                       $mwWordsToCheck = array( MAG_NUMBEROFUSERS => 'wfNumberOfUsers', MAG_NUMBEROFARTICLES => 'wfNumberOfArticles', MAG_NUMBEROFFILES => 'wfNumberOfFiles' );
+                       foreach( $mwWordsToCheck as $word => $func ) {
+                               $mwCurrentWord =& MagicWord::get( $word );
+                               if( $mwCurrentWord->matchStartAndRemove( $part1 ) ) {
+                                       $mwRawSuffix =& MagicWord::get( MAG_RAWSUFFIX );
+                                       if( $mwRawSuffix->match( $args[0] ) ) {
+                                               # Raw and unformatted
+                                               $text = $linestart . call_user_func( $func );
+                                       } else {
+                                               # Formatted according to the content default
+                                               $text = $linestart . $wgContLang->formatNum( call_user_func( $func ) );
+                                       }
+                                       $found = true;
+                               }
+                       }
+               }
+               
+                       /*$mwNumUsers =& MagicWord::get( MAG_NUMBEROFUSERS );
+                       if( $mwNumUsers->matchStartAndRemove( $part1 ) ) {
+                               $mwRawSuffix =& MagicWord::get( MAG_RAWSUFFIX );
+                               if( $mwRawSuffix->match( $args[0] ) ) {
+                                       # Raw and unformatted
+                                       $text = $linestart . wfNumberOfUsers();
+                               } else {
+                                       # Default; formatted form
+                                       $text = $linestart . $wgContLang->formatNum( wfNumberOfUsers() );
+                               }
                                $found = true;
                        }
+               }*/
+
+               # Extensions
+               if ( !$found && substr( $part1, 0, 1 ) == '#' ) {
+                       $colonPos = strpos( $part1, ':' );
+                       if ( $colonPos !== false ) {
+                               $function = strtolower( substr( $part1, 1, $colonPos - 1 ) );
+                               if ( isset( $this->mFunctionHooks[$function] ) ) {
+                                       $funcArgs = array_map( 'trim', $args );
+                                       $funcArgs = array_merge( array( &$this, trim( substr( $part1, $colonPos + 1 ) ) ), $funcArgs );
+                                       $result = call_user_func_array( $this->mFunctionHooks[$function], $funcArgs );
+                                       $found = true;
+
+                                       // The text is usually already parsed, doesn't need triple-brace tags expanded, etc.
+                                       //$noargs = true;
+                                       //$noparse = true;
+                                       
+                                       if ( is_array( $result ) ) {
+                                               $text = $linestart . $result[0];
+                                               unset( $result[0] );
+
+                                               // Extract flags into the local scope
+                                               // This allows callers to set flags such as nowiki, noparse, found, etc.
+                                               extract( $result );
+                                       } else {
+                                               $text = $linestart . $result;
+                                       }
+                               }
+                       }
                }
 
                # Template table test
 
                # Did we encounter this template already? If yes, it is in the cache
                # and we need to check for loops.
-               if ( !$found && isset( $this->mTemplates[$part1] ) ) {
+               if ( !$found && isset( $this->mTemplates[$piece['title']] ) ) {
                        $found = true;
 
                        # Infinite loop test
                        if ( isset( $this->mTemplatePath[$part1] ) ) {
                                $noparse = true;
+                               $noargs = true;
                                $found = true;
                                $text = $linestart .
                                        '{{' . $part1 . '}}' .
@@ -2526,61 +2744,72 @@ class Parser
                                wfDebug( "$fname: template loop broken at '$part1'\n" );
                        } else {
                                # set $text to cached message.
-                               $text = $linestart . $this->mTemplates[$part1];
+                               $text = $linestart . $this->mTemplates[$piece['title']];
                        }
                }
 
                # Load from database
-               $replaceHeadings = false;
-               $isHTML = false;
                $lastPathLevel = $this->mTemplatePath;
                if ( !$found ) {
                        $ns = NS_TEMPLATE;
-                       $part1 = $this->maybeDoSubpageLink( $part1, $subpage='' );
+                       # declaring $subpage directly in the function call
+                       # does not work correctly with references and breaks
+                       # {{/subpage}}-style inclusions
+                       $subpage = '';
+                       $part1 = $this->maybeDoSubpageLink( $part1, $subpage );
                        if ($subpage !== '') {
                                $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 ) ) {
-                                       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 ( !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;
+                                                               $noargs = 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().']]';
+                                       # If the title is valid but undisplayable, make a link to it
+                                       if ( $this->mOutputType == OT_HTML && !$found ) {
+                                               $text = '[['.$title->getPrefixedText().']]';
+                                               $found = true;
+                                       }
+                               } elseif ( $title->isTrans() ) {
+                                       // Interwiki transclusion
+                                       if ( $this->mOutputType == OT_HTML && !$forceRawInterwiki ) {
+                                               $text = $this->interwikiTransclude( $title, 'render' );
+                                               $isHTML = true;
+                                               $noparse = true;
+                                       } else {
+                                               $text = $this->interwikiTransclude( $title, 'raw' );
+                                               $replaceHeadings = true;
+                                       }
                                        $found = true;
                                }
-
+                               
                                # Template cache array insertion
+                               # Use the original $piece['title'] not the mangled $part1, so that
+                               # modifiers such as RAW: produce separate cache entries
                                if( $found ) {
-                                       $this->mTemplates[$part1] = $text;
+                                       $this->mTemplates[$piece['title']] = $text;
                                        $text = $linestart . $text;
                                }
                        }
@@ -2590,51 +2819,62 @@ class Parser
                # Only for HTML output
                if ( $nowiki && $found && $this->mOutputType == OT_HTML ) {
                        $text = wfEscapeWikiText( $text );
-               } elseif ( ($this->mOutputType == OT_HTML || $this->mOutputType == OT_WIKI) && $found && !$noparse) {
-                       # Clean up argument array
-                       $assocArgs = array();
-                       $index = 1;
-                       foreach( $args as $arg ) {
-                               $eqpos = strpos( $arg, '=' );
-                               if ( $eqpos === false ) {
-                                       $assocArgs[$index++] = $arg;
-                               } else {
-                                       $name = trim( substr( $arg, 0, $eqpos ) );
-                                       $value = trim( substr( $arg, $eqpos+1 ) );
-                                       if ( $value === false ) {
-                                               $value = '';
-                                       }
-                                       if ( $name !== false ) {
-                                               $assocArgs[$name] = $value;
+               } elseif ( ($this->mOutputType == OT_HTML || $this->mOutputType == OT_WIKI) && $found ) {
+                       if ( $noargs ) {
+                               $assocArgs = array();
+                       } else {
+                               # Clean up argument array
+                               $assocArgs = array();
+                               $index = 1;
+                               foreach( $args as $arg ) {
+                                       $eqpos = strpos( $arg, '=' );
+                                       if ( $eqpos === false ) {
+                                               $assocArgs[$index++] = $arg;
+                                       } else {
+                                               $name = trim( substr( $arg, 0, $eqpos ) );
+                                               $value = trim( substr( $arg, $eqpos+1 ) );
+                                               if ( $value === false ) {
+                                                       $value = '';
+                                               }
+                                               if ( $name !== false ) {
+                                                       $assocArgs[$name] = $value;
+                                               }
                                        }
                                }
-                       }
-
-                       # Add a new element to the templace recursion path
-                       $this->mTemplatePath[$part1] = 1;
 
-                       # If there are any <onlyinclude> tags, only include them
-                       if ( in_string( '<onlyinclude>', $text ) && in_string( '</onlyinclude>', $text ) ) {
-                               preg_match_all( '/<onlyinclude>(.*?)\n?<\/onlyinclude>/s', $text, $m );
-                               $text = '';
-                               foreach ($m[1] as $piece)
-                                       $text .= $piece;
+                               # Add a new element to the templace recursion path
+                               $this->mTemplatePath[$part1] = 1;
                        }
-                       # 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 );
-                               $text = Sanitizer::removeHTMLtags( $text, array( &$this, 'replaceVariables' ), $assocArgs );
-                       }
-                       $text = $this->replaceVariables( $text, $assocArgs );
+                       if ( !$noparse ) {
+                               # If there are any <onlyinclude> tags, only include them
+                               if ( in_string( '<onlyinclude>', $text ) && in_string( '</onlyinclude>', $text ) ) {
+                                       preg_match_all( '/<onlyinclude>(.*?)\n?<\/onlyinclude>/s', $text, $m );
+                                       $text = '';
+                                       foreach ($m[1] as $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 );
+                                       $text = Sanitizer::removeHTMLtags( $text, array( &$this, 'replaceVariables' ), $assocArgs );
+                               }
+                               $text = $this->replaceVariables( $text, $assocArgs );
 
-                       # 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)) {
-                               $text = "\n" . $text;
+                               # 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)) {
+                                       $text = "\n" . $text;
+                               }
+                       } elseif ( !$noargs ) {
+                               # $noparse and !$noargs
+                               # Just replace the arguments, not any double-brace items
+                               # This is used for rendered interwiki transclusion
+                               $text = $this->replaceVariables( $text, $assocArgs, true );
                        }
                }
                # Prune lower levels off the recursion check path
@@ -2692,7 +2932,7 @@ class Parser
        }
 
        /**
-        * Fetch the unparsed text of a template and register a reference to it. 
+        * Fetch the unparsed text of a template and register a reference to it.
         */
        function fetchTemplate( $title ) {
                $text = false;
@@ -2714,41 +2954,49 @@ class Parser
        }
 
        /**
-        * 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));
@@ -2758,7 +3006,7 @@ class Parser
 
        /**
         * Triple brace replacement -- used for template arguments
-        * @access private
+        * @private
         */
        function argSubstitution( $matches ) {
                $arg = trim( $matches['title'] );
@@ -2776,7 +3024,7 @@ class Parser
 
        /**
         * Returns true if the function is allowed to include this entity
-        * @access private
+        * @private
         */
        function incrementIncludeCount( $dbk ) {
                if ( !array_key_exists( $dbk, $this->mIncludeCount ) ) {
@@ -2801,7 +3049,7 @@ class Parser
         *
         * @param string $text
         * @param boolean $isMain
-        * @access private
+        * @private
         */
        function formatHeadings( $text, $isMain=true ) {
                global $wgMaxTocLevel, $wgContLang;
@@ -2836,6 +3084,12 @@ class Parser
                        $doShowToc = false;
                }
 
+               # Allow user to stipulate that a page should have a "new section"
+               # link added via __NEWSECTIONLINK__
+               $mw =& MagicWord::get( MAG_NEWSECTIONLINK );
+               if( $mw->matchAndRemove( $text ) )
+                       $this->mOutput->setNewSection( true );
+
                # if the string __TOC__ (not case-sensitive) occurs in the HTML,
                # override above conditions and always show TOC at that place
 
@@ -2902,7 +3156,9 @@ class Parser
                                        # Increase TOC level
                                        $toclevel++;
                                        $sublevelCount[$toclevel] = 0;
-                                       $toc .= $sk->tocIndent();
+                                       if( $toclevel<$wgMaxTocLevel ) {
+                                               $toc .= $sk->tocIndent();
+                                       }
                                }
                                elseif ( $level < $prevlevel && $toclevel > 1 ) {
                                        # Decrease TOC level, find level to jump to
@@ -2924,12 +3180,15 @@ class Parser
                                                        }
                                                }
                                        }
-
-                                       $toc .= $sk->tocUnindent( $prevtoclevel - $toclevel );
+                                       if( $toclevel<$wgMaxTocLevel ) {
+                                               $toc .= $sk->tocUnindent( $prevtoclevel - $toclevel );
+                                       }
                                }
                                else {
                                        # No change in level, end TOC line
-                                       $toc .= $sk->tocLineEnd();
+                                       if( $toclevel<$wgMaxTocLevel ) {
+                                               $toc .= $sk->tocLineEnd();
+                                       }
                                }
 
                                $levelCount[$toclevel] = $level;
@@ -2967,6 +3226,8 @@ class Parser
                        # strip out HTML
                        $canonized_headline = preg_replace( '/<.*?' . '>/','',$canonized_headline );
                        $tocline = trim( $canonized_headline );
+                       # Save headline for section edit hint before it's escaped
+                       $headline_hint = trim( $canonized_headline );
                        $canonized_headline = Sanitizer::escapeId( $tocline );
                        $refers[$headlineCount] = $canonized_headline;
 
@@ -2995,7 +3256,7 @@ class Parser
                                if( $istemplate )
                                        $head[$headlineCount] .= $sk->editSectionLinkForOther($templatetitle, $templatesection);
                                else
-                                       $head[$headlineCount] .= $sk->editSectionLink($this->mTitle, $sectionCount+1);
+                                       $head[$headlineCount] .= $sk->editSectionLink($this->mTitle, $sectionCount+1, $headline_hint);
                        }
 
                        # give headline the correct <h#> tag
@@ -3007,7 +3268,9 @@ class Parser
                }
 
                if( $doShowToc ) {
-                       $toc .= $sk->tocUnindent( $toclevel - 1 );
+                       if( $toclevel<$wgMaxTocLevel ) {
+                               $toc .= $sk->tocUnindent( $toclevel - 1 );
+                       }
                        $toc = $sk->tocList( $toc );
                }
 
@@ -3046,7 +3309,7 @@ class Parser
 
        /**
         * Return an HTML link for the "ISBN 123456" text
-        * @access private
+        * @private
         */
        function magicISBN( $text ) {
                $fname = 'Parser::magicISBN';
@@ -3061,6 +3324,13 @@ class Parser
                $valid = '0123456789-Xx';
 
                foreach ( $a as $x ) {
+                       # hack: don't replace inside thumbnail title/alt
+                       # attributes
+                       if(preg_match('/<[^>]+(alt|title)="[^">]*$/', $text)) {
+                               $text .= "ISBN $x";
+                               continue;
+                       }
+
                        $isbn = $blank = '' ;
                        while ( ' ' == $x{0} ) {
                                $blank .= ' ';
@@ -3083,7 +3353,7 @@ class Parser
                        } else {
                                $titleObj = Title::makeTitle( NS_SPECIAL, 'Booksources' );
                                $text .= '<a href="' .
-                               $titleObj->escapeLocalUrl( 'isbn='.$num ) .
+                                       $titleObj->escapeLocalUrl( 'isbn='.$num ) .
                                        "\" class=\"internal\">ISBN $isbn</a>";
                                $text .= $x;
                        }
@@ -3095,7 +3365,7 @@ class Parser
        /**
         * Return an HTML link for the "RFC 1234" text
         *
-        * @access private
+        * @private
         * @param string $text     Text to be processed
         * @param string $keyword  Magic keyword to use (default RFC)
         * @param string $urlmsg   Interface message to use (default rfcurl)
@@ -3127,6 +3397,13 @@ class Parser
                                continue;
                                }
 
+                       # hack: don't replace inside thumbnail title/alt
+                       # attributes
+                       if(preg_match('/<[^>]+(alt|title)="[^">]*$/', $text)) {
+                               $text .= $keyword . $x;
+                               continue;
+                       }
+                       
                        $id = $blank = '' ;
 
                        /** remove and save whitespaces in $blank */
@@ -3171,7 +3448,7 @@ class Parser
         * @param ParserOptions $options parsing options
         * @param bool $clearState whether to clear the parser state first
         * @return string the altered wiki markup
-        * @access public
+        * @public
         */
        function preSaveTransform( $text, &$title, &$user, $options, $clearState = true ) {
                $this->mOptions = $options;
@@ -3188,7 +3465,7 @@ class Parser
                );
                $text = str_replace( array_keys( $pairs ), array_values( $pairs ), $text );
                $text = $this->strip( $text, $stripState, true );
-               $text = $this->pstPass2( $text, $user );
+               $text = $this->pstPass2( $text, $stripState, $user );
                $text = $this->unstrip( $text, $stripState );
                $text = $this->unstripNoWiki( $text, $stripState );
                return $text;
@@ -3196,15 +3473,15 @@ class Parser
 
        /**
         * Pre-save transform helper function
-        * @access private
+        * @private
         */
-       function pstPass2( $text, &$user ) {
+       function pstPass2( $text, &$stripState, &$user ) {
                global $wgContLang, $wgLocaltimezone;
 
                /* 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
-                * than the one selected in each users preferences.
+                * everyone the same signature and use the default one rather
+                * than the one selected in each user's preferences.
                 */
                if ( isset( $wgLocaltimezone ) ) {
                        $oldtz = getenv( 'TZ' );
@@ -3220,9 +3497,12 @@ class Parser
                # Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
                $text = $this->replaceVariables( $text );
                
+               # Strip out <nowiki> etc. added via replaceVariables
+               $text = $this->strip( $text, $stripState );
+       
                # Signatures
                $sigText = $this->getUserSig( $user );
-               $text = strtr( $text, array( 
+               $text = strtr( $text, array(
                        '~~~~~' => $d,
                        '~~~~' => "$sigText $d",
                        '~~~' => $sigText
@@ -3272,11 +3552,9 @@ class Parser
         *
         * @param User $user
         * @return string
-        * @access private
+        * @private
         */
        function getUserSig( &$user ) {
-               global $wgContLang;
-
                $username = $user->getName();
                $nickname = $user->getOption( 'nickname' );
                $nickname = $nickname === '' ? $username : $nickname;
@@ -3285,7 +3563,7 @@ class Parser
                        # Sig. might contain markup; validate this
                        if( $this->validateSig( $nickname ) !== false ) {
                                # Validated; clean up (if needed) and return it
-                               return( $this->cleanSig( $nickname ) );
+                               return $this->cleanSig( $nickname, true );
                        } else {
                                # Failed to validate; fall back to the default
                                $nickname = $username;
@@ -3315,9 +3593,13 @@ class Parser
         * 2) Substitute all transclusions
         *
         * @param string $text
+        * @param $parsing Whether we're cleaning (preferences save) or parsing
         * @return string Signature text
         */
-       function cleanSig( $text ) {
+       function cleanSig( $text, $parsing = false ) {
+               global $wgTitle;
+               $this->startExternalParse( $wgTitle, new ParserOptions(), $parsing ? OT_WIKI : OT_MSG );
+       
                $substWord = MagicWord::get( MAG_SUBST );
                $substRegex = '/\{\{(?!(?:' . $substWord->getBaseRegex() . '))/x' . $substWord->getRegexCase();
                $substText = '{{' . $substWord->getSynonym( 0 );
@@ -3325,14 +3607,15 @@ class Parser
                $text = preg_replace( $substRegex, $substText, $text );
                $text = preg_replace( '/~{3,5}/', '', $text );
                $text = $this->replaceVariables( $text );
-       
+               
+               $this->clearState();    
                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
-        * @access public
+        * @public
         */
        function startExternalParse( &$title, $options, $outputType, $clearState = true ) {
                $this->mTitle =& $title;
@@ -3349,7 +3632,7 @@ class Parser
         * @param string $text the text to transform
         * @param ParserOptions $options  options
         * @return string the text with variables substituted
-        * @access public
+        * @public
         */
        function transformMsg( $text, $options ) {
                global $wgTitle;
@@ -3378,10 +3661,13 @@ class Parser
 
        /**
         * Create an HTML-style tag, e.g. <yourtag>special text</yourtag>
-        * Callback will be called with the text within
-        * Transform and return the text within
+        * The callback should have the following form:
+        *    function myParserHook( $text, $params, &$parser ) { ... }
+        *
+        * Transform and return $text. Use $parser for any required context, e.g. use
+        * $parser->getTitle() and $parser->getOptions() not $wgTitle or $wgOut->mParserOptions
         *
-        * @access public
+        * @public
         *
         * @param mixed $tag The tag to use, e.g. 'hook' for <hook>
         * @param mixed $callback The callback function (and object) to use for the tag
@@ -3395,6 +3681,35 @@ class Parser
                return $oldVal;
        }
 
+       /**
+        * Create a function, e.g. {{sum:1|2|3}}
+        * The callback function should have the form:
+        *    function myParserFunction( &$parser, $arg1, $arg2, $arg3 ) { ... }
+        *
+        * The callback may either return the text result of the function, or an array with the text 
+        * in element 0, and a number of flags in the other elements. The names of the flags are 
+        * specified in the keys. Valid flags are:
+        *   found                     The text returned is valid, stop processing the template. This 
+        *                             is on by default.
+        *   nowiki                    Wiki markup in the return value should be escaped
+        *   noparse                   Unsafe HTML tags should not be stripped, etc.
+        *   noargs                    Don't replace triple-brace arguments in the return value
+        *   isHTML                    The returned text is HTML, armour it against wikitext transformation
+        *
+        * @public
+        *
+        * @param string $name The function name. Function names are case-insensitive.
+        * @param mixed $callback The callback function (and object) to use
+        *
+        * @return The old callback function for this name, if any
+        */
+       function setFunctionHook( $name, $callback ) {
+               $name = strtolower( $name );
+               $oldVal = @$this->mFunctionHooks[$name];
+               $this->mFunctionHooks[$name] = $callback;
+               return $oldVal;
+       }
+
        /**
         * Replace <!--LINK--> link placeholders with actual links, in the buffer
         * Placeholders created in Skin::makeLinkObj()
@@ -3563,9 +3878,6 @@ class Parser
         * @return string
         */
        function replaceLinkHoldersText( $text ) {
-               global $wgUser;
-               global $wgOutputReplace;
-
                $fname = 'Parser::replaceLinkHoldersText';
                wfProfileIn( $fname );
 
@@ -3581,7 +3893,7 @@ class Parser
        /**
         * @param array $matches
         * @return string
-        * @access private
+        * @private
         */
        function replaceLinkHoldersTextCallback( $matches ) {
                $type = $matches[1];
@@ -3649,7 +3961,7 @@ class Parser
         * Parse image options text and use it to make an image
         */
        function makeImage( &$nt, $options ) {
-               global $wgContLang, $wgUseImageResize, $wgUser;
+               global $wgUseImageResize;
 
                $align = '';
 
@@ -3714,6 +4026,11 @@ class Parser
                }
                # Strip bad stuff out of the alt text
                $alt = $this->replaceLinkHoldersText( $caption );
+
+               # make sure there are no placeholders in thumbnail attributes
+               # that are later expanded to html- so expand them now and
+               # remove the tags
+               $alt = $this->unstrip($alt, $this->mStripState); 
                $alt = Sanitizer::stripAllTags( $alt );
 
                # Linker does the rest
@@ -3729,13 +4046,13 @@ class Parser
                $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
+        * @private
         */
        function attributeStripCallback( &$text, $args ) {
                $text = $this->replaceVariables( $text, $args );
@@ -3775,13 +4092,16 @@ class ParserOutput
                $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.
+               $mCacheTime,        # Time when this object was generated, 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
+               $mExternalLinks,    # External link URLs, in the key only
+               $mHTMLtitle,            # Display HTML title
+               $mSubtitle,                     # Additional subtitle
+               $mNewSection;           # Show a new section link?
 
        function ParserOutput( $text = '', $languageLinks = array(), $categoryLinks = array(),
                $containsOldMagic = false, $titletext = '' )
@@ -3797,10 +4117,13 @@ class ParserOutput
                $this->mTemplates = array();
                $this->mImages = array();
                $this->mExternalLinks = array();
+               $this->mHTMLtitle = "" ;
+               $this->mSubtitle = "" ;
+               $this->mNewSection = false;
        }
 
        function getText()                   { return $this->mText; }
-       function getLanguageLinks()          { return $this->mLanguageLinks; }
+       function &getLanguageLinks()          { return $this->mLanguageLinks; }
        function getCategoryLinks()          { return array_keys( $this->mCategories ); }
        function &getCategories()            { return $this->mCategories; }
        function getCacheTime()              { return $this->mCacheTime; }
@@ -3822,6 +4145,13 @@ class ParserOutput
        function addImage( $name )           { $this->mImages[$name] = 1; }
        function addLanguageLink( $t )       { $this->mLanguageLinks[] = $t; }
        function addExternalLink( $url )     { $this->mExternalLinks[$url] = 1; }
+       
+       function setNewSection( $value ) {
+               $this->mNewSection = (bool)$value;
+       }
+       function getNewSection() {
+               return (bool)$this->mNewSection;
+       }
 
        function addLink( $title, $id ) {
                $ns = $title->getNamespace();
@@ -3841,16 +4171,6 @@ class ParserOutput
                $this->mTemplates[$ns][$dbk] = $id;
        }
 
-       /**
-        * @deprecated
-        */
-       /*
-       function merge( $other ) {
-               $this->mLanguageLinks = array_merge( $this->mLanguageLinks, $other->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
         * per-article cache invalidation timestamps, or if it comes from
@@ -3858,7 +4178,7 @@ class ParserOutput
         *
         * @param string $touched the affected article's last touched timestamp
         * @return bool
-        * @access public
+        * @public
         */
        function expired( $touched ) {
                global $wgCacheEpoch;
@@ -3888,7 +4208,8 @@ 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
+       var $mTidy;                      # Ask for tidy cleanup
+       var $mInterfaceMessage;          # Which lang to call for PLURAL and GRAMMAR
 
        function getUseTeX()                        { return $this->mUseTeX; }
        function getUseDynamicDates()               { return $this->mUseDynamicDates; }
@@ -3900,7 +4221,8 @@ class ParserOptions
        function getEditSection()                   { return $this->mEditSection; }
        function getNumberHeadings()                { return $this->mNumberHeadings; }
        function getAllowSpecialInclusion()         { return $this->mAllowSpecialInclusion; }
-       function getTidy()                          { return $this->mTidy; }
+       function getTidy()                          { return $this->mTidy; }
+       function getInterfaceMessage()              { return $this->mInterfaceMessage; }
 
        function setUseTeX( $x )                    { return wfSetVar( $this->mUseTeX, $x ); }
        function setUseDynamicDates( $x )           { return wfSetVar( $this->mUseDynamicDates, $x ); }
@@ -3911,8 +4233,9 @@ 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 setTidy( $x )                      { return wfSetVar( $this->mTidy, $x); }
        function setSkin( &$x ) { $this->mSkin =& $x; }
+       function setInterfaceMessage( $x )          { return wfSetVar( $this->mInterfaceMessage, $x); }
 
        function ParserOptions() {
                global $wgUser;
@@ -3931,8 +4254,8 @@ class ParserOptions
 
        /** Get user options */
        function initialiseFromUser( &$userInput ) {
-               global $wgUseTeX, $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages,
-                      $wgAllowExternalImagesFrom, $wgAllowSpecialInclusion;
+               global $wgUseTeX, $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
+               global $wgAllowExternalImagesFrom, $wgAllowSpecialInclusion;
                $fname = 'ParserOptions::initialiseFromUser';
                wfProfileIn( $fname );
                if ( !$userInput ) {
@@ -3955,6 +4278,7 @@ class ParserOptions
                $this->mNumberHeadings = $user->getOption( 'numberheadings' );
                $this->mAllowSpecialInclusion = $wgAllowSpecialInclusion;
                $this->mTidy = false;
+               $this->mInterfaceMessage = false;
                wfProfileOut( $fname );
        }
 }
@@ -3982,19 +4306,31 @@ function wfNumberOfArticles() {
  * Return the number of files
  */
 function wfNumberOfFiles() {
-       $fname = 'Parser::wfNumberOfFiles';
+       $fname = 'wfNumberOfFiles';
 
        wfProfileIn( $fname );
        $dbr =& wfGetDB( DB_SLAVE );
-       $res = $dbr->selectField('image', 'COUNT(*)', array(), $fname );
+       $numImages = $dbr->selectField('site_stats', 'ss_images', array(), $fname );
        wfProfileOut( $fname );
 
-       return $res;
+       return $numImages;
+}
+
+/**
+ * Return the number of user accounts
+ * @return integer
+ */
+function wfNumberOfUsers() {
+       wfProfileIn( 'wfNumberOfUsers' );
+       $dbr =& wfGetDB( DB_SLAVE );
+       $count = $dbr->selectField( 'site_stats', 'ss_users', array(), 'wfNumberOfUsers' );
+       wfProfileOut( 'wfNumberOfUsers' );
+       return (int)$count;
 }
 
 /**
  * Get various statistics from the database
- * @access private
+ * @private
  */
 function wfLoadSiteStats() {
        global $wgNumberOfArticles, $wgTotalViews, $wgTotalEdits;
@@ -4020,7 +4356,7 @@ function wfLoadSiteStats() {
  * Escape html tags
  * Basically replacing " > and < with HTML entities ( &quot;, &gt;, &lt;)
  *
- * @param string $in Text that might contain HTML tags
+ * @param $in String: text that might contain HTML tags.
  * @return string Escaped string
  */
 function wfEscapeHTMLTagsOnly( $in ) {