Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F1428985
CxserverWebService.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
2 KB
Referenced Files
None
Subscribers
None
CxserverWebService.php
View Options
<?php
declare
(
strict_types
=
1
);
namespace
MediaWiki\Extension\Translate\WebService
;
use
FormatJson
;
use
MediaWiki\Http\HttpRequestFactory
;
/**
* Used for interacting with translation services supported by Cxserver
* @ingroup TranslationWebService
* @author Abijeet Patro
* @license GPL-2.0-or-later
* @since 2023.06
*/
abstract
class
CxserverWebService
extends
TranslationWebService
{
private
HttpRequestFactory
$httpRequestFactory
;
public
function
__construct
(
HttpRequestFactory
$httpRequestFactory
,
string
$service
,
array
$config
)
{
parent
::
__construct
(
$service
,
$config
);
if
(
!
isset
(
$this
->
config
[
'host'
]
)
)
{
throw
new
TranslationWebServiceConfigurationException
(
'Cxserver host not set'
);
}
$this
->
httpRequestFactory
=
$httpRequestFactory
;
}
public
function
getType
():
string
{
return
'mt'
;
}
protected
function
mapCode
(
string
$code
):
string
{
return
$code
;
}
protected
function
doPairs
():
array
{
$url
=
$this
->
config
[
'host'
]
.
'/v1/list/mt'
;
$json
=
$this
->
httpRequestFactory
->
get
(
$url
,
[
$this
->
config
[
'timeout'
]
],
__METHOD__
);
if
(
$json
===
null
)
{
throw
new
TranslationWebServiceException
(
'Failure encountered when contacting remote server'
);
}
$response
=
FormatJson
::
decode
(
$json
,
true
);
if
(
!
is_array
(
$response
)
)
{
throw
new
TranslationWebServiceException
(
'Malformed reply from remote server: '
.
$json
);
}
return
$this
->
handlePairsForService
(
$response
);
}
protected
function
getQuery
(
string
$text
,
string
$sourceLanguage
,
string
$targetLanguage
):
TranslationQuery
{
$text
=
trim
(
$text
);
$text
=
$this
->
wrapUntranslatable
(
$text
);
$url
=
$this
->
config
[
'host'
]
.
"/v1/mt/$sourceLanguage/$targetLanguage/{$this->getServiceName()}"
;
return
TranslationQuery
::
factory
(
$url
)
->
timeout
(
intval
(
$this
->
config
[
'timeout'
]
)
)
->
postWithData
(
wfArrayToCgi
(
[
'html'
=>
$text
]
)
);
}
protected
function
parseResponse
(
TranslationQueryResponse
$response
)
{
$body
=
$response
->
getBody
();
$parsedBody
=
FormatJson
::
decode
(
$body
,
true
);
if
(
!
is_array
(
$parsedBody
)
)
{
throw
new
TranslationWebServiceException
(
'Invalid json: '
.
serialize
(
$body
)
);
}
return
$this
->
handleServiceResponse
(
$parsedBody
);
}
abstract
protected
function
handlePairsForService
(
array
$response
):
array
;
abstract
protected
function
getServiceName
():
string
;
abstract
protected
function
handleServiceResponse
(
array
$responseBody
):
string
;
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Sat, May 16, 16:55 (11 h, 16 m)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
77/47/1c270c67c4057969839fad506cd1
Default Alt Text
CxserverWebService.php (2 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment