diff options
Diffstat (limited to 'MLEB/Translate/ttmserver/TTMServer.php')
-rw-r--r-- | MLEB/Translate/ttmserver/TTMServer.php | 97 |
1 files changed, 65 insertions, 32 deletions
diff --git a/MLEB/Translate/ttmserver/TTMServer.php b/MLEB/Translate/ttmserver/TTMServer.php index d1cf443a..98724869 100644 --- a/MLEB/Translate/ttmserver/TTMServer.php +++ b/MLEB/Translate/ttmserver/TTMServer.php @@ -4,8 +4,7 @@ * * @file * @author Niklas Laxström - * @copyright Copyright © 2012-2013, Niklas Laxström - * @license GPL-2.0+ + * @license GPL-2.0-or-later * @defgroup TTMServer The Translate extension translation memory interface */ @@ -39,19 +38,20 @@ class TTMServer { } } - throw new MWEXception( "TTMServer with no type" ); + throw new MWEXception( 'TTMServer with no type' ); } /** * Returns the primary server instance, useful for chaining. - * Primary one is defined as config with key TTMServer - * in $wgTranslateTranslationServices. + * Primary instance is defined by $wgTranslateTranslationDefaultService + * which is a key to $wgTranslateTranslationServices. * @return WritableTTMServer */ public static function primary() { - global $wgTranslateTranslationServices; - if ( isset( $wgTranslateTranslationServices['TTMServer'] ) ) { - $obj = self::factory( $wgTranslateTranslationServices['TTMServer'] ); + global $wgTranslateTranslationServices, + $wgTranslateTranslationDefaultService; + if ( isset( $wgTranslateTranslationServices[$wgTranslateTranslationDefaultService] ) ) { + $obj = self::factory( $wgTranslateTranslationServices[$wgTranslateTranslationDefaultService] ); if ( $obj instanceof WritableTTMServer ) { return $obj; } @@ -61,13 +61,13 @@ class TTMServer { } public static function sortSuggestions( array $suggestions ) { - usort( $suggestions, array( __CLASS__, 'qualitySort' ) ); + usort( $suggestions, [ __CLASS__, 'qualitySort' ] ); return $suggestions; } protected static function qualitySort( $a, $b ) { - list( $c, $d ) = array( $a['quality'], $b['quality'] ); + list( $c, $d ) = [ $a['quality'], $b['quality'] ]; if ( $c === $d ) { return 0; } @@ -81,17 +81,17 @@ class TTMServer { * Uses the native PHP implementation when possible for speed. * The native levenshtein is limited to 255 bytes. * - * @param $str1 - * @param $str2 - * @param $length1 - * @param $length2 + * @param string $str1 + * @param string $str2 + * @param int $length1 + * @param int $length2 * @return int */ public static function levenshtein( $str1, $str2, $length1, $length2 ) { - if ( $length1 == 0 ) { + if ( $length1 === 0 ) { return $length2; } - if ( $length2 == 0 ) { + if ( $length2 === 0 ) { return $length1; } if ( $str1 === $str2 ) { @@ -108,14 +108,14 @@ class TTMServer { $prevRow = range( 0, $length2 ); for ( $i = 0; $i < $length1; $i++ ) { - $currentRow = array(); + $currentRow = []; $currentRow[0] = $i + 1; $c1 = mb_substr( $str1, $i, 1 ); for ( $j = 0; $j < $length2; $j++ ) { $c2 = mb_substr( $str2, $j, 1 ); $insertions = $prevRow[$j + 1] + 1; $deletions = $currentRow[$j] + 1; - $substitutions = $prevRow[$j] + ( ( $c1 != $c2 ) ? 1 : 0 ); + $substitutions = $prevRow[$j] + ( ( $c1 !== $c2 ) ? 1 : 0 ); $currentRow[] = min( $insertions, $deletions, $substitutions ); } $prevRow = $currentRow; @@ -124,31 +124,64 @@ class TTMServer { return $prevRow[$length2]; } - /// Hook: ArticleDeleteComplete + /** + * Hook: ArticleDeleteComplete + * @param WikiPage $wikipage + */ public static function onDelete( WikiPage $wikipage ) { $handle = new MessageHandle( $wikipage->getTitle() ); - TTMServer::primary()->update( $handle, null ); - - return true; + $job = TTMServerMessageUpdateJob::newJob( $handle, 'delete' ); + JobQueueGroup::singleton()->push( $job ); } - /// Called from TranslateEditAddons::onSave + /** + * Called from TranslateEditAddons::onSave + * @param MessageHandle $handle + * @param string $text + * @param bool $fuzzy + */ public static function onChange( MessageHandle $handle, $text, $fuzzy ) { - if ( $fuzzy ) { - $text = null; - } - TTMServer::primary()->update( $handle, $text ); + $job = TTMServerMessageUpdateJob::newJob( $handle, 'refresh' ); + JobQueueGroup::singleton()->push( $job ); } public static function onGroupChange( MessageHandle $handle, $old, $new ) { - if ( $old === array() ) { + if ( $old === [] ) { // Don't bother for newly added messages - return true; + return; } - $job = TTMServerMessageUpdateJob::newJob( $handle ); - $job->insert(); + $job = TTMServerMessageUpdateJob::newJob( $handle, 'rebuild' ); + JobQueueGroup::singleton()->push( $job ); + } - return true; + /** + * @return string[] + */ + public function getMirrors() { + global $wgTranslateTranslationServices; + if ( isset( $this->config['mirrors'] ) ) { + $mirrors = []; + foreach ( $this->config['mirrors'] as $name ) { + if ( !is_string( $name ) ) { + throw new TTMServerException( "Invalid configuration set in " . + "mirrors, expected an array of strings" ); + } + if ( !isset( $wgTranslateTranslationServices[$name] ) ) { + throw new TTMServerException( "Invalid configuration in " . + "mirrors, unknown service $name" ); + } + $mirrors[$name] = true; + } + return array_keys( $mirrors ); + } + return []; + } + + /** + * @return bool + */ + public function isFrozen() { + return false; } } |