RemoteTTMServerWebService.php

Go to the documentation of this file.
00001 <?php
00017 class RemoteTTMServerWebService extends TranslationWebService {
00018     public function getSuggestions( $translations, $from, $to ) {
00019         if ( $this->checkTranslationServiceFailure() ) {
00020             return array();
00021         }
00022 
00023         try {
00024             $text = $translations[$from];
00025 
00026             return $this->doRequest( $text, $from, $to );
00027         } catch ( Exception $e ) {
00028             $this->reportTranslationServiceFailure( $e );
00029 
00030             return array();
00031         }
00032     }
00033 
00034     protected function mapCode( $code ) {
00035         return $code; // Unused
00036     }
00037 
00038     protected function doPairs() {
00039         return null; // Unused
00040     }
00041 
00042     protected function doRequest( $text, $from, $to ) {
00043         $params = array(
00044             'format' => 'json',
00045             'action' => 'ttmserver',
00046             'sourcelanguage' => $from,
00047             'targetlanguage' => $to,
00048             'text' => $text,
00049             '*', // Because we hate IE
00050         );
00051 
00052         $url = $this->config['url'] . '?';
00053         $url .= wfArrayToCgi( $params );
00054         $req = MWHttpRequest::factory( $url );
00055         wfProfileIn( 'TranslateWebServiceRequest-' . $this->service );
00056         $status = $req->execute();
00057         wfProfileOut( 'TranslateWebServiceRequest-' . $this->service );
00058         $response = $req->getContent();
00059 
00060         if ( !$status->isOK() ) {
00061             // Most likely a timeout or other general error
00062             throw new TranslationWebServiceException(
00063                 "Http::get failed:\n" .
00064                     "* " . serialize( $response ) . "\n" .
00065                     "* " . serialize( $status )
00066             );
00067         }
00068 
00069         $parsed = FormatJson::decode( $response, true );
00070         if ( !is_array( $parsed ) ) {
00071             throw new TranslationWebServiceException( serialize( $response ) );
00072         }
00073 
00074         if ( !isset( $parsed['ttmserver'] ) ) {
00075             throw new TranslationWebServiceException( 'Unexpected reply from remote server' );
00076         }
00077 
00078         return $parsed['ttmserver'];
00079     }
00080 }
Generated on Tue Oct 29 00:00:26 2013 for MediaWiki Translate Extension by  doxygen 1.6.3