Reverting r22430 for now; I don't really like this keyword creep, and even if I did...
[lhc/web/wiklou.git] / includes / Parser.php
index 9004aea..0a7e862 100644 (file)
@@ -1,5 +1,7 @@
 <?php
+
 /**
+ *
  * File for Parser and related classes
  *
  * @addtogroup Parser
@@ -96,7 +98,7 @@ class Parser
         */
        # Persistent:
        var $mTagHooks, $mFunctionHooks, $mFunctionSynonyms, $mVariables;
-
+       
        # Cleared with clearState():
        var $mOutput, $mAutonumber, $mDTopen, $mStripState;
        var $mIncludeCount, $mArgStack, $mLastSection, $mInPre;
@@ -130,7 +132,7 @@ class Parser
                $this->mFunctionSynonyms = array( 0 => array(), 1 => array() );
                $this->mFirstCall = true;
        }
-
+       
        /**
         * Do various kinds of initialisation on the first call of the parser
         */
@@ -138,12 +140,12 @@ class Parser
                if ( !$this->mFirstCall ) {
                        return;
                }
-
+               
                wfProfileIn( __METHOD__ );
                global $wgAllowDisplayTitle, $wgAllowSlowParserFunctions;
-
+               
                $this->setHook( 'pre', array( $this, 'renderPreTag' ) );
-
+               
                $this->setFunctionHook( 'int', array( 'CoreParserFunctions', 'intFunction' ), SFH_NO_HASH );
                $this->setFunctionHook( 'ns', array( 'CoreParserFunctions', 'ns' ), SFH_NO_HASH );
                $this->setFunctionHook( 'urlencode', array( 'CoreParserFunctions', 'urlencode' ), SFH_NO_HASH );
@@ -449,7 +451,7 @@ class Parser
         * @param $text Source text string.
         * @param $uniq_prefix
         *
-        * @private
+        * @public
         * @static
         */
        function extractTagsAndParams($elements, $text, &$matches, $uniq_prefix = ''){
@@ -1927,6 +1929,7 @@ class Parser
                        # Look at the first character
                        if( $target != '' && $target{0} == '/' ) {
                                # / at end means we don't want the slash to be shown
+                               $m = array();
                                $trailingSlashes = preg_match_all( '%(/+)$%', $target, $m );
                                if( $trailingSlashes ) {
                                        $noslash = $target = substr( $target, 1, -strlen($m[0][0]) );
@@ -3089,7 +3092,7 @@ class Parser
                                                $found = false; //access denied
                                                wfDebug( "$fname: template inclusion denied for " . $title->getPrefixedDBkey() );
                                        } else {
-                                               $articleContent = $this->fetchTemplate( $title );
+                                               list($articleContent,$title) = $this->fetchTemplateAndtitle( $title );
                                                if ( $articleContent !== false ) {
                                                        $found = true;
                                                        $text = $articleContent;
@@ -3212,18 +3215,8 @@ class Parser
                                # replace ==section headers==
                                # XXX this needs to go away once we have a better parser.
                                if ( !$this->ot['wiki'] && !$this->ot['pre'] && $replaceHeadings ) {
-                                      if( !is_null( $title ) ) {
-                                   
-                                               #Follow redirects
-                                               $redirect = new Article($title);
-                                               $redirect = $redirect->followRedirect();
-                                               if( ! ($redirect instanceof Title))
-                                                       $redirect = $title;
-                                        
-
-                                               $encodedname = base64_encode($redirect->getPrefixedDBkey());
-                                       }
-                                 
+                                       if( !is_null( $title ) )
+                                               $encodedname = base64_encode($title->getPrefixedDBkey());
                                        else
                                                $encodedname = base64_encode("");
                                        $m = preg_split('/(^={1,6}.*?={1,6}\s*?$)/m', $text, -1,
@@ -3266,8 +3259,9 @@ class Parser
        /**
         * Fetch the unparsed text of a template and register a reference to it.
         */
-       function fetchTemplate( $title ) {
+       function fetchTemplateAndtitle( $title ) {
                $text = false;
+               $finalTitle = $title;
                // Loop to fetch the article, with up to 1 redirect
                for ( $i = 0; $i < 2 && is_object( $title ); $i++ ) {
                        $rev = Revision::newFromTitle( $title );
@@ -3289,9 +3283,15 @@ class Parser
                                break;
                        }
                        // Redirect?
+                       $finalTitle = $title;
                        $title = Title::newFromRedirect( $text );
                }
-               return $text;
+               return array($text,$finalTitle);
+       }
+
+       function fetchTemplate( $title ) {
+               $rv = $this->fetchTemplateAndtitle($title);
+               return $rv[0];
        }
 
        /**
@@ -3500,6 +3500,7 @@ class Parser
 
                        if( $toclevel ) {
                                $prevlevel = $level;
+                               $prevtoclevel = $toclevel;
                        }
                        $level = $matches[1][$headlineCount];
 
@@ -3536,7 +3537,12 @@ class Parser
                                                }
                                        }
                                        if( $toclevel<$wgMaxTocLevel ) {
-                                               $toc .= $sk->tocUnindent( $prevtoclevel - $toclevel );
+                                               if($prevtoclevel < $wgMaxTocLevel) {
+                                                       # Unindent only if the previous toc level was shown :p
+                                                       $toc .= $sk->tocUnindent( $prevtoclevel - $toclevel );
+                                               } else {
+                                                       $toc .= $sk->tocLineEnd();
+                                               }
                                        }
                                }
                                else {
@@ -4068,16 +4074,11 @@ class Parser
                                        $linkCache->addGoodLinkObj( $s->page_id, $title );
                                        $this->mOutput->addLink( $title, $s->page_id );
 
-                                       if ( $threshold >  0 ) {
-                                               $size = $s->page_len;
-                                               if ( $s->page_is_redirect || $s->page_namespace != 0 || $size >= $threshold ) {
-                                                       $colours[$pdbk] = 1;
-                                               } else {
-                                                       $colours[$pdbk] = 2;
-                                               }
-                                       } else {
-                                               $colours[$pdbk] = 1;
-                                       }
+                                       $colours[$pdbk] = ( $threshold == 0 || (
+                                                               $s->page_len >= $threshold || # always true if $threshold <= 0
+                                                               $s->page_is_redirect ||
+                                                               !Namespace::isContent( $s->page_namespace ) )
+                                                           ? 1 : 2 );
                                }
                        }
                        wfProfileOut( $fname.'-check' );
@@ -4411,6 +4412,8 @@ class Parser
                #  * ___px              scale to ___ pixels width, no aligning. e.g. use in taxobox
                #  * center             center the image
                #  * framed             Keep original image size, no magnify-button.
+               #  * upright            reduce width for upright images, rounded to full __0 px
+               #  * border             draw a 1px border around the image
                # vertical-align values (no % or length right now):
                #  * baseline
                #  * sub
@@ -4433,11 +4436,16 @@ class Parser
                $mwManualThumb =& MagicWord::get( 'img_manualthumb' );
                $mwWidth  =& MagicWord::get( 'img_width' );
                $mwFramed =& MagicWord::get( 'img_framed' );
+               $mwUpright =& MagicWord::get( 'img_upright' );
+               $mwBorder =& MagicWord::get( 'img_border' );
                $mwPage   =& MagicWord::get( 'img_page' );
                $caption = '';
 
                $params = array();
                $framed = $thumb = false;
+               $upright = false;
+               $upright_factor = 0;
+               $border = false;
                $manual_thumb = '' ;
                $align = $valign = '';
                $sk = $this->mOptions->getSkin();
@@ -4445,6 +4453,11 @@ class Parser
                foreach( $part as $val ) {
                        if ( !is_null( $mwThumb->matchVariableStartToEnd($val) ) ) {
                                $thumb=true;
+                       } elseif ( !is_null( $match = $mwUpright->matchVariableStartToEnd( $val ) ) ) {
+                               $upright = true;
+                               $upright_factor = floatval( $match );
+                       } elseif ( !is_null( $mwBorder->matchVariableStartToEnd( $val ) ) ) {
+                               $border = true;
                        } elseif ( ! is_null( $match = $mwManualThumb->matchVariableStartToEnd($val) ) ) {
                                # use manually specified thumbnail
                                $thumb=true;
@@ -4491,7 +4504,7 @@ class Parser
                $alt = Sanitizer::stripAllTags( $alt );
 
                # Linker does the rest
-               return $sk->makeImageLinkObj( $nt, $caption, $alt, $align, $params, $framed, $thumb, $manual_thumb, $valign );
+               return $sk->makeImageLinkObj( $nt, $caption, $alt, $align, $params, $framed, $thumb, $manual_thumb, $valign, $upright, $upright_factor, $border );
        }
 
        /**