* @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() &&
* @return string
*/
public function formatTitle( $namespace, $text, $fragment = '', $interwiki = '' ) {
- if ( $namespace !== 0 && $namespace !== false ) {
- // Try to get a namespace name, but fallback
- // to empty string if it doesn't exist. And
- // assume that ns 0 is the empty string.
+ $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}";
}
- $text = $nsName . ':' . $text;
- }
- if ( $fragment !== '' ) {
- $text = $text . '#' . $fragment;
+ $out .= $nsName . ':';
}
+ $out .= $text;
- if ( $interwiki !== '' ) {
- $text = $interwiki . ':' . $text;
+ if ( $fragment !== '' ) {
+ $out .= '#' . $fragment;
}
- $text = str_replace( '_', ' ', $text );
+ $out = str_replace( '_', ' ', $out );
- return $text;
+ return $out;
}
/**
* @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()
+ ), ' ', '_' );
}
/**
// getGenderCache() provides a mock that considers first
// names ending in "a" to be female.
[ NS_USER, 'Lisa_Müller', '', 'de', 'Benutzerin:Lisa Müller' ],
- [ 1000000, 'Invalid_namespace', '', 'en', ':Invalid namespace' ],
+ [ 1000000, 'Invalid_namespace', '', 'en', 'Special:Badtitle/NS1000000:Invalid namespace' ],
];
}
[ NS_MAIN, 'Remote_page', '', 'remotetestiw', 'en', 'remotetestiw:Remote_page' ],
// non-existent namespace
- [ 10000000, 'Foobar', '', '', 'en', ':Foobar' ],
+ [ 10000000, 'Foobar', '', '', 'en', 'Special:Badtitle/NS10000000:Foobar' ],
];
}