* s~\t+$~~
[lhc/web/wiklou.git] / includes / Parser.php
index a5df217..727c0a4 100644 (file)
@@ -15,7 +15,7 @@ require_once( 'HttpFunctions.php' );
  * changes in an incompatible way, so the parser cache
  * can automatically discard old data.
  */
-define( 'MW_PARSER_VERSION', '1.5.0' );
+define( 'MW_PARSER_VERSION', '1.6.0' );
 
 /**
  * Variable substitution O(N^2) attack
@@ -73,7 +73,7 @@ define( 'EXT_IMAGE_REGEX',
  *   performs brace substitution on MediaWiki messages
  *
  * Globals used:
- *    objects:   $wgLang, $wgLinkCache
+ *    objects:   $wgLang
  *
  * NOT $wgArticle, $wgUser or $wgTitle. Keep them away!
  *
@@ -157,7 +157,7 @@ class Parser
                wfRunHooks( 'ParserClearState', array( &$this ) );
        }
 
-       /** 
+       /**
         * Convert wikitext to HTML
         * Do not call this function recursively.
         *
@@ -175,7 +175,7 @@ class Parser
                 * First pass--just handle <nowiki> sections, pass the rest off
                 * to internalParse() which does all the real work.
                 */
-               
+
                global $wgUseTidy, $wgContLang;
                $fname = 'Parser::parse';
                wfProfileIn( $fname );
@@ -236,7 +236,7 @@ class Parser
                wfRunHooks( 'ParserBeforeTidy', array( &$this, &$text ) );
 
                $text = Sanitizer::normalizeCharReferences( $text );
-               
+
                if ($wgUseTidy) {
                        $text = Parser::tidy($text);
                }
@@ -426,7 +426,7 @@ class Parser
                foreach( $gallery_content as $marker => $content ) {
                        require_once( 'ImageGallery.php' );
                        if ( $render ) {
-                               $gallery_content[$marker] = Parser::renderImageGallery( $content );
+                               $gallery_content[$marker] = $this->renderImageGallery( $content );
                        } else {
                                $gallery_content[$marker] = '<gallery>'.$content.'</gallery>';
                        }
@@ -498,7 +498,7 @@ class Parser
                if ( !is_array( $state ) ) {
                        return $text;
                }
-               
+
                # Must expand in reverse order, otherwise nested tags will be corrupted
                foreach( array_reverse( $state, true ) as $tag => $contentDict ) {
                        if( $tag != 'nowiki' && $tag != 'html' ) {
@@ -683,7 +683,7 @@ class Parser
                        $fc = substr ( $x , 0 , 1 ) ;
                        if ( preg_match( '/^(:*)\{\|(.*)$/', $x, $matches ) ) {
                                $indent_level = strlen( $matches[1] );
-                               
+
                                $attributes = $this->unstripForHTML( $matches[2] );
 
                                $t[$k] = str_repeat( '<dl><dd>', $indent_level ) .
@@ -1206,7 +1206,7 @@ class Parser
                $imagesfrom = $this->mOptions->getAllowExternalImagesFrom();
                $imagesexception = !empty($imagesfrom);
                $text = false;
-               if ( $this->mOptions->getAllowExternalImages() 
+               if ( $this->mOptions->getAllowExternalImages()
                     || ( $imagesexception && strpos( $url, $imagesfrom ) === 0 ) ) {
                        if ( preg_match( EXT_IMAGE_REGEX, $url ) ) {
                                # Image found
@@ -1222,7 +1222,7 @@ class Parser
         * @access private
         */
        function replaceInternalLinks( $s ) {
-               global $wgContLang, $wgLinkCache;
+               global $wgContLang;
                static $fname = 'Parser::replaceInternalLinks' ;
 
                wfProfileIn( $fname );
@@ -1421,7 +1421,7 @@ class Parser
 
                                                # cloak any absolute URLs inside the image markup, so replaceExternalLinks() won't touch them
                                                $s .= $prefix . $this->armorLinks( $this->makeImage( $nt, $text ) ) . $trail;
-                                               $this->mOutput->addImage( $nt->getDBkey() ); 
+                                               $this->mOutput->addImage( $nt->getDBkey() );
 
                                                wfProfileOut( "$fname-image" );
                                                continue;
@@ -1522,7 +1522,7 @@ class Parser
                }
                return $retVal;
        }
-       
+
        /**
         * Render a forced-blue link inline; protect against double expansion of
         * URLs if we're in a mode that prepends full URL prefixes to internal links.
@@ -1543,7 +1543,7 @@ class Parser
                $link = $sk->makeKnownLinkObj( $nt, $text, $query, $inside, $prefix );
                return $this->armorLinks( $link ) . $trail;
        }
-       
+
        /**
         * Insert a NOPARSE hacky thing into any inline links in a chunk that's
         * going to go through further parsing steps before inline URL expansion.
@@ -1961,7 +1961,7 @@ class Parser
                        case MAG_FULLPAGENAME:
                                return $this->mTitle->getPrefixedText();
                        case MAG_FULLPAGENAMEE:
-                               return wfUrlencode( $this->mTitle->getPrefixedText() );
+                               return $this->mTitle->getPrefixedURL();
                        case MAG_REVISIONID:
                                return $this->mRevisionId;
                        case MAG_NAMESPACE:
@@ -2048,14 +2048,14 @@ class Parser
 
                        if ($lastOpeningBrace >= 0) {
                                $pos = strpos ($text, $openingBraceStack[$lastOpeningBrace]['braceEnd'], $i);
-                       
+
                                if (false !== $pos && (-1 == $nextPos || $pos < $nextPos)){
                                        $rule = null;
                                        $nextPos = $pos;
                                }
 
                                $pos = strpos ($text, '|', $i);
-                               
+
                                if (false !== $pos && (-1 == $nextPos || $pos < $nextPos)){
                                        $rule = null;
                                        $nextPos = $pos;
@@ -2075,14 +2075,14 @@ class Parser
                                                           'title' => '',
                                                           'parts' => null);
 
-                               # count openning brace characters 
+                               # count openning brace characters
                                while ($i+1 < strlen($text) && $text[$i+1] == $piece['brace']) {
                                        $piece['count']++;
                                        $i++;
                                }
 
-                               $piece['startAt'] = $i+1; 
-                               $piece['partStart'] = $i+1; 
+                               $piece['startAt'] = $i+1;
+                               $piece['partStart'] = $i+1;
 
                                # we need to add to stack only if openning brace count is enough for any given rule
                                foreach ($rule['cb'] as $cnt => $fn) {
@@ -2116,7 +2116,7 @@ class Parser
                                                        $matchingCallback = $fn;
                                                }
                                        }
-                                       
+
                                        if ($matchingCount == 0) {
                                                $i += $count - 1;
                                                continue;
@@ -2130,7 +2130,7 @@ class Parser
 
                                        $pieceStart = $openingBraceStack[$lastOpeningBrace]['startAt'] - $matchingCount;
                                        $pieceEnd = $i + $matchingCount;
-                                       
+
                                        if( is_callable( $matchingCallback ) ) {
                                                $cbArgs = array (
                                                                                 'text' => substr($text, $pieceStart, $pieceEnd - $pieceStart),
@@ -2181,7 +2181,7 @@ class Parser
                                        }
                                        else
                                                $openingBraceStack[$lastOpeningBrace]['parts'][] = substr($text, $openingBraceStack[$lastOpeningBrace]['partStart'], $i - $openingBraceStack[$lastOpeningBrace]['partStart']);
-                                       
+
                                        $openingBraceStack[$lastOpeningBrace]['partStart'] = $i + 1;
                                }
                        }
@@ -2412,7 +2412,7 @@ class Parser
                        $mwLocalE =& MagicWord::get( MAG_LOCALURLE );
                        $mwFull =& MagicWord::get( MAG_FULLURL );
                        $mwFullE =& MagicWord::get( MAG_FULLURLE );
-                       
+
 
                        if ( $mwLocal->matchStartAndRemove( $part1 ) ) {
                                $func = 'getLocalURL';
@@ -2575,7 +2575,7 @@ class Parser
                        # 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 );
@@ -2642,7 +2642,7 @@ class Parser
                        return $text;
                }
        }
-       
+
        /**
         * Translude an interwiki link.
         */
@@ -2897,12 +2897,7 @@ class Parser
                        # strip out HTML
                        $canonized_headline = preg_replace( '/<.*?' . '>/','',$canonized_headline );
                        $tocline = trim( $canonized_headline );
-                       $canonized_headline = urlencode( Sanitizer::decodeCharReferences( str_replace(' ', '_', $tocline) ) );
-                       $replacearray = array(
-                               '%3A' => ':',
-                               '%' => '.'
-                       );
-                       $canonized_headline = str_replace(array_keys($replacearray),array_values($replacearray),$canonized_headline);
+                       $canonized_headline = Sanitizer::escapeId( $tocline );
                        $refers[$headlineCount] = $canonized_headline;
 
                        # count how many in assoc. array so we can track dupes in anchors
@@ -3168,7 +3163,7 @@ class Parser
                global $wgLegalTitleChars;
                $tc = "[$wgLegalTitleChars]";
                $np = str_replace( array( '(', ')' ), array( '', '' ), $tc ); # No parens
-               
+
                $namespacechar = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
                $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
 
@@ -3200,7 +3195,7 @@ class Parser
 
                return $text;
        }
-       
+
        /**
         * Fetch the user's signature text, if any, and normalize to
         * validated, ready-to-insert wikitext.
@@ -3215,7 +3210,7 @@ class Parser
                if ( '' == $nick ) {
                        $nick = $name;
                }
-               
+
                if( $user->getOption( 'fancysig' ) ) {
                        // A wikitext signature.
                        $valid = $this->validateSig( $nick );
@@ -3227,7 +3222,7 @@ class Parser
                                return $nick;
                        }
                }
-               
+
                // Plain text linking to the user's homepage
                global $wgContLang;
                $page = $user->getUserPage();
@@ -3237,7 +3232,7 @@ class Parser
                        wfEscapeWikIText( $nick ) .
                        "]]";
        }
-       
+
        /**
         * We want to enforce two rules on wikitext sigs here:
         * 1) Expand any templates at save time (forced subst:)
@@ -3314,7 +3309,7 @@ class Parser
        function setHook( $tag, $callback ) {
                $oldVal = @$this->mTagHooks[$tag];
                $this->mTagHooks[$tag] = $callback;
-               
+
                return $oldVal;
        }
 
@@ -3328,7 +3323,7 @@ class Parser
         * $options is a bit field, RLH_FOR_UPDATE to select for update
         */
        function replaceLinkHolders( &$text, $options = 0 ) {
-               global $wgUser, $wgLinkCache;
+               global $wgUser;
                global $wgOutputReplace;
 
                $fname = 'Parser::replaceLinkHolders';
@@ -3336,7 +3331,8 @@ class Parser
 
                $pdbks = array();
                $colours = array();
-               $sk = $this->mOptions->getSkin();
+               $sk =& $this->mOptions->getSkin();
+               $linkCache =& LinkCache::singleton();
 
                if ( !empty( $this->mLinkHolders['namespaces'] ) ) {
                        wfProfileIn( $fname.'-check' );
@@ -3363,10 +3359,10 @@ class Parser
                                # Check if it's a static known link, e.g. interwiki
                                if ( $title->isAlwaysKnown() ) {
                                        $colours[$pdbk] = 1;
-                               } elseif ( ( $id = $wgLinkCache->getGoodLinkID( $pdbk ) ) != 0 ) {
+                               } elseif ( ( $id = $linkCache->getGoodLinkID( $pdbk ) ) != 0 ) {
                                        $colours[$pdbk] = 1;
                                        $this->mOutput->addLink( $title, $id );
-                               } elseif ( $wgLinkCache->isBadLink( $pdbk ) ) {
+                               } elseif ( $linkCache->isBadLink( $pdbk ) ) {
                                        $colours[$pdbk] = 0;
                                } else {
                                        # Not in the link cache, add it to the query
@@ -3402,7 +3398,7 @@ class Parser
                                while ( $s = $dbr->fetchObject($res) ) {
                                        $title = Title::makeTitle( $s->page_namespace, $s->page_title );
                                        $pdbk = $title->getPrefixedDBkey();
-                                       $wgLinkCache->addGoodLinkObj( $s->page_id, $title );
+                                       $linkCache->addGoodLinkObj( $s->page_id, $title );
                                        $this->mOutput->addLink( $title, $s->page_id );
 
                                        if ( $threshold >  0 ) {
@@ -3427,7 +3423,7 @@ class Parser
                                $searchkey = "<!--LINK $key-->";
                                $title = $this->mLinkHolders['titles'][$key];
                                if ( empty( $colours[$pdbk] ) ) {
-                                       $wgLinkCache->addBadLinkObj( $title );
+                                       $linkCache->addBadLinkObj( $title );
                                        $colours[$pdbk] = 0;
                                        $this->mOutput->addLink( $title, 0 );
                                        $wgOutputReplace[$searchkey] = $sk->makeBrokenLinkObj( $title,
@@ -3528,12 +3524,9 @@ class Parser
         * given as text will return the HTML of a gallery with two images,
         * labeled 'The number "1"' and
         * 'A tree'.
-        *
-        * @static
         */
        function renderImageGallery( $text ) {
                # Setup the parser
-               global $wgTitle;
                $parserOptions = new ParserOptions;
                $localParser = new Parser();
 
@@ -3550,7 +3543,7 @@ class Parser
                        if ( count( $matches ) == 0 ) {
                                continue;
                        }
-                       $nt = Title::newFromURL( $matches[1] );
+                       $nt =& Title::newFromText( $matches[1] );
                        if( is_null( $nt ) ) {
                                # Bogus title. Ignore these so we don't bomb out later.
                                continue;
@@ -3561,8 +3554,8 @@ class Parser
                                $label = '';
                        }
 
-                       $html = $localParser->parse( $label , $wgTitle, $parserOptions );
-                       $html = $html->mText;
+                       $pout = $localParser->parse( $label , $this->mTitle, $parserOptions );
+                       $html = $pout->getText();
 
                        $ig->add( new Image( $nt ), $html );
                        $this->mOutput->addImage( $nt->getDBkey() );
@@ -3647,13 +3640,13 @@ class Parser
        }
 
        /**
-        * Set a flag in the output object indicating that the content is dynamic and 
+        * Set a flag in the output object indicating that the content is dynamic and
         * shouldn't be cached.
         */
        function disableCache() {
                $this->mOutput->mCacheTime = -1;
        }
-       
+
        /**
         * Callback from the Sanitizer for expanding items found in HTML attribute
         * values, so they can be safely tested and escaped.
@@ -3667,7 +3660,7 @@ class Parser
                $text = $this->unstripForHTML( $text );
                return $text;
        }
-       
+
        function unstripForHTML( $text ) {
                $text = $this->unstrip( $text, $this->mStripState );
                $text = $this->unstripNoWiki( $text, $this->mStripState );
@@ -3733,18 +3726,18 @@ class ParserOutput
        function addImage( $name )           { $this->mImages[$name] = 1; }
        function addLanguageLink( $t )       { $this->mLanguageLinks[] = $t; }
 
-       function addLink( $title, $id ) { 
+       function addLink( $title, $id ) {
                $ns = $title->getNamespace();
                $dbk = $title->getDBkey();
                if ( !isset( $this->mLinks[$ns] ) ) {
                        $this->mLinks[$ns] = array();
                }
-               $this->mLinks[$ns][$dbk] = $id; 
+               $this->mLinks[$ns][$dbk] = $id;
        }
 
        function addTemplate( $title, $id ) {
                $ns = $title->getNamespace();
-               $dbk = $title->getDBkey();      
+               $dbk = $title->getDBkey();
                if ( !isset( $this->mTemplates[$ns] ) ) {
                        $this->mTemplates[$ns] = array();
                }
@@ -3902,7 +3895,7 @@ function wfNumberOfFiles() {
 
 /**
  * Get various statistics from the database
- * @private
+ * @access private
  */
 function wfLoadSiteStats() {
        global $wgNumberOfArticles, $wgTotalViews, $wgTotalEdits;