1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
namespace MediaWiki\Specials;
use MediaWiki\Languages\LanguageNameUtils;
use MediaWiki\Page\RedirectLookup;
use MediaWiki\SpecialPage\RedirectSpecialArticle;
use MediaWiki\Title\Title;
/**
* Redirect to the appropriate translated version of a page if it exists.
*
* Usage: [[Special:MyLanguage/Page name|link text]]
*
* @since 1.24
* @ingroup SpecialPage
* @author Niklas Laxström
* @author Siebrand Mazeland
* @copyright Copyright © 2010-2013 Niklas Laxström, Siebrand Mazeland
*/
class SpecialMyLanguage extends RedirectSpecialArticle {
private LanguageNameUtils $languageNameUtils;
private RedirectLookup $redirectLookup;
public function __construct(
LanguageNameUtils $languageNameUtils,
RedirectLookup $redirectLookup
) {
parent::__construct( 'MyLanguage' );
$this->languageNameUtils = $languageNameUtils;
$this->redirectLookup = $redirectLookup;
}
/**
* If the special page is a redirect, then get the Title object it redirects to.
* False otherwise.
*
* @param string|null $subpage
* @return Title
*/
public function getRedirect( $subpage ) {
$title = $this->findTitle( $subpage );
// Go to the main page if given invalid title.
if ( !$title ) {
$title = Title::newMainPage();
}
return $title;
}
/**
* Find a title.
*
* This may return the base page, e.g. if the UI and
* content language are the same.
*
* Examples, assuming the UI language is fi and the content language
* is en:
* - input Page: returns Page/fi if it exists, otherwise Page
* - input Page/de: returns Page/fi if it exists, otherwise Page/de
* if it exists, otherwise Page
*
* @param string|null $subpage
* @return Title|null
*/
public function findTitle( $subpage ) {
return $this->findTitleInternal( $subpage, false );
}
/**
* Find a title for transclusion. This avoids returning the base
* page if a suitable alternative exists.
*
* Examples, assuming the UI language is fi and the content language
* is en:
* - input Page: returns Page/fi if it exists, otherwise Page/en if
* it exists, otherwise Page
* - input Page/de: returns Page/fi if it exists, otherwise Page/de
* if it exists, otherwise Page/en if it exists, otherwise Page
*
* @param string|null $subpage
* @return Title|null
*/
public function findTitleForTransclusion( $subpage ) {
return $this->findTitleInternal( $subpage, true );
}
/**
* Find a title, depending on the content language and the user's
* interface language.
*
* @param string|null $subpage
* @param bool $forTransclusion
* @return Title|null
*/
private function findTitleInternal( $subpage, $forTransclusion ) {
// base = title without the language code suffix
// provided = the title as it was given
$base = $provided = null;
if ( $subpage !== null ) {
$provided = Title::newFromText( $subpage );
$base = $provided;
if ( $provided && str_contains( $subpage, '/' ) ) {
$pos = strrpos( $subpage, '/' );
$basepage = substr( $subpage, 0, $pos );
$code = substr( $subpage, $pos + 1 );
if ( $code !== '' && $this->languageNameUtils->isKnownLanguageTag( $code ) ) {
$base = Title::newFromText( $basepage );
}
}
}
if ( !$base || !$base->canExist() ) {
// No subpage provided or base page does not exist
return null;
}
$fragment = '';
if ( $base->isRedirect() ) {
$target = $this->redirectLookup->getRedirectTarget( $base );
if ( $target !== null ) {
$base = Title::newFromLinkTarget( $target );
// Preserve the fragment from the redirect target
$fragment = $base->getFragment();
}
}
$uiLang = $this->getLanguage();
$baseLang = $base->getPageLanguage();
// T309329: Always use subpages for transclusion
if ( !$forTransclusion && $baseLang->equals( $uiLang ) ) {
// Short circuit when the current UI language is the
// page's content language to avoid unnecessary page lookups.
return $base;
}
// Check for a subpage in the current UI language
$proposed = $base->getSubpage( $uiLang->getCode() );
if ( $proposed && $proposed->exists() ) {
if ( $fragment !== '' ) {
$proposed->setFragment( $fragment );
}
return $proposed;
}
// Explicit language code given and the page exists
if ( $provided !== $base && $provided->exists() ) {
// Not based on the redirect target, don't need the fragment
return $provided;
}
// Check for fallback languages specified by the UI language
$possibilities = $uiLang->getFallbackLanguages();
foreach ( $possibilities as $lang ) {
// $base already include fragments
// T309329: Always use subpages for transclusion
// T333187: Do not ignore base language page if matched
if ( !$forTransclusion && $lang === $baseLang->getCode() ) {
return $base;
}
// Look for subpages if is for transclusion or didn't match base page language
$proposed = $base->getSubpage( $lang );
if ( $proposed && $proposed->exists() ) {
if ( $fragment !== '' ) {
$proposed->setFragment( $fragment );
}
return $proposed;
}
}
// When all else has failed, return the base page
return $base;
}
/**
* Target can identify a specific user's language preference.
*
* @see T109724
* @since 1.27
* @return bool
*/
public function personallyIdentifiableTarget() {
return true;
}
}
/**
* Retain the old class name for backwards compatibility.
* @deprecated since 1.41
*/
class_alias( SpecialMyLanguage::class, 'SpecialMyLanguage' );
|