diff.inc.php 8.2 KB

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