Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
TTMServer.php
Go to the documentation of this file.
1<?php
11use MediaWiki\MediaWikiServices;
12
19abstract class TTMServer {
21 protected $config;
22
24 public function __construct( array $config ) {
25 $this->config = $config;
26 }
27
32 public static function sortSuggestions( array $suggestions ) {
33 usort( $suggestions, static function ( $a, $b ) {
34 return $b['quality'] <=> $a['quality'];
35 } );
36
37 return $suggestions;
38 }
39
51 public static function levenshtein( $str1, $str2, $length1, $length2 ) {
52 if ( $length1 === 0 ) {
53 return $length2;
54 }
55 if ( $length2 === 0 ) {
56 return $length1;
57 }
58 if ( $str1 === $str2 ) {
59 return 0;
60 }
61
62 $bytelength1 = strlen( $str1 );
63 $bytelength2 = strlen( $str2 );
64 if ( $bytelength1 === $length1 && $bytelength1 <= 255
65 && $bytelength2 === $length2 && $bytelength2 <= 255
66 ) {
67 return levenshtein( $str1, $str2 );
68 }
69
70 $prevRow = range( 0, $length2 );
71 for ( $i = 0; $i < $length1; $i++ ) {
72 $currentRow = [];
73 $currentRow[0] = $i + 1;
74 $c1 = mb_substr( $str1, $i, 1 );
75 for ( $j = 0; $j < $length2; $j++ ) {
76 $c2 = mb_substr( $str2, $j, 1 );
77 $insertions = $prevRow[$j + 1] + 1;
78 $deletions = $currentRow[$j] + 1;
79 $substitutions = $prevRow[$j] + ( ( $c1 !== $c2 ) ? 1 : 0 );
80 $currentRow[] = min( $insertions, $deletions, $substitutions );
81 }
82 $prevRow = $currentRow;
83 }
84
85 return $prevRow[$length2];
86 }
87
92 public static function onDelete( WikiPage $wikipage ) {
93 $handle = new MessageHandle( $wikipage->getTitle() );
94 $job = TTMServerMessageUpdateJob::newJob( $handle, 'delete' );
95 MediaWikiServices::getInstance()->getJobQueueGroup()->push( $job );
96 }
97
102 public static function onChange( MessageHandle $handle ) {
103 $job = TTMServerMessageUpdateJob::newJob( $handle, 'refresh' );
104 MediaWikiServices::getInstance()->getJobQueueGroup()->push( $job );
105 }
106
111 public static function onGroupChange( MessageHandle $handle, $old ) {
112 if ( $old === [] ) {
113 // Don't bother for newly added messages
114 return;
115 }
116
117 $job = TTMServerMessageUpdateJob::newJob( $handle, 'rebuild' );
118 MediaWikiServices::getInstance()->getJobQueueGroup()->push( $job );
119 }
120
125 public function getMirrors(): array {
126 global $wgTranslateTranslationServices;
127 if ( isset( $this->config['mirrors'] ) ) {
128 $mirrors = [];
129 foreach ( $this->config['mirrors'] as $name ) {
130 if ( !is_string( $name ) ) {
131 throw new TTMServerException( "Invalid configuration set in " .
132 "mirrors, expected an array of strings" );
133 }
134 if ( !isset( $wgTranslateTranslationServices[$name] ) ) {
135 throw new TTMServerException( "Invalid configuration in " .
136 "mirrors, unknown service $name" );
137 }
138 $mirrors[$name] = true;
139 }
140 return array_keys( $mirrors );
141 }
142 return [];
143 }
144}
Class for pointing to messages, like Title class is for titles.
static newJob(MessageHandle $handle, $command)
Some general static methods for instantiating TTMServer and helpers.
Definition TTMServer.php:19
static sortSuggestions(array $suggestions)
Definition TTMServer.php:32
static levenshtein( $str1, $str2, $length1, $length2)
PHP implementation of Levenshtein edit distance algorithm.
Definition TTMServer.php:51
__construct(array $config)
Definition TTMServer.php:24
static onDelete(WikiPage $wikipage)
Hook: ArticleDeleteComplete.
Definition TTMServer.php:92
static onGroupChange(MessageHandle $handle, $old)
static onChange(MessageHandle $handle)
Called from TranslateEditAddons::onSave.