Debian Codegraph @

includes/language/TrivialLanguageConverter.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20 use MediaWiki\Language\ILanguageConverter;
21 use MediaWiki\Language\Language;
22 use MediaWiki\Linker\LinkTarget;
23 use MediaWiki\MediaWikiServices;
24 use MediaWiki\StubObject\StubUserLang;
25 use MediaWiki\Title\TitleFormatter;
26
27 /**
28 * A trivial language converter.
29 *
30 * For Languages which do not implement variant
31 * conversion, for example, German, TrivialLanguageConverter is provided rather than a
32 * LanguageConverter when asked for their converter. The TrivialLanguageConverter just
33 * returns text unchanged, i.e., it doesn't do any conversion.
34 *
35 * See https://www.mediawiki.org/wiki/Writing_systems#LanguageConverter.
36 *
37 * @since 1.35
38 * @ingroup Language
39 */
40 class TrivialLanguageConverter implements ILanguageConverter {
41
42 /**
43 * @var Language
44 */
45 protected $language;
46
47 /**
48 * @var TitleFormatter
49 */
50 private $titleFormatter;
51
52 /**
53 * Creates a converter for languages that don't have variants. This method is internal
54 * and should be called for LanguageConverterFactory only
55 *
56 * @param Language|StubUserLang $langobj
57 * @param TitleFormatter|null $titleFormatter
58 *
59 * @internal
60 */
61 public function __construct(
62 $langobj,
63 ?TitleFormatter $titleFormatter = null
64 ) {
65 $this->language = $langobj;
66 $this->titleFormatter = $titleFormatter ?? MediaWikiServices::getInstance()->getTitleFormatter();
67 }
68
69 public function autoConvert( $text, $variant = false ) {
70 return $text;
71 }
72
73 public function autoConvertToAllVariants( $text ) {
74 return [ $this->language->getCode() => $text ];
75 }
76
77 public function convert( $t ) {
78 return $t;
79 }
80
81 public function convertTo( $text, $variant, bool $clearState = true ) {
82 return $text;
83 }
84
85 public function convertSplitTitle( $title ) {
86 $mainText = $this->titleFormatter->getText( $title );
87
88 $index = $title->getNamespace();
89 try {
90 $nsWithUnderscores = $this->titleFormatter->getNamespaceName( $index, $mainText );
91 } catch ( InvalidArgumentException $e ) {
92 // T165149: see MediaWikiTitleCodec::formatTitle()
93 $nsWithUnderscores = $this->language->getNsText( NS_SPECIAL );
94 $mainText = "Badtitle/NS$index:$mainText";
95 }
96 $nsText = str_replace( '_', ' ', $nsWithUnderscores );
97
98 return [ $nsText, ':', $mainText ];
99 }
100
101 public function convertTitle( $title ) {
102 return $this->titleFormatter->getPrefixedText( $title );
103 }
104
105 public function convertNamespace( $index, $variant = null ) {
106 return $this->language->getFormattedNsText( $index );
107 }
108
109 public function getVariants() {
110 return [ $this->language->getCode() ];
111 }
112
113 public function getVariantFallbacks( $variant ) {
114 return $this->language->getCode();
115 }
116
117 public function getPreferredVariant() {
118 return $this->language->getCode();
119 }
120
121 public function getDefaultVariant() {
122 return $this->language->getCode();
123 }
124
125 public function getURLVariant() {
126 return '';
127 }
128
129 public function getConvRuleTitle() {
130 return false;
131 }
132
133 public function findVariantLink( &$l, &$n, $ignoreOtherCond = false ) {
134 }
135
136 public function getExtraHashOptions() {
137 return '';
138 }
139
140 public function guessVariant( $text, $variant ) {
141 return false;
142 }
143
144 public function markNoConversion( $text, $noParse = false ) {
145 return $text;
146 }
147
148 public function convertCategoryKey( $key ) {
149 return $key;
150 }
151
152 public function validateVariant( $variant = null ) {
153 if ( $variant === null ) {
154 return null;
155 }
156 $variant = strtolower( $variant );
157 return $variant === $this->language->getCode() ? $variant : null;
158 }
159
160 public function translate( $text, $variant ) {
161 return $text;
162 }
163
164 public function updateConversionTable( LinkTarget $linkTarget ) {
165 }
166
167 /**
168 * Used by test suites which need to reset the converter state.
169 *
170 * Called by ParserTestRunner with the help of TestingAccessWrapper
171 */
172 private function reloadTables() {
173 }
174
175 public function hasVariants() {
176 return count( $this->getVariants() ) > 1;
177 }
178
179 public function hasVariant( $variant ) {
180 return $variant && ( $variant === $this->validateVariant( $variant ) );
181 }
182
183 public function convertHtml( $text ) {
184 return htmlspecialchars( $this->convert( $text ) );
185 }
186 }