MediaWiki master
SpecialMyLanguage.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Specials;
22
27
40
41 private LanguageNameUtils $languageNameUtils;
42 private RedirectLookup $redirectLookup;
43
44 public function __construct(
45 LanguageNameUtils $languageNameUtils,
46 RedirectLookup $redirectLookup
47 ) {
48 parent::__construct( 'MyLanguage' );
49 $this->languageNameUtils = $languageNameUtils;
50 $this->redirectLookup = $redirectLookup;
51 }
52
60 public function getRedirect( $subpage ) {
61 $title = $this->findTitle( $subpage );
62 // Go to the main page if given invalid title.
63 if ( !$title ) {
64 $title = Title::newMainPage();
65 }
66 return $title;
67 }
68
84 public function findTitle( $subpage ) {
85 return $this->findTitleInternal( $subpage, false );
86 }
87
102 public function findTitleForTransclusion( $subpage ) {
103 return $this->findTitleInternal( $subpage, true );
104 }
105
114 private function findTitleInternal( $subpage, $forTransclusion ) {
115 // base = title without the language code suffix
116 // provided = the title as it was given
117 $base = $provided = null;
118 if ( $subpage !== null ) {
119 $provided = Title::newFromText( $subpage );
120 $base = $provided;
121
122 if ( $provided && str_contains( $subpage, '/' ) ) {
123 $pos = strrpos( $subpage, '/' );
124 $basepage = substr( $subpage, 0, $pos );
125 $code = substr( $subpage, $pos + 1 );
126 if ( strlen( $code ) && $this->languageNameUtils->isKnownLanguageTag( $code ) ) {
127 $base = Title::newFromText( $basepage );
128 }
129 }
130 }
131
132 if ( !$base || !$base->canExist() ) {
133 // No subpage provided or base page does not exist
134 return null;
135 }
136
137 $fragment = '';
138 if ( $base->isRedirect() ) {
139 $target = $this->redirectLookup->getRedirectTarget( $base );
140 if ( $target !== null ) {
141 $base = Title::newFromLinkTarget( $target );
142 // Preserve the fragment from the redirect target
143 $fragment = $base->getFragment();
144 }
145 }
146
147 $uiLang = $this->getLanguage();
148 $baseLang = $base->getPageLanguage();
149
150 // T309329: Always use subpages for transclusion
151 if ( !$forTransclusion && $baseLang->equals( $uiLang ) ) {
152 // Short circuit when the current UI language is the
153 // page's content language to avoid unnecessary page lookups.
154 return $base;
155 }
156
157 // Check for a subpage in the current UI language
158 $proposed = $base->getSubpage( $uiLang->getCode() );
159 if ( $proposed && $proposed->exists() ) {
160 if ( $fragment !== '' ) {
161 $proposed->setFragment( $fragment );
162 }
163 return $proposed;
164 }
165
166 // Explicit language code given and the page exists
167 if ( $provided !== $base && $provided->exists() ) {
168 // Not based on the redirect target, don't need the fragment
169 return $provided;
170 }
171
172 // Check for fallback languages specified by the UI language
173 $possibilities = $uiLang->getFallbackLanguages();
174 foreach ( $possibilities as $lang ) {
175 // $base already include fragments
176 // T309329: Always use subpages for transclusion
177 // T333187: Do not ignore base language page if matched
178 if ( !$forTransclusion && $lang === $baseLang->getCode() ) {
179 return $base;
180 }
181 // Look for subpages if is for transclusion or didn't match base page language
182 $proposed = $base->getSubpage( $lang );
183 if ( $proposed && $proposed->exists() ) {
184 if ( $fragment !== '' ) {
185 $proposed->setFragment( $fragment );
186 }
187 return $proposed;
188 }
189 }
190
191 // When all else has failed, return the base page
192 return $base;
193 }
194
203 return true;
204 }
205}
206
211class_alias( SpecialMyLanguage::class, 'SpecialMyLanguage' );
A service that provides utilities to do with language names and codes.
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:78
Service for resolving a wiki page redirect.