Translate extension for MediaWiki
Loading...
Searching...
No Matches
MessageSourceChange.php
Go to the documentation of this file.
1<?php
8
9namespace MediaWiki\Extension\Translate\MessageSync;
10
11use InvalidArgumentException;
12
25 protected $changes = [];
26 public const ADDITION = 'addition';
27 public const CHANGE = 'change';
28 public const DELETION = 'deletion';
29 public const RENAME = 'rename';
30 public const NONE = 'none';
31
32 private const SIMILARITY_THRESHOLD = 0.9;
33
38 protected $addFunctionMap;
43 protected $removeFunctionMap;
44
46 public function __construct( $changes = [] ) {
47 $this->changes = $changes;
48 $this->addFunctionMap = [
49 self::ADDITION => [ $this, 'addAddition' ],
50 self::DELETION => [ $this, 'addDeletion' ],
51 self::CHANGE => [ $this, 'addChange' ]
52 ];
53
54 $this->removeFunctionMap = [
55 self::ADDITION => [ $this, 'removeAdditions' ],
56 self::DELETION => [ $this, 'removeDeletions' ],
57 self::CHANGE => [ $this, 'removeChanges' ]
58 ];
59 }
60
67 public function addChange( $language, $key, $content ) {
68 $this->addModification( $language, self::CHANGE, $key, $content );
69 }
70
77 public function addAddition( $language, $key, $content ) {
78 $this->addModification( $language, self::ADDITION, $key, $content );
79 }
80
87 public function addDeletion( $language, $key, $content ) {
88 $this->addModification( $language, self::DELETION, $key, $content );
89 }
90
98 public function addRename( $language, $addedMessage, $deletedMessage, $similarity = 0 ) {
99 $this->changes[$language][self::RENAME][$addedMessage['key']] = [
100 'content' => $addedMessage['content'],
101 'similarity' => $similarity,
102 'matched_to' => $deletedMessage['key'],
103 'previous_state' => self::ADDITION,
104 'key' => $addedMessage['key']
105 ];
106
107 $this->changes[$language][self::RENAME][$deletedMessage['key']] = [
108 'content' => $deletedMessage['content'],
109 'similarity' => $similarity,
110 'matched_to' => $addedMessage['key'],
111 'previous_state' => self::DELETION,
112 'key' => $deletedMessage['key']
113 ];
114 }
115
121 public function setRenameState( $language, $msgKey, $state ) {
122 $possibleStates = [ self::ADDITION, self::CHANGE, self::DELETION,
123 self::NONE, self::RENAME ];
124 if ( !in_array( $state, $possibleStates ) ) {
125 throw new InvalidArgumentException(
126 "Invalid state passed - '$state'. Possible states - "
127 . implode( ', ', $possibleStates )
128 );
129 }
130
131 $languageChanges = null;
132 if ( isset( $this->changes[ $language ] ) ) {
133 $languageChanges = &$this->changes[ $language ];
134 }
135 if ( $languageChanges !== null && isset( $languageChanges[ 'rename' ][ $msgKey ] ) ) {
136 $languageChanges[ 'rename' ][ $msgKey ][ 'previous_state' ] = $state;
137 }
138 }
139
146 protected function addModification( $language, $type, $key, $content ) {
147 $this->changes[$language][$type][] = [
148 'key' => $key,
149 'content' => $content,
150 ];
151 }
152
158 public function getChanges( $language ) {
159 return $this->getModification( $language, self::CHANGE );
160 }
161
167 public function getDeletions( $language ) {
168 return $this->getModification( $language, self::DELETION );
169 }
170
176 public function getAdditions( $language ) {
177 return $this->getModification( $language, self::ADDITION );
178 }
179
188 public function findMessage( $language, $key, $possibleStates = [], &$modificationType = null ) {
189 if ( $possibleStates === [] ) {
190 $possibleStates = [ self::ADDITION, self::CHANGE, self::DELETION, self::RENAME ];
191 }
192
193 // Keep the existing lookup order
194 foreach ( [ self::ADDITION, self::DELETION, self::CHANGE, self::RENAME ] as $type ) {
195 if ( !in_array( $type, $possibleStates ) ) {
196 continue;
197 }
198
199 if ( $type === self::RENAME ) {
200 $rename = $this->changes[$language][self::RENAME][$key] ?? null;
201 if ( $rename !== null ) {
202 // Same shape as getRenames()
203 $rename['key'] = $key;
204 $modificationType = $type;
205 return $rename;
206 }
207 continue;
208 }
209
210 foreach ( $this->getModification( $language, $type ) as $modification ) {
211 if ( $modification['key'] === $key ) {
212 $modificationType = $type;
213 return $modification;
214 }
215 }
216 }
217
218 $modificationType = null;
219 return null;
220 }
221
228 public function breakRename( $languageCode, $msgKey ) {
229 $msg = $this->findMessage( $languageCode, $msgKey, [ self::RENAME ] );
230 if ( $msg === null ) {
231 return null;
232 }
233 $matchedMsg = $this->getMatchedMessage( $languageCode, $msg['key'] );
234 if ( $matchedMsg === null ) {
235 return null;
236 }
237
238 // Remove them from the renames array
239 $this->removeRenames( $languageCode, [ $matchedMsg['key'], $msg['key'] ] );
240
241 $matchedMsgState = $matchedMsg[ 'previous_state' ];
242 $msgState = $msg[ 'previous_state' ];
243
244 // Add them to the changes under the appropriate state
245 if ( $matchedMsgState !== self::NONE ) {
246 if ( $matchedMsgState === self::CHANGE ) {
247 $matchedMsg['key'] = $msg['key'];
248 }
249 $this->addFunctionMap[ $matchedMsgState ](
250 $languageCode,
251 $matchedMsg['key'],
252 $matchedMsg['content']
253 );
254 }
255
256 if ( $msgState !== self::NONE ) {
257 if ( $msgState === self::CHANGE ) {
258 $msg['key'] = $matchedMsg['key'];
259 }
260 $this->addFunctionMap[ $msgState ](
261 $languageCode,
262 $msg['key'],
263 $msg['content']
264 );
265 }
266
267 return $msgState;
268 }
269
275 public function getRenames( $language ) {
276 $renames = $this->getModification( $language, self::RENAME );
277 foreach ( $renames as $key => &$rename ) {
278 $rename['key'] = $key;
279 }
280
281 return $renames;
282 }
283
289 protected function getModification( $language, $type ) {
290 return $this->changes[$language][$type] ?? [];
291 }
292
298 public function removeAdditions( $language, $keysToRemove ) {
299 $this->removeModification( $language, self::ADDITION, $keysToRemove );
300 }
301
307 public function removeDeletions( $language, $keysToRemove ) {
308 $this->removeModification( $language, self::DELETION, $keysToRemove );
309 }
310
316 public function removeChanges( $language, $keysToRemove ) {
317 $this->removeModification( $language, self::CHANGE, $keysToRemove );
318 }
319
325 public function removeRenames( $language, $keysToRemove ) {
326 $this->removeModification( $language, self::RENAME, $keysToRemove );
327 }
328
336 public function removeBasedOnType( $language, $keysToRemove, $type ) {
337 $callable = $this->removeFunctionMap[ $type ] ?? null;
338
339 if ( $callable === null ) {
340 throw new InvalidArgumentException( 'Type should be one of ' .
341 implode( ', ', [ self::ADDITION, self::CHANGE, self::DELETION ] ) .
342 ". Invalid type $type passed."
343 );
344 }
345
346 $callable( $language, $keysToRemove );
347 }
348
353 public function removeChangesForLanguage( $language ) {
354 unset( $this->changes[ $language ] );
355 }
356
362 protected function removeModification( $language, $type, $keysToRemove = null ) {
363 if ( !isset( $this->changes[$language][$type] ) || $keysToRemove === [] ) {
364 // Nothing to do
365 return;
366 }
367
368 if ( $keysToRemove === null ) {
369 unset( $this->changes[$language][$type] );
370 return;
371 }
372
373 if ( $type === self::RENAME ) {
374 $this->changes[$language][$type] =
375 array_diff_key( $this->changes[$language][$type], array_flip( $keysToRemove ) );
376 } else {
377 $this->changes[$language][$type] = array_filter(
378 $this->changes[$language][$type],
379 static function ( $change ) use ( $keysToRemove ) {
380 return !in_array( $change['key'], $keysToRemove, true );
381 }
382 );
383 }
384 }
385
390 public function getAllModifications() {
391 return $this->changes;
392 }
393
399 public function getModificationsForLanguage( $language ) {
400 return $this->changes[$language] ?? [];
401 }
402
408 public static function loadModifications( $changesData ) {
409 return new self( $changesData );
410 }
411
416 public function getLanguages() {
417 return array_keys( $this->changes );
418 }
419
427 public function hasOnly( $language, $type ) {
428 $modifications = $this->getModificationsForLanguage( $language );
429 foreach ( [ self::ADDITION, self::CHANGE, self::DELETION, self::RENAME ] as $t ) {
430 // Stop if modifications of any type other than the allowed type exist
431 if ( $t !== $type && ( $modifications[$t] ?? null ) ) {
432 return false;
433 }
434 }
435 return true;
436 }
437
445 public function isPreviousState( $languageCode, $key, array $types ) {
446 $previousState = $this->changes[$languageCode][self::RENAME][$key]['previous_state'] ?? null;
447 return $previousState !== null && in_array( $previousState, $types );
448 }
449
456 public function getMatchedMessage( $languageCode, $key ) {
457 $matchedKey = $this->getMatchedKey( $languageCode, $key );
458 if ( $matchedKey ) {
459 return $this->changes[ $languageCode ][ self::RENAME ][ $matchedKey ] ?? null;
460 }
461
462 return null;
463 }
464
471 public function getMatchedKey( $languageCode, $key ) {
472 return $this->changes[ $languageCode ][ self::RENAME ][ $key ][ 'matched_to' ] ?? null;
473 }
474
481 public function getSimilarity( $languageCode, $key ) {
482 return $this->changes[$languageCode][self::RENAME][$key]['similarity'] ?? null;
483 }
484
491 public function isEqual( $languageCode, $key ) {
492 $similarity = $this->getSimilarity( $languageCode, $key );
493 // Comparators may return int 1 or float 1.0
494 return $similarity !== null && $similarity >= 1;
495 }
496
504 public function isSimilar( $languageCode, $key ) {
505 $similarity = $this->getSimilarity( $languageCode, $key );
506 return $similarity !== null && $this->areStringsSimilar( $similarity );
507 }
508
514 public function areStringsSimilar( $similarity ) {
515 return $similarity >= self::SIMILARITY_THRESHOLD;
516 }
517}
Class is used to track the changes made when importing messages from the remote sources using importE...
addRename( $language, $addedMessage, $deletedMessage, $similarity=0)
Adds a rename under a message group for a specific language.
getLanguages()
Get all language keys with modifications under the group.
removeAdditions( $language, $keysToRemove)
Remove additions for a language under the group.
getSimilarity( $languageCode, $key)
Returns the calculated similarity for a rename.
getMatchedKey( $languageCode, $key)
Get matched rename key for a given key.
isPreviousState( $languageCode, $key, array $types)
Checks if the previous state of a renamed message matches a given value.
static loadModifications( $changesData)
Loads the changes, and returns an instance of the class.
addAddition( $language, $key, $content)
Add an addition under a message group for a specific language.
findMessage( $language, $key, $possibleStates=[], &$modificationType=null)
Finds a message with the given key across different types of modifications.
getMatchedMessage( $languageCode, $key)
Get matched rename message for a given key.
addChange( $language, $key, $content)
Add a change under a message group for a specific language.
removeRenames( $language, $keysToRemove)
Remove renames for a language under the group.
isEqual( $languageCode, $key)
Checks if a given key is equal to matched rename message.
hasOnly( $language, $type)
Determines if the group has only a certain type of change under a language.
getModificationsForLanguage( $language)
Get all for a language under the group.
getChanges( $language)
Fetch changes for a message group under a language.
isSimilar( $languageCode, $key)
Checks if a given key is similar to matched rename message.
removeDeletions( $language, $keysToRemove)
Remove deletions for a language under the group.
getAdditions( $language)
Fetch additions for a message group under a language.
getRenames( $language)
Fetch renames for a message group under a language.
removeChanges( $language, $keysToRemove)
Remove changes for a language under the group.
removeChangesForLanguage( $language)
Remove all language related changes for a group.
getDeletions( $language)
Fetch deletions for a message group under a language.
addDeletion( $language, $key, $content)
Adds a deletion under a message group for a specific language.
removeBasedOnType( $language, $keysToRemove, $type)
Remove modifications based on the type.
areStringsSimilar( $similarity)
Checks if the similarity percent passed passes the min threshold.
breakRename( $languageCode, $msgKey)
Break renames, and put messages back into their previous state.