Fix HTMLFormField calling Message::setContext with null
[lhc/web/wiklou.git] / includes / Linker.php
index 8e79d9c..071f95e 100644 (file)
@@ -19,6 +19,7 @@
  *
  * @file
  */
+use MediaWiki\Linker\LinkTarget;
 
 /**
  * Some internal bits split of from Skin.php. These functions are used
@@ -270,24 +271,23 @@ class Linker {
        /**
         * Returns the Url used to link to a Title
         *
-        * @param Title $target
+        * @param LinkTarget $target
         * @param array $query Query parameters
         * @param array $options
         * @return string
         */
-       private static function linkUrl( $target, $query, $options ) {
+       private static function linkUrl( LinkTarget $target, $query, $options ) {
                # We don't want to include fragments for broken links, because they
                # generally make no sense.
                if ( in_array( 'broken', $options, true ) && $target->hasFragment() ) {
-                       $target = clone $target;
-                       $target->setFragment( '' );
+                       $target = $target->createFragmentTarget( '' );
                }
 
                # If it's a broken link, add the appropriate query pieces, unless
                # there's already an action specified, or unless 'edit' makes no sense
                # (i.e., for a nonexistent special page).
                if ( in_array( 'broken', $options, true ) && empty( $query['action'] )
-                       && !$target->isSpecialPage() ) {
+                       && $target->getNamespace() !== NS_SPECIAL ) {
                        $query['action'] = 'edit';
                        $query['redlink'] = '1';
                }
@@ -300,7 +300,8 @@ class Linker {
                        $proto = PROTO_RELATIVE;
                }
 
-               $ret = $target->getLinkURL( $query, false, $proto );
+               $title = Title::newFromLinkTarget( $target );
+               $ret = $title->getLinkURL( $query, false, $proto );
                return $ret;
        }
 
@@ -445,19 +446,19 @@ class Linker {
        }
 
        /**
-        * @param Title $title
-        * @return Title
+        * @param LinkTarget $target
+        * @return LinkTarget|Title You will get back the same type you passed in, or a Title object
         */
-       static function normaliseSpecialPage( Title $title ) {
-               if ( $title->isSpecialPage() ) {
-                       list( $name, $subpage ) = SpecialPageFactory::resolveAlias( $title->getDBkey() );
+       static function normaliseSpecialPage( LinkTarget $target ) {
+               if ( $target->getNamespace() == NS_SPECIAL ) {
+                       list( $name, $subpage ) = SpecialPageFactory::resolveAlias( $target->getDBkey() );
                        if ( !$name ) {
-                               return $title;
+                               return $target;
                        }
-                       $ret = SpecialPage::getTitleFor( $name, $subpage, $title->getFragment() );
+                       $ret = SpecialPage::getTitleFor( $name, $subpage, $target->getFragment() );
                        return $ret;
                } else {
-                       return $title;
+                       return $target;
                }
        }
 
@@ -992,7 +993,7 @@ class Linker {
         */
        public static function makeMediaLinkFile( Title $title, $file, $html = '' ) {
                if ( $file && $file->exists() ) {
-                       $url = $file->getURL();
+                       $url = $file->getUrl();
                        $class = 'internal';
                } else {
                        $url = self::getUploadUrl( $title );
@@ -1449,31 +1450,34 @@ class Linker {
                                        }
                                } else {
                                        # Other kind of link
-                                       if ( preg_match( $wgContLang->linkTrail(), $match[3], $submatch ) ) {
-                                               $trail = $submatch[1];
-                                       } else {
-                                               $trail = "";
-                                       }
-                                       $linkRegexp = '/\[\[(.*?)\]\]' . preg_quote( $trail, '/' ) . '/';
+                                       # Make sure its target is non-empty
                                        if ( isset( $match[1][0] ) && $match[1][0] == ':' ) {
                                                $match[1] = substr( $match[1], 1 );
                                        }
-                                       list( $inside, $trail ) = Linker::splitTrail( $trail );
-
-                                       $linkText = $text;
-                                       $linkTarget = Linker::normalizeSubpageLink( $title, $match[1], $linkText );
-
-                                       $target = Title::newFromText( $linkTarget );
-                                       if ( $target ) {
-                                               if ( $target->getText() == '' && !$target->isExternal()
-                                                       && !$local && $title
-                                               ) {
-                                                       $newTarget = clone $title;
-                                                       $newTarget->setFragment( '#' . $target->getFragment() );
-                                                       $target = $newTarget;
+                                       if ( $match[1] !== false && $match[1] !== '' ) {
+                                               if ( preg_match( $wgContLang->linkTrail(), $match[3], $submatch ) ) {
+                                                       $trail = $submatch[1];
+                                               } else {
+                                                       $trail = "";
                                                }
+                                               $linkRegexp = '/\[\[(.*?)\]\]' . preg_quote( $trail, '/' ) . '/';
+                                               list( $inside, $trail ) = Linker::splitTrail( $trail );
+
+                                               $linkText = $text;
+                                               $linkTarget = Linker::normalizeSubpageLink( $title, $match[1], $linkText );
+
+                                               $target = Title::newFromText( $linkTarget );
+                                               if ( $target ) {
+                                                       if ( $target->getText() == '' && !$target->isExternal()
+                                                               && !$local && $title
+                                                       ) {
+                                                               $newTarget = clone $title;
+                                                               $newTarget->setFragment( '#' . $target->getFragment() );
+                                                               $target = $newTarget;
+                                                       }
 
-                                               $thelink = Linker::makeCommentLink( $target, $linkText . $inside, $wikiId ) . $trail;
+                                                       $thelink = Linker::makeCommentLink( $target, $linkText . $inside, $wikiId ) . $trail;
+                                               }
                                        }
                                }
                                if ( $thelink ) {
@@ -2357,20 +2361,3 @@ class Linker {
 
 }
 
-/**
- * @since 1.18
- */
-class DummyLinker {
-
-       /**
-        * Use PHP's magic __call handler to transform instance calls to a dummy instance
-        * into static calls to the new Linker for backwards compatibility.
-        *
-        * @param string $fname Name of called method
-        * @param array $args Arguments to the method
-        * @return mixed
-        */
-       public function __call( $fname, $args ) {
-               return call_user_func_array( [ 'Linker', $fname ], $args );
-       }
-}