2 // PukiWiki - Yet another WikiWikiWeb clone.
3 // $Id: diff.php,v 1.10 2011/01/25 15:01:01 henoheno Exp $
5 // 2003-2005, 2007 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 // Visualize diff-style-text to text-with-CSS
22 // '+Added' => '<span added>Added</span>'
23 // '-Removed' => '<span removed>Removed</span>'
24 // ' Nothing' => 'Nothing'
25 function diff_style_to_css($str = '')
27 // Cut diff markers ('+' or '-' or ' ')
28 $str = preg_replace('/^\-(.*)$/m', '<span class="diff_removed">$1</span>', $str);
29 $str = preg_replace('/^\+(.*)$/m', '<span class="diff_added" >$1</span>', $str);
30 return preg_replace('/^ (.*)$/m', '$1', $str);
33 // Merge helper (when it conflicts)
34 function do_update_diff($pagestr, $poststr, $original)
36 $obj = new line_diff();
38 $obj->set_str('left', $original, $pagestr);
40 $diff1 = $obj->toArray();
42 $obj->set_str('right', $original, $poststr);
44 $diff2 = $obj->toArray();
46 $arr = $obj->arr_compare('all', $diff1, $diff2);
48 if (PKWK_DIFF_SHOW_CONFLICT_DETAIL) {
49 global $do_update_diff_table;
52 <p>l : between backup data and stored page data.<br />
53 r : between backup data and your post data.</p>
54 <table class="style_table">
61 $tags = array('th', 'th', 'td');
62 foreach ($arr as $_obj) {
64 $params = array($_obj->get('left'), $_obj->get('right'), $_obj->text());
65 foreach ($params as $key => $text) {
66 $text = htmlsc(rtrim($text));
67 if (empty($text)) $text = ' ';
69 ' <' . $tags[$key] . ' class="style_' . $tags[$key] . '">' .
71 '</' . $tags[$key] . '>';
75 $table[] = '</table>';
77 $do_update_diff_table = implode("\n", $table) . "\n";
82 foreach ($arr as $_obj) {
83 if ($_obj->get('left') != '-' && $_obj->get('right') != '-') {
84 $body[] = $_obj->text();
88 return array(rtrim(implode('', $body)) . "\n", 1);
92 // References of this class:
93 // S. Wu, <A HREF="http://www.cs.arizona.edu/people/gene/vita.html">
94 // E. Myers,</A> U. Manber, and W. Miller,
95 // <A HREF="http://www.cs.arizona.edu/people/gene/PAPERS/np_diff.ps">
96 // "An O(NP) Sequence Comparison Algorithm,"</A>
97 // Information Processing Letters 35, 6 (1990), 317-323.
100 var $arr1, $arr2, $m, $n, $pos, $key, $plus, $minus, $equal, $reverse;
102 function line_diff($plus = '+', $minus = '-', $equal = ' ')
105 $this->minus = $minus;
106 $this->equal = $equal;
109 function arr_compare($key, $arr1, $arr2)
115 $arr = $this->toArray();
119 function set_str($key, $str1, $str2)
122 $this->arr1 = array();
123 $this->arr2 = array();
124 $str1 = str_replace("\r", '', $str1);
125 $str2 = str_replace("\r", '', $str2);
126 foreach (explode("\n", $str1) as $line) {
127 $this->arr1[] = new DiffLine($line);
129 foreach (explode("\n", $str2) as $line) {
130 $this->arr2[] = new DiffLine($line);
134 function str_compare($str1, $str2)
136 $this->set_str('diff', $str1, $str2);
140 foreach ($this->toArray() as $obj) {
141 $str .= $obj->get('diff') . $obj->text();
148 $this->m = count($this->arr1);
149 $this->n = count($this->arr2);
151 if ($this->m == 0 || $this->n == 0) { // No need to compare
152 $this->result = array(array('x'=>0, 'y'=>0));
157 array_unshift($this->arr1, new DiffLine(''));
159 array_unshift($this->arr2, new DiffLine(''));
162 $this->reverse = ($this->n < $this->m);
163 if ($this->reverse) {
165 $tmp = $this->m; $this->m = $this->n; $this->n = $tmp;
166 $tmp = $this->arr1; $this->arr1 = $this->arr2; $this->arr2 = $tmp;
170 $delta = $this->n - $this->m; // Must be >=0;
173 $this->path = array();
175 for ($p = -($this->m + 1); $p <= ($this->n + 1); $p++) {
177 $this->path[$p] = array();
180 for ($p = 0;; $p++) {
181 for ($k = -$p; $k <= $delta - 1; $k++) {
182 $fp[$k] = $this->snake($k, $fp[$k - 1], $fp[$k + 1]);
184 for ($k = $delta + $p; $k >= $delta + 1; $k--) {
185 $fp[$k] = $this->snake($k, $fp[$k - 1], $fp[$k + 1]);
187 $fp[$delta] = $this->snake($delta, $fp[$delta - 1], $fp[$delta + 1]);
188 if ($fp[$delta] >= $this->n) {
189 $this->pos = $this->path[$delta]; // 経路を決定
195 function snake($k, $y1, $y2)
204 $this->path[$k] = $this->path[$_k];// ここまでの経路をコピー
206 while ((($x + 1) < $this->m) && (($y + 1) < $this->n)
207 and $this->arr1[$x + 1]->compare($this->arr2[$y + 1]))
210 $this->path[$k][] = array('x'=>$x, 'y'=>$y); // 経路を追加
218 if ($this->reverse) { // 姑息な…
219 $_x = 'y'; $_y = 'x'; $_m = $this->n; $arr1 =& $this->arr2; $arr2 =& $this->arr1;
221 $_x = 'x'; $_y = 'y'; $_m = $this->m; $arr1 =& $this->arr1; $arr2 =& $this->arr2;
225 $this->add_count = $this->delete_count = 0;
226 $this->pos[] = array('x'=>$this->m, 'y'=>$this->n); // Sentinel
227 foreach ($this->pos as $pos) {
228 $this->delete_count += ($pos[$_x] - $x);
229 $this->add_count += ($pos[$_y] - $y);
231 while ($pos[$_x] > $x) {
232 $arr1[$x]->set($this->key, $this->minus);
233 $arr[] = $arr1[$x++];
236 while ($pos[$_y] > $y) {
237 $arr2[$y]->set($this->key, $this->plus);
238 $arr[] = $arr2[$y++];
242 $arr1[$x]->merge($arr2[$y]);
243 $arr1[$x]->set($this->key, $this->equal);
257 function DiffLine($text)
259 $this->text = $text . "\n";
260 $this->status = array();
263 function compare($obj)
265 return $this->text == $obj->text;
268 function set($key, $status)
270 $this->status[$key] = $status;
275 return isset($this->status[$key]) ? $this->status[$key] : '';
280 $this->status += $obj->status;