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