Linker: Accept LinkTarget objects in link()
[lhc/web/wiklou.git] / includes / Linker.php
index f2e4ac4..8d2829f 100644 (file)
@@ -76,7 +76,7 @@ class Linker {
         * @since 1.18 Method exists since 1.16 as non-static, made static in 1.18.
         * @deprecated since 1.28, use MediaWiki\Linker\LinkRenderer instead
         *
-        * @param Title $target Can currently only be a Title, but this may
+        * @param LinkTarget $target Can currently only be a LinkTarget, but this may
         *   change to support Images, literal URLs, etc.
         * @param string $html The HTML contents of the <a> element, i.e.,
         *   the link text.  This is raw HTML and will not be escaped.  If null,
@@ -107,8 +107,8 @@ class Linker {
        public static function link(
                $target, $html = null, $customAttribs = [], $query = [], $options = []
        ) {
-               if ( !$target instanceof Title ) {
-                       wfWarn( __METHOD__ . ': Requires $target to be a Title object.', 2 );
+               if ( !$target instanceof LinkTarget ) {
+                       wfWarn( __METHOD__ . ': Requires $target to be a LinkTarget object.', 2 );
                        return "<!-- ERROR -->$html";
                }
 
@@ -154,6 +154,11 @@ class Linker {
         * @since 1.16.3
         * @deprecated since 1.28, use MediaWiki\Linker\LinkRenderer instead
         * @see Linker::link
+        * @param Title $target
+        * @param string $html
+        * @param array $customAttribs
+        * @param array $query
+        * @param string|array $options
         * @return string
         */
        public static function linkKnown(
@@ -1286,9 +1291,7 @@ class Linker {
                                                        if ( $target->getText() == '' && !$target->isExternal()
                                                                && !$local && $title
                                                        ) {
-                                                               $newTarget = clone $title;
-                                                               $newTarget->setFragment( '#' . $target->getFragment() );
-                                                               $target = $newTarget;
+                                                               $target = $title->createFragmentTarget( $target->getFragment() );
                                                        }
 
                                                        $thelink = Linker::makeCommentLink( $target, $linkText . $inside, $wikiId ) . $trail;
@@ -1328,7 +1331,7 @@ class Linker {
                Title $title, $text, $wikiId = null, $options = []
        ) {
                if ( $wikiId !== null && !$title->isExternal() ) {
-                       $link = Linker::makeExternalLink(
+                       $link = self::makeExternalLink(
                                WikiMap::getForeignURL(
                                        $wikiId,
                                        $title->getNamespace() === 0
@@ -1341,7 +1344,7 @@ class Linker {
                                /* escape = */ false // Already escaped
                        );
                } else {
-                       $link = Linker::link( $title, $text, [], [], $options );
+                       $link = self::link( $title, $text, [], [], $options );
                }
 
                return $link;
@@ -1350,7 +1353,7 @@ class Linker {
        /**
         * @param Title $contextTitle
         * @param string $target
-        * @param string $text
+        * @param string &$text
         * @return string
         */
        public static function normalizeSubpageLink( $contextTitle, $target, &$text ) {
@@ -1608,23 +1611,27 @@ class Linker {
         *   a space and ending with '>'
         *   This *must* be at least '>' for no attribs
         * @param string $anchor The anchor to give the headline (the bit after the #)
-        * @param string $html Html for the text of the header
+        * @param string $html HTML for the text of the header
         * @param string $link HTML to add for the section edit link
-        * @param bool|string $legacyAnchor A second, optional anchor to give for
+        * @param string|bool $fallbackAnchor A second, optional anchor to give for
         *   backward compatibility (false to omit)
         *
         * @return string HTML headline
         */
        public static function makeHeadline( $level, $attribs, $anchor, $html,
-               $link, $legacyAnchor = false
+               $link, $fallbackAnchor = false
        ) {
+               $anchorEscaped = htmlspecialchars( $anchor );
+               $fallback = '';
+               if ( $fallbackAnchor !== false && $fallbackAnchor !== $anchor ) {
+                       $fallbackAnchor = htmlspecialchars( $fallbackAnchor );
+                       $fallback = "<span id=\"$fallbackAnchor\"></span>";
+               }
                $ret = "<h$level$attribs"
-                       . "<span class=\"mw-headline\" id=\"$anchor\">$html</span>"
+                       . "$fallback<span class=\"mw-headline\" id=\"$anchorEscaped\">$html</span>"
                        . $link
                        . "</h$level>";
-               if ( $legacyAnchor !== false ) {
-                       $ret = "<div id=\"$legacyAnchor\"></div>$ret";
-               }
+
                return $ret;
        }
 
@@ -2021,7 +2028,7 @@ class Linker {
                }
 
                if ( !$rev->userCan( Revision::DELETED_RESTRICTED, $user ) ) {
-                       return Linker::revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
+                       return self::revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
                } else {
                        if ( $rev->getId() ) {
                                // RevDelete links using revision ID are stable across
@@ -2040,7 +2047,7 @@ class Linker {
                                        'ids' => $rev->getTimestamp()
                                ];
                        }
-                       return Linker::revDeleteLink( $query,
+                       return self::revDeleteLink( $query,
                                $rev->isDeleted( Revision::DELETED_RESTRICTED ), $canHide );
                }
        }