b2evolution PHP Cross Reference Blogging Systems

Source: /inc/_ext/phpsvnclient/ext/Diff/Diff3.php - 278 lines - 7270 bytes - Summary - Text - Print

Description: A class for computing three way diffs. $Horde: framework/Text_Diff/Diff3.php,v 1.2.10.7 2009/01/06 15:23:41 jan Exp $

   1  <?php
   2  /**
   3   * A class for computing three way diffs.
   4   *
   5   * $Horde: framework/Text_Diff/Diff3.php,v 1.2.10.7 2009/01/06 15:23:41 jan Exp $
   6   *
   7   * Copyright 2007-2009 The Horde Project (http://www.horde.org/)
   8   *
   9   * See the enclosed file COPYING for license information (LGPL). If you did
  10   * not receive this file, see http://opensource.org/licenses/lgpl-license.php.
  11   *
  12   * @package Text_Diff
  13   * @since   0.3.0
  14   */
  15  if( ! defined( 'EVO_MAIN_INIT' ) ) die( 'Please, do not access this page directly.' );
  16  
  17  /** Text_Diff */
  18  require_once 'Text/Diff.php';
  19  
  20  /**
  21   * A class for computing three way diffs.
  22   *
  23   * @package Text_Diff
  24   * @author  Geoffrey T. Dairiki <dairiki@dairiki.org>
  25   */
  26  class Text_Diff3 extends Text_Diff {
  27  
  28      /**
  29       * Conflict counter.
  30       *
  31       * @var integer
  32       */
  33      var $_conflictingBlocks = 0;
  34  
  35      /**
  36       * Computes diff between 3 sequences of strings.
  37       *
  38       * @param array $orig    The original lines to use.
  39       * @param array $final1  The first version to compare to.
  40       * @param array $final2  The second version to compare to.
  41       */
  42      function Text_Diff3($orig, $final1, $final2)
  43      {
  44          if (extension_loaded('xdiff')) {
  45              $engine = new Text_Diff_Engine_xdiff();
  46          } else {
  47              $engine = new Text_Diff_Engine_native();
  48          }
  49  
  50          $this->_edits = $this->_diff3($engine->diff($orig, $final1),
  51                                        $engine->diff($orig, $final2));
  52      }
  53  
  54      /**
  55       */
  56      function mergedOutput($label1 = false, $label2 = false)
  57      {
  58          $lines = array();
  59          foreach ($this->_edits as $edit) {
  60              if ($edit->isConflict()) {
  61                  /* FIXME: this should probably be moved somewhere else. */
  62                  $lines = array_merge($lines,
  63                                       array('<<<<<<<' . ($label1 ? ' ' . $label1 : '')),
  64                                       $edit->final1,
  65                                       array("======="),
  66                                       $edit->final2,
  67                                       array('>>>>>>>' . ($label2 ? ' ' . $label2 : '')));
  68                  $this->_conflictingBlocks++;
  69              } else {
  70                  $lines = array_merge($lines, $edit->merged());
  71              }
  72          }
  73  
  74          return $lines;
  75      }
  76  
  77      /**
  78       * @access private
  79       */
  80      function _diff3($edits1, $edits2)
  81      {
  82          $edits = array();
  83          $bb = new Text_Diff3_BlockBuilder();
  84  
  85          $e1 = current($edits1);
  86          $e2 = current($edits2);
  87          while ($e1 || $e2) {
  88              if ($e1 && $e2 && is_a($e1, 'Text_Diff_Op_copy') && is_a($e2, 'Text_Diff_Op_copy')) {
  89                  /* We have copy blocks from both diffs. This is the (only)
  90                   * time we want to emit a diff3 copy block.  Flush current
  91                   * diff3 diff block, if any. */
  92                  if ($edit = $bb->finish()) {
  93                      $edits[] = $edit;
  94                  }
  95  
  96                  $ncopy = min($e1->norig(), $e2->norig());
  97                  assert($ncopy > 0);
  98                  $edits[] = new Text_Diff3_Op_copy(array_slice($e1->orig, 0, $ncopy));
  99  
 100                  if ($e1->norig() > $ncopy) {
 101                      array_splice($e1->orig, 0, $ncopy);
 102                      array_splice($e1->final, 0, $ncopy);
 103                  } else {
 104                      $e1 = next($edits1);
 105                  }
 106  
 107                  if ($e2->norig() > $ncopy) {
 108                      array_splice($e2->orig, 0, $ncopy);
 109                      array_splice($e2->final, 0, $ncopy);
 110                  } else {
 111                      $e2 = next($edits2);
 112                  }
 113              } else {
 114                  if ($e1 && $e2) {
 115                      if ($e1->orig && $e2->orig) {
 116                          $norig = min($e1->norig(), $e2->norig());
 117                          $orig = array_splice($e1->orig, 0, $norig);
 118                          array_splice($e2->orig, 0, $norig);
 119                          $bb->input($orig);
 120                      }
 121  
 122                      if (is_a($e1, 'Text_Diff_Op_copy')) {
 123                          $bb->out1(array_splice($e1->final, 0, $norig));
 124                      }
 125  
 126                      if (is_a($e2, 'Text_Diff_Op_copy')) {
 127                          $bb->out2(array_splice($e2->final, 0, $norig));
 128                      }
 129                  }
 130  
 131                  if ($e1 && ! $e1->orig) {
 132                      $bb->out1($e1->final);
 133                      $e1 = next($edits1);
 134                  }
 135                  if ($e2 && ! $e2->orig) {
 136                      $bb->out2($e2->final);
 137                      $e2 = next($edits2);
 138                  }
 139              }
 140          }
 141  
 142          if ($edit = $bb->finish()) {
 143              $edits[] = $edit;
 144          }
 145  
 146          return $edits;
 147      }
 148  
 149  }
 150  
 151  /**
 152   * @package Text_Diff
 153   * @author  Geoffrey T. Dairiki <dairiki@dairiki.org>
 154   *
 155   * @access private
 156   */
 157  class Text_Diff3_Op {
 158  
 159      function Text_Diff3_Op($orig = false, $final1 = false, $final2 = false)
 160      {
 161          $this->orig = $orig ? $orig : array();
 162          $this->final1 = $final1 ? $final1 : array();
 163          $this->final2 = $final2 ? $final2 : array();
 164      }
 165  
 166      function merged()
 167      {
 168          if (!isset($this->_merged)) {
 169              if ($this->final1 === $this->final2) {
 170                  $this->_merged = &$this->final1;
 171              } elseif ($this->final1 === $this->orig) {
 172                  $this->_merged = &$this->final2;
 173              } elseif ($this->final2 === $this->orig) {
 174                  $this->_merged = &$this->final1;
 175              } else {
 176                  $this->_merged = false;
 177              }
 178          }
 179  
 180          return $this->_merged;
 181      }
 182  
 183      function isConflict()
 184      {
 185          return $this->merged() === false;
 186      }
 187  
 188  }
 189  
 190  /**
 191   * @package Text_Diff
 192   * @author  Geoffrey T. Dairiki <dairiki@dairiki.org>
 193   *
 194   * @access private
 195   */
 196  class Text_Diff3_Op_copy extends Text_Diff3_Op {
 197  
 198      function Text_Diff3_Op_Copy($lines = false)
 199      {
 200          $this->orig = $lines ? $lines : array();
 201          $this->final1 = &$this->orig;
 202          $this->final2 = &$this->orig;
 203      }
 204  
 205      function merged()
 206      {
 207          return $this->orig;
 208      }
 209  
 210      function isConflict()
 211      {
 212          return false;
 213      }
 214  
 215  }
 216  
 217  /**
 218   * @package Text_Diff
 219   * @author  Geoffrey T. Dairiki <dairiki@dairiki.org>
 220   *
 221   * @access private
 222   */
 223  class Text_Diff3_BlockBuilder {
 224  
 225      function Text_Diff3_BlockBuilder()
 226      {
 227          $this->_init();
 228      }
 229  
 230      function input($lines)
 231      {
 232          if ($lines) {
 233              $this->_append($this->orig, $lines);
 234          }
 235      }
 236  
 237      function out1($lines)
 238      {
 239          if ($lines) {
 240              $this->_append($this->final1, $lines);
 241          }
 242      }
 243  
 244      function out2($lines)
 245      {
 246          if ($lines) {
 247              $this->_append($this->final2, $lines);
 248          }
 249      }
 250  
 251      function isEmpty()
 252      {
 253          return !$this->orig && !$this->final1 && !$this->final2;
 254      }
 255  
 256      function finish()
 257      {
 258          if ($this->isEmpty()) {
 259              return false;
 260          } else {
 261              $edit = new Text_Diff3_Op($this->orig, $this->final1, $this->final2);
 262              $this->_init();
 263              return $edit;
 264          }
 265      }
 266  
 267      function _init()
 268      {
 269          $this->orig = $this->final1 = $this->final2 = array();
 270      }
 271  
 272      function _append(&$array, $lines)
 273      {
 274          array_splice($array, sizeof($array), 0, $lines);
 275      }
 276  
 277  }
 278  ?>

title

Description

title

Description

title

Description

title

title

Body