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