diff.inc.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php // $Id: lib.diff.php,v 1.12 2005/11/18 20:25:11 zefredz Exp $
  2. // vim: expandtab sw=4 ts=4 sts=4:
  3. /**
  4. * CLAROLINE
  5. *
  6. * @version 1.7 $Revision: 1.12 $
  7. *
  8. * @copyright 2001-2005 Universite catholique de Louvain (UCL)
  9. *
  10. * @license http://www.gnu.org/copyleft/gpl.html (GPL) GENERAL PUBLIC LICENSE
  11. * This program is under the terms of the GENERAL PUBLIC LICENSE (GPL)
  12. * as published by the FREE SOFTWARE FOUNDATION. The GPL is available
  13. * through the world-wide-web at http://www.gnu.org/copyleft/gpl.html
  14. *
  15. * @author Frederic Minne <zefredz@gmail.com>
  16. *
  17. * @package Wiki
  18. */
  19. define( "DIFF_EQUAL", "=" );
  20. define( "DIFF_ADDED", "+" );
  21. define( "DIFF_DELETED", "-" );
  22. define( "DIFF_MOVED", "M" );
  23. /**
  24. * Get difference between two strings
  25. * @param string old first string
  26. * @param string new second string
  27. * @param boolean show_equals set to true to see line that are equal between
  28. * the two strings (default true)
  29. * @param string format_line_function callback function to format line
  30. * (default 'format_line')
  31. * @return string formated diff output
  32. */
  33. function diff( $old, $new, $show_equals = false, $format_line_function = 'format_line' )
  34. {
  35. $oldArr = str_split_on_new_line( $old );
  36. $newArr = str_split_on_new_line( $new );
  37. $oldCount = count ( $oldArr );
  38. $newCount = count ( $newArr );
  39. $max = max( $oldCount, $newCount );
  40. //get added and deleted lines
  41. $deleted = array_diff_assoc( $oldArr, $newArr );
  42. $added = array_diff_assoc( $newArr, $oldArr );
  43. $moved = array();
  44. foreach ( $added as $key => $candidate )
  45. {
  46. foreach ( $deleted as $index => $content )
  47. {
  48. if ( $candidate == $content )
  49. {
  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. {
  60. // line changed
  61. if ( isset ( $deleted[$i] ) && isset( $added[$i] ) )
  62. {
  63. $output .= $format_line_function( $i, DIFF_DELETED, $deleted[$i] );
  64. $output .= $format_line_function( $i, DIFF_ADDED, $added[$i] );
  65. }
  66. // line deleted
  67. elseif ( isset ( $deleted[$i] ) && ! isset ( $added[$i] ) )
  68. {
  69. $output .= $format_line_function( $i, DIFF_DELETED, $deleted[$i] );
  70. }
  71. // line added
  72. elseif ( isset ( $added[$i] ) && ! isset ( $deleted[$i] ) )
  73. {
  74. $output .= $format_line_function( $i, DIFF_ADDED, $added[$i] );
  75. }
  76. // line moved
  77. elseif ( isset ( $moved[$i] ) )
  78. {
  79. $output .= $format_line_function( $i, DIFF_MOVED, $newArr[$i] );
  80. }
  81. // line unchanged
  82. elseif ( $show_equals == true )
  83. {
  84. $output .= $format_line_function( $i, DIFF_EQUAL, $newArr[$i] );
  85. }
  86. else
  87. {
  88. // skip
  89. }
  90. }
  91. return $output;
  92. }
  93. /**
  94. * Split strings on new line
  95. */
  96. function str_split_on_new_line( $str )
  97. {
  98. $content = array();
  99. if ( strpos( $str, "\r\n" ) != false )
  100. {
  101. $content = explode("\r\n", $str );
  102. }
  103. elseif ( strpos( $str, "\n" ) != false )
  104. {
  105. $content = explode( "\n", $str );
  106. }
  107. elseif ( strpos( $str, "\r" ) != false )
  108. {
  109. $content = explode( "\r", $str );
  110. }
  111. else
  112. {
  113. $content[] = $str;
  114. }
  115. return $content;
  116. }
  117. /**
  118. * Default and prototype format line function
  119. * @param int line line number
  120. * @param mixed type line type, must be one of the following :
  121. * DIFF_EQUAL, DIFF_MOVED, DIFF_ADDED, DIFF_DELETED
  122. * @param string value line content
  123. * @param boolean skip_empty skip empty lines (default false)
  124. * @return string formated diff line
  125. */
  126. function format_line( $line, $type, $value, $skip_empty = false )
  127. {
  128. if ( trim( $value ) == "" && $skip_empty )
  129. {
  130. return "";
  131. }
  132. elseif ( trim( $value ) == "" )
  133. {
  134. $value = '&nbsp;';
  135. }
  136. switch ( $type )
  137. {
  138. case DIFF_EQUAL:
  139. {
  140. // return $line. ' : ' . ' = <span class="diffEqual" >' . $value . '</span><br />' . "\n" ;
  141. return '<span class="diffEqual" >' . $value . '</span><br />' . "\n" ; //juan carlos muestra solo color
  142. break;
  143. }
  144. case DIFF_MOVED:
  145. {
  146. //return $line. ' : ' . ' M <span class="diffMoved" >' . $value . '</span><br />' . "\n" ; //juan carlos raña la sustitye la inverior
  147. return '<span class="diffMoved" >' . $value . '</span><br />' . "\n" ; //juan carlos muestra solo color
  148. break;
  149. }
  150. case DIFF_ADDED:
  151. {
  152. //return $line . ' : ' . ' + <span class="diffAdded" >' . $value . '</span><br />' . "\n" ;
  153. return '<span class="diffAdded" >' . $value . '</span><br />' . "\n" ; //juan carlos muestra solo color
  154. break;
  155. }
  156. case DIFF_DELETED:
  157. {
  158. //return $line . ' : ' . ' - <span class="diffDeleted" >' . $value . '</span><br />' . "\n" ; //juan carlos raña la sustitye la inverior
  159. return '<span class="diffDeleted" >' . $value . '</span><br />' . "\n" ; //juan carlos muestra solo color
  160. break;
  161. }
  162. }
  163. }
  164. /**
  165. * Table format line function
  166. * @see format_line
  167. */
  168. function format_table_line( $line, $type, $value, $skip_empty = false )
  169. {
  170. if ( trim( $value ) == "" && $skip_empty )
  171. {
  172. return "";
  173. }
  174. elseif ( trim( $value ) == "" )
  175. {
  176. $value = '&nbsp;';
  177. }
  178. switch ( $type )
  179. {
  180. case DIFF_EQUAL:
  181. {
  182. //return '<tr><td>' . $line. '&nbsp;:&nbsp;' . '&nbsp;=</td><td><span class="diffEqual" >' . $value . '</span></td></tr>' . "\n"; //juan carlos comentado
  183. 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.
  184. break;
  185. }
  186. case DIFF_MOVED:
  187. {
  188. // return '<tr><td>' . $line. '&nbsp;:&nbsp;' . '&nbsp;M</td><td><span class="diffMoved" >' . $value . '</span></td></tr>' . "\n" //juan carlos comenta
  189. ;
  190. 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.
  191. break;
  192. }
  193. case DIFF_ADDED:
  194. {
  195. // return '<tr><td>' . $line. '&nbsp;:&nbsp;' . '&nbsp;+</td><td><span class="diffAdded" >' . $value . '</span></td></tr>' . "\n" ; //juan carlos comentado
  196. 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.
  197. break;
  198. }
  199. case DIFF_DELETED:
  200. {
  201. // return '<tr><td>' . $line. '&nbsp;:&nbsp;' . '&nbsp;-</td><td><span class="diffDeleted" >' . $value . '</span></td></tr>' . "\n" ; //juan carlos comentado
  202. 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.
  203. }
  204. }
  205. }
  206. if (! function_exists('array_diff_assoc') )
  207. {
  208. /**
  209. * Replace array_diff_assoc()
  210. *
  211. * @link http://php.net/function.array_diff_assoc
  212. * @author Aidan Lister <aidan@php.net>
  213. * @since PHP 4.3.0
  214. * @require PHP 4.0.0 (user_error)
  215. */
  216. function array_diff_assoc()
  217. {
  218. // Check we have enough arguments
  219. $args = func_get_args();
  220. $count = count($args );
  221. if (count($args ) < 2 )
  222. {
  223. trigger_error('Wrong parameter count for array_diff_assoc()', E_USER_WARNING );
  224. return;
  225. }
  226. // Check arrays
  227. for ($i = 0; $i < $count; $i++ )
  228. {
  229. if (! is_array($args[$i] ) )
  230. {
  231. trigger_error('array_diff_assoc() Argument #' . ($i + 1) . ' is not an array', E_USER_WARNING );
  232. return;
  233. }
  234. }
  235. // Get the comparison array
  236. $array_comp = array_shift($args );
  237. --$count;
  238. // Traverse values of the first array
  239. foreach ($array_comp as $key => $value )
  240. {
  241. // Loop through the other arrays
  242. for ($i = 0; $i < $count; $i++ )
  243. {
  244. // Loop through this arrays key/value pairs and compare
  245. foreach ($args[$i] as $comp_key => $comp_value )
  246. {
  247. if ((string) $key === (string)$comp_key && (string) $value === (string) $comp_value )
  248. {
  249. unset($array_comp[$key] );
  250. }
  251. }
  252. }
  253. }
  254. return $array_comp;
  255. }
  256. }
  257. ?>