MediaWiki master
SpecialMyLanguage.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Specials;
8
13
26
27 public function __construct(
28 private readonly LanguageNameUtils $languageNameUtils,
29 private readonly RedirectLookup $redirectLookup,
30 ) {
31 parent::__construct( 'MyLanguage' );
32 }
33
41 public function getRedirect( $subpage ) {
42 $title = $this->findTitle( $subpage );
43 // Go to the main page if given invalid title.
44 if ( !$title ) {
45 $title = Title::newMainPage();
46 }
47 return $title;
48 }
49
65 public function findTitle( $subpage ) {
66 return $this->findTitleInternal( $subpage, false );
67 }
68
83 public function findTitleForTransclusion( $subpage ) {
84 return $this->findTitleInternal( $subpage, true );
85 }
86
95 private function findTitleInternal( $subpage, $forTransclusion ) {
96 // base = title without the language code suffix
97 // provided = the title as it was given
98 $base = $provided = null;
99 if ( $subpage !== null ) {
100 $provided = Title::newFromText( $subpage );
101 $base = $provided;
102
103 if ( $provided && str_contains( $subpage, '/' ) ) {
104 $pos = strrpos( $subpage, '/' );
105 $basepage = substr( $subpage, 0, $pos );
106 $code = substr( $subpage, $pos + 1 );
107 if ( $code !== '' && $this->languageNameUtils->isKnownLanguageTag( $code ) ) {
108 $base = Title::newFromText( $basepage );
109 }
110 }
111 }
112
113 if ( !$base || !$base->canExist() ) {
114 // No subpage provided or base page does not exist
115 return null;
116 }
117
118 $fragment = '';
119 if ( $base->isRedirect() ) {
120 $target = $this->redirectLookup->getRedirectTarget( $base );
121 if ( $target !== null ) {
122 $base = Title::newFromLinkTarget( $target );
123 // Preserve the fragment from the redirect target
124 $fragment = $base->getFragment();
125 }
126 }
127
128 $uiLang = $this->getLanguage();
129 $baseLang = $base->getPageLanguage();
130
131 // T309329: Always use subpages for transclusion
132 if ( !$forTransclusion && $baseLang->equals( $uiLang ) ) {
133 // Short circuit when the current UI language is the
134 // page's content language to avoid unnecessary page lookups.
135 return $base;
136 }
137
138 // Check for a subpage in the current UI language
139 $proposed = $base->getSubpage( $uiLang->getCode() );
140 if ( $proposed && $proposed->exists() ) {
141 if ( $fragment !== '' ) {
142 $proposed->setFragment( $fragment );
143 }
144 return $proposed;
145 }
146
147 // Explicit language code given and the page exists
148 if ( $provided !== $base && $provided->exists() ) {
149 // Not based on the redirect target, don't need the fragment
150 return $provided;
151 }
152
153 // Check for fallback languages specified by the UI language
154 $possibilities = $uiLang->getFallbackLanguages();
155 foreach ( $possibilities as $lang ) {
156 // $base already include fragments
157 // T309329: Always use subpages for transclusion
158 // T333187: Do not ignore base language page if matched
159 if ( !$forTransclusion && $lang === $baseLang->getCode() ) {
160 return $base;
161 }
162 // Look for subpages if is for transclusion or didn't match base page language
163 $proposed = $base->getSubpage( $lang );
164 if ( $proposed && $proposed->exists() ) {
165 if ( $fragment !== '' ) {
166 $proposed->setFragment( $fragment );
167 }
168 return $proposed;
169 }
170 }
171
172 // When all else has failed, return the base page
173 return $base;
174 }
175
184 return true;
185 }
186}
187
192class_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.
getRedirect( $subpage)
If the special page is a redirect, then get the Title object it redirects to.
__construct(private readonly LanguageNameUtils $languageNameUtils, private readonly RedirectLookup $redirectLookup,)
Represents a title within MediaWiki.
Definition Title.php:69
Service for resolving a wiki page redirect.