25 public function __construct() {
26 parent::__construct();
27 $this->addDescription(
'Test webservices.' );
31 'Which service to use',
38 'Source language tag',
45 'Target language tag',
55 $this->requireExtension(
'Translate' );
58 public function execute() {
59 global $wgTranslateTranslationServices;
61 $name = $this->getOption(
'service' );
63 if ( !isset( $wgTranslateTranslationServices[ $name ] ) ) {
64 $available = implode(
', ', array_keys( $wgTranslateTranslationServices ) );
65 $this->fatalError(
"Unknown service. Available services: $available\n" );
68 $logger =
new class( fn ( $msg ) => $this->output( $msg ) ) extends AbstractLogger {
71 public function __construct( $logger ) {
72 $this->logger = $logger;
75 public function log( $level, $msg, array $context = [] ) {
76 call_user_func( $this->logger,
"[$level] $msg\n" );
80 $service = TranslationWebService::factory( $name, $wgTranslateTranslationServices[ $name ] );
81 $service->setLogger( $logger );
83 $from = $this->getOption(
'from' );
84 $to = $this->getOption(
'to' );
85 $text = $this->getArg( 0 );
87 if ( !$service->isSupportedLanguagePair( $from, $to ) ) {
88 $this->fatalError(
"Unsupported language pair.\n" );
91 $query = $service->getQueries( $text, $from, $to );
92 if ( $query === [] ) {
93 $this->fatalError(
"Service query error.\n" );
97 $id = $agg->addQuery( $query[ 0 ] );
99 $res = $agg->getResponse( $id );
101 $this->output( $service->getResultData( $res ), 1 );