From: Liangent Date: Sun, 30 Jun 2013 08:02:37 +0000 (+0000) Subject: Create redirects to titles correctly in WikitextContentHandler X-Git-Tag: 1.31.0-rc.0~19266^2 X-Git-Url: http://git.cyclocoop.org/%7B%24admin_url%7Dmes_infos.php?a=commitdiff_plain;h=ff31437d45c24e56561dba1e657019379aaa087b;p=lhc%2Fweb%2Fwiklou.git Create redirects to titles correctly in WikitextContentHandler Bug: 50450 Bug: 50451 Change-Id: I6781420f01d8c90d47f4b9f3ad099cadf0f8ced5 --- diff --git a/includes/content/WikitextContentHandler.php b/includes/content/WikitextContentHandler.php index e1dcc668e0..8be85bc7cb 100644 --- a/includes/content/WikitextContentHandler.php +++ b/includes/content/WikitextContentHandler.php @@ -59,8 +59,19 @@ class WikitextContentHandler extends TextContentHandler { * @return Content */ public function makeRedirectContent( Title $destination ) { + $optionalColon = ''; + + if ( $destination->getNamespace() == NS_CATEGORY ) { + $optionalColon = ':'; + } else { + $iw = $destination->getInterwiki(); + if ( $iw && Language::fetchLanguageName( $iw, null, 'mw' ) ) { + $optionalColon = ':'; + } + } + $mwRedir = MagicWord::get( 'redirect' ); - $redirectText = $mwRedir->getSynonym( 0 ) . ' [[' . $destination->getPrefixedText() . ']]'; + $redirectText = $mwRedir->getSynonym( 0 ) . ' [[' . $optionalColon . $destination->getFullText() . ']]'; return new WikitextContent( $redirectText ); } diff --git a/tests/phpunit/includes/content/WikitextContentHandlerTest.php b/tests/phpunit/includes/content/WikitextContentHandlerTest.php index 45d814068f..d213251583 100644 --- a/tests/phpunit/includes/content/WikitextContentHandlerTest.php +++ b/tests/phpunit/includes/content/WikitextContentHandlerTest.php @@ -60,6 +60,36 @@ class WikitextContentHandlerTest extends MediaWikiLangTestCase { ); } + /** + * @dataProvider provideMakeRedirectContent + * @param Title|string $title Title object or string for Title::newFromText() + * @param string $expected Serialized form of the content object built + */ + public function testMakeRedirectContent( $title, $expected ) { + global $wgContLang; + $wgContLang->resetNamespaces(); + + if ( is_string( $title ) ) { + $title = Title::newFromText( $title ); + } + $content = $this->handler->makeRedirectContent( $title ); + $this->assertEquals( $expected, $content->serialize() ); + } + + public static function provideMakeRedirectContent() { + return array( + array( 'Hello', '#REDIRECT [[Hello]]' ), + array( 'Template:Hello', '#REDIRECT [[Template:Hello]]' ), + array( 'Hello#section', '#REDIRECT [[Hello#section]]' ), + array( 'user:john_doe#section', '#REDIRECT [[User:John doe#section]]' ), + array( 'MEDIAWIKI:FOOBAR', '#REDIRECT [[MediaWiki:FOOBAR]]' ), + array( 'Category:Foo', '#REDIRECT [[:Category:Foo]]' ), + array( Title::makeTitle( NS_MAIN, 'en:Foo' ), '#REDIRECT [[en:Foo]]' ), + array( Title::makeTitle( NS_MAIN, 'Foo', '', 'en' ), '#REDIRECT [[:en:Foo]]' ), + array( Title::makeTitle( NS_MAIN, 'Bar', 'fragment', 'google' ), '#REDIRECT [[google:Bar#fragment]]' ), + ); + } + /** * @dataProvider dataIsSupportedFormat */