r75317 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r75316‎ | r75317 | r75318 >
Date:16:56, 24 October 2010
Author:nikerabbit
Status:ok
Tags:
Comment:
Support for Microsoft Translator
Modified paths:
  • /trunk/extensions/Translate/Translate.php (modified) (history)
  • /trunk/extensions/Translate/utils/TranslationHelpers.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Translate/Translate.php
@@ -180,6 +180,13 @@
181181 'timeout-async' => 6,
182182 'type' => 'google',
183183 );
 184+$wgTranslateTranslationServices['Microsoft'] = array(
 185+ 'url' => 'http://api.microsofttranslator.com/V2/Http.svc/Translate',
 186+ 'key' => null,
 187+ 'timeout-sync' => 3,
 188+ 'timeout-async' => 6,
 189+ 'type' => 'microsoft',
 190+);
184191 $wgTranslateTranslationServices['Apertium'] = array(
185192 'url' => 'http://api.apertium.org/json/translate',
186193 'pairs' => 'http://api.apertium.org/json/listPairs',
Index: trunk/extensions/Translate/utils/TranslationHelpers.php
@@ -314,6 +314,8 @@
315315 $boxes[] = $this->getTmBox( $name, $config );
316316 } elseif ( $config['type'] === 'google' ) {
317317 $boxes[] = $this->getGoogleSuggestion( $name, $config );
 318+ } elseif ( $config['type'] === 'microsoft' ) {
 319+ $boxes[] = $this->getMicrosoftSuggestion( $name, $config );
318320 } elseif ( $config['type'] === 'apertium' ) {
319321 $boxes[] = $this->getApertiumSuggestion( $name, $config );
320322 } else {
@@ -413,6 +415,67 @@
414416 return $options;
415417 }
416418
 419+ protected function getMicrosoftSuggestion( $serviceName, $config ) {
 420+ global $wgMemc;
 421+
 422+ if ( self::checkTranslationServiceFailure( $serviceName ) ) {
 423+ return null;
 424+ }
 425+
 426+ $code = $this->targetLanguage;
 427+ $definition = trim( strval( $this->getDefinition() ) );
 428+ $definition = str_replace( "\n", "<newline/>", $definition );
 429+
 430+ $memckey = wfMemckey( 'translate-tmsug-badcodes-' . $serviceName );
 431+ $unsupported = $wgMemc->get( $memckey );
 432+
 433+ if ( $definition === '' || isset( $unsupported[$code] ) ) {
 434+ return null;
 435+ }
 436+
 437+ global $wgSitename, $wgVersion, $wgProxyKey;
 438+ $options = array();
 439+ $options['timeout'] = $config['timeout'];
 440+
 441+ $params = array(
 442+ 'text' => $definition,
 443+ 'to' => $code,
 444+ );
 445+
 446+ if ( isset( $config['key'] ) ) {
 447+ $params['appId'] = $config['key'];
 448+ } else {
 449+ return null;
 450+ }
 451+
 452+ $url = $config['url'] . '?' . wfArrayToCgi( $params );
 453+ $url = wfExpandUrl( $url );
 454+
 455+ $options['method'] = 'GET';
 456+ $req = HttpRequest::factory( $url, $options );
 457+ $status = $req->execute();
 458+
 459+ if ( !$status->isOK() ) {
 460+ $error = $req->getContent();
 461+ if ( strpos( $error, 'must be a valid language' ) !== false ) {
 462+ $unsupported[$code] = true;
 463+ $wgMemc->set( $memckey, $unsupported, 60 * 60 * 8 );
 464+ return null;
 465+ }
 466+
 467+ wfWarn( __METHOD__ . ': Http::get failed:' . $error );
 468+ // Most likely a timeout or other general error
 469+ self::reportTranslationServiceFailure( $serviceName );
 470+ return null;
 471+ }
 472+
 473+ $ret = $req->getContent();
 474+ $text = preg_replace( '~<string.*>(.*)</string>~', '\\1', $ret );
 475+ $text = Sanitizer::decodeCharReferences( $text );
 476+ $text = $this->suggestionField( $text );
 477+ return Html::rawElement( 'div', null, self::legend( $serviceName ) . $text . self::clear() );
 478+ }
 479+
417480 protected function getApertiumSuggestion( $serviceName, $config ) {
418481 global $wgMemc;
419482

Status & tagging log