9namespace MediaWiki\Extension\Translate\MessageSync;
11use InvalidArgumentException;
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';
32 private const SIMILARITY_THRESHOLD = 0.9;
38 protected $addFunctionMap;
43 protected $removeFunctionMap;
47 $this->changes = $changes;
48 $this->addFunctionMap = [
49 self::ADDITION => [ $this,
'addAddition' ],
50 self::DELETION => [ $this,
'addDeletion' ],
51 self::CHANGE => [ $this,
'addChange' ]
54 $this->removeFunctionMap = [
55 self::ADDITION => [ $this,
'removeAdditions' ],
56 self::DELETION => [ $this,
'removeDeletions' ],
57 self::CHANGE => [ $this,
'removeChanges' ]
67 public function addChange( $language, $key, $content ) {
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']
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']
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 )
131 $languageChanges =
null;
132 if ( isset( $this->changes[ $language ] ) ) {
133 $languageChanges = &$this->changes[ $language ];
135 if ( $languageChanges !==
null && isset( $languageChanges[
'rename' ][ $msgKey ] ) ) {
136 $languageChanges[
'rename' ][ $msgKey ][
'previous_state' ] = $state;
147 $this->changes[$language][$type][] = [
149 'content' => $content,
188 public function findMessage( $language, $key, $possibleStates = [], &$modificationType =
null ) {
189 if ( $possibleStates === [] ) {
190 $possibleStates = [ self::ADDITION, self::CHANGE, self::DELETION, self::RENAME ];
194 foreach ( [ self::ADDITION, self::DELETION, self::CHANGE, self::RENAME ] as $type ) {
195 if ( !in_array( $type, $possibleStates ) ) {
199 if ( $type === self::RENAME ) {
200 $rename = $this->changes[$language][self::RENAME][$key] ??
null;
201 if ( $rename !==
null ) {
203 $rename[
'key'] = $key;
204 $modificationType = $type;
210 foreach ( $this->
getModification( $language, $type ) as $modification ) {
211 if ( $modification[
'key'] === $key ) {
212 $modificationType = $type;
213 return $modification;
218 $modificationType =
null;
229 $msg = $this->
findMessage( $languageCode, $msgKey, [ self::RENAME ] );
230 if ( $msg ===
null ) {
234 if ( $matchedMsg ===
null ) {
239 $this->
removeRenames( $languageCode, [ $matchedMsg[
'key'], $msg[
'key'] ] );
241 $matchedMsgState = $matchedMsg[
'previous_state' ];
242 $msgState = $msg[
'previous_state' ];
245 if ( $matchedMsgState !== self::NONE ) {
246 if ( $matchedMsgState === self::CHANGE ) {
247 $matchedMsg[
'key'] = $msg[
'key'];
249 $this->addFunctionMap[ $matchedMsgState ](
252 $matchedMsg[
'content']
256 if ( $msgState !== self::NONE ) {
257 if ( $msgState === self::CHANGE ) {
258 $msg[
'key'] = $matchedMsg[
'key'];
260 $this->addFunctionMap[ $msgState ](
277 foreach ( $renames as $key => &$rename ) {
278 $rename[
'key'] = $key;
290 return $this->changes[$language][$type] ?? [];
337 $callable = $this->removeFunctionMap[ $type ] ??
null;
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."
346 $callable( $language, $keysToRemove );
354 unset( $this->changes[ $language ] );
363 if ( !isset( $this->changes[$language][$type] ) || $keysToRemove === [] ) {
368 if ( $keysToRemove ===
null ) {
369 unset( $this->changes[$language][$type] );
373 if ( $type === self::RENAME ) {
374 $this->changes[$language][$type] =
375 array_diff_key( $this->changes[$language][$type], array_flip( $keysToRemove ) );
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 );
391 return $this->changes;
400 return $this->changes[$language] ?? [];
409 return new self( $changesData );
417 return array_keys( $this->changes );
429 foreach ( [ self::ADDITION, self::CHANGE, self::DELETION, self::RENAME ] as $t ) {
431 if ( $t !== $type && ( $modifications[$t] ??
null ) ) {
446 $previousState = $this->changes[$languageCode][self::RENAME][$key][
'previous_state'] ??
null;
447 return $previousState !==
null && in_array( $previousState, $types );
459 return $this->changes[ $languageCode ][ self::RENAME ][ $matchedKey ] ??
null;
472 return $this->changes[ $languageCode ][ self::RENAME ][ $key ][
'matched_to' ] ??
null;
482 return $this->changes[$languageCode][self::RENAME][$key][
'similarity'] ??
null;
491 public function isEqual( $languageCode, $key ) {
494 return $similarity !==
null && $similarity >= 1;
515 return $similarity >= self::SIMILARITY_THRESHOLD;