MediaWiki  master
SpecialMyLanguage.php
Go to the documentation of this file.
1 <?php
28 
39 
42 
45 
50  public function __construct(
53  ) {
54  parent::__construct( 'MyLanguage' );
55  $this->languageNameUtils = $languageNameUtils;
56  $this->wikiPageFactory = $wikiPageFactory;
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 
84  public function findTitle( $subpage ) {
85  // base = title without language code suffix
86  // provided = the title as it was given
87  $base = $provided = null;
88  if ( $subpage !== null ) {
89  $provided = Title::newFromText( $subpage );
90  $base = $provided;
91 
92  if ( $provided && strpos( $subpage, '/' ) !== false ) {
93  $pos = strrpos( $subpage, '/' );
94  $basepage = substr( $subpage, 0, $pos );
95  $code = substr( $subpage, $pos + 1 );
96  if ( strlen( $code ) && $this->languageNameUtils->isKnownLanguageTag( $code ) ) {
97  $base = Title::newFromText( $basepage );
98  }
99  }
100  }
101 
102  if ( !$base || !$base->canExist() ) {
103  // No subpage provided or base page does not exist
104  return null;
105  }
106 
107  if ( $base->isRedirect() ) {
108  $page = $this->wikiPageFactory->newFromTitle( $base );
109  $base = $page->getRedirectTarget();
110  }
111 
112  $uiLang = $this->getLanguage();
113  $contLang = $this->getContentLanguage();
114 
115  if ( $uiLang->equals( $contLang ) ) {
116  // Short circuit when the current UI language is the
117  // wiki's default language to avoid unnecessary page lookups.
118  return $base;
119  }
120 
121  // Check for a subpage in current UI language
122  $proposed = $base->getSubpage( $uiLang->getCode() );
123  if ( $proposed && $proposed->exists() ) {
124  return $proposed;
125  }
126 
127  if ( $provided !== $base && $provided->exists() ) {
128  // Explicit language code given and the page exists
129  return $provided;
130  }
131 
132  // Check for fallback languages specified by the UI language
133  $possibilities = $uiLang->getFallbackLanguages();
134  foreach ( $possibilities as $lang ) {
135  if ( $lang !== $contLang->getCode() ) {
136  $proposed = $base->getSubpage( $lang );
137  if ( $proposed && $proposed->exists() ) {
138  return $proposed;
139  }
140  }
141  }
142 
143  // When all else has failed, return the base page
144  return $base;
145  }
146 
154  public function personallyIdentifiableTarget() {
155  return true;
156  }
157 }
SpecialMyLanguage\getRedirect
getRedirect( $subpage)
If the special page is a redirect, then get the Title object it redirects to.
Definition: SpecialMyLanguage.php:66
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:395
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:37
Title\newMainPage
static newMainPage(MessageLocalizer $localizer=null)
Create a new Title for the Main Page.
Definition: Title.php:725
SpecialPage\getLanguage
getLanguage()
Shortcut to get user's language.
Definition: SpecialPage.php:830
$base
$base
Definition: generateLocalAutoload.php:11
MediaWiki\Languages\LanguageNameUtils
A service that provides utilities to do with language names and codes.
Definition: LanguageNameUtils.php:43
SpecialMyLanguage\$wikiPageFactory
WikiPageFactory $wikiPageFactory
Definition: SpecialMyLanguage.php:44
SpecialMyLanguage\$languageNameUtils
LanguageNameUtils $languageNameUtils
Definition: SpecialMyLanguage.php:41
SpecialMyLanguage\findTitle
findTitle( $subpage)
Assuming the user's interface language is fi.
Definition: SpecialMyLanguage.php:84
SpecialMyLanguage\__construct
__construct(LanguageNameUtils $languageNameUtils, WikiPageFactory $wikiPageFactory)
Definition: SpecialMyLanguage.php:50
Page\WikiPageFactory
Definition: WikiPageFactory.php:19
$title
$title
Definition: testCompression.php:38
RedirectSpecialArticle
Superclass for any RedirectSpecialPage which redirects the user to a particular article (as opposed t...
Definition: RedirectSpecialArticle.php:89
SpecialMyLanguage\personallyIdentifiableTarget
personallyIdentifiableTarget()
Target can identify a specific user's language preference.
Definition: SpecialMyLanguage.php:154
SpecialPage\getContentLanguage
getContentLanguage()
Shortcut to get content language.
Definition: SpecialPage.php:840
SpecialMyLanguage
Unlisted special page just to redirect the user to the translated version of a page,...
Definition: SpecialMyLanguage.php:38