MediaWiki master
TrivialLanguageConverter.php
Go to the documentation of this file.
1<?php
26
41
45 protected $language;
46
50 private $titleFormatter;
51
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 ) {
92 // T165149: see TitleFormatter::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( PageIdentity $page ) {
165 }
166
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}
const NS_SPECIAL
Definition Defines.php:54
Base class for language-specific code.
Definition Language.php:81
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.
translate( $text, $variant)
Translate a string to a variant.
hasVariants()
Check if this is a language with variants.
convert( $t)
Convert text to different variants of a language.
getURLVariant()
Get the variant specified in the URL.
getPreferredVariant()
Get preferred language variant.
markNoConversion( $text, $noParse=false)
Enclose a string with the "no conversion" tag.
autoConvertToAllVariants( $text)
Call translate() to convert text to all valid variants.
__construct( $langobj, ?TitleFormatter $titleFormatter=null)
Creates a converter for languages that don't have variants.
getConvRuleTitle()
Get the title produced by the conversion rule.
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.
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.
getExtraHashOptions()
Returns language specific hash options.
guessVariant( $text, $variant)
Guess if a text is written in a variant.
convertTo( $text, $variant, bool $clearState=true)
Same as convert() except a extra parameter to custom variant.
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.
getVariants()
Get all valid variants.
convertNamespace( $index, $variant=null)
Get the namespace display name in the preferred variant.
hasVariant( $variant)
Strict check if the language has the specific variant.
getVariantFallbacks( $variant)
In case some variant is not defined in the markup, we need to have some fallback.
The shared interface for all language converters.
Interface for objects (potentially) representing an editable wiki page.