class-wpml-st-diff.php
1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
/**
* Based on https://github.com/paulgb/simplediff/blob/master/php/simplediff.php
*/
class WPML_ST_Diff {
/**
* @param string[] $old_words
* @param string[] $new_words
*
* @return array
*/
public static function diff( $old_words, $new_words ) {
$matrix = array();
$max_length = 0;
$old_max = null;
$new_max = null;
foreach ( $old_words as $old_index => $old_value ) {
$new_keys = array_keys( $new_words, $old_value );
foreach ( $new_keys as $new_index ) {
$matrix[ $old_index ][ $new_index ] = isset( $matrix[ $old_index - 1 ][ $new_index - 1 ] ) ?
$matrix[ $old_index - 1 ][ $new_index - 1 ] + 1 : 1;
if ( $matrix[ $old_index ][ $new_index ] > $max_length ) {
$max_length = $matrix[ $old_index ][ $new_index ];
$old_max = $old_index + 1 - $max_length;
$new_max = $new_index + 1 - $max_length;
}
}
}
if ( $max_length == 0 ) {
return array( array( 'deleted' => $old_words, 'inserted' => $new_words ) );
}
return array_merge(
self::diff( array_slice( $old_words, 0, $old_max ), array_slice( $new_words, 0, $new_max ) ),
array_slice( $new_words, $new_max, $max_length ),
self::diff( array_slice( $old_words, $old_max + $max_length ), array_slice( $new_words, $new_max + $max_length ) )
);
}
/**
* @param string $old_text
* @param string $new_text
*
* @return float|int
*/
public static function get_sameness_percent( $old_text, $new_text ) {
$old_text = $old_text ? strip_tags( $old_text ) : $old_text;
if ( $old_text ) {
$new_text = strip_tags( $new_text );
$diff = self::diff( preg_split( '/[\s]+/', $old_text ), preg_split( '/[\s]+/', $new_text ) );
$common_length = 0;
foreach ( $diff as $diff_data ) {
if ( ! is_array( $diff_data ) ) {
$common_length += strlen( $diff_data );
}
}
return ( $common_length * 100 ) / strlen( $old_text );
} else {
return 0;
}
}
}