Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
test-mt.php
Go to the documentation of this file.
1<?php
12
13// Standard boilerplate to define $IP
14if ( getenv( 'MW_INSTALL_PATH' ) !== false ) {
15 $IP = getenv( 'MW_INSTALL_PATH' );
16} else {
17 $dir = __DIR__;
18 $IP = "$dir/../../..";
19}
20require_once "$IP/maintenance/Maintenance.php";
21
22class TestMT extends Maintenance {
23 public function __construct() {
24 parent::__construct();
25 $this->addDescription( 'Test webservices.' );
26
27 $this->addOption(
28 'service',
29 'Which service to use',
30 true, /*required*/
31 true /*has arg*/
32 );
33
34 $this->addOption(
35 'from',
36 'Source language tag',
37 true, /*required*/
38 true /*has arg*/
39 );
40
41 $this->addOption(
42 'to',
43 'Target language tag',
44 true, /*required*/
45 true /*has arg*/
46 );
47
48 $this->addArg(
49 'text',
50 'Text to translate',
51 true /*required*/
52 );
53 $this->requireExtension( 'Translate' );
54 }
55
56 public function execute() {
57 global $wgTranslateTranslationServices;
58
59 $name = $this->getOption( 'service' );
60
61 if ( !isset( $wgTranslateTranslationServices[ $name ] ) ) {
62 $this->fatalError( "Unknown service.\n" );
63 }
64
65 $service = TranslationWebService::factory( $name, $wgTranslateTranslationServices[ $name ] );
66 $service->setLogger( new TranslateCliLogger( function ( $msg ) {
67 $this->output( "$msg\n" );
68 } ) );
69
70 $from = $this->getOption( 'from' );
71 $to = $this->getOption( 'to' );
72 $text = $this->getArg( 0 );
73
74 if ( !$service->isSupportedLanguagePair( $from, $to ) ) {
75 $this->fatalError( "Unsupported language pair.\n" );
76 }
77
78 $query = $service->getQueries( $text, $from, $to );
79 if ( $query === [] ) {
80 $this->fatalError( "Service query error.\n" );
81 }
82
83 $agg = new QueryAggregator();
84 $id = $agg->addQuery( $query[ 0 ] );
85 $agg->run();
86 $res = $agg->getResponse( $id );
87
88 $this->output( $service->getResultData( $res ), 1 );
89 }
90}
91
92$maintClass = TestMT::class;
93require_once RUN_MAINTENANCE_IF_MAIN;