Debian Codegraph @

includes/libs/Diff/Diff.php

raw symbols SCIP
1 <?php
2 /**
3 * A PHP diff engine for phpwiki. (Taken from phpwiki-1.3.3)
4 *
5 * Copyright © 2000, 2001 Geoffrey T. Dairiki <[email protected]>
6 * You may copy this code freely under the conditions of the GPL.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @file
24 * @ingroup DifferenceEngine
25 */
26
27 namespace Wikimedia\Diff;
28
29 /**
30 * Class representing a 'diff' between two sequences of strings.
31 * @newable
32 * @ingroup DifferenceEngine
33 */
34 class Diff {
35
36 /**
37 * @var DiffOp[]
38 */
39 public $edits;
40
41 /**
42 * @var int If this diff complexity is exceeded, a ComplexityException is thrown
43 * 0 means no limit.
44 */
45 protected $bailoutComplexity = 0;
46
47 /**
48 * Computes diff between sequences of strings.
49 *
50 * @stable to call
51 * @todo Don't do work in the constructor, use a service to create diffs instead (T257472).
52 *
53 * @param string[] $from_lines An array of strings.
54 * Typically these are lines from a file.
55 * @param string[] $to_lines An array of strings.
56 * @throws ComplexityException
57 */
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
64 /**
65 * @return DiffOp[]
66 */
67 public function getEdits() {
68 return $this->edits;
69 }
70
71 /**
72 * Compute reversed Diff.
73 *
74 * SYNOPSIS:
75 *
76 * $diff = new Diff($lines1, $lines2);
77 * $rev = $diff->reverse();
78 *
79 * @return self A Diff object representing the inverse of the
80 * original diff.
81 */
82 public function reverse() {
83 $rev = $this;
84 $rev->edits = [];
85 /** @var DiffOp $edit */
86 foreach ( $this->edits as $edit ) {
87 $rev->edits[] = $edit->reverse();
88 }
89
90 return $rev;
91 }
92
93 /**
94 * Check for empty diff.
95 *
96 * @return bool True if two sequences were identical.
97 */
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
108 /**
109 * Compute the length of the Longest Common Subsequence (LCS).
110 *
111 * This is mostly for diagnostic purposed.
112 *
113 * @return int The length of the LCS.
114 */
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
126 /**
127 * Get the original set of lines.
128 *
129 * This reconstructs the $from_lines parameter passed to the
130 * constructor.
131 *
132 * @return string[] The original sequence of strings.
133 */
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
146 /**
147 * Get the closing set of lines.
148 *
149 * This reconstructs the $to_lines parameter passed to the
150 * constructor.
151 *
152 * @return string[] The sequence of strings.
153 */
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
167 /** @deprecated class alias since 1.41 */
168 class_alias( Diff::class, 'Diff' );