Changing template argument syntax from {{arg}} to {{{arg}}}
[lhc/web/wiklou.git] / includes / Parser.php
index e292278..09026de 100644 (file)
@@ -316,18 +316,11 @@ class Parser
                $data = array () ;
                $id = $this->mTitle->getArticleID() ;
 
-               # For existing categories
-               if( $id ) {
-                       $sql = "SELECT DISTINCT cur_title,cur_namespace FROM cur,links WHERE l_to={$id} AND l_from=cur_id";
-                       $res = wfQuery ( $sql, DB_READ ) ;
-                       while ( $x = wfFetchObject ( $res ) ) $data[] = $x ;
-               } else {
-                       # For non-existing categories
-                       $t = wfStrencode( $this->mTitle->getPrefixedDBKey() );
-                       $sql = "SELECT DISTINCT cur_title,cur_namespace FROM cur,brokenlinks WHERE bl_to='$t' AND bl_from=cur_id" ;
-                       $res = wfQuery ( $sql, DB_READ ) ;
-                       while ( $x = wfFetchObject ( $res ) ) $data[] = $x ;
-               }
+               # FIXME: add limits
+               $t = wfStrencode( $this->mTitle->getDBKey() );
+               $sql = "SELECT DISTINCT cur_title,cur_namespace FROM cur,categorylinks WHERE cl_to='$t' AND cl_from=cur_id ORDER BY cl_sortkey" ;
+               $res = wfQuery ( $sql, DB_READ ) ;
+               while ( $x = wfFetchObject ( $res ) ) $data[] = $x ;
 
                # For all pages that link to this category
                foreach ( $data AS $x )
@@ -345,18 +338,14 @@ class Parser
                wfFreeResult ( $res ) ;
 
                # Showing subcategories
-               if ( count ( $children ) > 0 )
-               {
-                       asort ( $children ) ;
+               if ( count ( $children ) > 0 ) {
                        $r .= "<h2>".wfMsg("subcategories")."</h2>\n" ;
                        $r .= implode ( ", " , $children ) ;
                }
 
                # Showing pages in this category
-               if ( count ( $articles ) > 0 )
-               {
+               if ( count ( $articles ) > 0 ) {
                        $ti = $this->mTitle->getText() ;
-                       asort ( $articles ) ;
                        $h =  wfMsg( "category_header", $ti );
                        $r .= "<h2>{$h}</h2>\n" ;
                        $r .= implode ( ", " , $articles ) ;
@@ -547,6 +536,18 @@ class Parser
                return $t ;
        }
 
+       # Parses the text and adds the result to the strip state
+       # Returns the strip tag
+       function stripParse( $text, $linestart, $args ) 
+       {
+               $text = $this->strip( $text, $this->mStripState );
+               $text = $this->internalParse( $text, $linestart, $args, false );
+               if( $linestart ) {
+                       $text = "\n" . $text;
+               }
+               return $this->insertStripItem( $text, $this->mStripState );
+       }
+       
        function internalParse( $text, $linestart, $args = array(), $isMain=true )
        {
                $fname = "Parser::internalParse";
@@ -570,9 +571,9 @@ class Parser
                $text = $sk->transformContent( $text );
 
                if ( !isset ( $this->categoryMagicDone ) ) {
-                  $text .= $this->categoryMagic () ;
-                  $this->categoryMagicDone = true ;
-                  }
+                       $text .= $this->categoryMagic () ;
+                       $this->categoryMagicDone = true ;
+               }
 
                wfProfileOut( $fname );
                return $text;
@@ -1008,7 +1009,7 @@ class Parser
                        } else {
                                $noslash=substr($m[1],1);
                        }
-                       if($wgNamespacesWithSubpages[$this->mTitle->getNamespace()]) { # subpages allowed here
+                       if(!empty($wgNamespacesWithSubpages[$this->mTitle->getNamespace()])) { # subpages allowed here
                                $link = $this->mTitle->getPrefixedText(). "/" . trim($noslash);
                                if( "" == $text ) {
                                        $text= $m[1];
@@ -1021,7 +1022,8 @@ class Parser
                } else {
                        $link = substr( $m[1], 1 );
                }
-               if( "" == $text )
+               $wasblank = ( "" == $text );
+               if( $wasblank )
                        $text = $link;
 
                $nt = Title::newFromText( $link );
@@ -1045,7 +1047,13 @@ class Parser
                        if ( $ns == $category ) {
                                $t = $nt->getText() ;
                                $nnt = Title::newFromText ( Namespace::getCanonicalName($category).":".$t ) ;
+                               
+                               $wgLinkCache->suspend(); # Don't save in links/brokenlinks
                                $t = $sk->makeLinkObj( $nnt, $t, "", "" , $prefix );
+                               $wgLinkCache->resume();
+                               
+                               $sortkey = $wasblank ? $this->mTitle->getPrefixedText() : $text;
+                               $wgLinkCache->addCategoryLinkObj( $nt, $sortkey );
                                $this->mOutput->mCategoryLinks[] = $t ;
                                $s .= $prefix . $trail ;
                                return $s ;
@@ -1240,10 +1248,10 @@ class Parser
                                # No prefix (not in list)--go to paragraph mode
                                $uniq_prefix = UNIQ_PREFIX;
                                // XXX: use a stack for nestable elements like span, table and div
-                               $openmatch = preg_match("/(<table|<blockquote|<h1|<h2|<h3|<h4|<h5|<h6|<div|<pre|<tr|<td|<p|<ul|<li)/i", $t );
+                               $openmatch = preg_match("/(<table|<blockquote|<h1|<h2|<h3|<h4|<h5|<h6|<pre|<tr|<td|<p|<ul|<li)/i", $t );
                                $closematch = preg_match(
                                        "/(<\\/table|<\\/blockquote|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6|".
-                                       "<\\/div|<hr|<\\/td|<\\/pre|<\\/p|".$uniq_prefix."-pre|<\\/li|<\\/ul)/i", $t );
+                                       "<div|<\\/div|<hr|<\\/td|<\\/pre|<\\/p|".$uniq_prefix."-pre|<\\/li|<\\/ul)/i", $t );
                                if ( $openmatch or $closematch ) {
                                        $paragraphStack = false;
                                        $output .= $this->closeParagraph();
@@ -1365,13 +1373,20 @@ class Parser
                        $this->initialiseVariables();
                }
                $titleChars = Title::legalChars();
-               $regex = "/(\\n?){{([$titleChars]*?)(\\|.*?|)}}/s";
 
                # This function is called recursively. To keep track of arguments we need a stack:
                array_push( $this->mArgStack, $args );
 
                # PHP global rebinding syntax is a bit weird, need to use the GLOBALS array
                $GLOBALS['wgCurParser'] =& $this;
+
+               # Argument substitution
+               if ( $this->mOutputType == OT_HTML ) {
+                       $text = preg_replace_callback( "/(\\n?){{{([$titleChars]*?)}}}/", "wfArgSubstitution", $text );
+               }
+
+               # Double brace substitution
+               $regex = "/(\\n?){{([$titleChars]*?)(\\|.*?|)}}/s";
                $text = preg_replace_callback( $regex, "wfBraceSubstitution", $text );
 
                array_pop( $this->mArgStack );
@@ -1385,6 +1400,8 @@ class Parser
                $fname = "Parser::braceSubstitution";
                $found = false;
                $nowiki = false;
+               $noparse = false;
+               
                $title = NULL;
 
                # $newline is an optional newline character before the braces
@@ -1400,20 +1417,30 @@ class Parser
                        $args = array();
                }
                $argc = count( $args );
+       
+               # {{{}}}
+               if ( strpos( $matches[0], "{{{" ) !== false ) {
+                       $text = $matches[0];
+                       $found = true;
+                       $noparse = true;
+               }
 
                # SUBST
-               $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
+               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
                                $text = $matches[0];
                                $found = true;
                        }
-               } elseif ( $this->mOutputType == OT_WIKI ) {
-                       # SUBST not found in PST pass, do nothing
-                       $text = $matches[0];
-                       $found = true;
                }
 
                # MSG, MSGNW and INT
@@ -1488,14 +1515,14 @@ class Parser
                        $found = true;
                        $this->mOutput->mContainsOldMagic = true;
                }
-
+/*
                # Arguments input from the caller
                $inputArgs = end( $this->mArgStack );
                if ( !$found && array_key_exists( $part1, $inputArgs ) ) {
                        $text = $inputArgs[$part1];
                        $found = true;
                }
-
+*/
                # Load from database
                if ( !$found ) {
                        $title = Title::newFromText( $part1, NS_TEMPLATE );
@@ -1524,7 +1551,7 @@ class Parser
                # Only for HTML output
                if ( $nowiki && $found && $this->mOutputType == OT_HTML ) {
                        $text = wfEscapeWikiText( $text );
-               } elseif ( $this->mOutputType == OT_HTML && $found ) {
+               } elseif ( $this->mOutputType == OT_HTML && $found && !$noparse) {
                        # Clean up argument array
                        $assocArgs = array();
                        $index = 1;
@@ -1550,13 +1577,7 @@ class Parser
                        }
 
                        # Run full parser on the included text
-                       $text = $this->strip( $text, $this->mStripState );
-                       $text = $this->internalParse( $text, (bool)$newline, $assocArgs, false );
-                       if(!empty($newline)) $text = "\n".$text;
-
-                       # Add the result to the strip state for re-inclusion after
-                       # the rest of the processing
-                       $text = $this->insertStripItem( $text, $this->mStripState );
+                       $text = $this->stripParse( $text, (bool)$newline, $assocArgs );
 
                        # Resume the link cache and register the inclusion as a link
                        if ( !is_null( $title ) ) {
@@ -1572,6 +1593,21 @@ class Parser
                }
        }
 
+       # Triple brace replacement -- used for template arguments
+       function argSubstitution( $matches )
+       {
+               $newline = $matches[1];
+               $arg = trim( $matches[2] );
+               $text = $matches[0];
+               $inputArgs = end( $this->mArgStack );
+
+               if ( array_key_exists( $arg, $inputArgs ) ) {
+                       $text = $this->stripParse( $inputArgs[$arg], (bool)$newline, array() );
+               }
+               
+               return $text;
+       }
+
        # Returns true if the function is allowed to include this entity
        function incrementIncludeCount( $dbk )
        {
@@ -2233,4 +2269,10 @@ function wfBraceSubstitution( $matches )
        return $wgCurParser->braceSubstitution( $matches );
 }
 
+function wfArgSubstitution( $matches )
+{
+       global $wgCurParser;
+       return $wgCurParser->argSubstitution( $matches );
+}
+
 ?>