MediaWiki REL1_39
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 ) {
70 $title = Title::newMainPage();
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 && strpos( $subpage, '/' ) !== false ) {
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 if ( $base->isRedirect() ) {
142 $base = $this->redirectLookup->getRedirectTarget( $base );
143 }
144
145 $uiLang = $this->getLanguage();
146 $contLang = $this->getContentLanguage();
147
148 if ( $uiLang->equals( $contLang ) && !$forTransclusion ) {
149 // Short circuit when the current UI language is the
150 // wiki's default language to avoid unnecessary page lookups.
151 return $base;
152 }
153
154 // Check for a subpage in current UI language
155 $proposed = $base->getSubpage( $uiLang->getCode() );
156 if ( $proposed && $proposed->exists() ) {
157 return $proposed;
158 }
159
160 if ( $provided !== $base && $provided->exists() ) {
161 // Explicit language code given and the page exists
162 return $provided;
163 }
164
165 // Check for fallback languages specified by the UI language
166 $possibilities = $uiLang->getFallbackLanguages();
167 foreach ( $possibilities as $lang ) {
168 if ( $forTransclusion || $lang !== $contLang->getCode() ) {
169 $proposed = $base->getSubpage( $lang );
170 if ( $proposed && $proposed->exists() ) {
171 return $proposed;
172 }
173 }
174 }
175
176 // When all else has failed, return the base page
177 return $base;
178 }
179
188 return true;
189 }
190}
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.
getContentLanguage()
Shortcut to get content language.
Service for resolving a wiki page redirect.
if(!isset( $args[0])) $lang