PageRenderTime 63ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/php/main/wiki/diff.inc.php

https://bitbucket.org/frchico/chamilo_openshift
PHP | 298 lines | 185 code | 38 blank | 75 comment | 25 complexity | 266369c20b44828bbfae320aef8b8079 MD5 | raw file
  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. /**
  20. * Code
  21. */
  22. define( "DIFF_EQUAL", "=" );
  23. define( "DIFF_ADDED", "+" );
  24. define( "DIFF_DELETED", "-" );
  25. define( "DIFF_MOVED", "M" );
  26. /**
  27. * Get difference between two strings
  28. * @param string old first string
  29. * @param string new second string
  30. * @param boolean show_equals set to true to see line that are equal between
  31. * the two strings (default true)
  32. * @param string format_line_function callback function to format line
  33. * (default 'format_line')
  34. * @return string formated diff output
  35. */
  36. function diff( $old, $new, $show_equals = false, $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 = array();
  47. foreach ( $added as $key => $candidate )
  48. {
  49. foreach ( $deleted as $index => $content )
  50. {
  51. if ( $candidate == $content )
  52. {
  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. {
  63. // line changed
  64. if ( isset ( $deleted[$i] ) && isset( $added[$i] ) )
  65. {
  66. $output .= $format_line_function( $i, DIFF_DELETED, $deleted[$i] );
  67. $output .= $format_line_function( $i, DIFF_ADDED, $added[$i] );
  68. }
  69. // line deleted
  70. elseif ( isset ( $deleted[$i] ) && ! isset ( $added[$i] ) )
  71. {
  72. $output .= $format_line_function( $i, DIFF_DELETED, $deleted[$i] );
  73. }
  74. // line added
  75. elseif ( isset ( $added[$i] ) && ! isset ( $deleted[$i] ) )
  76. {
  77. $output .= $format_line_function( $i, DIFF_ADDED, $added[$i] );
  78. }
  79. // line moved
  80. elseif ( isset ( $moved[$i] ) )
  81. {
  82. $output .= $format_line_function( $i, DIFF_MOVED, $newArr[$i] );
  83. }
  84. // line unchanged
  85. elseif ( $show_equals )
  86. {
  87. $output .= $format_line_function( $i, DIFF_EQUAL, $newArr[$i] );
  88. }
  89. else
  90. {
  91. // skip
  92. }
  93. }
  94. return $output;
  95. }
  96. /**
  97. * Split strings on new line
  98. */
  99. function str_split_on_new_line( $str )
  100. {
  101. $content = array();
  102. if ( api_strpos( $str, "\r\n" ) !== false )
  103. {
  104. $content = explode("\r\n", $str );
  105. }
  106. elseif ( api_strpos( $str, "\n" ) !== false )
  107. {
  108. $content = explode( "\n", $str );
  109. }
  110. elseif ( api_strpos( $str, "\r" ) !== false )
  111. {
  112. $content = explode( "\r", $str );
  113. }
  114. else
  115. {
  116. $content[] = $str;
  117. }
  118. return $content;
  119. }
  120. /**
  121. * Default and prototype format line function
  122. * @param int line line number
  123. * @param mixed type line type, must be one of the following :
  124. * DIFF_EQUAL, DIFF_MOVED, DIFF_ADDED, DIFF_DELETED
  125. * @param string value line content
  126. * @param boolean skip_empty skip empty lines (default false)
  127. * @return string formated diff line
  128. */
  129. function format_line( $line, $type, $value, $skip_empty = false )
  130. {
  131. if ( trim( $value ) == "" && $skip_empty )
  132. {
  133. return "";
  134. }
  135. elseif ( trim( $value ) == "" )
  136. {
  137. $value = '&nbsp;';
  138. }
  139. switch ( $type )
  140. {
  141. case DIFF_EQUAL:
  142. {
  143. // return $line. ' : ' . ' = <span class="diffEqual" >' . $value . '</span><br />' . "\n" ;
  144. return '<span class="diffEqual" >' . $value . '</span><br />' . "\n" ; //juan carlos muestra solo color
  145. break;
  146. }
  147. case DIFF_MOVED:
  148. {
  149. //return $line. ' : ' . ' M <span class="diffMoved" >' . $value . '</span><br />' . "\n" ; //juan carlos ra�a la sustitye la inverior
  150. return '<span class="diffMoved" >' . $value . '</span><br />' . "\n" ; //juan carlos muestra solo color
  151. break;
  152. }
  153. case DIFF_ADDED:
  154. {
  155. //return $line . ' : ' . ' + <span class="diffAdded" >' . $value . '</span><br />' . "\n" ;
  156. return '<span class="diffAdded" >' . $value . '</span><br />' . "\n" ; //juan carlos muestra solo color
  157. break;
  158. }
  159. case DIFF_DELETED:
  160. {
  161. //return $line . ' : ' . ' - <span class="diffDeleted" >' . $value . '</span><br />' . "\n" ; //juan carlos ra�a la sustitye la inverior
  162. return '<span class="diffDeleted" >' . $value . '</span><br />' . "\n" ; //juan carlos muestra solo color
  163. break;
  164. }
  165. }
  166. }
  167. /**
  168. * Table format line function
  169. * @see format_line
  170. */
  171. function format_table_line( $line, $type, $value, $skip_empty = false )
  172. {
  173. if ( trim( $value ) == "" && $skip_empty )
  174. {
  175. return "";
  176. }
  177. elseif ( trim( $value ) == "" )
  178. {
  179. $value = '&nbsp;';
  180. }
  181. switch ( $type )
  182. {
  183. case DIFF_EQUAL:
  184. {
  185. //return '<tr><td>' . $line. '&nbsp;:&nbsp;' . '&nbsp;=</td><td><span class="diffEqual" >' . $value . '</span></td></tr>' . "\n"; //juan carlos comentado
  186. 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.
  187. break;
  188. }
  189. case DIFF_MOVED:
  190. {
  191. // return '<tr><td>' . $line. '&nbsp;:&nbsp;' . '&nbsp;M</td><td><span class="diffMoved" >' . $value . '</span></td></tr>' . "\n" //juan carlos comenta
  192. ;
  193. 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.
  194. break;
  195. }
  196. case DIFF_ADDED:
  197. {
  198. // return '<tr><td>' . $line. '&nbsp;:&nbsp;' . '&nbsp;+</td><td><span class="diffAdded" >' . $value . '</span></td></tr>' . "\n" ; //juan carlos comentado
  199. 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.
  200. break;
  201. }
  202. case DIFF_DELETED:
  203. {
  204. // return '<tr><td>' . $line. '&nbsp;:&nbsp;' . '&nbsp;-</td><td><span class="diffDeleted" >' . $value . '</span></td></tr>' . "\n" ; //juan carlos comentado
  205. 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.
  206. }
  207. }
  208. }
  209. if (! function_exists('array_diff_assoc') )
  210. {
  211. /**
  212. * Replace array_diff_assoc()
  213. *
  214. * @link http://php.net/function.array_diff_assoc
  215. * @author Aidan Lister <aidan@php.net>
  216. * @since PHP 4.3.0
  217. * @require PHP 4.0.0 (user_error)
  218. */
  219. function array_diff_assoc()
  220. {
  221. // Check we have enough arguments
  222. $args = func_get_args();
  223. $count = count($args );
  224. if (count($args ) < 2 )
  225. {
  226. trigger_error('Wrong parameter count for array_diff_assoc()', E_USER_WARNING );
  227. return;
  228. }
  229. // Check arrays
  230. for ($i = 0; $i < $count; $i++ )
  231. {
  232. if (! is_array($args[$i] ) )
  233. {
  234. trigger_error('array_diff_assoc() Argument #' . ($i + 1) . ' is not an array', E_USER_WARNING );
  235. return;
  236. }
  237. }
  238. // Get the comparison array
  239. $array_comp = array_shift($args );
  240. --$count;
  241. // Traverse values of the first array
  242. foreach ($array_comp as $key => $value )
  243. {
  244. // Loop through the other arrays
  245. for ($i = 0; $i < $count; $i++ )
  246. {
  247. // Loop through this arrays key/value pairs and compare
  248. foreach ($args[$i] as $comp_key => $comp_value )
  249. {
  250. if ((string) $key === (string)$comp_key && (string) $value === (string) $comp_value )
  251. {
  252. unset($array_comp[$key] );
  253. }
  254. }
  255. }
  256. }
  257. return $array_comp;
  258. }
  259. }
  260. ?>