Merge "RevisionStoreDbTestBase, remove redundant needsDB override"
[lhc/web/wiklou.git] / includes / title / MediaWikiTitleCodec.php
index 890a870..f6a4c06 100644 (file)
@@ -79,7 +79,7 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser {
         * @param string $text
         *
         * @throws InvalidArgumentException If the namespace is invalid
-        * @return string
+        * @return string Namespace name with underscores (not spaces)
         */
        public function getNamespaceName( $namespace, $text ) {
                if ( $this->language->needsGenderDistinction() &&
@@ -112,31 +112,30 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser {
         * @return string
         */
        public function formatTitle( $namespace, $text, $fragment = '', $interwiki = '' ) {
-               if ( $namespace !== false ) {
-                       // Try to get a namespace name, but fallback
-                       // to empty string if it doesn't exist
+               $out = '';
+               if ( $interwiki !== '' ) {
+                       $out = $interwiki . ':';
+               }
+
+               if ( $namespace != 0 ) {
                        try {
                                $nsName = $this->getNamespaceName( $namespace, $text );
                        } catch ( InvalidArgumentException $e ) {
-                               $nsName = '';
+                               // See T165149. Awkward, but better than erroneously linking to the main namespace.
+                               $nsName = $this->language->getNsText( NS_SPECIAL ) . ":Badtitle/NS{$namespace}";
                        }
 
-                       if ( $namespace !== 0 ) {
-                               $text = $nsName . ':' . $text;
-                       }
+                       $out .= $nsName . ':';
                }
+               $out .= $text;
 
                if ( $fragment !== '' ) {
-                       $text = $text . '#' . $fragment;
+                       $out .= '#' . $fragment;
                }
 
-               if ( $interwiki !== '' ) {
-                       $text = $interwiki . ':' . $text;
-               }
+               $out = str_replace( '_', ' ', $out );
 
-               $text = str_replace( '_', ' ', $text );
-
-               return $text;
+               return $out;
        }
 
        /**
@@ -149,7 +148,7 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser {
         * @throws MalformedTitleException
         * @return TitleValue
         */
-       public function parseTitle( $text, $defaultNamespace ) {
+       public function parseTitle( $text, $defaultNamespace = NS_MAIN ) {
                // NOTE: this is an ugly cludge that allows this class to share the
                // code for parsing with the old Title class. The parser code should
                // be refactored to avoid this.
@@ -176,7 +175,7 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser {
         * @return string $title->getText()
         */
        public function getText( LinkTarget $title ) {
-               return $this->formatTitle( false, $title->getText(), '' );
+               return $title->getText();
        }
 
        /**
@@ -187,12 +186,16 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser {
         * @return string
         */
        public function getPrefixedText( LinkTarget $title ) {
-               return $this->formatTitle(
-                       $title->getNamespace(),
-                       $title->getText(),
-                       '',
-                       $title->getInterwiki()
-               );
+               if ( !isset( $title->prefixedText ) ) {
+                       $title->prefixedText = $this->formatTitle(
+                               $title->getNamespace(),
+                               $title->getText(),
+                               '',
+                               $title->getInterwiki()
+                       );
+               }
+
+               return $title->prefixedText;
        }
 
        /**
@@ -202,28 +205,12 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser {
         * @return string
         */
        public function getPrefixedDBkey( LinkTarget $target ) {
-               $key = '';
-               if ( $target->isExternal() ) {
-                       $key .= $target->getInterwiki() . ':';
-               }
-               // Try to get a namespace name, but fallback
-               // to empty string if it doesn't exist
-               try {
-                       $nsName = $this->getNamespaceName(
-                               $target->getNamespace(),
-                               $target->getText()
-                       );
-               } catch ( InvalidArgumentException $e ) {
-                       $nsName = '';
-               }
-
-               if ( $target->getNamespace() !== 0 ) {
-                       $key .= $nsName . ':';
-               }
-
-               $key .= $target->getText();
-
-               return strtr( $key, ' ', '_' );
+               return strtr( $this->formatTitle(
+                       $target->getNamespace(),
+                       $target->getDBkey(),
+                       '',
+                       $target->getInterwiki()
+               ), ' ', '_' );
        }
 
        /**