MediaWiki master
SpecialMyLanguage.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Specials;
8
9use MediaWiki\Languages\LanguageNameUtils;
13
26
27 private LanguageNameUtils $languageNameUtils;
28 private RedirectLookup $redirectLookup;
29
30 public function __construct(
31 LanguageNameUtils $languageNameUtils,
32 RedirectLookup $redirectLookup
33 ) {
34 parent::__construct( 'MyLanguage' );
35 $this->languageNameUtils = $languageNameUtils;
36 $this->redirectLookup = $redirectLookup;
37 }
38
46 public function getRedirect( $subpage ) {
47 $title = $this->findTitle( $subpage );
48 // Go to the main page if given invalid title.
49 if ( !$title ) {
50 $title = Title::newMainPage();
51 }
52 return $title;
53 }
54
70 public function findTitle( $subpage ) {
71 return $this->findTitleInternal( $subpage, false );
72 }
73
88 public function findTitleForTransclusion( $subpage ) {
89 return $this->findTitleInternal( $subpage, true );
90 }
91
100 private function findTitleInternal( $subpage, $forTransclusion ) {
101 // base = title without the language code suffix
102 // provided = the title as it was given
103 $base = $provided = null;
104 if ( $subpage !== null ) {
105 $provided = Title::newFromText( $subpage );
106 $base = $provided;
107
108 if ( $provided && str_contains( $subpage, '/' ) ) {
109 $pos = strrpos( $subpage, '/' );
110 $basepage = substr( $subpage, 0, $pos );
111 $code = substr( $subpage, $pos + 1 );
112 if ( $code !== '' && $this->languageNameUtils->isKnownLanguageTag( $code ) ) {
113 $base = Title::newFromText( $basepage );
114 }
115 }
116 }
117
118 if ( !$base || !$base->canExist() ) {
119 // No subpage provided or base page does not exist
120 return null;
121 }
122
123 $fragment = '';
124 if ( $base->isRedirect() ) {
125 $target = $this->redirectLookup->getRedirectTarget( $base );
126 if ( $target !== null ) {
127 $base = Title::newFromLinkTarget( $target );
128 // Preserve the fragment from the redirect target
129 $fragment = $base->getFragment();
130 }
131 }
132
133 $uiLang = $this->getLanguage();
134 $baseLang = $base->getPageLanguage();
135
136 // T309329: Always use subpages for transclusion
137 if ( !$forTransclusion && $baseLang->equals( $uiLang ) ) {
138 // Short circuit when the current UI language is the
139 // page's content language to avoid unnecessary page lookups.
140 return $base;
141 }
142
143 // Check for a subpage in the current UI language
144 $proposed = $base->getSubpage( $uiLang->getCode() );
145 if ( $proposed && $proposed->exists() ) {
146 if ( $fragment !== '' ) {
147 $proposed->setFragment( $fragment );
148 }
149 return $proposed;
150 }
151
152 // Explicit language code given and the page exists
153 if ( $provided !== $base && $provided->exists() ) {
154 // Not based on the redirect target, don't need the fragment
155 return $provided;
156 }
157
158 // Check for fallback languages specified by the UI language
159 $possibilities = $uiLang->getFallbackLanguages();
160 foreach ( $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
164 if ( !$forTransclusion && $lang === $baseLang->getCode() ) {
165 return $base;
166 }
167 // Look for subpages if is for transclusion or didn't match base page language
168 $proposed = $base->getSubpage( $lang );
169 if ( $proposed && $proposed->exists() ) {
170 if ( $fragment !== '' ) {
171 $proposed->setFragment( $fragment );
172 }
173 return $proposed;
174 }
175 }
176
177 // When all else has failed, return the base page
178 return $base;
179 }
180
189 return true;
190 }
191}
192
197class_alias( SpecialMyLanguage::class, 'SpecialMyLanguage' );
Helper for any RedirectSpecialPage which redirects the user to a particular article (as opposed to us...
getLanguage()
Shortcut to get user's language.
Redirect to the appropriate translated version of a page if it exists.
findTitleForTransclusion( $subpage)
Find a title for transclusion.
personallyIdentifiableTarget()
Target can identify a specific user's language preference.
__construct(LanguageNameUtils $languageNameUtils, RedirectLookup $redirectLookup)
getRedirect( $subpage)
If the special page is a redirect, then get the Title object it redirects to.
Represents a title within MediaWiki.
Definition Title.php:69
Service for resolving a wiki page redirect.