OSDN Git Service

htmlsc(): Just sugar for htmlspecialchars(), and a foundation
[pukiwiki/pukiwiki.git] / lib / diff.php
1 <?php
2 // PukiWiki - Yet another WikiWikiWeb clone.
3 // $Id: diff.php,v 1.10 2011/01/25 15:01:01 henoheno Exp $
4 // Copyright (C)
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
8 //
9
10 // Show more information when it conflicts
11 define('PKWK_DIFF_SHOW_CONFLICT_DETAIL', 1);
12
13 // Create diff-style data between arrays
14 function do_diff($strlines1, $strlines2)
15 {
16         $obj = new line_diff();
17         $str = $obj->str_compare($strlines1, $strlines2);
18         return $str;
19 }
20
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 = '')
26 {
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);
31 }
32
33 // Merge helper (when it conflicts)
34 function do_update_diff($pagestr, $poststr, $original)
35 {
36         $obj = new line_diff();
37
38         $obj->set_str('left', $original, $pagestr);
39         $obj->compare();
40         $diff1 = $obj->toArray();
41
42         $obj->set_str('right', $original, $poststr);
43         $obj->compare();
44         $diff2 = $obj->toArray();
45
46         $arr = $obj->arr_compare('all', $diff1, $diff2);
47
48         if (PKWK_DIFF_SHOW_CONFLICT_DETAIL) {
49                 global $do_update_diff_table;
50
51                 $do_update_diff_table = <<<EOD
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">
55  <tr>
56   <th>l</th>
57   <th>r</th>
58   <th>text</th>
59  </tr>
60 EOD;
61                 $tags = array('th', 'th', 'td');
62                 foreach ($arr as $_obj) {
63                         $do_update_diff_table .= '<tr>';
64                         $params = array($_obj->get('left'), $_obj->get('right'), $_obj->text());
65                         foreach ($params as $key=>$text) {
66                                 $text = htmlsc($text);
67                                 if (trim($text) == '') $text = '&nbsp;';
68                                 $do_update_diff_table .= '<' . $tags[$key] .
69                                         ' class="style_' . $tags[$key] . '">' . $text .
70                                         '</' . $tags[$key] . '>';
71                         }
72                         $do_update_diff_table .= '</tr>' . "\n";
73                 }
74                 $do_update_diff_table .= '</table>' . "\n";
75         }
76
77         $body = '';
78         foreach ($arr as $_obj) {
79                 if ($_obj->get('left') != '-' && $_obj->get('right') != '-')
80                         $body .= $_obj->text();
81         }
82
83         $auto = 1;
84
85         return array(rtrim($body) . "\n", $auto);
86 }
87
88
89 // References of this class:
90 // S. Wu, <A HREF="http://www.cs.arizona.edu/people/gene/vita.html">
91 // E. Myers,</A> U. Manber, and W. Miller,
92 // <A HREF="http://www.cs.arizona.edu/people/gene/PAPERS/np_diff.ps">
93 // "An O(NP) Sequence Comparison Algorithm,"</A>
94 // Information Processing Letters 35, 6 (1990), 317-323.
95 class line_diff
96 {
97         var $arr1, $arr2, $m, $n, $pos, $key, $plus, $minus, $equal, $reverse;
98
99         function line_diff($plus = '+', $minus = '-', $equal = ' ')
100         {
101                 $this->plus  = $plus;
102                 $this->minus = $minus;
103                 $this->equal = $equal;
104         }
105
106         function arr_compare($key, $arr1, $arr2)
107         {
108                 $this->key  = $key;
109                 $this->arr1 = $arr1;
110                 $this->arr2 = $arr2;
111                 $this->compare();
112                 $arr = $this->toArray();
113                 return $arr;
114         }
115
116         function set_str($key, $str1, $str2)
117         {
118                 $this->key  = $key;
119                 $this->arr1 = array();
120                 $this->arr2 = array();
121                 $str1 = str_replace("\r", '', $str1);
122                 $str2 = str_replace("\r", '', $str2);
123                 foreach (explode("\n", $str1) as $line) {
124                         $this->arr1[] = new DiffLine($line);
125                 }
126                 foreach (explode("\n", $str2) as $line) {
127                         $this->arr2[] = new DiffLine($line);
128                 }
129         }
130
131         function str_compare($str1, $str2)
132         {
133                 $this->set_str('diff', $str1, $str2);
134                 $this->compare();
135
136                 $str = '';
137                 foreach ($this->toArray() as $obj) {
138                         $str .= $obj->get('diff') . $obj->text();
139                 }
140                 return $str;
141         }
142
143         function compare()
144         {
145                 $this->m = count($this->arr1);
146                 $this->n = count($this->arr2);
147
148                 if ($this->m == 0 || $this->n == 0) { // No need to compare
149                         $this->result = array(array('x'=>0, 'y'=>0));
150                         return;
151                 }
152
153                 // Sentinel
154                 array_unshift($this->arr1, new DiffLine(''));
155                 $this->m++;
156                 array_unshift($this->arr2, new DiffLine(''));
157                 $this->n++;
158
159                 $this->reverse = ($this->n < $this->m);
160                 if ($this->reverse) {
161                         // Swap
162                         $tmp = $this->m; $this->m = $this->n; $this->n = $tmp;
163                         $tmp = $this->arr1; $this->arr1 = $this->arr2; $this->arr2 = $tmp;
164                         unset($tmp);
165                 }
166
167                 $delta = $this->n - $this->m; // Must be >=0;
168
169                 $fp = array();
170                 $this->path = array();
171
172                 for ($p = -($this->m + 1); $p <= ($this->n + 1); $p++) {
173                         $fp[$p] = -1;
174                         $this->path[$p] = array();
175                 }
176
177                 for ($p = 0;; $p++) {
178                         for ($k = -$p; $k <= $delta - 1; $k++) {
179                                 $fp[$k] = $this->snake($k, $fp[$k - 1], $fp[$k + 1]);
180                         }
181                         for ($k = $delta + $p; $k >= $delta + 1; $k--) {
182                                 $fp[$k] = $this->snake($k, $fp[$k - 1], $fp[$k + 1]);
183                         }
184                         $fp[$delta] = $this->snake($delta, $fp[$delta - 1], $fp[$delta + 1]);
185                         if ($fp[$delta] >= $this->n) {
186                                 $this->pos = $this->path[$delta]; // ·ÐÏ©¤ò·èÄê
187                                 return;
188                         }
189                 }
190         }
191
192         function snake($k, $y1, $y2)
193         {
194                 if ($y1 >= $y2) {
195                         $_k = $k - 1;
196                         $y = $y1 + 1;
197                 } else {
198                         $_k = $k + 1;
199                         $y = $y2;
200                 }
201                 $this->path[$k] = $this->path[$_k];// ¤³¤³¤Þ¤Ç¤Î·ÐÏ©¤ò¥³¥Ô¡¼
202                 $x = $y - $k;
203                 while ((($x + 1) < $this->m) && (($y + 1) < $this->n)
204                         and $this->arr1[$x + 1]->compare($this->arr2[$y + 1]))
205                 {
206                         ++$x; ++$y;
207                         $this->path[$k][] = array('x'=>$x, 'y'=>$y); // ·ÐÏ©¤òÄɲÃ
208                 }
209                 return $y;
210         }
211
212         function toArray()
213         {
214                 $arr = array();
215                 if ($this->reverse) { // ¸È©¤Ê¡Ä
216                         $_x = 'y'; $_y = 'x'; $_m = $this->n; $arr1 =& $this->arr2; $arr2 =& $this->arr1;
217                 } else {
218                         $_x = 'x'; $_y = 'y'; $_m = $this->m; $arr1 =& $this->arr1; $arr2 =& $this->arr2;
219                 }
220
221                 $x = $y = 1;
222                 $this->add_count = $this->delete_count = 0;
223                 $this->pos[] = array('x'=>$this->m, 'y'=>$this->n); // Sentinel
224                 foreach ($this->pos as $pos) {
225                         $this->delete_count += ($pos[$_x] - $x);
226                         $this->add_count    += ($pos[$_y] - $y);
227
228                         while ($pos[$_x] > $x) {
229                                 $arr1[$x]->set($this->key, $this->minus);
230                                 $arr[] = $arr1[$x++];
231                         }
232
233                         while ($pos[$_y] > $y) {
234                                 $arr2[$y]->set($this->key, $this->plus);
235                                 $arr[] =  $arr2[$y++];
236                         }
237
238                         if ($x < $_m) {
239                                 $arr1[$x]->merge($arr2[$y]);
240                                 $arr1[$x]->set($this->key, $this->equal);
241                                 $arr[] = $arr1[$x];
242                         }
243                         ++$x; ++$y;
244                 }
245                 return $arr;
246         }
247 }
248
249 class DiffLine
250 {
251         var $text;
252         var $status;
253
254         function DiffLine($text)
255         {
256                 $this->text   = $text . "\n";
257                 $this->status = array();
258         }
259
260         function compare($obj)
261         {
262                 return $this->text == $obj->text;
263         }
264
265         function set($key, $status)
266         {
267                 $this->status[$key] = $status;
268         }
269
270         function get($key)
271         {
272                 return isset($this->status[$key]) ? $this->status[$key] : '';
273         }
274
275         function merge($obj)
276         {
277                 $this->status += $obj->status;
278         }
279
280         function text()
281         {
282                 return $this->text;
283         }
284 }
285 ?>