* @copyright Copyright © 2010-2013 Niklas Laxström, Siebrand Mazeland
*/
+use MediaWiki\MediaWikiServices;
+
/**
* Unlisted special page just to redirect the user to the translated version of
* a page, if it exists.
}
if ( !$base ) {
+ // No subpage provided or base page does not exist
return null;
}
$base = $page->getRedirectTarget();
}
- $uiCode = $this->getLanguage()->getCode();
- $proposed = $base->getSubpage( $uiCode );
- if ( $proposed && $proposed->exists() && $uiCode !== $base->getPageLanguage()->getCode() ) {
+ $uiLang = $this->getLanguage();
+ $contLang = MediaWikiServices::getInstance()->getContentLanguage();
+
+ if ( $uiLang->equals( $contLang ) ) {
+ // Short circuit when the current UI language is the
+ // wiki's default language to avoid unnecessary page lookups.
+ return $base;
+ }
+
+ // Check for a subpage in current UI language
+ $proposed = $base->getSubpage( $uiLang->getCode() );
+ if ( $proposed && $proposed->exists() ) {
return $proposed;
- } elseif ( $provided && $provided->exists() ) {
+ }
+
+ if ( $provided !== $base && $provided->exists() ) {
+ // Explicit language code given and the page exists
return $provided;
- } else {
- return $base;
}
+
+ // Check for fallback languages specified by the UI language
+ $possibilities = $uiLang->getFallbackLanguages();
+ foreach ( $possibilities as $lang ) {
+ if ( $lang !== $contLang->getCode() ) {
+ $proposed = $base->getSubpage( $lang );
+ if ( $proposed && $proposed->exists() ) {
+ return $proposed;
+ }
+ }
+ }
+
+ // When all else has failed, return the base page
+ return $base;
}
/**