Skip to content

Commit ac22200

Browse files
committed
Initial import
0 parents  commit ac22200

File tree

13 files changed

+1941
-0
lines changed

13 files changed

+1941
-0
lines changed

README

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
PHP DiffLib
2+
3+
Introduction
4+
------------
5+
PHP DiffLib is a comprehensive library for generating differences between
6+
two hasable objects (strings or arrays). Generated differences can be
7+
rendered in all of the standard formats including:
8+
* Unified
9+
* Context
10+
* Inline HTML
11+
* Side by Side HTML
12+
13+
The logic behind the core of the diff engine (ie, the sequence matcher)
14+
is primarily based on the Python difflib package. The reason for doing
15+
so is primarily because of its high degree of accuracy.
16+
17+
Example Use
18+
-----------
19+
A quick usage example can be found in the example/ directory and under
20+
example.php.
21+
22+
More complete documentation will be available shortly.
23+
24+
Todo
25+
----
26+
* Ability to ignore blank line changes, case changes and spacing changes
27+
* 3 way diff support
28+
* Performance optimizations
29+
30+
License (BSD License)
31+
---------------------
32+
Copyright (c) 2009 Chris Boulton <chris.boulton@interspire.com>
33+
All rights reserved.
34+
35+
Redistribution and use in source and binary forms, with or without
36+
modification, are permitted provided that the following conditions are met:
37+
38+
- Redistributions of source code must retain the above copyright notice,
39+
this list of conditions and the following disclaimer.
40+
- Redistributions in binary form must reproduce the above copyright notice,
41+
this list of conditions and the following disclaimer in the documentation
42+
and/or other materials provided with the distribution.
43+
- Neither the name of the Chris Boulton, Inc. nor the names of its contributors
44+
may be used to endorse or promote products derived from this software
45+
without specific prior written permission.
46+
47+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
48+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50+
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
51+
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
52+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
53+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
54+
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
55+
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
56+
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
57+
POSSIBILITY OF SUCH DAMAGE.

difflib.php

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
<?php
2+
/**
3+
* PHP LibDiff
4+
*
5+
* A comprehensive library for generating differences between two strings
6+
* in multiple formats (unified, side by side HTML etc)
7+
*
8+
* PHP version 5
9+
*
10+
* Copyright (c) 2009 Chris Boulton <chris.boulton@interspire.com>
11+
*
12+
* All rights reserved.
13+
*
14+
* Redistribution and use in source and binary forms, with or without
15+
* modification, are permitted provided that the following conditions are met:
16+
*
17+
* - Redistributions of source code must retain the above copyright notice,
18+
* this list of conditions and the following disclaimer.
19+
* - Redistributions in binary form must reproduce the above copyright notice,
20+
* this list of conditions and the following disclaimer in the documentation
21+
* and/or other materials provided with the distribution.
22+
* - Neither the name of the Chris Boulton, Inc. nor the names of its contributors
23+
* may be used to endorse or promote products derived from this software
24+
* without specific prior written permission.
25+
*
26+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36+
* POSSIBILITY OF SUCH DAMAGE.
37+
*
38+
* @package DiffLib
39+
* @author Chris Boulton <chris.boulton@interspire.com>
40+
* @copyright (c) 2009 Chris Boulton
41+
* @license New BSD License http://www.opensource.org/licenses/bsd-license.php
42+
* @version 1.0
43+
* @link http://github.com/chrisboulton/phpdifflib/
44+
*/
45+
46+
class DiffLib
47+
{
48+
/**
49+
* @var array The "old" sequence to use as the basis for the comparison.
50+
*/
51+
private $a = null;
52+
53+
/**
54+
* @var array The "new" sequence to generate the changes for.
55+
*/
56+
private $b = null;
57+
58+
/**
59+
* @var array Array containing the generated opcodes for the differences between the two items.
60+
*/
61+
private $groupedCodes = null;
62+
63+
/**
64+
* @var array Associative array of the default options available for the diff class and their default value.
65+
*/
66+
private $defaultOptions = array(
67+
'context' => 3,
68+
'ignoreNewLines' => false,
69+
'ignoreWhitespace' => false,
70+
'ignoreCase' => false
71+
);
72+
73+
/**
74+
* @var array Array of the options that have been applied for generating the diff.
75+
*/
76+
private $options = array();
77+
78+
/**
79+
* The constructor.
80+
*
81+
* @param array $a Array containing the lines of the first string to compare.
82+
* @param array $b Array containing the lines for the second string to compare.
83+
*/
84+
public function __construct($a, $b, $options=array())
85+
{
86+
$this->a = $a;
87+
$this->b = $b;
88+
89+
$this->options = array_merge($this->defaultOptions, $options);
90+
}
91+
92+
/**
93+
* Render a diff using the supplied rendering class and return it.
94+
*
95+
* @param object $renderer An instance of the rendering object to use for generating the diff.
96+
* @return mixed The generated diff. Exact return value depends on the rendered.
97+
*/
98+
public function Render(DiffLib_Renderer_Base $renderer)
99+
{
100+
$renderer->diff = $this;
101+
return $renderer->Render();
102+
}
103+
104+
/**
105+
* Get a range of lines from $start to $end from the first comparison string
106+
* and return them as an array. If no values are supplied, the entire string
107+
* is returned. It's also possible to specify just one line to return only
108+
* that line.
109+
*
110+
* @param int $start The starting number.
111+
* @param int $end The ending number. If not supplied, only the item in $start will be returned.
112+
* @return array Array of all of the lines between the specified range.
113+
*/
114+
public function GetA($start=0, $end=null)
115+
{
116+
if($start == 0 && $end === null) {
117+
return $this->a;
118+
}
119+
120+
if($end === null) {
121+
$length = 1;
122+
}
123+
else {
124+
$length = $end - $start;
125+
}
126+
127+
return array_slice($this->a, $start, $length);
128+
129+
}
130+
131+
/**
132+
* Get a range of lines from $start to $end from the second comparison string
133+
* and return them as an array. If no values are supplied, the entire string
134+
* is returned. It's also possible to specify just one line to return only
135+
* that line.
136+
*
137+
* @param int $start The starting number.
138+
* @param int $end The ending number. If not supplied, only the item in $start will be returned.
139+
* @return array Array of all of the lines between the specified range.
140+
*/
141+
public function GetB($start=0, $end=null)
142+
{
143+
if($start == 0 && $end === null) {
144+
return $this->b;
145+
}
146+
147+
if($end === null) {
148+
$length = 1;
149+
}
150+
else {
151+
$length = $end - $start;
152+
}
153+
154+
return array_slice($this->b, $start, $length);
155+
}
156+
157+
/**
158+
* Generate a list of the compiled and grouped opcodes for the differences between the
159+
* two strings. Generally called by the renderer, this class instantiates the sequence
160+
* matcher and performs the actual diff generation and return an array of the opcodes
161+
* for it. Once generated, the results are cached in the diff class instance.
162+
*
163+
* @return array Array of the grouped opcodes for the generated diff.
164+
*/
165+
public function GetGroupedOpcodes()
166+
{
167+
if(!is_null($this->groupedCodes)) {
168+
return $this->groupedCodes;
169+
}
170+
171+
require_once dirname(__FILE__).'/sequencematcher.php';
172+
$sequenceMatcher = new DiffLib_SequenceMatcher($this->a, $this->b);
173+
$this->groupedCodes = $sequenceMatcher->GetGroupedOpCodes();
174+
return $this->groupedCodes;
175+
}
176+
}

example/a.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<html>
2+
<head>
3+
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
4+
<title>Hello World!</title>
5+
</head>
6+
<body>
7+
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
8+
9+
<h2>A heading we'll be removing</h2>
10+
11+
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
12+
</body>
13+
</html>

example/b.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<html>
2+
<head>
3+
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
4+
<title>Goodbye Cruel World!</title>
5+
</head>
6+
<body>
7+
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
8+
9+
10+
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
11+
12+
<p>Just a small amount of new text...</p>
13+
</body>
14+
</html>

example/example.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3+
<html>
4+
<head>
5+
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
6+
<title>PHP LibDiff - Examples</title>
7+
<link rel="stylesheet" href="styles.css" type="text/css" charset="utf-8"/>
8+
</head>
9+
<body>
10+
<h1>PHP LibDiff - Examples</h1>
11+
<hr />
12+
<?php
13+
14+
// Include the diff class
15+
require_once dirname(__FILE__).'/../difflib.php';
16+
17+
// Include two sample files for comparison
18+
$a = explode("\n", file_get_contents(dirname(__FILE__).'/a.txt'));
19+
$b = explode("\n", file_get_contents(dirname(__FILE__).'/b.txt'));
20+
21+
// Options for generating the diff
22+
$options = array(
23+
);
24+
25+
// Initialize the diff class
26+
$diff = new DiffLib($a, $b, $options);
27+
28+
?>
29+
<h2>Side by Side Diff</h2>
30+
<?php
31+
32+
// Generate a side by side diff
33+
require_once dirname(__FILE__).'/../renderer/sidebyside_html.php';
34+
$renderer = new DiffLib_Renderer_SideBySide_Html;
35+
echo $diff->Render($renderer);
36+
37+
?>
38+
<h2>Inline Diff</h2>
39+
<?php
40+
41+
// Generate an inline diff
42+
require_once dirname(__FILE__).'/../renderer/inline_html.php';
43+
$renderer = new DiffLib_Renderer_Inline_Html;
44+
echo $diff->Render($renderer);
45+
46+
?>
47+
<h2>Unified Diff</h2>
48+
<pre><?php
49+
50+
// Generate a unified diff
51+
require_once dirname(__FILE__).'/../renderer/unified.php';
52+
$renderer = new DiffLib_Renderer_Unified;
53+
echo htmlspecialchars($diff->Render($renderer));
54+
55+
?>
56+
</pre>
57+
<h2>Context Diff</h2>
58+
<pre><?php
59+
60+
// Generate a context diff
61+
require_once dirname(__FILE__).'/../renderer/context.php';
62+
$renderer = new DiffLib_Renderer_Context;
63+
echo htmlspecialchars($diff->Render($renderer));
64+
?>
65+
</pre>
66+
</body>
67+
</html>

0 commit comments

Comments
 (0)