2 // PukiWiki - Yet another WikiWikiWeb clone.
3 // $Id: diff.php,v 1.6 2005/12/10 12:23:24 henoheno Exp $
5 // 2003-2005 PukiWiki Developers Team
6 // 2001-2002 Originally written by yu-ji
7 // License: GPL v2 or (at your option) any later version
10 // Show more information when it conflicts
11 define('PKWK_DIFF_SHOW_CONFLICT_DETAIL', 1);
13 // Create diff-style data between arrays
14 function do_diff($strlines1, $strlines2)
16 $obj = new line_diff();
17 $str = $obj->str_compare($strlines1, $strlines2);
21 // Merge helper (when it conflicts)
22 function do_update_diff($pagestr, $poststr, $original)
24 $obj = new line_diff();
26 $obj->set_str('left', $original, $pagestr);
28 $diff1 = $obj->toArray();
30 $obj->set_str('right', $original, $poststr);
32 $diff2 = $obj->toArray();
34 $arr = $obj->arr_compare('all', $diff1, $diff2);
36 if (PKWK_DIFF_SHOW_CONFLICT_DETAIL) {
37 global $do_update_diff_table;
39 $do_update_diff_table = <<<EOD
40 <p>l : between backup data and stored page data.<br />
41 r : between backup data and your post data.</p>
42 <table class="style_table">
49 $tags = array('th', 'th', 'td');
50 foreach ($arr as $_obj) {
51 $do_update_diff_table .= '<tr>';
52 $params = array($_obj->get('left'), $_obj->get('right'), $_obj->text());
53 foreach ($params as $key=>$text) {
54 $text = htmlspecialchars($text);
55 if (trim($text) == '') $text = ' ';
56 $do_update_diff_table .= '<' . $tags[$key] .
57 ' class="style_' . $tags[$key] . '">' . $text .
58 '</' . $tags[$key] . '>';
60 $do_update_diff_table .= '</tr>' . "\n";
62 $do_update_diff_table .= '</table>' . "\n";
66 foreach ($arr as $_obj) {
67 if ($_obj->get('left') != '-' && $_obj->get('right') != '-')
68 $body .= $_obj->text();
73 return array(rtrim($body) . "\n", $auto);
77 // References of this class:
78 // S. Wu, <A HREF="http://www.cs.arizona.edu/people/gene/vita.html">
79 // E. Myers,</A> U. Manber, and W. Miller,
80 // <A HREF="http://www.cs.arizona.edu/people/gene/PAPERS/np_diff.ps">
81 // "An O(NP) Sequence Comparison Algorithm,"</A>
82 // Information Processing Letters 35, 6 (1990), 317-323.
85 var $arr1, $arr2, $m, $n, $pos, $key, $plus, $minus, $equal, $reverse;
87 function line_diff($plus = '+', $minus = '-', $equal = ' ')
90 $this->minus = $minus;
91 $this->equal = $equal;
94 function arr_compare($key, $arr1, $arr2)
100 $arr = $this->toArray();
104 function set_str($key, $str1, $str2)
107 $this->arr1 = array();
108 $this->arr2 = array();
109 $str1 = str_replace("\r", '', $str1);
110 $str2 = str_replace("\r", '', $str2);
111 foreach (explode("\n", $str1) as $line) {
112 $this->arr1[] = new DiffLine($line);
114 foreach (explode("\n", $str2) as $line) {
115 $this->arr2[] = new DiffLine($line);
119 function str_compare($str1, $str2)
121 $this->set_str('diff', $str1, $str2);
125 foreach ($this->toArray() as $obj) {
126 $str .= $obj->get('diff') . $obj->text();
133 $this->m = count($this->arr1);
134 $this->n = count($this->arr2);
136 if ($this->m == 0 || $this->n == 0) { // No need to compare
137 $this->result = array(array('x'=>0, 'y'=>0));
142 array_unshift($this->arr1, new DiffLine(''));
144 array_unshift($this->arr2, new DiffLine(''));
147 $this->reverse = ($this->n < $this->m);
148 if ($this->reverse) {
150 $tmp = $this->m; $this->m = $this->n; $this->n = $tmp;
151 $tmp = $this->arr1; $this->arr1 = $this->arr2; $this->arr2 = $tmp;
155 $delta = $this->n - $this->m; // Must be >=0;
158 $this->path = array();
160 for ($p = -($this->m + 1); $p <= ($this->n + 1); $p++) {
162 $this->path[$p] = array();
165 for ($p = 0;; $p++) {
166 for ($k = -$p; $k <= $delta - 1; $k++) {
167 $fp[$k] = $this->snake($k, $fp[$k - 1], $fp[$k + 1]);
169 for ($k = $delta + $p; $k >= $delta + 1; $k--) {
170 $fp[$k] = $this->snake($k, $fp[$k - 1], $fp[$k + 1]);
172 $fp[$delta] = $this->snake($delta, $fp[$delta - 1], $fp[$delta + 1]);
173 if ($fp[$delta] >= $this->n) {
174 $this->pos = $this->path[$delta]; // ·ÐÏ©¤ò·èÄê
180 function snake($k, $y1, $y2)
189 $this->path[$k] = $this->path[$_k];// ¤³¤³¤Þ¤Ç¤Î·ÐÏ©¤ò¥³¥Ô¡¼
191 while ((($x + 1) < $this->m) && (($y + 1) < $this->n)
192 and $this->arr1[$x + 1]->compare($this->arr2[$y + 1]))
195 $this->path[$k][] = array('x'=>$x, 'y'=>$y); // ·ÐÏ©¤òÄɲÃ
203 if ($this->reverse) { // ¸È©¤Ê¡Ä
204 $_x = 'y'; $_y = 'x'; $_m = $this->n; $arr1 =& $this->arr2; $arr2 =& $this->arr1;
206 $_x = 'x'; $_y = 'y'; $_m = $this->m; $arr1 =& $this->arr1; $arr2 =& $this->arr2;
210 $this->add_count = $this->delete_count = 0;
211 $this->pos[] = array('x'=>$this->m, 'y'=>$this->n); // Sentinel
212 foreach ($this->pos as $pos) {
213 $this->delete_count += ($pos[$_x] - $x);
214 $this->add_count += ($pos[$_y] - $y);
216 while ($pos[$_x] > $x) {
217 $arr1[$x]->set($this->key, $this->minus);
218 $arr[] = $arr1[$x++];
221 while ($pos[$_y] > $y) {
222 $arr2[$y]->set($this->key, $this->plus);
223 $arr[] = $arr2[$y++];
227 $arr1[$x]->merge($arr2[$y]);
228 $arr1[$x]->set($this->key, $this->equal);
242 function DiffLine($text)
244 $this->text = $text . "\n";
245 $this->status = array();
248 function compare($obj)
250 return $this->text == $obj->text;
253 function set($key, $status)
255 $this->status[$key] = $status;
260 return isset($this->status[$key]) ? $this->status[$key] : '';
265 $this->status += $obj->status;