MediaWiki  master
TrivialLanguageConverter.php
Go to the documentation of this file.
1 <?php
25 
40 
44  protected $language;
45 
49  private $titleFormatter;
50 
60  public function __construct(
61  $langobj,
62  TitleFormatter $titleFormatter = null
63  ) {
64  $this->language = $langobj;
65  $this->titleFormatter = $titleFormatter ?? MediaWikiServices::getInstance()->getTitleFormatter();
66  }
67 
68  public function autoConvert( $text, $variant = false ) {
69  return $text;
70  }
71 
72  public function autoConvertToAllVariants( $text ) {
73  return [ $this->language->getCode() => $text ];
74  }
75 
76  public function convert( $t ) {
77  return $t;
78  }
79 
80  public function convertTo( $text, $variant, bool $clearState = true ) {
81  return $text;
82  }
83 
89  public function convertSplitTitle( $title ) {
90  $mainText = $this->titleFormatter->getText( $title );
91 
92  $index = $title->getNamespace();
93  try {
94  $nsWithUnderscores = $this->titleFormatter->getNamespaceName( $index, $mainText );
95  } catch ( InvalidArgumentException $e ) {
96  // T165149: see MediaWikiTitleCodec::formatTitle()
97  $nsWithUnderscores = $this->language->getNsText( NS_SPECIAL );
98  $mainText = "Badtitle/NS$index:$mainText";
99  }
100  $nsText = str_replace( '_', ' ', $nsWithUnderscores );
101 
102  return [ $nsText, ':', $mainText ];
103  }
104 
109  public function convertTitle( $title ) {
110  return $this->titleFormatter->getPrefixedText( $title );
111  }
112 
113  public function convertNamespace( $index, $variant = null ) {
114  return $this->language->getFormattedNsText( $index );
115  }
116 
120  public function getVariants() {
121  return [ $this->language->getCode() ];
122  }
123 
124  public function getVariantFallbacks( $variant ) {
125  return $this->language->getCode();
126  }
127 
128  public function getPreferredVariant() {
129  return $this->language->getCode();
130  }
131 
132  public function getDefaultVariant() {
133  return $this->language->getCode();
134  }
135 
136  public function getURLVariant() {
137  return '';
138  }
139 
140  public function getConvRuleTitle() {
141  return false;
142  }
143 
144  public function findVariantLink( &$l, &$n, $ignoreOtherCond = false ) {
145  }
146 
147  public function getExtraHashOptions() {
148  return '';
149  }
150 
151  public function guessVariant( $text, $variant ) {
152  return false;
153  }
154 
155  public function markNoConversion( $text, $noParse = false ) {
156  return $text;
157  }
158 
159  public function convertCategoryKey( $key ) {
160  return $key;
161  }
162 
163  public function validateVariant( $variant = null ) {
164  if ( $variant === null ) {
165  return null;
166  }
167  $variant = strtolower( $variant );
168  return $variant === $this->language->getCode() ? $variant : null;
169  }
170 
171  public function translate( $text, $variant ) {
172  return $text;
173  }
174 
175  public function updateConversionTable( LinkTarget $linkTarget ) {
176  }
177 
183  private function reloadTables() {
184  }
185 
193  public function hasVariants() {
194  return count( $this->getVariants() ) > 1;
195  }
196 
207  public function hasVariant( $variant ) {
208  return $variant && ( $variant === $this->validateVariant( $variant ) );
209  }
210 
219  public function convertHtml( $text ) {
220  return htmlspecialchars( $this->convert( $text ) );
221  }
222 }
const NS_SPECIAL
Definition: Defines.php:53
Service locator for MediaWiki core services.
Stub object for the user language.
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.
__construct( $langobj, TitleFormatter $titleFormatter=null)
Creates a converter for languages that don't have variants.
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.
getConvRuleTitle()
Get the title produced by the conversion rule.
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.
updateConversionTable(LinkTarget $linkTarget)
Refresh the cache of conversion tables when MediaWiki:Conversiontable* is updated.
convertTo( $text, $variant, bool $clearState=true)
Same as convert() except a extra parameter to custom variant.
validateVariant( $variant=null)
Validate the variant and return an appropriate strict internal variant code if one exists.
convertCategoryKey( $key)
Convert the sorting key for category links.
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.
Represents the target of a wiki link.
Definition: LinkTarget.php:30
Interface for objects (potentially) representing a page that can be viewable and linked to on a wiki.
A title formatter service for MediaWiki.