2 /////////////////////////////////////////////////
3 // PukiWiki - Yet another WikiWikiWeb clone.
5 // $Id: diff.php,v 1.2 2004/10/07 14:34:55 henoheno Exp $
8 define('DIFF_SHOW_TABLE',TRUE);
11 function do_diff($strlines1, $strlines2)
13 $obj = new line_diff();
14 $str = $obj->str_compare($strlines1, $strlines2);
18 // º¹Ê¬¤ÎºîÀ®(¹¹¿·¤Î¾×ÆÍ)
19 function do_update_diff($pagestr, $poststr, $original)
21 $obj = new line_diff();
23 $obj->set_str('left', $original, $pagestr);
25 $diff1 = $obj->toArray();
27 $obj->set_str('right', $original, $poststr);
29 $diff2 = $obj->toArray();
31 $arr = $obj->arr_compare('all', $diff1, $diff2);
35 global $do_update_diff_table;
37 $do_update_diff_table = <<<EOD
38 <p>l : between backup data and stored page data.<br />
39 r : between backup data and your post data.</p>
40 <table class="style_table">
47 $tags = array('th', 'th', 'td');
48 foreach ($arr as $_obj)
50 $do_update_diff_table .= '<tr>';
51 $params = array($_obj->get('left'), $_obj->get('right'), $_obj->text());
52 foreach ($params as $key=>$text) {
53 $text = htmlspecialchars($text);
54 if (trim($text) == '') $text = ' ';
55 $do_update_diff_table .= "<{$tags[$key]} class=\"style_{$tags[$key]}\">$text</{$tags[$key]}>";
57 $do_update_diff_table .= '</tr>'."\n";
59 $do_update_diff_table .= '</table>'."\n";
63 foreach ($arr as $_obj) {
64 if ($_obj->get('left') != '-' && $_obj->get('right') != '-')
65 $body .= $_obj->text();
70 return array(rtrim($body) . "\n", $auto);
76 °Ê²¼¤Î¾ðÊó¤ò»²¹Í¤Ë¤·¤ÆºîÀ®¤·¤Þ¤·¤¿¡£
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.
88 var $arr1, $arr2, $m, $n, $pos, $key, $plus, $minus, $equal, $reverse;
90 function line_diff($plus = '+', $minus = '-', $equal = ' ')
93 $this->minus = $minus;
94 $this->equal = $equal;
97 function arr_compare($key, $arr1, $arr2)
103 $arr = $this->toArray();
107 function set_str($key, $str1, $str2)
110 $this->arr1 = array();
111 $this->arr2 = array();
112 $str1 = preg_replace("/\r/",'',$str1);
113 $str2 = preg_replace("/\r/",'',$str2);
114 foreach (explode("\n", $str1) as $line) {
115 $this->arr1[] = new DiffLine($line);
117 foreach (explode("\n", $str2) as $line) {
118 $this->arr2[] = new DiffLine($line);
122 function str_compare($str1, $str2)
124 $this->set_str('diff', $str1, $str2);
128 foreach ($this->toArray() as $obj) {
129 $str .= $obj->get('diff') . $obj->text();
136 $this->m = count($this->arr1);
137 $this->n = count($this->arr2);
139 if ($this->m == 0 || $this->n == 0) { // no need compare.
140 $this->result = array(array('x'=>0, 'y'=>0));
145 array_unshift($this->arr1, new DiffLine(''));
147 array_unshift($this->arr2, new DiffLine(''));
150 $this->reverse = ($this->n < $this->m);
151 if ($this->reverse) {
153 $tmp = $this->m; $this->m = $this->n; $this->n = $tmp;
154 $tmp = $this->arr1; $this->arr1 = $this->arr2; $this->arr2 = $tmp;
158 $delta = $this->n - $this->m; // Must be >=0;
161 $this->path = array();
163 for ($p = -($this->m + 1); $p <= ($this->n + 1); $p++) {
165 $this->path[$p] = array();
168 for ($p = 0;; $p++) {
169 for ($k = -$p; $k <= $delta - 1; $k++) {
170 $fp[$k] = $this->snake($k, $fp[$k - 1], $fp[$k + 1]);
172 for ($k = $delta + $p; $k >= $delta + 1; $k--) {
173 $fp[$k] = $this->snake($k, $fp[$k - 1], $fp[$k + 1]);
175 $fp[$delta] = $this->snake($delta, $fp[$delta - 1], $fp[$delta + 1]);
176 if ($fp[$delta] >= $this->n) {
177 $this->pos = $this->path[$delta]; // ·ÐÏ©¤ò·èÄê
183 function snake($k, $y1, $y2)
192 $this->path[$k] = $this->path[$_k];// ¤³¤³¤Þ¤Ç¤Î·ÐÏ©¤ò¥³¥Ô¡¼
194 while ((($x + 1) < $this->m) && (($y + 1) < $this->n)
195 and $this->arr1[$x + 1]->compare($this->arr2[$y + 1]))
198 $this->path[$k][] = array('x'=>$x, 'y'=>$y); // ·ÐÏ©¤òÄɲÃ
206 if ($this->reverse) { //¸È©¤Ê¡Ä
207 $_x = 'y'; $_y = 'x'; $_m = $this->n; $arr1 =& $this->arr2; $arr2 =& $this->arr1;
209 $_x = 'x'; $_y = 'y'; $_m = $this->m; $arr1 =& $this->arr1; $arr2 =& $this->arr2;
213 $this->add_count = $this->delete_count = 0;
214 $this->pos[] = array('x'=>$this->m, 'y'=>$this->n); // Sentinel
215 foreach ($this->pos as $pos) {
216 $this->delete_count += ($pos[$_x] - $x);
217 $this->add_count += ($pos[$_y] - $y);
219 while ($pos[$_x] > $x) {
220 $arr1[$x]->set($this->key, $this->minus);
221 $arr[] = $arr1[$x++];
224 while ($pos[$_y] > $y) {
225 $arr2[$y]->set($this->key, $this->plus);
226 $arr[] = $arr2[$y++];
230 $arr1[$x]->merge($arr2[$y]);
231 $arr1[$x]->set($this->key, $this->equal);
245 function DiffLine($text)
247 $this->text = "$text\n";
248 $this->status = array();
251 function compare($obj)
253 return $this->text == $obj->text;
256 function set($key, $status)
258 $this->status[$key] = $status;
263 return isset($this->status[$key]) ? $this->status[$key] : '';
268 $this->status += $obj->status;