diff --git a/includes/specials/SpecialMyLanguage.php b/includes/specials/SpecialMyLanguage.php index 7e389cc4a1e..061b59f0ce6 100644 --- a/includes/specials/SpecialMyLanguage.php +++ b/includes/specials/SpecialMyLanguage.php @@ -138,8 +138,11 @@ class SpecialMyLanguage extends RedirectSpecialArticle { return null; } + $fragment = ''; if ( $base->isRedirect() ) { $base = $this->redirectLookup->getRedirectTarget( $base ); + // Preserve the fragment from the redirect target + $fragment = $base->getFragment(); } $uiLang = $this->getLanguage(); @@ -154,11 +157,15 @@ class SpecialMyLanguage extends RedirectSpecialArticle { // Check for a subpage in current UI language $proposed = $base->getSubpage( $uiLang->getCode() ); if ( $proposed && $proposed->exists() ) { + if ( $fragment !== '' ) { + $proposed->setFragment( $fragment ); + } return $proposed; } + // Explicit language code given and the page exists if ( $provided !== $base && $provided->exists() ) { - // Explicit language code given and the page exists + // Not based on the redirect target, don't need the fragment return $provided; } @@ -168,6 +175,9 @@ class SpecialMyLanguage extends RedirectSpecialArticle { if ( $forTransclusion || $lang !== $baseLang->getCode() ) { $proposed = $base->getSubpage( $lang ); if ( $proposed && $proposed->exists() ) { + if ( $fragment !== '' ) { + $proposed->setFragment( $fragment ); + } return $proposed; } } diff --git a/tests/phpunit/includes/specials/SpecialMyLanguageTest.php b/tests/phpunit/includes/specials/SpecialMyLanguageTest.php index 59dd5d9bfea..f8fa048ecf1 100644 --- a/tests/phpunit/includes/specials/SpecialMyLanguageTest.php +++ b/tests/phpunit/includes/specials/SpecialMyLanguageTest.php @@ -14,17 +14,21 @@ class SpecialMyLanguageTest extends MediaWikiIntegrationTestCase { 'Page/Another/zh', 'Page/Foreign', 'Page/Foreign/en', + 'Page/Redirect', ]; // In the real-world, they are in respective languages, // but we don't need to set all of them for tests. $pageLang = [ 'Page/Foreign' => 'sq', ]; + $pageContent = [ + 'Page/Redirect' => '#REDIRECT [[Page/Another#Section]]', + ]; $user = $this->getTestSysop()->getAuthority(); foreach ( $titles as $title ) { $this->editPage( $title, - new WikitextContent( 'UTContent' ), + new WikitextContent( $pageContent[$title] ?? 'UTContent' ), 'UTPageSummary', NS_MAIN, $user @@ -65,10 +69,12 @@ class SpecialMyLanguageTest extends MediaWikiIntegrationTestCase { * @param Title|null $title */ private function assertTitle( $expected, $title ) { - if ( $title ) { - $title = $title->getPrefixedText(); + if ( $expected === null ) { + $this->assertNull( $title ); + } else { + $expected = Title::newFromText( $expected ); + $this->assertTrue( $expected->isSameLinkAs( $title ) ); } - $this->assertEquals( $expected, $title ); } public static function provideFindTitle() { @@ -94,6 +100,7 @@ class SpecialMyLanguageTest extends MediaWikiIntegrationTestCase { [ null, 'Special:Blankpage', 'en', 'ar' ], [ null, 'Media:Fail', 'en', 'ar' ], [ 'Page/Foreign/en', 'Page/Foreign', 'en', 'en' ], + [ 'Page/Another/ar#Section', 'Page/Redirect', 'en', 'ar' ], ]; }