diff.inc.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 <zefredz@gmail.com>
  13. *
  14. * @package Wiki
  15. */
  16. define("DIFF_EQUAL", "=");
  17. define("DIFF_ADDED", "+");
  18. define("DIFF_DELETED", "-");
  19. define("DIFF_MOVED", "M");
  20. /**
  21. * Get difference between two strings.
  22. *
  23. * @param string old first string
  24. * @param string new second string
  25. * @param bool show_equals set to true to see line that are equal between
  26. * the two strings (default true)
  27. * @param string format_line_function callback function to format line
  28. * (default 'format_line')
  29. *
  30. * @return string formated diff output
  31. */
  32. function diff(
  33. $old,
  34. $new,
  35. $show_equals = false,
  36. $format_line_function = 'format_line'
  37. ) {
  38. $oldArr = str_split_on_new_line($old);
  39. $newArr = str_split_on_new_line($new);
  40. $oldCount = count($oldArr);
  41. $newCount = count($newArr);
  42. $max = max($oldCount, $newCount);
  43. //get added and deleted lines
  44. $deleted = array_diff_assoc($oldArr, $newArr);
  45. $added = array_diff_assoc($newArr, $oldArr);
  46. $moved = [];
  47. foreach ($added as $key => $candidate) {
  48. foreach ($deleted as $index => $content) {
  49. if ($candidate == $content) {
  50. $moved[$key] = $candidate;
  51. unset($added[$key]);
  52. unset($deleted[$index]);
  53. break;
  54. }
  55. }
  56. }
  57. $output = '';
  58. for ($i = 0; $i < $max; $i++) {
  59. // line changed
  60. if (isset($deleted[$i]) && isset($added[$i])) {
  61. $output .= $format_line_function($i, DIFF_DELETED, $deleted[$i]);
  62. $output .= $format_line_function($i, DIFF_ADDED, $added[$i]);
  63. } elseif (isset($deleted[$i]) && !isset($added[$i])) {
  64. // line deleted
  65. $output .= $format_line_function($i, DIFF_DELETED, $deleted[$i]);
  66. } elseif (isset($added[$i]) && !isset($deleted[$i])) {
  67. // line added
  68. $output .= $format_line_function($i, DIFF_ADDED, $added[$i]);
  69. } elseif (isset($moved[$i])) {
  70. // line moved
  71. $output .= $format_line_function($i, DIFF_MOVED, $newArr[$i]);
  72. } elseif ($show_equals) {
  73. // line unchanged
  74. $output .= $format_line_function($i, DIFF_EQUAL, $newArr[$i]);
  75. }
  76. }
  77. return $output;
  78. }
  79. /**
  80. * Split strings on new line.
  81. */
  82. function str_split_on_new_line($str)
  83. {
  84. $content = [];
  85. if (api_strpos($str, "\r\n") !== false) {
  86. $content = explode("\r\n", $str);
  87. } elseif (api_strpos($str, "\n") !== false) {
  88. $content = explode("\n", $str);
  89. } elseif (api_strpos($str, "\r") !== false) {
  90. $content = explode("\r", $str);
  91. } else {
  92. $content[] = $str;
  93. }
  94. return $content;
  95. }
  96. /**
  97. * Default and prototype format line function.
  98. *
  99. * @param int line line number
  100. * @param mixed type line type, must be one of the following :
  101. * DIFF_EQUAL, DIFF_MOVED, DIFF_ADDED, DIFF_DELETED
  102. * @param string value line content
  103. * @param bool skip_empty skip empty lines (default false)
  104. *
  105. * @return string formated diff line
  106. */
  107. function format_line($line, $type, $value, $skip_empty = false)
  108. {
  109. if (trim($value) == "" && $skip_empty) {
  110. return "";
  111. } elseif (trim($value) == "") {
  112. $value = '&nbsp;';
  113. }
  114. switch ($type) {
  115. case DIFF_EQUAL:
  116. // return $line. ' : ' . ' = <span class="diffEqual" >' . $value . '</span><br />' . "\n" ;
  117. return '<span class="diffEqual" >'.$value.'</span><br />'."\n"; //juan carlos muestra solo color
  118. break;
  119. case DIFF_MOVED:
  120. //return $line. ' : ' . ' M <span class="diffMoved" >' . $value . '</span><br />' . "\n" ; //juan carlos ra�a la sustitye la inverior
  121. return '<span class="diffMoved" >'.$value.'</span><br />'."\n"; //juan carlos muestra solo color
  122. break;
  123. case DIFF_ADDED:
  124. //return $line . ' : ' . ' + <span class="diffAdded" >' . $value . '</span><br />' . "\n" ;
  125. return '<span class="diffAdded" >'.$value.'</span><br />'."\n"; //juan carlos muestra solo color
  126. break;
  127. case DIFF_DELETED:
  128. //return $line . ' : ' . ' - <span class="diffDeleted" >' . $value . '</span><br />' . "\n" ; //juan carlos ra�a la sustitye la inverior
  129. return '<span class="diffDeleted" >'.$value.'</span><br />'."\n"; //juan carlos muestra solo color
  130. break;
  131. }
  132. }
  133. /**
  134. * Table format line function.
  135. *
  136. * @see format_line
  137. */
  138. function format_table_line($line, $type, $value, $skip_empty = false)
  139. {
  140. if (trim($value) == "" && $skip_empty) {
  141. return "";
  142. } elseif (trim($value) == "") {
  143. $value = '&nbsp;';
  144. }
  145. switch ($type) {
  146. case DIFF_EQUAL:
  147. return '<tr><td></td><td bgcolor="#FFFFFF">'.$value.'</td></tr>'."\n";
  148. //juan carlos muestra solo color (no tambi�n la l�nea).
  149. // Adem�s EN IEXPLORER VA BIEN PERO EN FIREFOX 3 la etiqueta span no muestra el color de fondo que
  150. // est� definido en la hoja de estilos como background-color, aceptando s�lo la propiedad color
  151. // pero esta solo da color al texto con lo cual los cambios quedan poco resaltados, adem�s
  152. // los cambios de otros objetos que no sean texto no se indican por ej. a�adir una imagen,
  153. // por esta raz�n doy el color de fondo al td directamente.
  154. break;
  155. case DIFF_MOVED:
  156. return '<tr><td></td><td bgcolor="#FFFFAA">'.$value.'</td></tr>'."\n";
  157. //juan carlos muestra solo color (no tambi�n la l�nea). Adem�s EN IEXPLORER VA BIEN PERO EN FIREFOX 3
  158. // la etiqueta span no muestra el color de fondo que est� definido en la hoja de estilos como
  159. // background-color, aceptando s�lo la propiedad color pero esta solo da color al texto con lo cual
  160. // los cambios quedan poco resaltados, adem�s los cambios de otros objetos que no sean texto no se indican
  161. // por ej. a�adir una imagen, por esta raz�n doy el color de fondo al td directamente.
  162. break;
  163. case DIFF_ADDED:
  164. return '<tr><td></td><td bgcolor="#CCFFCC">'.$value.'</td></tr>'."\n";
  165. //juan carlos muestra solo color (no tambi�n la l�nea). Adem�s EN IEXPLORER VA BIEN
  166. // PERO EN FIREFOX 3 la etiqueta span no muestra el color de fondo que est� definido en la
  167. // hoja de estilos como background-color, aceptando s�lo la propiedad color pero esta solo
  168. // da color al texto con lo cual los cambios quedan poco resaltados, adem�s los cambios de
  169. // otros objetos que no sean texto no se indican por ej. a�adir una imagen, por esta raz�n
  170. // doy el color de fondo al td directamente.
  171. break;
  172. case DIFF_DELETED:
  173. return '<tr><td></td><td bgcolor="#FFAAAA">'.$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 background-color,
  176. // aceptando s�lo la propiedad color pero esta solo da color al texto con lo cual los cambios quedan poco
  177. // resaltados, adem�s los cambios de otros objetos que no sean texto no se indican por ej. a�adir una imagen,
  178. // por esta raz�n doy el color de fondo al td directamente.
  179. }
  180. }