MediaWiki  master
SpecialMyLanguage.php
Go to the documentation of this file.
1 <?php
28 
39 
41  private $languageNameUtils;
42 
44  private $redirectLookup;
45 
50  public function __construct(
51  LanguageNameUtils $languageNameUtils,
52  RedirectLookup $redirectLookup
53  ) {
54  parent::__construct( 'MyLanguage' );
55  $this->languageNameUtils = $languageNameUtils;
56  $this->redirectLookup = $redirectLookup;
57  }
58 
66  public function getRedirect( $subpage ) {
67  $title = $this->findTitle( $subpage );
68  // Go to the main page if given invalid title.
69  if ( !$title ) {
71  }
72  return $title;
73  }
74 
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 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  $base = $this->redirectLookup->getRedirectTarget( $base );
144  // Preserve the fragment from the redirect target
145  $fragment = $base->getFragment();
146  }
147 
148  $uiLang = $this->getLanguage();
149  $baseLang = $base->getPageLanguage();
150 
151  // $baseLang can be StubUserLang, this order would pass the typehint on Language::equals - T326400
152  if ( !$forTransclusion && $baseLang->equals( $uiLang ) ) {
153  // Short circuit when the current UI language is the
154  // page's content language to avoid unnecessary page lookups.
155  return $base;
156  }
157 
158  // Check for a subpage in current UI language
159  $proposed = $base->getSubpage( $uiLang->getCode() );
160  if ( $proposed && $proposed->exists() ) {
161  if ( $fragment !== '' ) {
162  $proposed->setFragment( $fragment );
163  }
164  return $proposed;
165  }
166 
167  // Explicit language code given and the page exists
168  if ( $provided !== $base && $provided->exists() ) {
169  // Not based on the redirect target, don't need the fragment
170  return $provided;
171  }
172 
173  // Check for fallback languages specified by the UI language
174  $possibilities = $uiLang->getFallbackLanguages();
175  foreach ( $possibilities as $lang ) {
176  if ( $forTransclusion || $lang !== $baseLang->getCode() ) {
177  $proposed = $base->getSubpage( $lang );
178  if ( $proposed && $proposed->exists() ) {
179  if ( $fragment !== '' ) {
180  $proposed->setFragment( $fragment );
181  }
182  return $proposed;
183  }
184  }
185  }
186 
187  // When all else has failed, return the base page
188  return $base;
189  }
190 
198  public function personallyIdentifiableTarget() {
199  return true;
200  }
201 }
A service that provides utilities to do with language names and codes.
Superclass for any RedirectSpecialPage which redirects the user to a particular article (as opposed t...
Unlisted special page just to redirect the user to the translated version of a page,...
findTitle( $subpage)
Find a title.
__construct(LanguageNameUtils $languageNameUtils, RedirectLookup $redirectLookup)
getRedirect( $subpage)
If the special page is a redirect, then get the Title object it redirects to.
personallyIdentifiableTarget()
Target can identify a specific user's language preference.
findTitleForTransclusion( $subpage)
Find a title for transclusion.
getLanguage()
Shortcut to get user's language.
static newMainPage(MessageLocalizer $localizer=null)
Create a new Title for the Main Page.
Definition: Title.php:703
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:373
Service for resolving a wiki page redirect.
if(!isset( $args[0])) $lang