ApertiumWebService.php

Go to the documentation of this file.
00001 <?php
00017 class ApertiumWebService extends TranslationWebService {
00018     protected function mapCode( $code ) {
00019         return str_replace( '-', '_', wfBCP47( $code ) );
00020     }
00021 
00022     protected function doPairs() {
00023         $pairs = array();
00024         $json = Http::get( $this->config['pairs'], $this->config['timeout'] );
00025         $response = FormatJson::decode( $json );
00026 
00027         if ( !is_object( $response ) ) {
00028             $error = 'Malformed reply from remote server: ' . strval( $json );
00029             throw new TranslationWebServiceException( $error );
00030         }
00031 
00032         foreach ( $response->responseData as $pair ) {
00033             $source = $pair->sourceLanguage;
00034             $target = $pair->targetLanguage;
00035             $pairs[$source][$target] = true;
00036         }
00037 
00038         return $pairs;
00039     }
00040 
00041     protected function doRequest( $text, $from, $to ) {
00042         $service = $this->service;
00043 
00044         $text = trim( $text );
00045         $text = $this->wrapUntranslatable( $text );
00046 
00047         $options = array();
00048         $options['timeout'] = $this->config['timeout'];
00049         $params = array(
00050             'q' => $text,
00051             'langpair' => "$from|$to",
00052             'x-application' => "Translate " . TRANSLATE_VERSION . ")",
00053         );
00054 
00055         if ( $this->config['key'] ) {
00056             $params['key'] = $this->config['key'];
00057         }
00058 
00059         $url = $this->config['url'] . '?' . wfArrayToCgi( $params );
00060 
00061         $req = MWHttpRequest::factory( $url, $options );
00062         wfProfileIn( 'TranslateWebServiceRequest-' . $this->service );
00063         $status = $req->execute();
00064         wfProfileOut( 'TranslateWebServiceRequest-' . $this->service );
00065 
00066         if ( !$status->isOK() ) {
00067             $error = $req->getContent();
00068             // Most likely a timeout or other general error
00069             throw new TranslationWebServiceException(
00070                 "Http::get failed:\n" .
00071                     "* " . serialize( $error ) . "\n" .
00072                     "* " . serialize( $status )
00073             );
00074         }
00075 
00076         $response = FormatJson::decode( $req->getContent() );
00077         if ( !is_object( $response ) ) {
00078             throw new TranslationWebServiceException( serialize( $req->getContent() ) );
00079         } elseif ( $response->responseStatus !== 200 ) {
00080             $error = "(HTTP {$response->responseStatus}) with ($service ($from|$to)): " .
00081                 $response->responseDetails;
00082             throw new TranslationWebServiceException( $error );
00083         }
00084 
00085         $sug = Sanitizer::decodeCharReferences( $response->responseData->translatedText );
00086         $sug = $this->unwrapUntranslatable( $sug );
00087 
00088         return trim( $sug );
00089     }
00090 }
Generated on Tue Oct 29 00:00:26 2013 for MediaWiki Translate Extension by  doxygen 1.6.3