| 1 |
<?php |
| 2 |
declare( strict_types = 1 ); |
| 3 |
|
| 4 |
namespace Wikimedia\Parsoid\Language; |
| 5 |
|
| 6 |
use Wikimedia\Bcp47Code\Bcp47Code; |
| 7 |
use Wikimedia\LangConv\FstReplacementMachine; |
| 8 |
use Wikimedia\Parsoid\Utils\Utils; |
| 9 |
|
| 10 |
class SrConverter extends LanguageConverter { |
| 11 |
|
| 12 |
public function loadDefaultTables() { |
| 13 |
# T320662: should be converted from mediawiki-internal codes |
| 14 |
$this->setMachine( new FstReplacementMachine( 'sr', [ 'sr-ec', 'sr-el' ] ) ); |
| 15 |
} |
| 16 |
|
| 17 |
// phpcs:ignore MediaWiki.Commenting.FunctionComment.MissingDocumentationPublic |
| 18 |
public function findVariantLink( $link, $nt, $ignoreOtherCond ) { |
| 19 |
$ns = $nt->getNamespace(); |
| 20 |
// do not try to find variants for usernames |
| 21 |
if ( $ns->isUser() || $ns->isUserTalk ) { |
| 22 |
return [ 'nt' => $nt, 'link' => $link ]; |
| 23 |
} |
| 24 |
// FIXME check whether selected language is 'sr' |
| 25 |
return parent::findVariantLink( $link, $nt, $ignoreOtherCond ); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Variant based on the ReplacementMachine's bracketing abilities |
| 30 |
* @param string $text |
| 31 |
* @param Bcp47Code $variant a language code |
| 32 |
* @return bool |
| 33 |
*/ |
| 34 |
public function guessVariant( $text, $variant ) { |
| 35 |
# T320662 This code is implemented using MW-internal codes |
| 36 |
$variant = Utils::bcp47ToMwCode( $variant ); |
| 37 |
$r = []; |
| 38 |
$machine = $this->getMachine(); |
| 39 |
'@phan-var FstReplacementMachine $machine'; /* @var FstReplacementMachine $machine */ |
| 40 |
foreach ( $machine->getCodes() as $code => $ignore1 ) { |
| 41 |
foreach ( $machine->getCodes() as $othercode => $ignore2 ) { |
| 42 |
if ( $code === $othercode ) { |
| 43 |
return false; |
| 44 |
} |
| 45 |
$r[] = [ |
| 46 |
'code' => $code, |
| 47 |
'othercode' => $othercode, |
| 48 |
'stats' => $machine->countBrackets( $text, $code, $othercode ) |
| 49 |
]; |
| 50 |
} |
| 51 |
} |
| 52 |
uasort( $r, static function ( $a, $b ) { |
| 53 |
return $a['stats']->unsafe - $b['stats']->unsafe; |
| 54 |
} ); |
| 55 |
return $r[0]['othercode'] === $variant; |
| 56 |
} |
| 57 |
} |