Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
TtmServerActionApi.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\TtmServer;
5
6use MediaWiki\Api\ApiBase;
7use MediaWiki\Api\ApiMain;
8use MediaWiki\Config\Config;
9use MediaWiki\Config\ServiceOptions;
10use Wikimedia\ParamValidator\ParamValidator;
11
19class TtmServerActionApi extends ApiBase {
20
22 private $options;
23
24 private const CONSTRUCTOR_OPTIONS = [
25 'LanguageCode',
26 'TranslateTranslationDefaultService',
27 'TranslateTranslationServices',
28 ];
29
30 public function __construct(
31 ApiMain $mainModule,
32 string $moduleName,
33 private readonly TtmServerFactory $ttmServerFactory,
34 Config $config
35 ) {
36 parent::__construct( $mainModule, $moduleName );
37 $this->options = new ServiceOptions( self::CONSTRUCTOR_OPTIONS, $config );
38 }
39
40 public function execute(): void {
41 if ( !$this->getAvailableTranslationServices() ) {
42 $this->dieWithError( 'apierror-translate-notranslationservices' );
43 }
44
45 $params = $this->extractRequestParams();
46
47 $server = $this->ttmServerFactory->create( $params[ 'service' ] );
48 if ( !$server instanceof ReadableTtmServer ) {
49 $this->dieWithError( 'apierror-translate-notranslationservices' );
50 }
51
52 $suggestions = $server->query(
53 $params['sourcelanguage'],
54 $params['targetlanguage'],
55 $params['text']
56 );
57
58 $result = $this->getResult();
59 foreach ( $suggestions as $sug ) {
60 $sug['location'] = $server->expandLocation( $sug );
61 unset( $sug['wiki'] );
62 $result->addValue( $this->getModuleName(), null, $sug );
63 }
64
65 $result->addIndexedTagName( $this->getModuleName(), 'suggestion' );
66 }
67
68 private function getAvailableTranslationServices(): array {
69 $translationServices = $this->options->get( 'TranslateTranslationServices' );
70
71 $good = [];
72 foreach ( $translationServices as $id => $config ) {
73 $public = $config['public'] ?? false;
74 if ( $config['type'] === 'ttmserver' && $public ) {
75 $good[] = $id;
76 }
77 }
78
79 return $good;
80 }
81
82 protected function getAllowedParams(): array {
83 $available = $this->getAvailableTranslationServices();
84
85 $ret = [
86 'service' => [
87 ParamValidator::PARAM_TYPE => $available,
88 ],
89 'sourcelanguage' => [
90 ParamValidator::PARAM_TYPE => 'string',
91 ParamValidator::PARAM_REQUIRED => true,
92 ],
93 'targetlanguage' => [
94 ParamValidator::PARAM_TYPE => 'string',
95 ParamValidator::PARAM_REQUIRED => true,
96 ],
97 'text' => [
98 ParamValidator::PARAM_TYPE => 'string',
99 ParamValidator::PARAM_REQUIRED => true,
100 ],
101 ];
102
103 if ( $available ) {
104 // Don't add this if no services are available, it makes
105 // ApiStructureTest unhappy
106 $ret['service'][ParamValidator::PARAM_DEFAULT] =
107 $this->options->get( 'TranslateTranslationDefaultService' );
108 }
109
110 return $ret;
111 }
112
113 protected function getExamplesMessages(): array {
114 return [
115 'action=ttmserver&sourcelanguage=en&targetlanguage=fi&text=Help'
116 => 'apihelp-ttmserver-example-1',
117 ];
118 }
119}
Interface for TtmServer that can be queried (=all of them).