Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 41 |
|
0.00% |
0 / 27 |
CRAP | |
0.00% |
0 / 1 |
| TrivialLanguageConverter | |
0.00% |
0 / 40 |
|
0.00% |
0 / 27 |
1056 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| autoConvert | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| autoConvertToAllVariants | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| convert | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| convertTo | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| convertSplitTitle | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
| convertTitle | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| convertNamespace | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getVariants | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getVariantFallbacks | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getPreferredVariant | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getDefaultVariant | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getURLVariant | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getConvRuleTitle | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getConvRuleTitleFragment | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| findVariantLink | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getExtraHashOptions | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| guessVariant | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| markNoConversion | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| convertCategoryKey | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| validateVariant | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
20 | |||
| translate | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| updateConversionTable | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| reloadTables | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| hasVariants | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| hasVariant | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| convertHtml | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * @license GPL-2.0-or-later |
| 4 | * @file |
| 5 | */ |
| 6 | |
| 7 | namespace MediaWiki\Language; |
| 8 | |
| 9 | use InvalidArgumentException; |
| 10 | use MediaWiki\MediaWikiServices; |
| 11 | use MediaWiki\Page\PageIdentity; |
| 12 | use MediaWiki\StubObject\StubUserLang; |
| 13 | use MediaWiki\Title\TitleFormatter; |
| 14 | use Wikimedia\Parsoid\DOM\Document; |
| 15 | use Wikimedia\Parsoid\DOM\DocumentFragment; |
| 16 | |
| 17 | /** |
| 18 | * A trivial language converter. |
| 19 | * |
| 20 | * For Languages which do not implement variant |
| 21 | * conversion, for example, German, TrivialLanguageConverter is provided rather than a |
| 22 | * LanguageConverter when asked for their converter. The TrivialLanguageConverter just |
| 23 | * returns text unchanged, i.e., it doesn't do any conversion. |
| 24 | * |
| 25 | * See https://www.mediawiki.org/wiki/Writing_systems#LanguageConverter. |
| 26 | * |
| 27 | * @since 1.35 |
| 28 | * @ingroup Language |
| 29 | */ |
| 30 | class TrivialLanguageConverter implements ILanguageConverter { |
| 31 | |
| 32 | /** |
| 33 | * @var Language |
| 34 | */ |
| 35 | protected $language; |
| 36 | |
| 37 | /** |
| 38 | * @var TitleFormatter |
| 39 | */ |
| 40 | private $titleFormatter; |
| 41 | |
| 42 | /** |
| 43 | * Creates a converter for languages that don't have variants. This method is internal |
| 44 | * and should be called for LanguageConverterFactory only |
| 45 | * |
| 46 | * @param Language|StubUserLang $langobj |
| 47 | * @param TitleFormatter|null $titleFormatter |
| 48 | * |
| 49 | * @internal |
| 50 | */ |
| 51 | public function __construct( |
| 52 | $langobj, |
| 53 | ?TitleFormatter $titleFormatter = null |
| 54 | ) { |
| 55 | $this->language = $langobj; |
| 56 | $this->titleFormatter = $titleFormatter ?? MediaWikiServices::getInstance()->getTitleFormatter(); |
| 57 | } |
| 58 | |
| 59 | /** @inheritDoc */ |
| 60 | public function autoConvert( $text, $variant = false ) { |
| 61 | return $text; |
| 62 | } |
| 63 | |
| 64 | /** @inheritDoc */ |
| 65 | public function autoConvertToAllVariants( $text ) { |
| 66 | return [ $this->language->getCode() => $text ]; |
| 67 | } |
| 68 | |
| 69 | /** @inheritDoc */ |
| 70 | public function convert( $t ) { |
| 71 | return $t; |
| 72 | } |
| 73 | |
| 74 | /** @inheritDoc */ |
| 75 | public function convertTo( $text, $variant, bool $clearState = true ) { |
| 76 | return $text; |
| 77 | } |
| 78 | |
| 79 | /** @inheritDoc */ |
| 80 | public function convertSplitTitle( $title, ?string $variant = null ) { |
| 81 | $mainText = $this->titleFormatter->getText( $title ); |
| 82 | |
| 83 | $index = $title->getNamespace(); |
| 84 | try { |
| 85 | $nsWithUnderscores = $this->titleFormatter->getNamespaceName( $index, $mainText ); |
| 86 | } catch ( InvalidArgumentException ) { |
| 87 | // T165149: see TitleFormatter::formatTitle() |
| 88 | $nsWithUnderscores = $this->language->getNsText( NS_SPECIAL ); |
| 89 | $mainText = "Badtitle/NS$index:$mainText"; |
| 90 | } |
| 91 | $nsText = str_replace( '_', ' ', $nsWithUnderscores ); |
| 92 | |
| 93 | return [ $nsText, ':', $mainText ]; |
| 94 | } |
| 95 | |
| 96 | /** @inheritDoc */ |
| 97 | public function convertTitle( $title ) { |
| 98 | return $this->titleFormatter->getPrefixedText( $title ); |
| 99 | } |
| 100 | |
| 101 | /** @inheritDoc */ |
| 102 | public function convertNamespace( $index, $variant = null ) { |
| 103 | return $this->language->getFormattedNsText( $index ); |
| 104 | } |
| 105 | |
| 106 | /** @inheritDoc */ |
| 107 | public function getVariants() { |
| 108 | return [ $this->language->getCode() ]; |
| 109 | } |
| 110 | |
| 111 | /** @inheritDoc */ |
| 112 | public function getVariantFallbacks( $variant ) { |
| 113 | return $this->language->getCode(); |
| 114 | } |
| 115 | |
| 116 | /** @inheritDoc */ |
| 117 | public function getPreferredVariant() { |
| 118 | return $this->language->getCode(); |
| 119 | } |
| 120 | |
| 121 | /** @inheritDoc */ |
| 122 | public function getDefaultVariant() { |
| 123 | return $this->language->getCode(); |
| 124 | } |
| 125 | |
| 126 | /** @inheritDoc */ |
| 127 | public function getURLVariant() { |
| 128 | return ''; |
| 129 | } |
| 130 | |
| 131 | /** @inheritDoc */ |
| 132 | public function getConvRuleTitle() { |
| 133 | return false; |
| 134 | } |
| 135 | |
| 136 | /** @inheritDoc */ |
| 137 | public function getConvRuleTitleFragment( Document $ownerDocument ): ?DocumentFragment { |
| 138 | return null; |
| 139 | } |
| 140 | |
| 141 | /** @inheritDoc */ |
| 142 | public function findVariantLink( &$l, &$n, $ignoreOtherCond = false ) { |
| 143 | } |
| 144 | |
| 145 | /** @inheritDoc */ |
| 146 | public function getExtraHashOptions() { |
| 147 | return ''; |
| 148 | } |
| 149 | |
| 150 | /** @inheritDoc */ |
| 151 | public function guessVariant( $text, $variant ) { |
| 152 | return false; |
| 153 | } |
| 154 | |
| 155 | /** @inheritDoc */ |
| 156 | public function markNoConversion( $text, $noParse = false ) { |
| 157 | return $text; |
| 158 | } |
| 159 | |
| 160 | /** @inheritDoc */ |
| 161 | public function convertCategoryKey( $key ) { |
| 162 | return $key; |
| 163 | } |
| 164 | |
| 165 | /** @inheritDoc */ |
| 166 | public function validateVariant( $variant = null, bool $fallbackToDefault = false ) { |
| 167 | if ( $fallbackToDefault ) { |
| 168 | return $this->language->getCode(); |
| 169 | } |
| 170 | if ( $variant === null ) { |
| 171 | return null; |
| 172 | } |
| 173 | $variant = strtolower( $variant ); |
| 174 | return $variant === $this->language->getCode() ? $variant : null; |
| 175 | } |
| 176 | |
| 177 | /** @inheritDoc */ |
| 178 | public function translate( $text, $variant ) { |
| 179 | return $text; |
| 180 | } |
| 181 | |
| 182 | /** @inheritDoc */ |
| 183 | public function updateConversionTable( PageIdentity $page ) { |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Used by test suites which need to reset the converter state. |
| 188 | * |
| 189 | * Called by ParserTestRunner with the help of TestingAccessWrapper |
| 190 | */ |
| 191 | private function reloadTables() { |
| 192 | } |
| 193 | |
| 194 | /** @inheritDoc */ |
| 195 | public function hasVariants() { |
| 196 | return count( $this->getVariants() ) > 1; |
| 197 | } |
| 198 | |
| 199 | /** @inheritDoc */ |
| 200 | public function hasVariant( $variant ) { |
| 201 | return $variant && ( $variant === $this->validateVariant( $variant ) ); |
| 202 | } |
| 203 | |
| 204 | /** @inheritDoc */ |
| 205 | public function convertHtml( $text ) { |
| 206 | return htmlspecialchars( $this->convert( $text ) ); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | /** @deprecated class alias since 1.46 */ |
| 211 | class_alias( TrivialLanguageConverter::class, 'TrivialLanguageConverter' ); |