MediaWiki master
Diff.php
Go to the documentation of this file.
1<?php
13namespace Wikimedia\Diff;
14
20class Diff {
21
25 public $edits;
26
31 protected $bailoutComplexity = 0;
32
44 public function __construct( $from_lines, $to_lines ) {
45 $eng = new DiffEngine;
46 $eng->setBailoutComplexity( $this->bailoutComplexity );
47 $this->edits = $eng->diff( $from_lines, $to_lines );
48 }
49
53 public function getEdits() {
54 return $this->edits;
55 }
56
68 public function reverse() {
69 $rev = $this;
70 $rev->edits = [];
72 foreach ( $this->edits as $edit ) {
73 $rev->edits[] = $edit->reverse();
74 }
75
76 return $rev;
77 }
78
84 public function isEmpty() {
85 foreach ( $this->edits as $edit ) {
86 if ( $edit->type != 'copy' ) {
87 return false;
88 }
89 }
90
91 return true;
92 }
93
101 public function lcs() {
102 $lcs = 0;
103 foreach ( $this->edits as $edit ) {
104 if ( $edit->type == 'copy' ) {
105 $lcs += count( $edit->orig );
106 }
107 }
108
109 return $lcs;
110 }
111
120 public function orig() {
121 $lines = [];
122
123 foreach ( $this->edits as $edit ) {
124 if ( $edit->orig ) {
125 array_splice( $lines, count( $lines ), 0, $edit->orig );
126 }
127 }
128
129 return $lines;
130 }
131
140 public function closing() {
141 $lines = [];
142
143 foreach ( $this->edits as $edit ) {
144 if ( $edit->closing ) {
145 array_splice( $lines, count( $lines ), 0, $edit->closing );
146 }
147 }
148
149 return $lines;
150 }
151}
152
154class_alias( Diff::class, 'Diff' );
This diff implementation is mainly lifted from the LCS algorithm of the Eclipse project which in turn...
setBailoutComplexity( $value)
Sets the complexity (in comparison operations) that can't be exceeded.
The base class for all other DiffOp classes.
Definition DiffOp.php:24
Class representing a 'diff' between two sequences of strings.
Definition Diff.php:20
__construct( $from_lines, $to_lines)
Computes diff between sequences of strings.
Definition Diff.php:44
isEmpty()
Check for empty diff.
Definition Diff.php:84
DiffOp[] $edits
Definition Diff.php:25
reverse()
Compute reversed Diff.
Definition Diff.php:68
int $bailoutComplexity
If this diff complexity is exceeded, a ComplexityException is thrown 0 means no limit.
Definition Diff.php:31
orig()
Get the original set of lines.
Definition Diff.php:120
lcs()
Compute the length of the Longest Common Subsequence (LCS).
Definition Diff.php:101
closing()
Get the closing set of lines.
Definition Diff.php:140