YandexWebService.php

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