From: Kunal Mehta Date: Thu, 26 May 2016 20:29:02 +0000 (-0700) Subject: TitleParser: In formatTitle(), don't throw exceptions on bad namespaces X-Git-Tag: 1.31.0-rc.0~6806^2 X-Git-Url: https://git.cyclocoop.org/%7B%24admin_url%7Dmembres/%7B%7B%20url_for%28%27vote%27%2C%20idvote=vote.voteid%29%20%7D%7D?a=commitdiff_plain;h=a22ab0eab02c6de1335ded722cf5b712fc79504a;p=lhc%2Fweb%2Fwiklou.git TitleParser: In formatTitle(), don't throw exceptions on bad namespaces This ocassionally happens for whatever reason, but it doesn't really make sense to throw an exception when creating a broken-looking link would also work. We already do this for TitleParser::getPrefixedDBkey(), and this also matches the behavior of Title::getNsText(). Bug: T136352 Bug: T136356 Change-Id: Ic7eb17f8917f7fbb28b11d94b742dac1fe5582a1 --- diff --git a/includes/title/MediaWikiTitleCodec.php b/includes/title/MediaWikiTitleCodec.php index 38e9ecdacf..a937e757f1 100644 --- a/includes/title/MediaWikiTitleCodec.php +++ b/includes/title/MediaWikiTitleCodec.php @@ -105,10 +105,16 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser { */ public function formatTitle( $namespace, $text, $fragment = '', $interwiki = '' ) { if ( $namespace !== false ) { - $namespace = $this->getNamespaceName( $namespace, $text ); + // Try to get a namespace name, but fallback + // to empty string if it doesn't exist + try { + $nsName = $this->getNamespaceName( $namespace, $text ); + } catch ( InvalidArgumentException $e ) { + $nsName = ''; + } - if ( $namespace !== '' ) { - $text = $namespace . ':' . $text; + if ( $namespace !== 0 ) { + $text = $nsName . ':' . $text; } } diff --git a/tests/phpunit/includes/title/MediaWikiTitleCodecTest.php b/tests/phpunit/includes/title/MediaWikiTitleCodecTest.php index e55a3a4438..4d914e4680 100644 --- a/tests/phpunit/includes/title/MediaWikiTitleCodecTest.php +++ b/tests/phpunit/includes/title/MediaWikiTitleCodecTest.php @@ -164,6 +164,7 @@ class MediaWikiTitleCodecTest extends MediaWikiTestCase { // 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' ], ]; }