|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* CLAROLINE. |
|
4
|
|
|
* |
|
5
|
|
|
* @version 1.7 $Revision: 1.12 $ |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright 2001-2005 Universite catholique de Louvain (UCL) |
|
8
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html (GPL) GENERAL PUBLIC LICENSE |
|
9
|
|
|
* This program is under the terms of the GENERAL PUBLIC LICENSE (GPL) |
|
10
|
|
|
* as published by the FREE SOFTWARE FOUNDATION. The GPL is available |
|
11
|
|
|
* through the world-wide-web at http://www.gnu.org/copyleft/gpl.html |
|
12
|
|
|
* @author Frederic Minne <[email protected]> |
|
13
|
|
|
* |
|
14
|
|
|
* @package Wiki |
|
15
|
|
|
*/ |
|
16
|
|
|
define("DIFF_EQUAL", "="); |
|
17
|
|
|
define("DIFF_ADDED", "+"); |
|
18
|
|
|
define("DIFF_DELETED", "-"); |
|
19
|
|
|
define("DIFF_MOVED", "M"); |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Get difference between two strings. |
|
23
|
|
|
* |
|
24
|
|
|
* @param string old first string |
|
25
|
|
|
* @param string new second string |
|
26
|
|
|
* @param bool show_equals set to true to see line that are equal between |
|
27
|
|
|
* the two strings (default true) |
|
28
|
|
|
* @param string format_line_function callback function to format line |
|
29
|
|
|
* (default 'format_line') |
|
30
|
|
|
* |
|
31
|
|
|
* @return string formated diff output |
|
32
|
|
|
*/ |
|
33
|
|
|
function diff( |
|
34
|
|
|
$old, |
|
35
|
|
|
$new, |
|
36
|
|
|
$show_equals = false, |
|
37
|
|
|
$format_line_function = 'format_line' |
|
38
|
|
|
) { |
|
39
|
|
|
$oldArr = str_split_on_new_line($old); |
|
40
|
|
|
$newArr = str_split_on_new_line($new); |
|
41
|
|
|
|
|
42
|
|
|
$oldCount = count($oldArr); |
|
43
|
|
|
$newCount = count($newArr); |
|
44
|
|
|
|
|
45
|
|
|
$max = max($oldCount, $newCount); |
|
46
|
|
|
|
|
47
|
|
|
//get added and deleted lines |
|
48
|
|
|
|
|
49
|
|
|
$deleted = array_diff_assoc($oldArr, $newArr); |
|
50
|
|
|
$added = array_diff_assoc($newArr, $oldArr); |
|
51
|
|
|
|
|
52
|
|
|
$moved = []; |
|
53
|
|
|
|
|
54
|
|
|
foreach ($added as $key => $candidate) { |
|
55
|
|
|
foreach ($deleted as $index => $content) { |
|
56
|
|
|
if ($candidate == $content) { |
|
57
|
|
|
$moved[$key] = $candidate; |
|
58
|
|
|
unset($added[$key]); |
|
59
|
|
|
unset($deleted[$index]); |
|
60
|
|
|
break; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$output = ''; |
|
66
|
|
|
|
|
67
|
|
|
for ($i = 0; $i < $max; $i++) { |
|
68
|
|
|
// line changed |
|
69
|
|
|
if (isset($deleted[$i]) && isset($added[$i])) { |
|
70
|
|
|
$output .= $format_line_function($i, DIFF_DELETED, $deleted[$i]); |
|
71
|
|
|
$output .= $format_line_function($i, DIFF_ADDED, $added[$i]); |
|
72
|
|
|
} elseif (isset($deleted[$i]) && !isset($added[$i])) { |
|
73
|
|
|
// line deleted |
|
74
|
|
|
$output .= $format_line_function($i, DIFF_DELETED, $deleted[$i]); |
|
75
|
|
|
} elseif (isset($added[$i]) && !isset($deleted[$i])) { |
|
76
|
|
|
// line added |
|
77
|
|
|
$output .= $format_line_function($i, DIFF_ADDED, $added[$i]); |
|
78
|
|
|
} elseif (isset($moved[$i])) { |
|
79
|
|
|
// line moved |
|
80
|
|
|
$output .= $format_line_function($i, DIFF_MOVED, $newArr[$i]); |
|
81
|
|
|
} elseif ($show_equals) { |
|
82
|
|
|
// line unchanged |
|
83
|
|
|
$output .= $format_line_function($i, DIFF_EQUAL, $newArr[$i]); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $output; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Split strings on new line. |
|
92
|
|
|
*/ |
|
93
|
|
|
function str_split_on_new_line($str) |
|
94
|
|
|
{ |
|
95
|
|
|
$content = []; |
|
96
|
|
|
|
|
97
|
|
|
if (api_strpos($str, "\r\n") !== false) { |
|
98
|
|
|
$content = explode("\r\n", $str); |
|
99
|
|
|
} elseif (api_strpos($str, "\n") !== false) { |
|
100
|
|
|
$content = explode("\n", $str); |
|
101
|
|
|
} elseif (api_strpos($str, "\r") !== false) { |
|
102
|
|
|
$content = explode("\r", $str); |
|
103
|
|
|
} else { |
|
104
|
|
|
$content[] = $str; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $content; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Default and prototype format line function. |
|
112
|
|
|
* |
|
113
|
|
|
* @param int line line number |
|
114
|
|
|
* @param mixed type line type, must be one of the following : |
|
115
|
|
|
* DIFF_EQUAL, DIFF_MOVED, DIFF_ADDED, DIFF_DELETED |
|
116
|
|
|
* @param string value line content |
|
117
|
|
|
* @param bool skip_empty skip empty lines (default false) |
|
118
|
|
|
* |
|
119
|
|
|
* @return string formated diff line |
|
120
|
|
|
*/ |
|
121
|
|
|
function format_line($line, $type, $value, $skip_empty = false) |
|
122
|
|
|
{ |
|
123
|
|
|
if (trim($value) == "" && $skip_empty) { |
|
124
|
|
|
return ""; |
|
125
|
|
|
} elseif (trim($value) == "") { |
|
126
|
|
|
$value = ' '; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
switch ($type) { |
|
130
|
|
|
case DIFF_EQUAL: |
|
131
|
|
|
// return $line. ' : ' . ' = <span class="diffEqual" >' . $value . '</span><br />' . "\n" ; |
|
132
|
|
|
return '<span class="diffEqual" >'.$value.'</span><br />'."\n"; //juan carlos muestra solo color |
|
133
|
|
|
break; |
|
|
|
|
|
|
134
|
|
|
case DIFF_MOVED: |
|
135
|
|
|
//return $line. ' : ' . ' M <span class="diffMoved" >' . $value . '</span><br />' . "\n" ; //juan carlos ra�a la sustitye la inverior |
|
136
|
|
|
return '<span class="diffMoved" >'.$value.'</span><br />'."\n"; //juan carlos muestra solo color |
|
137
|
|
|
break; |
|
138
|
|
|
case DIFF_ADDED: |
|
139
|
|
|
//return $line . ' : ' . ' + <span class="diffAdded" >' . $value . '</span><br />' . "\n" ; |
|
140
|
|
|
return '<span class="diffAdded" >'.$value.'</span><br />'."\n"; //juan carlos muestra solo color |
|
141
|
|
|
break; |
|
142
|
|
|
case DIFF_DELETED: |
|
143
|
|
|
//return $line . ' : ' . ' - <span class="diffDeleted" >' . $value . '</span><br />' . "\n" ; //juan carlos ra�a la sustitye la inverior |
|
144
|
|
|
return '<span class="diffDeleted" >'.$value.'</span><br />'."\n"; //juan carlos muestra solo color |
|
145
|
|
|
break; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Table format line function. |
|
151
|
|
|
* |
|
152
|
|
|
* @see format_line |
|
153
|
|
|
*/ |
|
154
|
|
|
function format_table_line($line, $type, $value, $skip_empty = false) |
|
155
|
|
|
{ |
|
156
|
|
|
if (trim($value) == "" && $skip_empty) { |
|
157
|
|
|
return ""; |
|
158
|
|
|
} elseif (trim($value) == "") { |
|
159
|
|
|
$value = ' '; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
switch ($type) { |
|
163
|
|
|
case DIFF_EQUAL: |
|
164
|
|
|
return '<tr><td></td><td bgcolor="#FFFFFF">'.$value.'</td></tr>'."\n"; |
|
165
|
|
|
//juan carlos muestra solo color (no tambi�n la l�nea). |
|
166
|
|
|
// Adem�s EN IEXPLORER VA BIEN PERO EN FIREFOX 3 la etiqueta span no muestra el color de fondo que |
|
167
|
|
|
// est� definido en la hoja de estilos como background-color, aceptando s�lo la propiedad color |
|
168
|
|
|
// pero esta solo da color al texto con lo cual los cambios quedan poco resaltados, adem�s |
|
169
|
|
|
// los cambios de otros objetos que no sean texto no se indican por ej. a�adir una imagen, |
|
170
|
|
|
// por esta raz�n doy el color de fondo al td directamente. |
|
171
|
|
|
break; |
|
|
|
|
|
|
172
|
|
|
case DIFF_MOVED: |
|
173
|
|
|
return '<tr><td></td><td bgcolor="#FFFFAA">'.$value.'</td></tr>'."\n"; |
|
174
|
|
|
//juan carlos muestra solo color (no tambi�n la l�nea). Adem�s EN IEXPLORER VA BIEN PERO EN FIREFOX 3 |
|
175
|
|
|
// la etiqueta span no muestra el color de fondo que est� definido en la hoja de estilos como |
|
176
|
|
|
// background-color, aceptando s�lo la propiedad color pero esta solo da color al texto con lo cual |
|
177
|
|
|
// los cambios quedan poco resaltados, adem�s los cambios de otros objetos que no sean texto no se indican |
|
178
|
|
|
// por ej. a�adir una imagen, por esta raz�n doy el color de fondo al td directamente. |
|
179
|
|
|
break; |
|
180
|
|
|
case DIFF_ADDED: |
|
181
|
|
|
return '<tr><td></td><td bgcolor="#CCFFCC">'.$value.'</td></tr>'."\n"; |
|
182
|
|
|
//juan carlos muestra solo color (no tambi�n la l�nea). Adem�s EN IEXPLORER VA BIEN |
|
183
|
|
|
// PERO EN FIREFOX 3 la etiqueta span no muestra el color de fondo que est� definido en la |
|
184
|
|
|
// hoja de estilos como background-color, aceptando s�lo la propiedad color pero esta solo |
|
185
|
|
|
// da color al texto con lo cual los cambios quedan poco resaltados, adem�s los cambios de |
|
186
|
|
|
// otros objetos que no sean texto no se indican por ej. a�adir una imagen, por esta raz�n |
|
187
|
|
|
// doy el color de fondo al td directamente. |
|
188
|
|
|
break; |
|
189
|
|
|
case DIFF_DELETED: |
|
190
|
|
|
return '<tr><td></td><td bgcolor="#FFAAAA">'.$value.'</td></tr>'."\n"; |
|
191
|
|
|
//juan carlos muestra solo color (no tambi�n la l�nea). Adem�s EN IEXPLORER VA BIEN PERO EN FIREFOX 3 |
|
192
|
|
|
// la etiqueta span no muestra el color de fondo que est� definido en la hoja de estilos como background-color, |
|
193
|
|
|
// aceptando s�lo la propiedad color pero esta solo da color al texto con lo cual los cambios quedan poco |
|
194
|
|
|
// resaltados, adem�s los cambios de otros objetos que no sean texto no se indican por ej. a�adir una imagen, |
|
195
|
|
|
// por esta raz�n doy el color de fondo al td directamente. |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|
The
breakstatement is not necessary if it is preceded for example by areturnstatement:If you would like to keep this construct to be consistent with other
casestatements, you can safely mark this issue as a false-positive.