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
48 public function __construct(
49 LanguageNameUtils $languageNameUtils,
50 RedirectLookup $redirectLookup
51 ) {
52 parent::__construct( 'MyLanguage' );
53 $this->languageNameUtils = $languageNameUtils;
54 $this->redirectLookup = $redirectLookup;
55 }
56
64 public function getRedirect( $subpage ) {
65 $title = $this->findTitle( $subpage );
66 // Go to the main page if given invalid title.
67 if ( !$title ) {
68 $title = Title::newMainPage();
69 }
70 return $title;
71 }
72
88 public function findTitle( $subpage ) {
89 return $this->findTitleInternal( $subpage, false );
90 }
91
106 public function findTitleForTransclusion( $subpage ) {
107 return $this->findTitleInternal( $subpage, true );
108 }
109
118 private function findTitleInternal( $subpage, $forTransclusion ) {
119 // base = title without the language code suffix
120 // provided = the title as it was given
121 $base = $provided = null;
122 if ( $subpage !== null ) {
123 $provided = Title::newFromText( $subpage );
124 $base = $provided;
125
126 if ( $provided && str_contains( $subpage, '/' ) ) {
127 $pos = strrpos( $subpage, '/' );
128 $basepage = substr( $subpage, 0, $pos );
129 $code = substr( $subpage, $pos + 1 );
130 if ( strlen( $code ) && $this->languageNameUtils->isKnownLanguageTag( $code ) ) {
131 $base = Title::newFromText( $basepage );
132 }
133 }
134 }
135
136 if ( !$base || !$base->canExist() ) {
137 // No subpage provided or base page does not exist
138 return null;
139 }
140
141 $fragment = '';
142 if ( $base->isRedirect() ) {
143 $target = $this->redirectLookup->getRedirectTarget( $base );
144 if ( $target !== null ) {
145 $base = Title::newFromLinkTarget( $target );
146 // Preserve the fragment from the redirect target
147 $fragment = $base->getFragment();
148 }
149 }
150
151 $uiLang = $this->getLanguage();
152 $baseLang = $base->getPageLanguage();
153
154 // T309329: Always use subpages for transclusion
155 if ( !$forTransclusion && $baseLang->equals( $uiLang ) ) {
156 // Short circuit when the current UI language is the
157 // page's content language to avoid unnecessary page lookups.
158 return $base;
159 }
160
161 // Check for a subpage in the current UI language
162 $proposed = $base->getSubpage( $uiLang->getCode() );
163 if ( $proposed && $proposed->exists() ) {
164 if ( $fragment !== '' ) {
165 $proposed->setFragment( $fragment );
166 }
167 return $proposed;
168 }
169
170 // Explicit language code given and the page exists
171 if ( $provided !== $base && $provided->exists() ) {
172 // Not based on the redirect target, don't need the fragment
173 return $provided;
174 }
175
176 // Check for fallback languages specified by the UI language
177 $possibilities = $uiLang->getFallbackLanguages();
178 foreach ( $possibilities as $lang ) {
179 // $base already include fragments
180 // T309329: Always use subpages for transclusion
181 // T333187: Do not ignore base language page if matched
182 if ( !$forTransclusion && $lang === $baseLang->getCode() ) {
183 return $base;
184 }
185 // Look for subpages if is for transclusion or didn't match base page language
186 $proposed = $base->getSubpage( $lang );
187 if ( $proposed && $proposed->exists() ) {
188 if ( $fragment !== '' ) {
189 $proposed->setFragment( $fragment );
190 }
191 return $proposed;
192 }
193 }
194
195 // When all else has failed, return the base page
196 return $base;
197 }
198
207 return true;
208 }
209}
210
215class_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:79
Service for resolving a wiki page redirect.