performance tweak related to title conversion
[lhc/web/wiklou.git] / includes / Parser.php
index f30ad68..4b79b83 100644 (file)
@@ -1,7 +1,5 @@
 <?php
 
-// require_once('Tokenizer.php');
-
 /**
  * File for Parser and related classes
  *
@@ -18,6 +16,7 @@
  * Hence, we limit the number of inclusions of any given page, thus bringing any
  * attack back to O(N).
  */
+
 define( 'MAX_INCLUDE_REPEAT', 100 );
 define( 'MAX_INCLUDE_SIZE', 1000000 ); // 1 Million
 
@@ -39,8 +38,7 @@ define( 'UNIQ_PREFIX', 'NaodW29');
 define( 'URL_PROTOCOLS', 'http|https|ftp|irc|gopher|news|mailto' );
 define( 'HTTP_PROTOCOLS', 'http|https' );
 # Everything except bracket, space, or control characters
-define( 'EXT_LINK_URL_CLASS', '[^]\\x00-\\x20\\x7F]' );
-define( 'INVERSE_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]' );
@@ -139,7 +137,7 @@ class Parser
         * @return ParserOutput a ParserOutput
         */
        function parse( $text, &$title, $options, $linestart = true, $clearState = true ) {
-               global $wgUseTidy;
+               global $wgUseTidy, $wgContLang;
                $fname = 'Parser::parse';
                wfProfileIn( $fname );
 
@@ -153,6 +151,7 @@ class Parser
 
                $stripState = NULL;
                $text = $this->strip( $text, $this->mStripState );
+
                $text = $this->internalParse( $text, $linestart );
                $text = $this->unstrip( $text, $this->mStripState );
                # Clean up special characters, only run once, next-to-last before doBlockLevels
@@ -185,10 +184,10 @@ class Parser
                }
                # only once and last
                $text = $this->doBlockLevels( $text, $linestart );
+
+               $text = $wgContLang->convert($text);
+
                $text = $this->unstripNoWiki( $text, $this->mStripState );
-               if($wgUseTidy) {
-                       $text = $this->tidy($text);
-               }
                $this->mOutput->setText( $text );
                wfProfileOut( $fname );
                return $this->mOutput;
@@ -485,7 +484,8 @@ class Parser
        /**
         * interface with html tidy, used if $wgUseTidy = true
         *
-        * @access private
+        * @access public
+        * @static
         */
        function tidy ( $text ) {
                global $wgTidyConf, $wgTidyBin, $wgTidyOpts;
@@ -494,15 +494,16 @@ class Parser
                wfProfileIn( $fname );
 
                $cleansource = '';
+               $opts = '';
                switch(strtoupper($wgOutputEncoding)) {
                        case 'ISO-8859-1':
-                               $wgTidyOpts .= ($wgInputEncoding == $wgOutputEncoding)? ' -latin1':' -raw';
+                               $opts .= ($wgInputEncoding == $wgOutputEncoding)? ' -latin1':' -raw';
                                break;
                        case 'UTF-8':
-                               $wgTidyOpts .= ($wgInputEncoding == $wgOutputEncoding)? ' -utf8':' -raw';
+                               $opts .= ($wgInputEncoding == $wgOutputEncoding)? ' -utf8':' -raw';
                                break;
                        default:
-                               $wgTidyOpts .= ' -raw';
+                               $opts .= ' -raw';
                        }
 
                $wrappedtext = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'.
@@ -513,7 +514,7 @@ class Parser
                        1 => array('pipe', 'w'),
                        2 => array('file', '/dev/null', 'a')
                );
-               $process = proc_open("$wgTidyBin -config $wgTidyConf $wgTidyOpts", $descriptorspec, $pipes);
+               $process = proc_open("$wgTidyBin -config $wgTidyConf $wgTidyOpts$opts", $descriptorspec, $pipes);
                if (is_resource($process)) {
                        fwrite($pipes[0], $wrappedtext);
                        fclose($pipes[0]);
@@ -587,6 +588,7 @@ class Parser
                                array_push ( $ltr , $this->fixTagAttributes ( $x ) ) ;
                        }
                        else if ( '|' == $fc || '!' == $fc || '|+' == substr ( $x , 0 , 2 ) ) { # Caption
+                               # $x is a table row
                                if ( '|+' == substr ( $x , 0 , 2 ) ) {
                                        $fc = '+' ;
                                        $x = substr ( $x , 1 ) ;
@@ -595,6 +597,8 @@ class Parser
                                if ( $fc == '!' ) $after = str_replace ( '!!' , '||' , $after ) ;
                                $after = explode ( '||' , $after ) ;
                                $t[$k] = '' ;
+
+                               # Loop through each table cell
                                foreach ( $after AS $theline )
                                {
                                        $z = '' ;
@@ -613,8 +617,16 @@ class Parser
                                        else if ( $fc == '+' ) $l = 'caption' ;
                                        else $l = '' ;
                                        array_push ( $ltd , $l ) ;
+
+                                       # Cell parameters
                                        $y = explode ( '|' , $theline , 2 ) ;
-                                       if ( count ( $y ) == 1 ) $y = "{$z}<{$l}>{$y[0]}" ;
+                                       # Note that a '|' inside an invalid link should not
+                                       # be mistaken as delimiting cell parameters
+                                       if ( strpos( $y[0], '[[' ) !== false ) {
+                                               $y = array ($theline);
+                                       }
+                                       if ( count ( $y ) == 1 )
+                                               $y = "{$z}<{$l}>{$y[0]}" ;
                                        else $y = $y = "{$z}<{$l} ".$this->fixTagAttributes($y[0]).">{$y[1]}" ;
                                        $t[$k] .= $y ;
                                        array_push ( $td , true ) ;
@@ -643,7 +655,7 @@ class Parser
         * @access private
         */
        function internalParse( $text, $linestart, $args = array(), $isMain=true ) {
-        global $wgLang;
+        global $wgContLang;
 
                $fname = 'Parser::internalParse';
                wfProfileIn( $fname );
@@ -651,8 +663,6 @@ class Parser
                $text = $this->removeHTMLtags( $text );
                $text = $this->replaceVariables( $text, $args );
 
-               $text = $wgLang->convert($text);
-
                $text = preg_replace( '/(^|\n)-----*/', '\\1<hr />', $text );
 
                $text = $this->doHeadings( $text );
@@ -661,15 +671,9 @@ class Parser
                        $text = $wgDateFormatter->reformat( $this->mOptions->getDateFormat(), $text );
                }
                $text = $this->doAllQuotes( $text );
+               $text = $this->replaceInternalLinks ( $text );
                $text = $this->replaceExternalLinks( $text );
                $text = $this->doMagicLinks( $text );
-               $text = $this->replaceInternalLinks ( $text );
-               # Another call to replace links and images inside captions of images
-               $text = $this->replaceInternalLinks ( $text );
-
-               $text = $this->unstrip( $text, $this->mStripState );
-               $text = $this->unstripNoWiki( $text, $this->mStripState );
-
                $text = $this->doTableStuff( $text );
                $text = $this->formatHeadings( $text, $isMain );
                $sk =& $this->mOptions->getSkin();
@@ -922,7 +926,7 @@ class Parser
                wfProfileIn( $fname );
 
                $sk =& $this->mOptions->getSkin();
-               $linktrail = wfMsg('linktrail');
+               $linktrail = wfMsgForContent('linktrail');
                $bits = preg_split( EXT_LINK_BRACKETED, $text, -1, PREG_SPLIT_DELIM_CAPTURE );
 
                $s = $this->replaceFreeExternalLinks( array_shift( $bits ) );
@@ -978,13 +982,11 @@ class Parser
                        # replacing any non-bracketed links
                        $trail = $this->replaceFreeExternalLinks( $trail );
 
-                       $la = $sk->getExternalLinkAttributes( $url, $text );
-
                        # 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 .= "<a href=\"{$url}\" {$la}>{$text}</a>{$dtrail}{$paren}{$trail}";
+                       $s .= $sk->makeExternalLink( $url, $text, false ) . $dtrail. $paren . $trail;
                }
 
                wfProfileOut( $fname );
@@ -1062,10 +1064,13 @@ class Parser
         *
         * @access private
         */
+
        function replaceInternalLinks( $s ) {
-               global $wgLang, $wgLinkCache;
-               global $wgNamespacesWithSubpages;
+               global $wgLang, $wgContLang, $wgLinkCache;
                static $fname = 'Parser::replaceInternalLinks' ;
+               # use a counter to prevent too much unknown links from
+               # being checked for different language variants.
+               static $convertCount;
                wfProfileIn( $fname );
 
                wfProfileIn( $fname.'-setup' );
@@ -1076,19 +1081,23 @@ class Parser
 
                $redirect = MagicWord::get ( MAG_REDIRECT ) ;
 
+               #split the entire text string on occurences of [[
                $a = explode( '[[', ' ' . $s );
+               #get the first element (all text up to first [[), and remove the space we added
                $s = array_shift( $a );
                $s = substr( $s, 1 );
 
                # Match a link having the form [[namespace:link|alternate]]trail
                static $e1 = FALSE;
                if ( !$e1 ) { $e1 = "/^([{$tc}]+)(?:\\|([^]]+))?]](.*)\$/sD"; }
+               # Match cases where there is no "]]", which might still be images
+               static $e1_img = FALSE;
+               if ( !$e1_img ) { $e1_img = "/^([{$tc}]+)\\|(.*)\$/sD"; }
                # Match the end of a line for a word that's not followed by whitespace,
                # e.g. in the case of 'The Arab al[[Razi]]', 'al' will be matched
                static $e2 = '/^(.*?)([a-zA-Z\x80-\xff]+)$/sD';
 
-               $useLinkPrefixExtension = $wgLang->linkPrefixExtension();
-               # Special and Media are pseudo-namespaces; no pages actually exist in them
+               $useLinkPrefixExtension = $wgContLang->linkPrefixExtension();
 
                $nottalk = !Namespace::isTalk( $this->mTitle->getNamespace() );
 
@@ -1105,8 +1114,9 @@ class Parser
 
                wfProfileOut( $fname.'-setup' );
 
-               # start procedeeding each line
-               foreach ( $a as $line ) {
+               # Loop for each link
+               for ($k = 0; isset( $a[$k] ); $k++) {
+                       $line = $a[$k];
                        wfProfileIn( $fname.'-prefixhandling' );
                        if ( $useLinkPrefixExtension ) {
                                if ( preg_match( $e2, $s, $m ) ) {
@@ -1123,72 +1133,119 @@ class Parser
                        }
                        wfProfileOut( $fname.'-prefixhandling' );
 
+                       $might_be_img = false;
+                       
                        if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
                                $text = $m[2];
                                # fix up urlencoded title texts
                                if(preg_match('/%/', $m[1] )) $m[1] = 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;
+                               $text = $m[2];
+                               if(preg_match('/%/', $m[1] )) $m[1] = urldecode($m[1]);
+                               $trail = "";
                        } else { # Invalid form; output directly
                                $s .= $prefix . '[[' . $line ;
                                continue;
                        }
 
-                       # Valid link forms:
-                       # Foobar -- normal
-                       # :Foobar -- override special treatment of prefix (images, language links)
-                       # /Foobar -- convert to CurrentPage/Foobar
-                       # /Foobar/ -- convert to CurrentPage/Foobar, strip the initial / from text
-                       
-                       # Look at the first character
-                       $c = substr($m[1],0,1);
-                       $noforce = ($c != ':');
-                       
-                       # subpage
-                       if( $c == '/' ) {
-                               # / at end means we don't want the slash to be shown
-                               if(substr($m[1],-1,1)=='/') {
-                                       $m[1]=substr($m[1],1,strlen($m[1])-2);
-                                       $noslash=$m[1];
-                               } else {
-                                       $noslash=substr($m[1],1);
-                               }
-                               
-                               # Some namespaces don't allow subpages
-                               if(!empty($wgNamespacesWithSubpages[$this->mTitle->getNamespace()])) {
-                                       # subpages allowed here
-                                       $link = $this->mTitle->getPrefixedText(). '/' . trim($noslash);
-                                       if( '' == $text ) {
-                                               $text= $m[1];
-                                       } # this might be changed for ugliness reasons
-                               } else {
-                                       # no subpage allowed, use standard link
-                                       $link = $noslash;
-                               }
-                               
-                       } elseif( $noforce ) { # no subpage
-                               $link = $m[1];
-                       } else {
-                               # We don't want to keep the first character
-                               $link = substr( $m[1], 1 );
+                       # Don't allow internal links to pages containing
+                       # PROTO: where PROTO is a valid URL protocol; these
+                       # should be external links.
+                       if (preg_match('/((?:'.URL_PROTOCOLS.'):)/', $m[1])) {
+                               $s .= $prefix . '[[' . $line ;
+                               continue;
                        }
-                       
-                       $wasblank = ( '' == $text );
-                       if( $wasblank ) $text = $link;
 
+                       # Make subpage if necessary
+                       $link = $this->maybeDoSubpageLink( $m[1], $text );
+
+                       $noforce = (substr($m[1], 0, 1) != ':');
+                       if (!$noforce) {
+                               # Strip off leading ':'
+                               $link = substr($link, 1);
+                       }
+                       
                        $nt = Title::newFromText( $link );
                        if( !$nt ) {
                                $s .= $prefix . '[[' . $line;
                                continue;
                        }
-                       
+
+                       //check other language variants of the link
+                       //if the article does not exist
+                       global $wgContLang;
+                       $variants = $wgContLang->getVariants();
+
+                       if(sizeof($variants) > 1 && $convertCount < 200) {
+                               $varnt = false; 
+                               if($nt->getArticleID() == 0) {
+                                       foreach ( $variants as $v ) {
+                                               if($v == $wgContLang->getPreferredVariant())
+                                                       continue;
+                                               $convertCount ++;
+                                               $varlink = $wgContLang->autoConvert($link, $v);
+                                               $varnt = Title::newFromText($varlink);
+                                               if($varnt && $varnt->getArticleID()>0) {
+                                                       break;
+                                               }
+                                       }
+                               }
+                               if($varnt && $varnt->getArticleID()>0) {
+                                       $nt = $varnt;
+                                       $link = $varlink;
+                               }
+                       }
+
                        $ns = $nt->getNamespace();
                        $iw = $nt->getInterWiki();
                        
+                       if ($might_be_img) { # if this is actually an invalid link
+                               if ($ns == NS_IMAGE && $noforce) { #but might be an image
+                                       $found = false;
+                                       while (isset ($a[$k+1]) ) {
+                                               #look at the next 'line' to see if we can close it there
+                                               $next_line =  array_shift(array_splice( $a, $k + 1, 1) );
+                                               if( preg_match("/^(.*?]].*?)]](.*)$/sD", $next_line, $m) ) {
+                                               # the first ]] closes the inner link, the second the image
+                                                       $found = true;
+                                                       $text .= '[[' . $m[1];
+                                                       $trail = $m[2];
+                                                       break;
+                                               } elseif( preg_match("/^.*?]].*$/sD", $next_line, $m) ) {
+                                                       #if there's exactly one ]] that's fine, we'll keep looking
+                                                       $text .= '[[' . $m[0];
+                                               } else {
+                                                       #if $next_line is invalid too, we need look no further
+                                                       $text .= '[[' . $next_line;
+                                                       break;
+                                               }
+                                       }
+                                       if ( !$found ) {
+                                               # we couldn't find the end of this imageLink, so output it raw
+                                               #but don't ignore what might be perfectly normal links in the text we've examined
+                                               $text = $this->replaceInternalLinks($text);
+                                               $s .= $prefix . '[[' . $link . '|' . $text;
+                                               # note: no $trail, because without an end, there *is* no trail
+                                               continue;
+                                       }
+                               } else { #it's not an image, so output it raw
+                                       $s .= $prefix . '[[' . $link . '|' . $text;
+                                       # note: no $trail, because without an end, there *is* no trail
+                                       continue;
+                               }
+                       }
+
+                       $wasblank = ( '' == $text );
+                       if( $wasblank ) $text = $link;
+
+                       
                        # Link not escaped by : , create the various objects
                        if( $noforce ) {
 
                                # Interwikis
-                               if( $iw && $this->mOptions->getInterwikiMagic() && $nottalk && $wgLang->getLanguageName( $iw ) ) {
+                               if( $iw && $this->mOptions->getInterwikiMagic() && $nottalk && $wgContLang->getLanguageName( $iw ) ) {
                                        array_push( $this->mOutput->mLanguageLinks, $nt->getFullText() );
                                        $tmp = $prefix . $trail ;
                                        $s .= (trim($tmp) == '')? '': $tmp;
@@ -1196,19 +1253,25 @@ class Parser
                                }
                                
                                if ( $ns == NS_IMAGE ) {
-                                       $s .= $prefix . $sk->makeImageLinkObj( $nt, $text ) . $trail;
+                                       # recursively parse links inside the image caption
+                                       # actually, this will parse them in any other parameters, too,
+                                       # but it might be hard to fix that, and it doesn't matter ATM
+                                       $text = $this->replaceExternalLinks($text);
+                                       $text = $this->replaceInternalLinks($text);
+                                       
+                                       # replace the image with a link-holder so that replaceExternalLinks() can't mess with it
+                                       $s .= $prefix . $this->insertStripItem( $sk->makeImageLinkObj( $nt, $text ), $this->mStripState ) . $trail;
                                        $wgLinkCache->addImageLinkObj( $nt );
                                        continue;
                                }
                                
                                if ( $ns == NS_CATEGORY ) {
                                        $t = $nt->getText() ;
-                                       $nnt = Title::newFromText ( Namespace::getCanonicalName(NS_CATEGORY).':'.$t ) ;
 
                                        $wgLinkCache->suspend(); # Don't save in links/brokenlinks
                                        $pPLC=$sk->postParseLinkColour();
                                        $sk->postParseLinkColour( false );
-                                       $t = $sk->makeLinkObj( $nnt, $t, '', '' , $prefix );
+                                       $t = $sk->makeLinkObj( $nt, $t, '', '' , $prefix );
                                        $sk->postParseLinkColour( $pPLC );
                                        $wgLinkCache->resume();
 
@@ -1227,7 +1290,9 @@ class Parser
                                        continue;
                                }
                        }
-                       
+
+            $text = $wgContLang->convert($text);                       
+
                        if( ( $nt->getPrefixedText() === $this->mTitle->getPrefixedText() ) &&
                            ( strpos( $link, '#' ) === FALSE ) ) {
                                # Self-links are handled specially; generally de-link and change to bold.
@@ -1235,6 +1300,7 @@ class Parser
                                continue;
                        }
 
+                       # Special and Media are pseudo-namespaces; no pages actually exist in them
                        if( $ns == NS_MEDIA ) {
                                $s .= $prefix . $sk->makeMediaLinkObj( $nt, $text ) . $trail;
                                $wgLinkCache->addImageLinkObj( $nt );
@@ -1249,6 +1315,53 @@ class Parser
                return $s;
        }
 
+       /**
+        * Handle link to subpage if necessary
+        * @param $target string the source of the link
+        * @param &$text the link text, modified as necessary
+        * @return string the full name of the link
+        * @access private
+        */
+       function maybeDoSubpageLink($target, &$text) {
+               # Valid link forms:
+               # Foobar -- normal
+               # :Foobar -- override special treatment of prefix (images, language links)
+               # /Foobar -- convert to CurrentPage/Foobar
+               # /Foobar/ -- convert to CurrentPage/Foobar, strip the initial / from text
+               global $wgNamespacesWithSubpages;
+
+               $fname = 'Parser::maybeDoSubpageLink';
+               wfProfileIn( $fname );
+               # Look at the first character
+               if( $target{0} == '/' ) {
+                       # / at end means we don't want the slash to be shown
+                       if(substr($target,-1,1)=='/') {
+                               $target=substr($target,1,-1);
+                               $noslash=$target;
+                       } else {
+                               $noslash=substr($target,1);
+                       }
+                               
+                       # Some namespaces don't allow subpages
+                       if(!empty($wgNamespacesWithSubpages[$this->mTitle->getNamespace()])) {
+                               # subpages allowed here
+                               $ret = $this->mTitle->getPrefixedText(). '/' . trim($noslash);
+                               if( '' === $text ) {
+                                       $text = $target;
+                               } # this might be changed for ugliness reasons
+                       } else {
+                               # no subpage allowed, use standard link
+                               $ret = $target;
+                       }
+               } else {
+                       # no subpage
+                       $ret = $target;
+               }
+
+               wfProfileOut( $fname );
+               return $ret;
+       }
+
        /**#@+
         * Used by doBlockLevels()
         * @access private
@@ -1380,11 +1493,9 @@ class Parser
                                        # ; title : definition text
                                        # So we check for : in the remainder text to split up the
                                        # title and definition, without b0rking links.
-                                       # FIXME: This is not foolproof. Something better in Tokenizer might help.
-                                       if( preg_match( '/^(.*?(?:\s|&nbsp;)):(.*)$/', $t, $match ) ) {
-                                               $term = $match[1];
+                                       if ($this->findColonNoLinks($t, $term, $t2) !== false) {
+                                               $t = $t2;
                                                $output .= $term . $this->nextItem( ':' );
-                                               $t = $match[2];
                                        }
                                }
                        } elseif( $prefixLength || $lastPrefixLength ) {
@@ -1405,10 +1516,9 @@ class Parser
 
                                        if ( ';' == $char ) {
                                                # FIXME: This is dupe of code above
-                                               if( preg_match( '/^(.*?(?:\s|&nbsp;)):(.*)$/', $t, $match ) ) {
-                                                       $term = $match[1];
+                                               if ($this->findColonNoLinks($t, $term, $t2) !== false) {
+                                                       $t = $t2;
                                                        $output .= $term . $this->nextItem( ':' );
-                                                       $t = $match[2];
                                                }
                                        }
                                        ++$commonPrefixLength;
@@ -1489,38 +1599,78 @@ class Parser
                return $output;
        }
 
+       /**
+        * Split up a string on ':', ignoring any occurences inside
+        * <a>..</a> or <span>...</span>
+        * @param $str string the string to split
+        * @param &$before string set to everything before the ':'
+        * @param &$after string set to everything after the ':'
+        * return string the position of the ':', or false if none found
+        */
+       function findColonNoLinks($str, &$before, &$after) {
+               # I wonder if we should make this count all tags, not just <a>
+               # and <span>. That would prevent us from matching a ':' that
+               # comes in the middle of italics other such formatting....
+               # -- Wil
+               $fname = 'Parser::findColonNoLinks';
+               wfProfileIn( $fname );
+               $pos = 0;
+               do {
+                       $colon = strpos($str, ':', $pos);
+
+                       if ($colon !== false) {
+                               $before = substr($str, 0, $colon);
+                               $after = substr($str, $colon + 1);
+
+                               # Skip any ':' within <a> or <span> pairs
+                               $a = substr_count($before, '<a');
+                               $s = substr_count($before, '<span');
+                               $ca = substr_count($before, '</a>');
+                               $cs = substr_count($before, '</span>');
+
+                               if ($a <= $ca and $s <= $cs) {
+                                       # Tags are balanced before ':'; ok
+                                       break;
+                               }
+                               $pos = $colon + 1;
+                       }
+               } while ($colon !== false);
+               wfProfileOut( $fname );
+               return $colon;
+       }
+
        /**
         * Return value of a magic variable (like PAGENAME)
         *
         * @access private
         */
        function getVariableValue( $index ) {
-               global $wgLang, $wgSitename, $wgServer;
+               global $wgContLang, $wgSitename, $wgServer;
 
                switch ( $index ) {
                        case MAG_CURRENTMONTH:
-                               return $wgLang->formatNum( date( 'm' ) );
+                               return $wgContLang->formatNum( date( 'm' ) );
                        case MAG_CURRENTMONTHNAME:
-                               return $wgLang->getMonthName( date('n') );
+                               return $wgContLang->getMonthName( date('n') );
                        case MAG_CURRENTMONTHNAMEGEN:
-                               return $wgLang->getMonthNameGen( date('n') );
+                               return $wgContLang->getMonthNameGen( date('n') );
                        case MAG_CURRENTDAY:
-                               return $wgLang->formatNum( date('j') );
+                               return $wgContLang->formatNum( date('j') );
                        case MAG_PAGENAME:
                                return $this->mTitle->getText();
                        case MAG_PAGENAMEE:
                                return $this->mTitle->getPartialURL();
                        case MAG_NAMESPACE:
                                # return Namespace::getCanonicalName($this->mTitle->getNamespace());
-                               return $wgLang->getNsText($this->mTitle->getNamespace()); # Patch by Dori
+                               return $wgContLang->getNsText($this->mTitle->getNamespace()); # Patch by Dori
                        case MAG_CURRENTDAYNAME:
-                               return $wgLang->getWeekdayName( date('w')+1 );
+                               return $wgContLang->getWeekdayName( date('w')+1 );
                        case MAG_CURRENTYEAR:
-                               return $wgLang->formatNum( date( 'Y' ) );
+                               return $wgContLang->formatNum( date( 'Y' ) );
                        case MAG_CURRENTTIME:
-                               return $wgLang->time( wfTimestampNow(), false );
+                               return $wgContLang->time( wfTimestampNow(), false );
                        case MAG_NUMBEROFARTICLES:
-                               return $wgLang->formatNum( wfNumberOfArticles() );
+                               return $wgContLang->formatNum( wfNumberOfArticles() );
                        case MAG_SITENAME:
                                return $wgSitename;
                        case MAG_SERVER:
@@ -1536,12 +1686,15 @@ class Parser
         * @access private
         */
        function initialiseVariables() {
+               $fname = 'Parser::initialiseVariables';
+               wfProfileIn( $fname );
                global $wgVariableIDs;
                $this->mVariables = array();
                foreach ( $wgVariableIDs as $id ) {
                        $mw =& MagicWord::get( $id );
                        $mw->addToArray( $this->mVariables, $this->getVariableValue( $id ) );
                }
+               wfProfileOut( $fname );
        }
 
        /**
@@ -1579,12 +1732,12 @@ class Parser
                # Variable substitution
                $text = preg_replace_callback( "/{{([$titleChars]*?)}}/", 'wfVariableSubstitution', $text );
                
-               if ( $this->mOutputType == OT_HTML ) {
+               if ( $this->mOutputType == OT_HTML || $this->mOutputType == OT_WIKI ) {
                        # Argument substitution
                        $text = preg_replace_callback( "/{{{([$titleChars]*?)}}}/", 'wfArgSubstitution', $text );
                }
                # Template substitution
-               $regex = '/{{(['.$titleChars.']*)(\\|.*?|)}}/s';
+               $regex = '/(\\n|{)?{{(['.$titleChars.']*)(\\|.*?|)}}/s';
                $text = preg_replace_callback( $regex, 'wfBraceSubstitution', $text );
 
                array_pop( $this->mArgStack );
@@ -1656,26 +1809,30 @@ class Parser
         * @access private
         */
        function braceSubstitution( $matches ) {
-               global $wgLinkCache, $wgLang;
+               global $wgLinkCache, $wgContLang;
                $fname = 'Parser::braceSubstitution';
                $found = false;
                $nowiki = false;
                $noparse = false;
-               $itcamefromthedatabase = false;
 
                $title = NULL;
 
+               # Need to know if the template comes at the start of a line,
+               # to treat the beginning of the template like the beginning
+               # of a line for tables and block-level elements.
+               $linestart = $matches[1];
+
                # $part1 is the bit before the first |, and must contain only title characters
                # $args is a list of arguments, starting from index 0, not including $part1
 
-               $part1 = $matches[1];
-               # If the second subpattern matched anything, it will start with |
+               $part1 = $matches[2];
+               # If the third subpattern matched anything, it will start with |
 
-               $args = $this->getTemplateArgs($matches[2]);
+               $args = $this->getTemplateArgs($matches[3]);
                $argc = count( $args );
 
-               # {{{}}}
-               if ( strpos( $matches[0], '{{{' ) !== false ) {
+               # Don't parse {{{}}} because that's only for template arguments
+               if ( $linestart === '{' ) {
                        $text = $matches[0];
                        $found = true;
                        $noparse = true;
@@ -1684,18 +1841,14 @@ class Parser
                # SUBST
                if ( !$found ) {
                        $mwSubst =& MagicWord::get( MAG_SUBST );
-                       if ( $mwSubst->matchStartAndRemove( $part1 ) ) {
-                               if ( $this->mOutputType != OT_WIKI ) {
-                                       # Invalid SUBST not replaced at PST time
-                                       # Return without further processing
-                                       $text = $matches[0];
-                                       $found = true;
-                                       $noparse= true;
-                               }
-                       } elseif ( $this->mOutputType == OT_WIKI ) {
-                               # SUBST not found in PST pass, do nothing
+                       if ( $mwSubst->matchStartAndRemove( $part1 ) xor ($this->mOutputType == OT_WIKI) ) {
+                               # One of two possibilities is true:
+                               # 1) Found SUBST but not in the PST phase
+                               # 2) Didn't find SUBST and in the PST phase
+                               # In either case, return without further processing
                                $text = $matches[0];
                                $found = true;
+                               $noparse = true;
                        }
                }
 
@@ -1715,7 +1868,7 @@ class Parser
                        $mwInt =& MagicWord::get( MAG_INT );
                        if ( $mwInt->matchStartAndRemove( $part1 ) ) {
                                if ( $this->incrementIncludeCount( 'int:'.$part1 ) ) {
-                                       $text = wfMsgReal( $part1, $args, true );
+                                       $text = $linestart . wfMsgReal( $part1, $args, true );
                                        $found = true;
                                }
                        }
@@ -1727,12 +1880,12 @@ class Parser
                        $mwNs = MagicWord::get( MAG_NS );
                        if ( $mwNs->matchStartAndRemove( $part1 ) ) {
                                if ( intval( $part1 ) ) {
-                                       $text = $wgLang->getNsText( intval( $part1 ) );
+                                       $text = $linestart . $wgContLang->getNsText( intval( $part1 ) );
                                        $found = true;
                                } else {
                                        $index = Namespace::getCanonicalIndex( strtolower( $part1 ) );
                                        if ( !is_null( $index ) ) {
-                                               $text = $wgLang->getNsText( $index );
+                                               $text = $linestart . $wgContLang->getNsText( $index );
                                                $found = true;
                                        }
                                }
@@ -1756,9 +1909,9 @@ class Parser
                                $title = Title::newFromText( $part1 );
                                if ( !is_null( $title ) ) {
                                        if ( $argc > 0 ) {
-                                               $text = $title->$func( $args[0] );
+                                               $text = $linestart . $title->$func( $args[0] );
                                        } else {
-                                               $text = $title->$func();
+                                               $text = $linestart . $title->$func();
                                        }
                                        $found = true;
                                }
@@ -1769,7 +1922,7 @@ class Parser
                if ( !$found && $argc == 1 ) {
                        $mwGrammar =& MagicWord::get( MAG_GRAMMAR );
                        if ( $mwGrammar->matchStartAndRemove( $part1 ) ) {
-                               $text = $wgLang->convertGrammar( $args[0], $part1 );
+                               $text = $linestart . $wgContLang->convertGrammar( $args[0], $part1 );
                                $found = true;
                        }
                }
@@ -1778,20 +1931,28 @@ class Parser
 
                # Did we encounter this template already? If yes, it is in the cache
                # and we need to check for loops.
-               if ( isset( $this->mTemplates[$part1] ) ) {
+               if ( !$found && isset( $this->mTemplates[$part1] ) ) {
+                       # set $text to cached message.
+                       $text = $linestart . $this->mTemplates[$part1];
+                       $found = true;
+
                        # Infinite loop test
                        if ( isset( $this->mTemplatePath[$part1] ) ) {
                                $noparse = true;
                                $found = true;
+                               $text .= '<!-- WARNING: template loop detected -->';
                        }
-                       # set $text to cached message.
-                       $text = $this->mTemplates[$part1];
-                       $found = true;
                }
 
                # Load from database
+               $itcamefromthedatabase = false;
                if ( !$found ) {
-                       $title = Title::newFromText( $part1, NS_TEMPLATE );
+                       $ns = NS_TEMPLATE;
+                       $part1 = $this->maybeDoSubpageLink( $part1, $subpage='' );
+                       if ($subpage !== '') {
+                               $ns = $this->mTitle->getNamespace();
+                       }
+                       $title = Title::newFromText( $part1, $ns );
                        if ( !is_null( $title ) && !$title->isExternal() ) {
                                # Check for excessive inclusion
                                $dbk = $title->getPrefixedDBkey();
@@ -1801,14 +1962,14 @@ class Parser
                                        $articleContent = $article->getContentWithoutUsingSoManyDamnGlobals();
                                        if ( $articleContent !== false ) {
                                                $found = true;
-                                               $text = $articleContent;
+                                               $text = $linestart . $articleContent;
                                                $itcamefromthedatabase = true;
                                        }
                                }
 
                                # If the title is valid but undisplayable, make a link to it
                                if ( $this->mOutputType == OT_HTML && !$found ) {
-                                       $text = '[['.$title->getPrefixedText().']]';
+                                       $text = $linestart . '[['.$title->getPrefixedText().']]';
                                        $found = true;
                                }
 
@@ -1821,7 +1982,7 @@ class Parser
                # Only for HTML output
                if ( $nowiki && $found && $this->mOutputType == OT_HTML ) {
                        $text = wfEscapeWikiText( $text );
-               } elseif ( $this->mOutputType == OT_HTML && $found && !$noparse) {
+               } elseif ( ($this->mOutputType == OT_HTML || $this->mOutputType == OT_WIKI) && $found && !$noparse) {
                        # Clean up argument array
                        $assocArgs = array();
                        $index = 1;
@@ -1849,9 +2010,15 @@ class Parser
                        $text = $this->replaceVariables( $text, $assocArgs );
 
                        # Resume the link cache and register the inclusion as a link
-                       if ( !is_null( $title ) ) {
+                       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 ($linestart !== '\n' && preg_match('/^({\\||:|;|#|\*)/', $text)) {
+                               $text = "\n" . $text;
+                       }
                }
 
                # Empties the template path
@@ -1866,14 +2033,14 @@ class Parser
                                        $encodedname = base64_encode($title->getPrefixedDBkey());
                                else
                                        $encodedname = base64_encode("");
-                               $matches = preg_split('/(^={1,6}.*?={1,6}\s*?$)/m', $text, -1,
+                               $m = preg_split('/(^={1,6}.*?={1,6}\s*?$)/m', $text, -1,
                                        PREG_SPLIT_DELIM_CAPTURE);
                                $text = '';
                                $nsec = 0;
-                               for( $i = 0; $i < count($matches); $i += 2 ) {
-                                       $text .= $matches[$i];
-                                       if (!isset($matches[$i + 1]) || $matches[$i + 1] == "") continue;
-                                       $hl = $matches[$i + 1];
+                               for( $i = 0; $i < count($m); $i += 2 ) {
+                                       $text .= $m[$i];
+                                       if (!isset($m[$i + 1]) || $m[$i + 1] == "") continue;
+                                       $hl = $m[$i + 1];
                                        if( strstr($hl, "<!--MWTEMPLATESECTION") ) {
                                                $text .= $hl;
                                                continue;
@@ -1885,6 +2052,13 @@ class Parser
                                        $nsec++;
                                }
                        }
+               }
+
+               # Empties the template path
+               $this->mTemplatePath = array();
+               if ( !$found ) {
+                       return $matches[0];
+               } else {
                        return $text;
                }
        }
@@ -2097,7 +2271,7 @@ class Parser
         * @access private
         */
        /* private */ function formatHeadings( $text, $isMain=true ) {
-               global $wgInputEncoding, $wgMaxTocLevel, $wgLang, $wgLinkHolders;
+               global $wgInputEncoding, $wgMaxTocLevel, $wgContLang, $wgLinkHolders;
 
                $doNumberHeadings = $this->mOptions->getNumberHeadings();
                $doShowToc = $this->mOptions->getShowToc();
@@ -2208,7 +2382,7 @@ class Parser
                                                if( $dot ) {
                                                        $numbering .= '.';
                                                }
-                                               $numbering .= $wgLang->formatNum( $sublevelCount[$i] );
+                                               $numbering .= $wgContLang->formatNum( $sublevelCount[$i] );
                                                $dot = 1;
                                        }
                                }
@@ -2535,7 +2709,7 @@ class Parser
         * @access private
         */
        function pstPass2( $text, &$user ) {
-               global $wgLang, $wgLocaltimezone, $wgCurParser;
+               global $wgLang, $wgContLang, $wgLocaltimezone, $wgCurParser;
 
                # Variable replacement
                # Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
@@ -2550,13 +2724,13 @@ class Parser
                        $oldtz = getenv('TZ'); putenv('TZ='.$wgLocaltimezone);
                }
                /* Note: this is an ugly timezone hack for the European wikis */
-               $d = $wgLang->timeanddate( date( 'YmdHis' ), false ) .
+               $d = $wgContLang->timeanddate( date( 'YmdHis' ), false ) .
                  ' (' . date( 'T' ) . ')';
                if(isset($wgLocaltimezone)) putenv('TZ='.$oldtzs);
 
                $text = preg_replace( '/~~~~~/', $d, $text );
-               $text = preg_replace( '/~~~~/', '[[' . $wgLang->getNsText( NS_USER ) . ":$n|$k]] $d", $text );
-               $text = preg_replace( '/~~~/', '[[' . $wgLang->getNsText( NS_USER ) . ":$n|$k]]", $text );
+               $text = preg_replace( '/~~~~/', '[[' . $wgContLang->getNsText( NS_USER ) . ":$n|$k]] $d", $text );
+               $text = preg_replace( '/~~~/', '[[' . $wgContLang->getNsText( NS_USER ) . ":$n|$k]]", $text );
 
                # Context links: [[|name]] and [[name (context)|]]
                #
@@ -2584,13 +2758,6 @@ class Parser
                        $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
                }
 
-               /*
-               $mw =& MagicWord::get( MAG_SUBST );
-               $wgCurParser = $this->fork();
-               $text = $mw->substituteCallback( $text, "wfBraceSubstitution" );
-               $this->merge( $wgCurParser );
-               */
-
                # Trim trailing whitespace
                # MAG_END (__END__) tag allows for trailing
                # whitespace to be deliberately included