MediaWiki REL1_37
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 ) {
70 $title = Title::newMainPage();
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
155 return true;
156 }
157}
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,...
LanguageNameUtils $languageNameUtils
__construct(LanguageNameUtils $languageNameUtils, WikiPageFactory $wikiPageFactory)
findTitle( $subpage)
Assuming the user's interface language is fi.
WikiPageFactory $wikiPageFactory
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.
getLanguage()
Shortcut to get user's language.
getContentLanguage()
Shortcut to get content language.
if(!isset( $args[0])) $lang