Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
TTMServer.php
Go to the documentation of this file.
1<?php
12use MediaWiki\MediaWikiServices;
13
20abstract class TTMServer {
22 protected $config;
23
25 public function __construct( array $config ) {
26 $this->config = $config;
27 }
28
35 public static function factory( array $config ) {
36 // Cannot call factory directly because we don't have the name.
37 if ( isset( $config['class'] ) ) {
38 $class = $config['class'];
39
40 return new $class( $config );
41 } elseif ( isset( $config['type'] ) ) {
42 $type = $config['type'];
43 switch ( $type ) {
44 case 'ttmserver':
45 return new DatabaseTTMServer( $config );
46 case 'remote-ttmserver':
47 return new RemoteTTMServer( $config );
48 default:
49 return null;
50 }
51 }
52
53 throw new MWException( 'TTMServer with no type' );
54 }
55
63 public static function primary() {
64 return Services::getInstance()->getTtmServerFactory()->getDefault();
65 }
66
71 public static function sortSuggestions( array $suggestions ) {
72 usort( $suggestions, static function ( $a, $b ) {
73 return $b['quality'] <=> $a['quality'];
74 } );
75
76 return $suggestions;
77 }
78
90 public static function levenshtein( $str1, $str2, $length1, $length2 ) {
91 if ( $length1 === 0 ) {
92 return $length2;
93 }
94 if ( $length2 === 0 ) {
95 return $length1;
96 }
97 if ( $str1 === $str2 ) {
98 return 0;
99 }
100
101 $bytelength1 = strlen( $str1 );
102 $bytelength2 = strlen( $str2 );
103 if ( $bytelength1 === $length1 && $bytelength1 <= 255
104 && $bytelength2 === $length2 && $bytelength2 <= 255
105 ) {
106 return levenshtein( $str1, $str2 );
107 }
108
109 $prevRow = range( 0, $length2 );
110 for ( $i = 0; $i < $length1; $i++ ) {
111 $currentRow = [];
112 $currentRow[0] = $i + 1;
113 $c1 = mb_substr( $str1, $i, 1 );
114 for ( $j = 0; $j < $length2; $j++ ) {
115 $c2 = mb_substr( $str2, $j, 1 );
116 $insertions = $prevRow[$j + 1] + 1;
117 $deletions = $currentRow[$j] + 1;
118 $substitutions = $prevRow[$j] + ( ( $c1 !== $c2 ) ? 1 : 0 );
119 $currentRow[] = min( $insertions, $deletions, $substitutions );
120 }
121 $prevRow = $currentRow;
122 }
123
124 return $prevRow[$length2];
125 }
126
131 public static function onDelete( WikiPage $wikipage ) {
132 $handle = new MessageHandle( $wikipage->getTitle() );
133 $job = TTMServerMessageUpdateJob::newJob( $handle, 'delete' );
134 MediaWikiServices::getInstance()->getJobQueueGroup()->push( $job );
135 }
136
141 public static function onChange( MessageHandle $handle ) {
142 $job = TTMServerMessageUpdateJob::newJob( $handle, 'refresh' );
143 MediaWikiServices::getInstance()->getJobQueueGroup()->push( $job );
144 }
145
150 public static function onGroupChange( MessageHandle $handle, $old ) {
151 if ( $old === [] ) {
152 // Don't bother for newly added messages
153 return;
154 }
155
156 $job = TTMServerMessageUpdateJob::newJob( $handle, 'rebuild' );
157 MediaWikiServices::getInstance()->getJobQueueGroup()->push( $job );
158 }
159
161 public function getMirrors() {
162 global $wgTranslateTranslationServices;
163 if ( isset( $this->config['mirrors'] ) ) {
164 $mirrors = [];
165 foreach ( $this->config['mirrors'] as $name ) {
166 if ( !is_string( $name ) ) {
167 throw new TTMServerException( "Invalid configuration set in " .
168 "mirrors, expected an array of strings" );
169 }
170 if ( !isset( $wgTranslateTranslationServices[$name] ) ) {
171 throw new TTMServerException( "Invalid configuration in " .
172 "mirrors, unknown service $name" );
173 }
174 $mirrors[$name] = true;
175 }
176 return array_keys( $mirrors );
177 }
178 return [];
179 }
180
182 public function isFrozen() {
183 return false;
184 }
185}
Mysql based backend.
Minimal service container.
Definition Services.php:40
Class for pointing to messages, like Title class is for titles.
Class for handling remote TTMServers over MediaWiki API.
static newJob(MessageHandle $handle, $command)
Some general static methods for instantiating TTMServer and helpers.
Definition TTMServer.php:20
static sortSuggestions(array $suggestions)
Definition TTMServer.php:71
static levenshtein( $str1, $str2, $length1, $length2)
PHP implementation of Levenshtein edit distance algorithm.
Definition TTMServer.php:90
__construct(array $config)
Definition TTMServer.php:25
static primary()
Returns the primary server instance, useful for chaining.
Definition TTMServer.php:63
static onDelete(WikiPage $wikipage)
Hook: ArticleDeleteComplete.
static factory(array $config)
Definition TTMServer.php:35
static onGroupChange(MessageHandle $handle, $old)
static onChange(MessageHandle $handle)
Called from TranslateEditAddons::onSave.