Create redirects to titles correctly in WikitextContentHandler
authorLiangent <liangent@gmail.com>
Sun, 30 Jun 2013 08:02:37 +0000 (08:02 +0000)
committerLiangent <liangent@gmail.com>
Tue, 2 Jul 2013 21:06:35 +0000 (21:06 +0000)
Bug: 50450
Bug: 50451
Change-Id: I6781420f01d8c90d47f4b9f3ad099cadf0f8ced5

includes/content/WikitextContentHandler.php
tests/phpunit/includes/content/WikitextContentHandlerTest.php

index e1dcc66..8be85bc 100644 (file)
@@ -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 );
        }
index 45d8140..d213251 100644 (file)
@@ -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
         */