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