MediaWiki master
TrivialLanguageConverter.php
Go to the documentation of this file.
1<?php
12
27
31 protected $language;
32
36 private $titleFormatter;
37
47 public function __construct(
48 $langobj,
49 ?TitleFormatter $titleFormatter = null
50 ) {
51 $this->language = $langobj;
52 $this->titleFormatter = $titleFormatter ?? MediaWikiServices::getInstance()->getTitleFormatter();
53 }
54
56 public function autoConvert( $text, $variant = false ) {
57 return $text;
58 }
59
61 public function autoConvertToAllVariants( $text ) {
62 return [ $this->language->getCode() => $text ];
63 }
64
66 public function convert( $t ) {
67 return $t;
68 }
69
71 public function convertTo( $text, $variant, bool $clearState = true ) {
72 return $text;
73 }
74
76 public function convertSplitTitle( $title ) {
77 $mainText = $this->titleFormatter->getText( $title );
78
79 $index = $title->getNamespace();
80 try {
81 $nsWithUnderscores = $this->titleFormatter->getNamespaceName( $index, $mainText );
82 } catch ( InvalidArgumentException ) {
83 // T165149: see TitleFormatter::formatTitle()
84 $nsWithUnderscores = $this->language->getNsText( NS_SPECIAL );
85 $mainText = "Badtitle/NS$index:$mainText";
86 }
87 $nsText = str_replace( '_', ' ', $nsWithUnderscores );
88
89 return [ $nsText, ':', $mainText ];
90 }
91
93 public function convertTitle( $title ) {
94 return $this->titleFormatter->getPrefixedText( $title );
95 }
96
98 public function convertNamespace( $index, $variant = null ) {
99 return $this->language->getFormattedNsText( $index );
100 }
101
103 public function getVariants() {
104 return [ $this->language->getCode() ];
105 }
106
108 public function getVariantFallbacks( $variant ) {
109 return $this->language->getCode();
110 }
111
113 public function getPreferredVariant() {
114 return $this->language->getCode();
115 }
116
118 public function getDefaultVariant() {
119 return $this->language->getCode();
120 }
121
123 public function getURLVariant() {
124 return '';
125 }
126
128 public function getConvRuleTitle() {
129 return false;
130 }
131
133 public function findVariantLink( &$l, &$n, $ignoreOtherCond = false ) {
134 }
135
137 public function getExtraHashOptions() {
138 return '';
139 }
140
142 public function guessVariant( $text, $variant ) {
143 return false;
144 }
145
147 public function markNoConversion( $text, $noParse = false ) {
148 return $text;
149 }
150
152 public function convertCategoryKey( $key ) {
153 return $key;
154 }
155
157 public function validateVariant( $variant = null ) {
158 if ( $variant === null ) {
159 return null;
160 }
161 $variant = strtolower( $variant );
162 return $variant === $this->language->getCode() ? $variant : null;
163 }
164
166 public function translate( $text, $variant ) {
167 return $text;
168 }
169
171 public function updateConversionTable( PageIdentity $page ) {
172 }
173
179 private function reloadTables() {
180 }
181
183 public function hasVariants() {
184 return count( $this->getVariants() ) > 1;
185 }
186
188 public function hasVariant( $variant ) {
189 return $variant && ( $variant === $this->validateVariant( $variant ) );
190 }
191
193 public function convertHtml( $text ) {
194 return htmlspecialchars( $this->convert( $text ) );
195 }
196}
const NS_SPECIAL
Definition Defines.php:40
Base class for language-specific code.
Definition Language.php:68
Service locator for MediaWiki core services.
Stub object for the user language.
A title formatter service for MediaWiki.
A trivial language converter.
convertHtml( $text)
Perform output conversion on a string, and encode for safe HTML output.1.35string string converted to...
translate( $text, $variant)
Translate a string to a variant.Doesn't parse rules or do any of that other stuff,...
hasVariants()
Check if this is a language with variants.1.35bool
convert( $t)
Convert text to different variants of a language.The automatic conversion is done in autoConvert()....
getURLVariant()
Get the variant specified in the URL.string|null Variant if one found, null otherwise
getPreferredVariant()
Get preferred language variant.string The preferred language code
markNoConversion( $text, $noParse=false)
Enclose a string with the "no conversion" tag.This is used by various functions in the Parser....
autoConvertToAllVariants( $text)
Call translate() to convert text to all valid variants.array Variant => converted text
__construct( $langobj, ?TitleFormatter $titleFormatter=null)
Creates a converter for languages that don't have variants.
getConvRuleTitle()
Get the title produced by the conversion rule.string|false The converted title text
convertSplitTitle( $title)
Automatically converts a LinkTarget or PageReference to a readable string in the preferred variant,...
getDefaultVariant()
This function would not be affected by user's settings.string The default variant code
findVariantLink(&$l, &$n, $ignoreOtherCond=false)
If a language supports multiple variants, it is possible that non-existing link in one variant actual...
autoConvert( $text, $variant=false)
Dictionary-based conversion.This function would not parse the conversion rules. If you want to parse ...
getExtraHashOptions()
Returns language specific hash options.string
guessVariant( $text, $variant)
Guess if a text is written in a variant.This should be implemented in subclasses.bool True if $text a...
convertTo( $text, $variant, bool $clearState=true)
Same as convert() except a extra parameter to custom variant.string Converted text
updateConversionTable(PageIdentity $page)
Refresh the cache of conversion tables when MediaWiki:Conversiontable* is updated.
validateVariant( $variant=null)
Validate the variant and return an appropriate strict internal variant code if one exists....
convertTitle( $title)
Automatically convert a LinkTarget or PageReference to a readable string in the preferred variant....
convertCategoryKey( $key)
Convert the sorting key for category links.This should make different keys that are variants of each ...
getVariants()
Get all valid variants.string[] Contains all valid variants
convertNamespace( $index, $variant=null)
Get the namespace display name in the preferred variant.string Namespace name for display
hasVariant( $variant)
Strict check if the language has the specific variant.Compare to LanguageConverter::validateVariant()...
getVariantFallbacks( $variant)
In case some variant is not defined in the markup, we need to have some fallback.For example,...
The shared interface for all language converters.
Interface for objects (potentially) representing an editable wiki page.