27private LanguageNameUtils $languageNameUtils;
31 LanguageNameUtils $languageNameUtils,
34 parent::__construct(
'MyLanguage' );
35 $this->languageNameUtils = $languageNameUtils;
36 $this->redirectLookup = $redirectLookup;
48// Go to the main page if given invalid title. 50 $title = Title::newMainPage();
71return $this->findTitleInternal( $subpage,
false );
89return $this->findTitleInternal( $subpage,
true );
100privatefunction findTitleInternal( $subpage, $forTransclusion ) {
101// base = title without the language code suffix 102// provided = the title as it was given 103 $base = $provided =
null;
104if ( $subpage !==
null ) {
105 $provided = Title::newFromText( $subpage );
108if ( $provided && str_contains( $subpage,
'/' ) ) {
109 $pos = strrpos( $subpage,
'/' );
110 $basepage = substr( $subpage, 0, $pos );
111 $code = substr( $subpage, $pos + 1 );
112if ( $code !==
'' && $this->languageNameUtils->isKnownLanguageTag( $code ) ) {
113 $base = Title::newFromText( $basepage );
118if ( !$base || !$base->canExist() ) {
119// No subpage provided or base page does not exist 124if ( $base->isRedirect() ) {
125 $target = $this->redirectLookup->getRedirectTarget( $base );
126if ( $target !==
null ) {
127 $base = Title::newFromLinkTarget( $target );
128// Preserve the fragment from the redirect target 129 $fragment = $base->getFragment();
134 $baseLang = $base->getPageLanguage();
136// T309329: Always use subpages for transclusion 137if ( !$forTransclusion && $baseLang->equals( $uiLang ) ) {
138// Short circuit when the current UI language is the 139// page's content language to avoid unnecessary page lookups. 143// Check for a subpage in the current UI language 144 $proposed = $base->getSubpage( $uiLang->getCode() );
145if ( $proposed && $proposed->exists() ) {
146if ( $fragment !==
'' ) {
147 $proposed->setFragment( $fragment );
152// Explicit language code given and the page exists 153if ( $provided !== $base && $provided->exists() ) {
154// Not based on the redirect target, don't need the fragment 158// Check for fallback languages specified by the UI language 159 $possibilities = $uiLang->getFallbackLanguages();
160foreach ( $possibilities as $lang ) {
161// $base already include fragments 162// T309329: Always use subpages for transclusion 163// T333187: Do not ignore base language page if matched 164if ( !$forTransclusion && $lang === $baseLang->getCode() ) {
167// Look for subpages if is for transclusion or didn't match base page language 168 $proposed = $base->getSubpage( $lang );
169if ( $proposed && $proposed->exists() ) {
170if ( $fragment !==
'' ) {
171 $proposed->setFragment( $fragment );
177// When all else has failed, return the base page