Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 14 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
MintCxserverWebService | |
0.00% |
0 / 14 |
|
0.00% |
0 / 4 |
72 | |
0.00% |
0 / 1 |
handlePairsForService | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
getServiceName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
handleServiceResponse | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
wrapUntranslatable | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | declare( strict_types = 1 ); |
3 | |
4 | namespace MediaWiki\Extension\Translate\WebService; |
5 | |
6 | /** |
7 | * Implements support for MinT translation service via the Cxserver API |
8 | * @ingroup TranslationWebService |
9 | * @author Abijeet Patro |
10 | * @license GPL-2.0-or-later |
11 | * @since 2023.06 |
12 | */ |
13 | class MintCxserverWebService extends CxserverWebService { |
14 | private const WIKITEXT_REGEX = [ |
15 | '/\b(PLURAL|GRAMMAR|GENDER)\b/', |
16 | '/==\s*([^=]+)\s*==/', // heading |
17 | "/''.+?''/", // italics & bold |
18 | '/\[\[([^\[\]]+)\]\]/', // links |
19 | '/\{\{([^{}]+)\}\}/', // templates |
20 | ]; |
21 | private const EXCLUDED_TARGET_LANGUAGES = [ 'zh' ]; |
22 | |
23 | protected function handlePairsForService( array $response ): array { |
24 | $pairs = []; |
25 | foreach ( $response[$this->getServiceName()] as $source => $targets ) { |
26 | $filteredTargets = array_diff( $targets, self::EXCLUDED_TARGET_LANGUAGES ); |
27 | foreach ( $filteredTargets as $target ) { |
28 | $pairs[$source][$target] = true; |
29 | } |
30 | } |
31 | |
32 | return $pairs; |
33 | } |
34 | |
35 | protected function getServiceName(): string { |
36 | return 'MinT'; |
37 | } |
38 | |
39 | protected function handleServiceResponse( array $responseBody ): string { |
40 | return trim( $this->unwrapUntranslatable( $responseBody[ 'contents' ] ) ); |
41 | } |
42 | |
43 | protected function wrapUntranslatable( string $text ): string { |
44 | // Check if at least one instance of the patterns exists in the source string |
45 | foreach ( self::WIKITEXT_REGEX as $pattern ) { |
46 | if ( preg_match( $pattern, $text ) ) { |
47 | throw new TranslationWebServiceInvalidInputException( |
48 | 'Wikitext instance(s) in source string. See T349375' |
49 | ); |
50 | } |
51 | } |
52 | |
53 | return $text; |
54 | } |
55 | } |