ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
Text_Diff Class Reference
+ Inheritance diagram for Text_Diff:
+ Collaboration diagram for Text_Diff:

Public Member Functions

 Text_Diff ($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.

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 165 of file Diff.php.

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

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 155 of file Diff.php.

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

◆ getDiff()

Text_Diff::getDiff ( )

Returns the array of differences.

Definition at line 49 of file Diff.php.

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

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 137 of file Diff.php.

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

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 119 of file Diff.php.

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

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 84 of file Diff.php.

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

◆ 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 101 of file Diff.php.

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

◆ 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 68 of file Diff.php.

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

Referenced by _check().

+ Here is the caller graph for this function:

◆ Text_Diff()

Text_Diff::Text_Diff (   $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 32 of file Diff.php.

33 {
34 array_walk($from_lines, array($this, '_trimNewlines'));
35 array_walk($to_lines, array($this, '_trimNewlines'));
36
37 if (extension_loaded('xdiff')) {
38 $engine = new Text_Diff_Engine_xdiff();
39 } else {
40 $engine = new Text_Diff_Engine_native();
41 }
42
43 $this->_edits = $engine->diff($from_lines, $to_lines);
44 }

Field Documentation

◆ $_edits

Text_Diff::$_edits

Definition at line 23 of file Diff.php.

Referenced by getDiff().


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