ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Text_Diff Class Reference
+ Inheritance diagram for Text_Diff:
+ Collaboration diagram for Text_Diff:

Public Member Functions

 __construct ($from_lines, $to_lines)
 Computes diffs between sequences of strings. More...
 
 getDiff ()
 Returns the array of differences. More...
 
 reverse ()
 Computes a reversed diff. More...
 
 isEmpty ()
 Checks for an empty diff. More...
 
 lcs ()
 Computes the length of the Longest Common Subsequence (LCS). More...
 
 getOriginal ()
 Gets the original set of lines. More...
 
 getFinal ()
 Gets the final set of lines. More...
 
 _trimNewlines (&$line, $key)
 Removes trailing newlines from a line of text. More...
 
 _check ($from_lines, $to_lines)
 Checks a diff for validity. More...
 

Data Fields

 $_edits
 

Detailed Description

Definition at line 16 of file Diff.php.

Constructor & Destructor Documentation

◆ __construct()

Text_Diff::__construct (   $from_lines,
  $to_lines 
)

Computes diffs between sequences of strings.

Parameters
array$from_linesAn array of strings. Typically these are lines from a file.
array$to_linesAn array of strings.

Definition at line 33 of file Diff.php.

34 {
35 array_walk($from_lines, array($this, '_trimNewlines'));
36 array_walk($to_lines, array($this, '_trimNewlines'));
37
38 if (extension_loaded('xdiff')) {
40 } else {
42 }
43
44 $this->_edits = $engine->diff($from_lines, $to_lines);
45 }
$engine
Definition: workflow.php:89

References $engine.

Member Function Documentation

◆ _check()

Text_Diff::_check (   $from_lines,
  $to_lines 
)

Checks a diff for validity.

This is here only for debugging purposes.

Definition at line 166 of file Diff.php.

167 {
168 if (serialize($from_lines) != serialize($this->getOriginal())) {
169 trigger_error("Reconstructed original doesn't match", E_USER_ERROR);
170 }
171 if (serialize($to_lines) != serialize($this->getFinal())) {
172 trigger_error("Reconstructed final doesn't match", E_USER_ERROR);
173 }
174
175 $rev = $this->reverse();
176 if (serialize($to_lines) != serialize($rev->getOriginal())) {
177 trigger_error("Reversed original doesn't match", E_USER_ERROR);
178 }
179 if (serialize($from_lines) != serialize($rev->getFinal())) {
180 trigger_error("Reversed final doesn't match", E_USER_ERROR);
181 }
182
183 $prevtype = null;
184 foreach ($this->_edits as $edit) {
185 if ($prevtype == get_class($edit)) {
186 trigger_error("Edit sequence is non-optimal", E_USER_ERROR);
187 }
188 $prevtype = get_class($edit);
189 }
190
191 return true;
192 }
getOriginal()
Gets the original set of lines.
Definition: Diff.php:120
reverse()
Computes a reversed diff.
Definition: Diff.php:69
getFinal()
Gets the final set of lines.
Definition: Diff.php:138

References getFinal(), getOriginal(), and reverse().

+ Here is the call graph for this function:

◆ _trimNewlines()

Text_Diff::_trimNewlines ( $line,
  $key 
)

Removes trailing newlines from a line of text.

This is meant to be used with array_walk().

Parameters
string$lineThe line to trim.
integer$keyThe index of the line in the array. Not used.

Definition at line 156 of file Diff.php.

157 {
158 $line = str_replace(array("\n", "\r"), '', $line);
159 }

◆ getDiff()

Text_Diff::getDiff ( )

Returns the array of differences.

Definition at line 50 of file Diff.php.

51 {
52 return $this->_edits;
53 }
$_edits
Definition: Diff.php:24

References $_edits.

◆ getFinal()

Text_Diff::getFinal ( )

Gets the final set of lines.

This reconstructs the $to_lines parameter passed to the constructor.

Returns
array The sequence of strings.

Definition at line 138 of file Diff.php.

139 {
140 $lines = array();
141 foreach ($this->_edits as $edit) {
142 if ($edit->final) {
143 array_splice($lines, count($lines), 0, $edit->final);
144 }
145 }
146 return $lines;
147 }

Referenced by _check().

+ Here is the caller graph for this function:

◆ getOriginal()

Text_Diff::getOriginal ( )

Gets the original set of lines.

This reconstructs the $from_lines parameter passed to the constructor.

Returns
array The original sequence of strings.

Definition at line 120 of file Diff.php.

121 {
122 $lines = array();
123 foreach ($this->_edits as $edit) {
124 if ($edit->orig) {
125 array_splice($lines, count($lines), 0, $edit->orig);
126 }
127 }
128 return $lines;
129 }

Referenced by _check().

+ Here is the caller graph for this function:

◆ isEmpty()

Text_Diff::isEmpty ( )

Checks for an empty diff.

Returns
boolean True if two sequences were identical.

Definition at line 85 of file Diff.php.

86 {
87 foreach ($this->_edits as $edit) {
88 if (!$edit instanceof Text_Diff_Op_copy) {
89 return false;
90 }
91 }
92 return true;
93 }

◆ lcs()

Text_Diff::lcs ( )

Computes the length of the Longest Common Subsequence (LCS).

This is mostly for diagnostic purposes.

Returns
integer The length of the LCS.

Definition at line 102 of file Diff.php.

103 {
104 $lcs = 0;
105 foreach ($this->_edits as $edit) {
106 if ($edit instanceof Text_Diff_Op_copy) {
107 $lcs += count($edit->orig);
108 }
109 }
110 return $lcs;
111 }

◆ reverse()

Text_Diff::reverse ( )

Computes a reversed diff.

Example: $diff = &new Text_Diff($lines1, $lines2); $rev = $diff->reverse();

Returns
Text_Diff A Diff object representing the inverse of the original diff. Note that we purposely don't return a reference here, since this essentially is a clone() method.

Definition at line 69 of file Diff.php.

70 {
71 $rev = clone($obj);
72
73 $rev->_edits = array();
74 foreach ($this->_edits as $edit) {
75 $rev->_edits[] = $edit->reverse();
76 }
77 return $rev;
78 }

Referenced by _check().

+ Here is the caller graph for this function:

Field Documentation

◆ $_edits

Text_Diff::$_edits

Definition at line 24 of file Diff.php.

Referenced by getDiff().


The documentation for this class was generated from the following file: