MediaWiki  master
Diff.php
Go to the documentation of this file.
1 <?php
27 namespace Wikimedia\Diff;
28 
34 class Diff {
35 
39  public $edits;
40 
45  protected $bailoutComplexity = 0;
46 
58  public function __construct( $from_lines, $to_lines ) {
59  $eng = new DiffEngine;
60  $eng->setBailoutComplexity( $this->bailoutComplexity );
61  $this->edits = $eng->diff( $from_lines, $to_lines );
62  }
63 
67  public function getEdits() {
68  return $this->edits;
69  }
70 
82  public function reverse() {
83  $rev = $this;
84  $rev->edits = [];
86  foreach ( $this->edits as $edit ) {
87  $rev->edits[] = $edit->reverse();
88  }
89 
90  return $rev;
91  }
92 
98  public function isEmpty() {
99  foreach ( $this->edits as $edit ) {
100  if ( $edit->type != 'copy' ) {
101  return false;
102  }
103  }
104 
105  return true;
106  }
107 
115  public function lcs() {
116  $lcs = 0;
117  foreach ( $this->edits as $edit ) {
118  if ( $edit->type == 'copy' ) {
119  $lcs += count( $edit->orig );
120  }
121  }
122 
123  return $lcs;
124  }
125 
134  public function orig() {
135  $lines = [];
136 
137  foreach ( $this->edits as $edit ) {
138  if ( $edit->orig ) {
139  array_splice( $lines, count( $lines ), 0, $edit->orig );
140  }
141  }
142 
143  return $lines;
144  }
145 
154  public function closing() {
155  $lines = [];
156 
157  foreach ( $this->edits as $edit ) {
158  if ( $edit->closing ) {
159  array_splice( $lines, count( $lines ), 0, $edit->closing );
160  }
161  }
162 
163  return $lines;
164  }
165 }
166 
168 class_alias( Diff::class, 'Diff' );
This diff implementation is mainly lifted from the LCS algorithm of the Eclipse project which in turn...
Definition: DiffEngine.php:48
setBailoutComplexity( $value)
Sets the complexity (in comparison operations) that can't be exceeded.
Definition: DiffEngine.php:159
Class representing a 'diff' between two sequences of strings.
Definition: Diff.php:34
__construct( $from_lines, $to_lines)
Computes diff between sequences of strings.
Definition: Diff.php:58
isEmpty()
Check for empty diff.
Definition: Diff.php:98
DiffOp[] $edits
Definition: Diff.php:39
reverse()
Compute reversed Diff.
Definition: Diff.php:82
int $bailoutComplexity
If this diff complexity is exceeded, a ComplexityException is thrown 0 means no limit.
Definition: Diff.php:45
orig()
Get the original set of lines.
Definition: Diff.php:134
lcs()
Compute the length of the Longest Common Subsequence (LCS).
Definition: Diff.php:115
closing()
Get the closing set of lines.
Definition: Diff.php:154
if(!file_exists( $CREDITS)) $lines