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