Per my comment on r68760: Make MWfunction class, complete with call_user_func helper...
[lhc/web/wiklou.git] / includes / Linker.php
index b0779ce..0a22ad7 100644 (file)
@@ -198,7 +198,7 @@ class Linker {
 
                $attribs = array_merge(
                        $attribs,
-                       $this->linkAttribs( $target, $customAttribs, $options )
+                       $this->linkAttribs( $target, $customAttribs, $options, $text )
                );
                if ( is_null( $text ) ) {
                        $text = $this->linkText( $target );
@@ -248,7 +248,7 @@ class Linker {
        /**
         * Returns the array of attributes used when linking to the Title $target
         */
-       private function linkAttribs( $target, $attribs, $options ) {
+       private function linkAttribs( $target, $attribs, $options, $linkText ) {
                wfProfileIn( __METHOD__ );
                global $wgUser;
                $defaults = array();
@@ -279,12 +279,13 @@ class Linker {
                }
 
                # Get a default title attribute.
+               $known = in_array( 'known', $options );
                if ( $target->getPrefixedText() == '' ) {
                        # A link like [[#Foo]].  This used to mean an empty title
                        # attribute, but that's silly.  Just don't output a title.
-               } elseif ( in_array( 'known', $options ) ) {
+               } elseif ( $known && strtolower($linkText) !== strtolower($target->getPrefixedText() ) ) {
                        $defaults['title'] = $target->getPrefixedText();
-               } else {
+               } elseif ( !$known ) {
                        $defaults['title'] = wfMsg( 'red-link-title', $target->getPrefixedText() );
                }
 
@@ -324,14 +325,14 @@ class Linker {
         * Generate either a normal exists-style link or a stub link, depending
         * on the given page size.
         *
-        * @param $size Integer
-        * @param $nt Title object.
-        * @param $text String
-        * @param $query String
-        * @param $trail String
-        * @param $prefix String
-        * @return string HTML of link
-        * @deprecated
+        * @param $size Integer
+        * @param $nt Title object.
+        * @param $text String
+        * @param $query String
+        * @param $trail String
+        * @param $prefix String
+        * @return string HTML of link
+        * @deprecated
         */
        function makeSizeLinkObj( $size, $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
                global $wgUser;
@@ -427,12 +428,14 @@ class Linker {
         *          caption         HTML for image caption.
         *          link-url        URL to link to
         *          link-title      Title object to link to
+        *          link-target     Value for the target attribue, only with link-url
         *          no-link         Boolean, suppress description link
         *
         * @param $handlerParams Array: associative array of media handler parameters, to be passed
         *       to transform(). Typical keys are "width" and "page".
         * @param $time String: timestamp of the file, set as false for current
         * @param $query String: query params for desc url
+        * @param $widthOption: Used by the parser to remember the user preference thumbnailsize
         * @return String: HTML for an image, with links, wrappers, etc.
         */
        function makeImageLink2( Title $title, $file, $frameParams = array(), $handlerParams = array(), $time = false, $query = "", $widthOption = null ) {
@@ -442,7 +445,6 @@ class Linker {
                        return $res;
                }
 
-               global $wgContLang, $wgThumbLimits, $wgThumbUpright;
                if ( $file && !$file->allowInlineDisplay() ) {
                        wfDebug( __METHOD__ . ': ' . $title->getPrefixedDBkey() . " does not allow inline display\n" );
                        return $this->link( $title );
@@ -469,28 +471,31 @@ class Linker {
                        $hp['width'] = $file->getWidth( $page );
 
                        if ( isset( $fp['thumbnail'] ) || isset( $fp['framed'] ) || isset( $fp['frameless'] ) || !$hp['width'] ) {
+                               global $wgThumbLimits, $wgThumbUpright;
                                if ( !isset( $widthOption ) || !isset( $wgThumbLimits[$widthOption] ) ) {
-                                        $widthOption = User::getDefaultOption( 'thumbsize' );
+                                       $widthOption = User::getDefaultOption( 'thumbsize' );
                                }
 
                                // Reduce width for upright images when parameter 'upright' is used
                                if ( isset( $fp['upright'] ) && $fp['upright'] == 0 ) {
                                        $fp['upright'] = $wgThumbUpright;
                                }
-                               // Use width which is smaller: real image width or user preference width
                                // For caching health: If width scaled down due to upright parameter, round to full __0 pixel to avoid the creation of a lot of odd thumbs
                                $prefWidth = isset( $fp['upright'] ) ?
                                        round( $wgThumbLimits[$widthOption] * $fp['upright'], -1 ) :
                                        $wgThumbLimits[$widthOption];
-                               if ( $hp['width'] <= 0 || $prefWidth < $hp['width'] ) {
-                                       if ( !isset( $hp['height'] ) ) {
-                                               $hp['width'] = $prefWidth;
-                                       }
+
+                               // Use width which is smaller: real image width or user preference width
+                               // Unless image is scalable vector.
+                               if ( !isset( $hp['height'] ) && ( $hp['width'] <= 0 ||
+                                               $prefWidth < $hp['width'] || $file->isVectorized() ) ) {
+                                       $hp['width'] = $prefWidth;
                                }
                        }
                }
 
                if ( isset( $fp['thumbnail'] ) || isset( $fp['manualthumb'] ) || isset( $fp['framed'] ) ) {
+                       global $wgContLang;
                        # Create a thumbnail. Alignment depends on language
                        # writing direction, # right aligned for left-to-right-
                        # languages ("Western languages"), left-aligned
@@ -513,7 +518,7 @@ class Linker {
                        }
                }
 
-               if ( $file && $hp['width'] ) {
+               if ( $file && isset( $hp['width'] ) ) {
                        # Create a resized image, without the additional thumbnail features
                        $thumb = $file->transform( $hp );
                } else {
@@ -548,8 +553,11 @@ class Linker {
                $mtoParams = array();
                if ( isset( $frameParams['link-url'] ) && $frameParams['link-url'] !== '' ) {
                        $mtoParams['custom-url-link'] = $frameParams['link-url'];
+                       if ( isset( $frameParams['link-target'] ) ) {
+                               $mtoParams['custom-target-link'] = $frameParams['link-target'];
+                       }
                } elseif ( isset( $frameParams['link-title'] ) && $frameParams['link-title'] !== '' ) {
-                       $mtoParams['custom-title-link'] = $frameParams['link-title'];
+                       $mtoParams['custom-title-link'] = $this->normaliseSpecialPage( $frameParams['link-title'] );
                } elseif ( !empty( $frameParams['no-link'] ) ) {
                        // No link
                } else {
@@ -687,28 +695,28 @@ class Linker {
                if ( $title instanceof Title ) {
                        wfProfileIn( __METHOD__ );
                        $currentExists = $time ? ( wfFindFile( $title ) != false ) : false;
-                       if ( ( $wgUploadMissingFileUrl || $wgEnableUploads ) && !$currentExists ) {
-                               if ( $text == '' )
-                                       $text = htmlspecialchars( $title->getPrefixedText() );
 
+                       list( $inside, $trail ) = self::splitTrail( $trail );
+                       if ( $text == '' )
+                               $text = htmlspecialchars( $title->getPrefixedText() );
+
+                       if ( ( $wgUploadMissingFileUrl || $wgEnableUploads ) && !$currentExists ) {
                                $redir = RepoGroup::singleton()->getLocalRepo()->checkRedirect( $title );
+
                                if ( $redir ) {
                                        wfProfileOut( __METHOD__ );
-                                       return $this->makeKnownLinkObj( $title, $text, $query, $trail, $prefix );
+                                       return $this->linkKnown( $title, "$prefix$text$inside", array(), $query ) . $trail;
                                }
 
                                $href = $this->getUploadUrl( $title, $query );
 
-
-                               list( $inside, $trail ) = self::splitTrail( $trail );
-
                                wfProfileOut( __METHOD__ );
                                return '<a href="' . htmlspecialchars( $href ) . '" class="new" title="' .
-                                                               htmlspecialchars( $title->getPrefixedText(), ENT_QUOTES ) . '">' .
-                                                               htmlspecialchars( $prefix . $text . $inside, ENT_NOQUOTES ) . '</a>' . $trail;
+                                       htmlspecialchars( $title->getPrefixedText(), ENT_QUOTES ) . '">' .
+                                       htmlspecialchars( $prefix . $text . $inside, ENT_NOQUOTES ) . '</a>' . $trail;
                        } else {
                                wfProfileOut( __METHOD__ );
-                               return $this->makeKnownLinkObj( $title, $text, $query, $trail, $prefix );
+                               return $this->linkKnown( $title, "$prefix$text$inside", array(), $query ) . $trail;
                        }
                } else {
                        return "<!-- ERROR -->{$prefix}{$text}{$trail}";
@@ -776,7 +784,7 @@ class Linker {
        function specialLink( $name, $key = '' ) {
                if ( $key == '' ) { $key = strtolower( $name ); }
 
-               return $this->link( SpecialPage::getTitleFor( $name ) , wfMsg( $key ), array(), array(), array( 'known', 'noclasses' ) );
+               return $this->linkKnown( SpecialPage::getTitleFor( $name ) , wfMsg( $key ) );
        }
 
        /**
@@ -1094,7 +1102,7 @@ class Linker {
 
                # fix up urlencoded title texts (copied from Parser::replaceInternalLinks)
                if ( strpos( $match[1], '%' ) !== false ) {
-                       $match[1] = str_replace( array( '<', '>' ), array( '&lt;', '&gt;' ), urldecode( $match[1] ) );
+                       $match[1] = str_replace( array( '<', '>' ), array( '&lt;', '&gt;' ), rawurldecode( $match[1] ) );
                }
 
                # Handle link renaming [[foo|text]] will show link as "text"
@@ -1314,7 +1322,7 @@ class Linker {
         */
        function tocLineEnd() {
                return "</li>\n";
-       }
+       }
 
        /**
         * Wraps the TOC in a table and provides the hide/collapse javascript.
@@ -1328,16 +1336,7 @@ class Linker {
                   '<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>"
-                . Html::inlineScript(
-                       'if (window.showTocToggle) {'
-                       . ' var tocShowText = "' . Xml::escapeJsString( wfMsg( 'showtoc' ) ) . '";'
-                       . ' var tocHideText = "' . Xml::escapeJsString( wfMsg( 'hidetoc' ) ) . '";'
-                       . ' showTocToggle();'
-                       . ' } ' )
-               . "\n";
+                . "</ul>\n</td></tr></table>\n";
        }
 
        /**
@@ -1386,6 +1385,8 @@ class Linker {
                // added to it for LTR text on RTL pages
                $attribs = array();
                if ( !is_null( $tooltip ) ) {
+                       # Bug 25462: undo double-escaping.
+                       $tooltip = Sanitizer::decodeCharReferences( $tooltip );
                        $attribs['title'] = wfMsgReal( 'editsectionhint', array( $tooltip ), true, $lang );
                }
                $link = $this->link( $nt, wfMsgExt( 'editsection', array( 'language' => $lang ) ),
@@ -1402,13 +1403,13 @@ class Linker {
                        $attribs = " title=\"$attribs\"";
                }
                $result = null;
-               wfRunHooks( 'EditSectionLink', array( &$this, $nt, $section, $attribs, $link, &$result ) );
+               wfRunHooks( 'EditSectionLink', array( &$this, $nt, $section, $attribs, $link, &$result, $lang ) );
                if ( !is_null( $result ) ) {
                        # For reverse compatibility, add the brackets *after* the hook is
                        # run, and even add them to hook-provided text.  (This is the main
                        # reason that the EditSectionLink hook is deprecated in favor of
                        # DoEditSectionLink: it can't change the brackets or the span.)
-                       $result = wfMsgExt( 'editsection-brackets', array( 'escape', 'replaceafter', 'language' => $lang ), $link );
+                       $result = wfMsgExt( 'editsection-brackets', array( 'escape', 'replaceafter', 'language' => $lang ), $result );
                        return "<span class=\"editsection\">$result</span>";
                }
 
@@ -1417,7 +1418,7 @@ class Linker {
                $result = wfMsgExt( 'editsection-brackets', array( 'escape', 'replaceafter', 'language' => $lang ), $link );
                $result = "<span class=\"editsection\">$result</span>";
 
-               wfRunHooks( 'DoEditSectionLink', array( $this, $nt, $section, $tooltip, &$result ) );
+               wfRunHooks( 'DoEditSectionLink', array( $this, $nt, $section, $tooltip, &$result, $lang ) );
                return $result;
        }
 
@@ -1498,17 +1499,20 @@ class Linker {
                $title = $rev->getTitle();
                $query = array(
                        'action' => 'rollback',
-                       'from' => $rev->getUserText()
+                       'from' => $rev->getUserText(),
+                       'token' => $wgUser->editToken( array( $title->getPrefixedText(), $rev->getUserText() ) ),
                );
                if ( $wgRequest->getBool( 'bot' ) ) {
                        $query['bot'] = '1';
                        $query['hidediff'] = '1'; // bug 15999
                }
-               $query['token'] = $wgUser->editToken( array( $title->getPrefixedText(),
-                       $rev->getUserText() ) );
-               return $this->link( $title, wfMsgHtml( 'rollbacklink' ),
+               return $this->link( 
+                       $title, 
+                       wfMsgHtml( 'rollbacklink' ),
                        array( 'title' => wfMsg( 'tooltip-rollback' ) ),
-                       $query, array( 'known', 'noclasses' ) );
+                       $query, 
+                       array( 'known', 'noclasses' ) 
+               );
        }
 
        /**
@@ -1630,10 +1634,12 @@ class Linker {
        public function titleAttrib( $name, $options = null ) {
                wfProfileIn( __METHOD__ );
 
-               if ( wfEmptyMsg( "tooltip-$name" ) ) {
+               $message = wfMessage( "tooltip-$name" );
+
+               if ( !$message->exists() ) {
                        $tooltip = false;
                } else {
-                       $tooltip = wfMsg( "tooltip-$name" );
+                       $tooltip = $message->text();
                        # Compatibility: formerly some tooltips had [alt-.] hardcoded
                        $tooltip = preg_replace( "/ ?\[alt-.\]$/", '', $tooltip );
                        # Message equal to '-' means suppress it.
@@ -1670,10 +1676,16 @@ class Linker {
        public function accesskey( $name ) {
                wfProfileIn( __METHOD__ );
 
-               if ( wfEmptyMsg( "accesskey-$name" ) ) {
+               if ( isset( $this->accesskeycache[$name] ) ) {
+                       return $this->accesskeycache[$name];
+               }
+
+               $message = wfMessage( "accesskey-$name" );
+
+               if ( !$message->exists() ) {
                        $accesskey = false;
                } else {
-                       $accesskey = wfMsg( "accesskey-$name" );
+                       $accesskey = $message->plain();
                        if ( $accesskey === '' || $accesskey === '-' ) {
                                # FIXME: Per standard MW behavior, a value of '-' means to suppress the
                                # attribute, but this is broken for accesskey: that might be a useful
@@ -1683,7 +1695,7 @@ class Linker {
                }
 
                wfProfileOut( __METHOD__ );
-               return $accesskey;
+               return $this->accesskeycache[$name] = $accesskey;
        }
 
        /**
@@ -1743,7 +1755,7 @@ class Linker {
         */
        function makeLink( $title, $text = '', $query = '', $trail = '' ) {
                wfProfileIn( __METHOD__ );
-               $nt = Title::newFromText( $title );
+               $nt = Title::newFromText( $title );
                if ( $nt instanceof Title ) {
                        $result = $this->makeLinkObj( $nt, $text, $query, $trail );
                } else {
@@ -1936,7 +1948,6 @@ class Linker {
         * @param $prefix String: Optional prefix
         */
        function makeStubLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
-               // wfDeprecated( __METHOD__ );
                return $this->makeColouredLinkObj( $nt, 'stub', $text, $query, $trail, $prefix );
        }
 
@@ -1955,7 +1966,6 @@ class Linker {
         * @param $prefix String: Optional prefix
         */
        function makeColouredLinkObj( $nt, $colour, $text = '', $query = '', $trail = '', $prefix = '' ) {
-               // wfDeprecated( __METHOD__ );
                if ( $colour != '' ) {
                        $style = $this->getInternalLinkAttributesObj( $nt, $text, $colour );
                } else $style = '';
@@ -2072,7 +2082,6 @@ class Linker {
                return Xml::expandAttributes( $this->tooltipAndAccesskeyAttribs( $name ) );
        }
 
-
        /** @deprecated Returns raw bits of HTML, use titleAttrib() */
        public function tooltip( $name, $options = null ) {
                global $wgEnableTooltipsAndAccesskeys;