diff options
Diffstat (limited to 'www/wiki/extensions/Translate/webservices/TranslationWebService.php')
-rw-r--r-- | www/wiki/extensions/Translate/webservices/TranslationWebService.php | 75 |
1 files changed, 54 insertions, 21 deletions
diff --git a/www/wiki/extensions/Translate/webservices/TranslationWebService.php b/www/wiki/extensions/Translate/webservices/TranslationWebService.php index 95f14e86..a72be868 100644 --- a/www/wiki/extensions/Translate/webservices/TranslationWebService.php +++ b/www/wiki/extensions/Translate/webservices/TranslationWebService.php @@ -4,9 +4,13 @@ * * @file * @author Niklas Laxström - * @license GPL-2.0+ + * @license GPL-2.0-or-later */ +use MediaWiki\Logger\LoggerFactory; +use Psr\Log\LoggerAwareInterface; +use Psr\Log\LoggerInterface; + /** * Multipurpose class: * - 1) Interface for web services. @@ -16,7 +20,7 @@ * @since 2013-01-01 * @defgroup TranslationWebService Translation Web Services */ -abstract class TranslationWebService { +abstract class TranslationWebService implements LoggerAwareInterface { /* Public api */ /** @@ -28,13 +32,15 @@ abstract class TranslationWebService { * @return TranslationWebService|null */ public static function factory( $name, $config ) { - $handlers = array( + $handlers = [ 'microsoft' => 'MicrosoftWebService', 'apertium' => 'ApertiumWebService', 'yandex' => 'YandexWebService', 'remote-ttmserver' => 'RemoteTTMServerWebService', 'cxserver' => 'CxserverWebService', - ); + 'restbase' => 'RESTBaseWebService', + 'caighdean' => 'CaighdeanWebService', + ]; if ( !isset( $config['timeout'] ) ) { $config['timeout'] = 3; @@ -57,7 +63,9 @@ abstract class TranslationWebService { if ( isset( $handlers[$config['type']] ) ) { $class = $handlers[$config['type']]; - return new $class( $name, $config ); + $obj = new $class( $name, $config ); + $obj->setLogger( LoggerFactory::getInstance( 'translationservices' ) ); + return $obj; } return null; @@ -82,13 +90,20 @@ abstract class TranslationWebService { * @param string $to Target language * @return TranslationQuery[] * @since 2015.12 + * @throws TranslationWebServiceConfigurationException */ public function getQueries( $text, $from, $to ) { + $from = $this->mapCode( $from ); + $to = $this->mapCode( $to ); + try { - return array( $this->getQuery( $text, $from, $to ) ); - } catch ( Exception $e ) { + return [ $this->getQuery( $text, $from, $to ) ]; + } catch ( TranslationWebServiceException $e ) { $this->reportTranslationServiceFailure( $e->getMessage() ); - return array(); + return []; + } catch ( TranslationWebServiceInvalidInputException $e ) { + // Not much we can do about this, just ignore. + return []; } } @@ -96,20 +111,23 @@ abstract class TranslationWebService { * Get the web service specific response returned by QueryAggregator. * * @param TranslationQueryResponse $response - * @return mixed + * @return mixed|null Returns null on error. * @since 2015.12 */ public function getResultData( TranslationQueryResponse $response ) { if ( $response->getStatusCode() !== 200 ) { - $this->reportTranslationServiceFailure( $response->getStatusMessage() ); - return array(); + $this->reportTranslationServiceFailure( + 'STATUS: ' . $response->getStatusMessage() . "\n" . + 'BODY: ' . $response->getBody() + ); + return null; } try { return $this->parseResponse( $response ); - } catch ( Exception $e ) { + } catch ( TranslationWebServiceException $e ) { $this->reportTranslationServiceFailure( $e->getMessage() ); - return array(); + return null; } } @@ -137,6 +155,8 @@ abstract class TranslationWebService { * getSupportedLanguagePairs. * * @return array $list[source language][target language] = true + * @throws TranslationWebServiceException + * @throws TranslationWebServiceConfigurationException */ abstract protected function doPairs(); @@ -148,6 +168,9 @@ abstract class TranslationWebService { * @param string $to Language code of the translation, as used by the service. * @return TranslationQuery * @since 2015.02 + * @throws TranslationWebServiceException + * @throws TranslationWebServiceConfigurationException + * @throws TranslationWebServiceInvalidInputException */ abstract protected function getQuery( $text, $from, $to ); @@ -155,8 +178,9 @@ abstract class TranslationWebService { * Get the response. See getResultData for the public method. * * @param TranslationQueryResponse $response - * @return mixed + * @return string * @since 2015.02 + * @throws TranslationWebServiceException */ abstract protected function parseResponse( TranslationQueryResponse $response ); @@ -173,7 +197,6 @@ abstract class TranslationWebService { protected $config; /** - * TranslationWebService constructor. * @param string $service Name of the webservice * @param array $config */ @@ -189,14 +212,20 @@ abstract class TranslationWebService { * @param string $to Target language * @return bool * @since 2015.12 + * @throws TranslationWebServiceConfigurationException */ public function isSupportedLanguagePair( $from, $to ) { $pairs = $this->getSupportedLanguagePairs(); + $from = $this->mapCode( $from ); + $to = $this->mapCode( $to ); + return isset( $pairs[$from][$to] ); } /** - * @see doPairs + * @see self::doPairs + * @return array + * @throws TranslationWebServiceConfigurationException */ protected function getSupportedLanguagePairs() { $key = wfMemcKey( 'translate-tmsug-pairs-' . $this->service ); @@ -206,7 +235,7 @@ abstract class TranslationWebService { $pairs = $this->doPairs(); } catch ( Exception $e ) { $this->reportTranslationServiceFailure( $e->getMessage() ); - return array(); + return []; } // Cache the result for a day wfGetCache( CACHE_ANYTHING )->set( $key, $pairs, 60 * 60 * 24 ); @@ -242,6 +271,10 @@ abstract class TranslationWebService { /* Failure handling and suspending */ + public function setLogger( LoggerInterface $logger ) { + $this->logger = $logger; + } + /** * @var int How many failures during failure period need to happen to * consider the service being temporarily off-line. @@ -269,7 +302,7 @@ abstract class TranslationWebService { if ( $failed + ( 2 * $this->serviceFailurePeriod ) < wfTimestamp() ) { if ( $count >= $this->serviceFailureCount ) { - wfDebugLog( 'translationservices', "Translation service $service (was) restored" ); + $this->logger->warning( "Translation service $service (was) restored" ); } wfGetCache( CACHE_ANYTHING )->delete( $key ); @@ -292,7 +325,7 @@ abstract class TranslationWebService { */ protected function reportTranslationServiceFailure( $msg ) { $service = $this->service; - wfDebugLog( 'translationservices', "Translation service $service problem: $msg" ); + $this->logger->warning( "Translation service $service problem: $msg" ); $key = wfMemcKey( "translate-service-$service" ); $value = wfGetCache( CACHE_ANYTHING )->get( $key ); @@ -311,9 +344,9 @@ abstract class TranslationWebService { ); if ( $count === $this->serviceFailureCount ) { - wfDebugLog( 'translationservices', "Translation service $service suspended" ); + $this->logger->error( "Translation service $service suspended" ); } elseif ( $count > $this->serviceFailureCount ) { - wfDebugLog( 'translationservices', "Translation service $service still suspended" ); + $this->logger->warning( "Translation service $service still suspended" ); } } } |