[ Index ]

PHP Cross Reference of MyBB

title

Body

[close]

/inc/3rdparty/diff/Diff/ -> ThreeWay.php (source)

   1  <?php
   2  /**
   3   * A class for computing three way merges.
   4   *
   5   * Copyright 2007-2011 Horde LLC (http://www.horde.org/)
   6   *
   7   * See the enclosed file COPYING for license information (LGPL). If you did
   8   * not receive this file, see http://www.horde.org/licenses/lgpl21.
   9   *
  10   * @package Text_Diff
  11   * @author  Geoffrey T. Dairiki <dairiki@dairiki.org>
  12   */
  13  
  14  // Disallow direct access to this file for security reasons
  15  if(!defined("IN_MYBB"))
  16  {
  17      die("Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.");
  18  }
  19  
  20  class Horde_Text_Diff_ThreeWay
  21  {
  22      /**
  23       * Array of changes.
  24       *
  25       * @var array
  26       */
  27      protected $_edits;
  28  
  29      /**
  30       * Conflict counter.
  31       *
  32       * @var integer
  33       */
  34      protected $_conflictingBlocks = 0;
  35  
  36      /**
  37       * Computes diff between 3 sequences of strings.
  38       *
  39       * @param array $orig    The original lines to use.
  40       * @param array $final1  The first version to compare to.
  41       * @param array $final2  The second version to compare to.
  42       */
  43      public function __construct($orig, $final1, $final2)
  44      {
  45          if (extension_loaded('xdiff')) {
  46              $engine = new Horde_Text_Diff_Engine_Xdiff();
  47          } else {
  48              $engine = new Horde_Text_Diff_Engine_Native();
  49          }
  50  
  51          $this->_edits = $this->_diff3($engine->diff($orig, $final1),
  52                                        $engine->diff($orig, $final2));
  53      }
  54  
  55      /**
  56       */
  57      public function mergedOutput($label1 = false, $label2 = false)
  58      {
  59          $lines = array();
  60          foreach ($this->_edits as $edit) {
  61              if ($edit->isConflict()) {
  62                  /* FIXME: this should probably be moved somewhere else. */
  63                  $lines = array_merge($lines,
  64                                       array('<<<<<<<' . ($label1 ? ' ' . $label1 : '')),
  65                                       $edit->final1,
  66                                       array("======="),
  67                                       $edit->final2,
  68                                       array('>>>>>>>' . ($label2 ? ' ' . $label2 : '')));
  69                  $this->_conflictingBlocks++;
  70              } else {
  71                  $lines = array_merge($lines, $edit->merged());
  72              }
  73          }
  74  
  75          return $lines;
  76      }
  77  
  78      /**
  79       */
  80      protected function _diff3($edits1, $edits2)
  81      {
  82          $edits = array();
  83          $bb = new Horde_Text_Diff_ThreeWay_BlockBuilder();
  84  
  85          $e1 = current($edits1);
  86          $e2 = current($edits2);
  87          while ($e1 || $e2) {
  88              if ($e1 && $e2 &&
  89                  $e1 instanceof Horde_Text_Diff_Op_Copy &&
  90                  $e2 instanceof Horde_Text_Diff_Op_Copy) {
  91                  /* We have copy blocks from both diffs. This is the (only)
  92                   * time we want to emit a diff3 copy block.  Flush current
  93                   * diff3 diff block, if any. */
  94                  if ($edit = $bb->finish()) {
  95                      $edits[] = $edit;
  96                  }
  97  
  98                  $ncopy = min($e1->norig(), $e2->norig());
  99                  assert($ncopy > 0);
 100                  $edits[] = new Horde_Text_Diff_ThreeWay_Op_Copy(array_slice($e1->orig, 0, $ncopy));
 101  
 102                  if ($e1->norig() > $ncopy) {
 103                      array_splice($e1->orig, 0, $ncopy);
 104                      array_splice($e1->final, 0, $ncopy);
 105                  } else {
 106                      $e1 = next($edits1);
 107                  }
 108  
 109                  if ($e2->norig() > $ncopy) {
 110                      array_splice($e2->orig, 0, $ncopy);
 111                      array_splice($e2->final, 0, $ncopy);
 112                  } else {
 113                      $e2 = next($edits2);
 114                  }
 115              } else {
 116                  if ($e1 && $e2) {
 117                      if ($e1->orig && $e2->orig) {
 118                          $norig = min($e1->norig(), $e2->norig());
 119                          $orig = array_splice($e1->orig, 0, $norig);
 120                          array_splice($e2->orig, 0, $norig);
 121                          $bb->input($orig);
 122                      }
 123  
 124                      if ($e1 instanceof Horde_Text_Diff_Op_Copy) {
 125                          $bb->out1(array_splice($e1->final, 0, $norig));
 126                      }
 127  
 128                      if ($e2 instanceof Horde_Text_Diff_Op_Copy) {
 129                          $bb->out2(array_splice($e2->final, 0, $norig));
 130                      }
 131                  }
 132  
 133                  if ($e1 && ! $e1->orig) {
 134                      $bb->out1($e1->final);
 135                      $e1 = next($edits1);
 136                  }
 137                  if ($e2 && ! $e2->orig) {
 138                      $bb->out2($e2->final);
 139                      $e2 = next($edits2);
 140                  }
 141              }
 142          }
 143  
 144          if ($edit = $bb->finish()) {
 145              $edits[] = $edit;
 146          }
 147  
 148          return $edits;
 149      }
 150  }


Generated: Tue Oct 8 19:19:50 2013 Cross-referenced by PHPXref 0.7.1