DiffHelper.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. App::uses('AppHelper', 'View/Helper');
  3. App::uses('DiffLib', 'Tools.Lib');
  4. /**
  5. * DiffHelper class
  6. *
  7. * This class is a wrapper for PEAR Text_Diff with modified renderers from Horde
  8. * You need the stable Text_Diff from PEAR and (if you want to use them) two
  9. * renderers attached with this helper (sidebyside.php and character.php)
  10. *
  11. * To use this helper you either have to use the Vendor files in the Tools Plugin.
  12. * Wraps the DiffLib (which does all the heavy lifting) for the view layer.
  13. *
  14. * @author Marcin Domanski aka kabturek <blog@kabturek.info>
  15. * @license MIT
  16. * @modified Mark Scherer (Make it work with 2.x and clean it up into Lib + Helper)
  17. */
  18. class DiffHelper extends AppHelper {
  19. public $helpers = array('Html');
  20. /**
  21. * Construct function
  22. * Loads the vendor classes and sets the include path for autoloader to work
  23. *
  24. * @return void
  25. */
  26. public function __construct($View = null, $settings = array()) {
  27. parent::__construct($View, $settings);
  28. $this->Diff = new DiffLib();
  29. }
  30. /**
  31. * @param string $renderType
  32. * 'unified', 'inline', 'context', 'sidebyside'
  33. * defaults to "inline"
  34. * @return boolean true on success, false otherwise
  35. * 2010-01-12 ms
  36. */
  37. public function renderType($type = null) {
  38. return $this->Diff->renderType($type);
  39. }
  40. /**
  41. * @param string $engineType
  42. * 'auto', 'native', 'xdiff', 'shell', 'string'
  43. * defaults to "auto"
  44. * @return boolean true on success, false otherwise
  45. * 2010-01-12 ms
  46. */
  47. public function engineType($type = null) {
  48. return $this->Diff->engineType($type);
  49. }
  50. /**
  51. * compare function
  52. * Compares two strings/arrays using the specified method and renderer
  53. *
  54. * @param mixed $original
  55. * @param mixed $changed
  56. * @param array $options
  57. * - div: true/false
  58. * - class: defaults to "diff"
  59. * - escape: defaults to true
  60. * @return string $output
  61. */
  62. public function compare($original, $changed, $options = array()) {
  63. $original = $this->_prep($original);
  64. $changed = $this->_prep($changed);
  65. $string = $this->Diff->compare($original, $changed, $options);
  66. if (isset($options['div']) && $options['div'] === false) {
  67. return $string;
  68. }
  69. $defaults = array(
  70. 'class' => 'diff'
  71. );
  72. $options = array_merge($defaults, $options);
  73. $options['escape'] = null;
  74. return $this->Html->tag('div', $string, $options);
  75. }
  76. /**
  77. * @param string $string Either context or unified diff snippet
  78. * @param array $options
  79. * - mode (autodetect, context, unified)
  80. */
  81. public function reverse($string, $options = array()) {
  82. $string = $this->Diff->reverse($string, $options);
  83. if (isset($options['div']) && $options['div'] === false) {
  84. return $string;
  85. }
  86. $defaults = array(
  87. 'class' => 'diff'
  88. );
  89. $options = array_merge($defaults, $options);
  90. $options['escape'] = null;
  91. return $this->Html->tag('div', $string, $options);
  92. }
  93. /**
  94. * Prep for security
  95. * maybe switch to do that after comparison?
  96. *
  97. * @param string $string
  98. * @param array $options
  99. * @return string
  100. * 2010-01-12 ms
  101. */
  102. protected function _prep($string, $options = array()) {
  103. if ($this->renderer === 'inline' || isset($options['escape']) && $options['escape'] === false) {
  104. return $string;
  105. }
  106. return h($string);
  107. }
  108. }