For HTML 5, drop type="" attributes for CSS/JS
[lhc/web/wiklou.git] / includes / Linker.php
index 7e2f131..8497a83 100644 (file)
@@ -922,7 +922,7 @@ class Linker {
 
                # Render autocomments and make links:
                $comment = $this->formatAutoComments( $comment, $title, $local );
-               $comment = $this->formatLinksInComment( $comment, $title );
+               $comment = $this->formatLinksInComment( $comment, $title, $local );
 
                wfProfileOut( __METHOD__ );
                return $comment;
@@ -1009,13 +1009,27 @@ class Linker {
         * @param string $comment Text to format links in
         * @return string
         */
-       public function formatLinksInComment( $comment, $title = null ) {
+       public function formatLinksInComment( $comment, $title = null, $local = false ) {
                $this->commentContextTitle = $title;
-               $html = preg_replace_callback(
-                       '/\[\[:?(.*?)(\|(.*?))*\]\]([^[]*)/',
-                       array( $this, 'formatLinksInCommentCallback' ),
-                       $comment );
+               $this->commentLocal = $local;
+               # Borrowed from Parser::replaceInternalLinks2
+               $parts = StringUtils::explode( '[[', ' ' . $comment );
+               $start = $parts->current();
+               $parts->next();
+               $line = $parts->current();
+               $html = substr( $start, 1 );
+               for ( ; $line !== false && $line !== null ; $parts->next(), $line = $parts->current() ) {                       
+                       $linked = preg_replace_callback(
+                               '/^:?(.*?)(\|(.*?))*\]\]([^[]*)/',
+                               array( $this, 'formatLinksInCommentCallback' ),
+                               $line, -1, $count );
+                       if( !$count ) { // No valid link found, put the brackets back
+                               $linked = '[[' . $linked;
+                       }
+                       $html .= $linked;
+               }
                unset( $this->commentContextTitle );
+               unset( $this->commentLocal );
                return $html;
        }
 
@@ -1039,9 +1053,10 @@ class Linker {
                        $text = $match[1];
                }
                $submatch = array();
+               $thelink = null;
                if( preg_match( '/^' . $medians . '(.*)$/i', $match[1], $submatch ) ) {
                        # Media link; trail not supported.
-                       $linkRegexp = '/\[\[(.*?)\]\]/';
+                       $linkRegexp = '/^(.*?)\]\]/';
                        $title = Title::makeTitleSafe( NS_FILE, $submatch[1] );
                        $thelink = $this->makeMediaLinkObj( $title, $text );
                } else {
@@ -1051,7 +1066,7 @@ class Linker {
                        } else {
                                $trail = "";
                        }
-                       $linkRegexp = '/\[\[(.*?)\]\]' . preg_quote( $trail, '/' ) . '/';
+                       $linkRegexp = '/^(.*?)\]\]' . preg_quote( $trail, '/' ) . '/';
                        if (isset($match[1][0]) && $match[1][0] == ':')
                                $match[1] = substr($match[1], 1);
                        list( $inside, $trail ) = Linker::splitTrail( $trail );
@@ -1060,12 +1075,23 @@ class Linker {
                        $linkTarget = Linker::normalizeSubpageLink( $this->commentContextTitle,
                                $match[1], $linkText );
                        
-                       $thelink = $this->link(
-                               Title::newFromText( $linkTarget ),
-                               $linkText . $inside
-                       ) . $trail;
+                       $target = Title::newFromText( $linkTarget );
+                       if( $target ) {
+                               if( $target->getText() == '' && !$this->commentLocal && $this->commentContextTitle ) {
+                                       $newTarget = clone( $this->commentContextTitle );
+                                       $newTarget->setFragment( '#' . $target->getFragment() );
+                                       $target = $newTarget;
+                               }
+                               $thelink = $this->link(
+                                       $target,
+                                       $linkText . $inside
+                               ) . $trail;
+                       }
+               }
+               if( $thelink ) {
+                       // If the link is still valid, go ahead and replace it in!
+                       $comment = preg_replace( $linkRegexp, StringUtils::escapeRegexReplacement( $thelink ), $comment, 1 );
                }
-               $comment = preg_replace( $linkRegexp, StringUtils::escapeRegexReplacement( $thelink ), $comment, 1 );
 
                return $comment;
        }
@@ -1231,22 +1257,21 @@ class Linker {
 
        /** @todo document */
        function tocList($toc) {
-               global $wgJsMimeType;
                $title = wfMsgHtml('toc') ;
                return
-                  '<table id="toc" class="toc" summary="' . $title .'"><tr><td>'
+                  '<table id="toc" class="toc"><tr><td>'
                 . '<div id="toctitle"><h2>' . $title . "</h2></div>\n"
                 . $toc
                 # no trailing newline, script should not be wrapped in a
                 # paragraph
                 . "</ul>\n</td></tr></table>"
-                . '<script type="' . $wgJsMimeType . '">'
-                . ' if (window.showTocToggle) {'
-                . ' var tocShowText = "' . Xml::escapeJsString( wfMsg('showtoc') ) . '";'
-                . ' var tocHideText = "' . Xml::escapeJsString( wfMsg('hidetoc') ) . '";'
-                . ' showTocToggle();'
-                . ' } '
-                . "</script>\n";
+                . Html::inlineScript(
+                       'if (window.showTocToggle) {'
+                       . ' var tocShowText = "' . Xml::escapeJsString( wfMsg('showtoc') ) . '";'
+                       . ' var tocHideText = "' . Xml::escapeJsString( wfMsg('hidetoc') ) . '";'
+                       . ' showTocToggle();'
+                       . ' } ' )
+               . "\n";
        }
        
        /**
@@ -1340,9 +1365,9 @@ class Linker {
         * @return string HTML headline
         */
        public function makeHeadline( $level, $attribs, $anchor, $text, $link, $legacyAnchor = false ) {
-               $ret = "<h$level id=\"$anchor\"$attribs"
+               $ret = "<h$level$attribs"
                        . $link
-                       . " <span class=\"mw-headline\">$text</span>"
+                       . " <span class=\"mw-headline\" id=\"$anchor\">$text</span>"
                        . "</h$level>";
                if ( $legacyAnchor !== false ) {
                        $ret = "<a id=\"$legacyAnchor\"></a>$ret";