24 public function __construct() {
25 parent::__construct();
26 $this->addDescription(
'Test webservices.' );
30 'Which service to use',
37 'Source language tag',
44 'Target language tag',
54 $this->requireExtension(
'Translate' );
57 public function execute() {
58 global $wgTranslateTranslationServices;
60 $name = $this->getOption(
'service' );
62 if ( !isset( $wgTranslateTranslationServices[ $name ] ) ) {
63 $available = implode(
', ', array_keys( $wgTranslateTranslationServices ) );
64 $this->fatalError(
"Unknown service. Available services: $available\n" );
67 $logger =
new class( fn ( $msg ) => $this->output( $msg ) ) extends AbstractLogger {
70 public function __construct( $logger ) {
71 $this->logger = $logger;
74 public function log( $level, $msg, array $context = [] ) {
75 call_user_func( $this->logger,
"[$level] $msg\n" );
79 $service = TranslationWebService::factory( $name, $wgTranslateTranslationServices[ $name ] );
80 $service->setLogger( $logger );
82 $from = $this->getOption(
'from' );
83 $to = $this->getOption(
'to' );
84 $text = $this->getArg( 0 );
86 if ( !$service->isSupportedLanguagePair( $from, $to ) ) {
87 $this->fatalError(
"Unsupported language pair.\n" );
90 $query = $service->getQueries( $text, $from, $to );
91 if ( $query === [] ) {
92 $this->fatalError(
"Service query error.\n" );
96 $id = $agg->addQuery( $query[ 0 ] );
98 $res = $agg->getResponse( $id );
100 $this->output( $service->getResultData( $res ), 1 );