summaryrefslogtreecommitdiff
blob: 2ae947a46986aa3e19ba9b0b5f2fe3b5795000bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
/**
 * Script to test web services from the command line
 *
 * @author Niklas Laxström
 * @license GPL-2.0-or-later
 * @file
 */

// Standard boilerplate to define $IP
if ( getenv( 'MW_INSTALL_PATH' ) !== false ) {
	$IP = getenv( 'MW_INSTALL_PATH' );
} else {
	$dir = __DIR__;
	$IP = "$dir/../../..";
}
require_once "$IP/maintenance/Maintenance.php";

class TestMT extends Maintenance {
	public function __construct() {
		parent::__construct();
		$this->mDescription = 'Test webservices.';

		$this->addOption(
			'service',
			'Which service to use',
			true, /*required*/
			true /*has arg*/
		);

		$this->addOption(
			'from',
			'Source language tag',
			true, /*required*/
			true /*has arg*/
		);

		$this->addOption(
			'to',
			'Target language tag',
			true, /*required*/
			true /*has arg*/
		);

		$this->addArg(
			'text',
			'Text to translate',
			true /*required*/
		);
	}

	public function execute() {
		global $wgTranslateTranslationServices;

		$name = $this->getOption( 'service' );

		if ( !isset( $wgTranslateTranslationServices[ $name ] ) ) {
			$this->fatalError( "Unknown service.\n" );
		}

		$service = TranslationWebService::factory( $name, $wgTranslateTranslationServices[ $name ] );
		$service->setLogger( new TranslateCliLogger( function ( $msg ) {
			$this->output( "$msg\n" );
		} ) );

		$from = $this->getOption( 'from' );
		$to = $this->getOption( 'to' );
		$text = $this->getArg( 0 );

		if ( !$service->isSupportedLanguagePair( $from, $to ) ) {
			$this->fatalError( "Unsupported language pair.\n" );
		}

		$query = $service->getQueries( $text, $from, $to );
		if ( $query === [] ) {
			$this->fatalError( "Service query error.\n" );
		}

		$agg = new QueryAggregator();
		$id = $agg->addQuery( $query[ 0 ] );
		$agg->run();
		$res = $agg->getResponse( $id );
		if ( $res === null ) {
			$this->fatalError( "Service response error.\n" );
		}

		$this->output( $service->getResultData( $res ), 1 );
	}
}

$maintClass = 'TestMT';
require_once RUN_MAINTENANCE_IF_MAIN;