]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/diff.php
Reformat code
[SourceForge/phpwiki.git] / lib / diff.php
1 <?php
2
3 // diff.php
4 //
5 // PhpWiki diff output code.
6 //
7 // Copyright (C) 2000, 2001 Geoffrey T. Dairiki <dairiki@dairiki.org>
8 // You may copy this code freely under the conditions of the GPL.
9 //
10
11 require_once 'lib/difflib.php';
12
13 class _HWLDF_WordAccumulator
14 {
15     function _HWLDF_WordAccumulator()
16     {
17         $this->_lines = array();
18         $this->_line = false;
19         $this->_group = false;
20         $this->_tag = '~begin';
21     }
22
23     function _flushGroup($new_tag)
24     {
25         if ($this->_group !== false) {
26             if (!$this->_line)
27                 $this->_line = HTML();
28             $this->_line->pushContent($this->_tag
29                 ? new HtmlElement($this->_tag,
30                     $this->_group)
31                 : $this->_group);
32         }
33         $this->_group = '';
34         $this->_tag = $new_tag;
35     }
36
37     function _flushLine($new_tag)
38     {
39         $this->_flushGroup($new_tag);
40         if ($this->_line)
41             $this->_lines[] = $this->_line;
42         $this->_line = HTML();
43     }
44
45     function addWords($words, $tag = '')
46     {
47         if ($tag != $this->_tag)
48             $this->_flushGroup($tag);
49
50         foreach ($words as $word) {
51             // new-line should only come as first char of word.
52             if ($word === "")
53                 continue;
54             if ($word[0] == "\n") {
55                 $this->_group .= " ";
56                 $this->_flushLine($tag);
57                 $word = substr($word, 1);
58             }
59             assert(!strstr($word, "\n"));
60             $this->_group .= $word;
61         }
62     }
63
64     function getLines()
65     {
66         $this->_flushLine('~done');
67         return $this->_lines;
68     }
69 }
70
71 class WordLevelDiff extends MappedDiff
72 {
73     function WordLevelDiff($orig_lines, $final_lines)
74     {
75         list ($orig_words, $orig_stripped) = $this->_split($orig_lines);
76         list ($final_words, $final_stripped) = $this->_split($final_lines);
77
78         $this->MappedDiff($orig_words, $final_words,
79             $orig_stripped, $final_stripped);
80     }
81
82     function _split($lines)
83     {
84         // FIXME: fix POSIX char class.
85         if (!preg_match_all('/ ( [^\S\n]+ | [[:alnum:]]+ | . ) (?: (?!< \n) [^\S\n])? /xs',
86             implode("\n", $lines),
87             $m)
88         ) {
89             return array(array(''), array(''));
90         }
91         return array($m[0], $m[1]);
92     }
93
94     function orig()
95     {
96         $orig = new _HWLDF_WordAccumulator;
97
98         foreach ($this->edits as $edit) {
99             if ($edit->type == 'copy')
100                 $orig->addWords($edit->orig);
101             elseif ($edit->orig)
102                 $orig->addWords($edit->orig, 'del');
103         }
104         return $orig->getLines();
105     }
106
107     function _final()
108     {
109         $final = new _HWLDF_WordAccumulator;
110
111         foreach ($this->edits as $edit) {
112             if ($edit->type == 'copy')
113                 $final->addWords($edit->final);
114             elseif ($edit->final)
115                 $final->addWords($edit->final, 'ins');
116         }
117         return $final->getLines();
118     }
119 }
120
121 /**
122  * HTML unified diff formatter.
123  *
124  * This class formats a diff into a CSS-based
125  * unified diff format.
126  *
127  * Within groups of changed lines, diffs are highlit
128  * at the character-diff level.
129  */
130 class HtmlUnifiedDiffFormatter extends UnifiedDiffFormatter
131 {
132     function HtmlUnifiedDiffFormatter($context_lines = 4)
133     {
134         $this->UnifiedDiffFormatter($context_lines);
135     }
136
137     function _start_diff()
138     {
139         $this->_top = HTML::div(array('class' => 'diff'));
140     }
141
142     function _end_diff()
143     {
144         $val = $this->_top;
145         unset($this->_top);
146         return $val;
147     }
148
149     function _start_block($header)
150     {
151         $this->_block = HTML::div(array('class' => 'block'),
152             HTML::tt($header));
153     }
154
155     function _end_block()
156     {
157         $this->_top->pushContent($this->_block);
158         unset($this->_block);
159     }
160
161     function _lines($lines, $class, $prefix = false, $elem = false)
162     {
163         if (!$prefix)
164             $prefix = HTML::raw('&nbsp;');
165         $div = HTML::div(array('class' => 'difftext'));
166         foreach ($lines as $line) {
167             if ($elem)
168                 $line = new HtmlElement($elem, $line);
169             $div->pushContent(HTML::div(array('class' => $class),
170                 HTML::tt(array('class' => 'prefix'),
171                     $prefix),
172                 $line, HTML::raw('&nbsp;')));
173         }
174         $this->_block->pushContent($div);
175     }
176
177     function _context($lines)
178     {
179         $this->_lines($lines, 'context');
180     }
181
182     function _deleted($lines)
183     {
184         $this->_lines($lines, 'deleted', '-', 'del');
185     }
186
187     function _added($lines)
188     {
189         $this->_lines($lines, 'added', '+', 'ins');
190     }
191
192     function _changed($orig, $final)
193     {
194         $diff = new WordLevelDiff($orig, $final);
195         $this->_lines($diff->orig(), 'original', '-');
196         $this->_lines($diff->_final(), 'final', '+');
197     }
198 }
199
200 /////////////////////////////////////////////////////////////////
201
202 function PageInfoRow($label, $rev, &$request, $is_current = false)
203 {
204     global $WikiTheme;
205
206     $row = HTML::tr(HTML::td(array('align' => 'right'), $label));
207     if ($rev) {
208         $author = $rev->get('author');
209         $dbi = $request->getDbh();
210
211         $iswikipage = (isWikiWord($author) && $dbi->isWikiPage($author));
212         $authorlink = $iswikipage ? WikiLink($author) : $author;
213         $version = $rev->getVersion();
214         $linked_version = WikiLink($rev, 'existing', $version);
215         if ($is_current)
216             $revertbutton = HTML();
217         else
218             $revertbutton = $WikiTheme->makeActionButton(array('action' => 'revert',
219                     'version' => $version),
220                 false, $rev);
221         $row->pushContent(HTML::td(fmt("version %s", $linked_version)),
222             HTML::td($WikiTheme->getLastModifiedMessage($rev,
223                 false)),
224             HTML::td(fmt("by %s", $authorlink)),
225             HTML::td($revertbutton)
226         );
227     } else {
228         $row->pushContent(HTML::td(array('colspan' => '4'), _("None")));
229     }
230     return $row;
231 }
232
233 function showDiff(&$request)
234 {
235     $pagename = $request->getArg('pagename');
236     if (is_array($versions = $request->getArg('versions'))) {
237         // Version selection from pageinfo.php display:
238         rsort($versions);
239         list ($version, $previous) = $versions;
240     } else {
241         $version = $request->getArg('version');
242         $previous = $request->getArg('previous');
243     }
244
245     // abort if page doesn't exist
246     $dbi = $request->getDbh();
247     $page = $request->getPage();
248     $current = $page->getCurrentRevision(false);
249     if ($current->getVersion() < 1) {
250         $html = HTML::div(array('class' => 'wikitext', 'id' => 'difftext'),
251             HTML::p(fmt("I'm sorry, there is no such page as %s.",
252                 WikiLink($pagename, 'unknown'))));
253         require_once 'lib/Template.php';
254         GeneratePage($html, sprintf(_("Diff: %s"), $pagename), false);
255         return; //early return
256     }
257
258     if ($version) {
259         if (!($new = $page->getRevision($version)))
260             NoSuchRevision($request, $page, $version);
261         $new_version = fmt("version %d", $version);
262     } else {
263         $new = $current;
264         $new_version = _("current version");
265     }
266
267     if (preg_match('/^\d+$/', $previous)) {
268         if (!($old = $page->getRevision($previous)))
269             NoSuchRevision($request, $page, $previous);
270         $old_version = fmt("version %d", $previous);
271         $others = array('major', 'minor', 'author');
272     } else {
273         switch ($previous) {
274             case 'author':
275                 $old = $new;
276                 while ($old = $page->getRevisionBefore($old)) {
277                     if ($old->get('author') != $new->get('author'))
278                         break;
279                 }
280                 $old_version = _("revision by previous author");
281                 $others = array('major', 'minor');
282                 break;
283             case 'minor':
284                 $previous = 'minor';
285                 $old = $page->getRevisionBefore($new);
286                 $old_version = _("previous revision");
287                 $others = array('major', 'author');
288                 break;
289             case 'major':
290             default:
291                 $old = $new;
292                 while ($old && $old->get('is_minor_edit'))
293                     $old = $page->getRevisionBefore($old);
294                 if ($old)
295                     $old = $page->getRevisionBefore($old);
296                 $old_version = _("predecessor to the previous major change");
297                 $others = array('minor', 'author');
298                 break;
299         }
300     }
301
302     $new_link = WikiLink($new, '', $new_version);
303     $old_link = $old ? WikiLink($old, '', $old_version) : $old_version;
304     $page_link = WikiLink($page);
305
306     $html = HTML::div(array('class' => 'wikitext', 'id' => 'difftext'),
307         HTML::p(fmt("Differences between %s and %s of %s.",
308             $new_link, $old_link, $page_link)));
309
310     $otherdiffs = HTML::p(_("Other diffs:"));
311     $label = array('major' => _("Previous Major Revision"),
312         'minor' => _("Previous Revision"),
313         'author' => _("Previous Author"));
314     foreach ($others as $other) {
315         $args = array('action' => 'diff', 'previous' => $other);
316         if ($version)
317             $args['version'] = $version;
318         if (count($otherdiffs->getContent()) > 1)
319             $otherdiffs->pushContent(", ");
320         else
321             $otherdiffs->pushContent(" ");
322         $otherdiffs->pushContent(Button($args, $label[$other]));
323     }
324     $html->pushContent($otherdiffs);
325
326     if ($old and $old->getVersion() == 0)
327         $old = false;
328
329     $html->pushContent(HTML::Table(PageInfoRow(_("Newer page:"), $new,
330             $request, empty($version)),
331         PageInfoRow(_("Older page:"), $old,
332             $request, false)));
333
334     if ($new && $old) {
335         $diff = new Diff($old->getContent(), $new->getContent());
336
337         if ($diff->isEmpty()) {
338             $html->pushContent(HTML::hr(),
339                 HTML::p(sprintf(_('Content of versions %1$s and %2$s is identical.'),
340                     $old->getVersion(),
341                     $new->getVersion())));
342             // If two consecutive versions have the same content, it is because the page was
343             // renamed, or metadata changed: ACL, owner, markup.
344             // We give the reason by printing the summary.
345             if (($new->getVersion() - $old->getVersion()) == 1) {
346                 $html->pushContent(HTML::p(sprintf(_('Version %1$s was created because: %2$s'),
347                     $new->getVersion(),
348                     $new->get('summary'))));
349             }
350         } else {
351             $fmt = new HtmlUnifiedDiffFormatter;
352             $html->pushContent($fmt->format($diff));
353         }
354
355         $html->pushContent(HTML::hr(), HTML::h2($new_version));
356         require_once 'lib/BlockParser.php';
357         $html->pushContent(TransformText($new, $new->get('markup'), $pagename));
358     }
359
360     require_once 'lib/Template.php';
361     GeneratePage($html, sprintf(_("Diff: %s"), $pagename), $new);
362 }
363
364 // Local Variables:
365 // mode: php
366 // tab-width: 8
367 // c-basic-offset: 4
368 // c-hanging-comment-ender-p: nil
369 // indent-tabs-mode: nil
370 // End: