]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/diff.php
WikiUserNew support (temp. ENABLE_USER_NEW constant)
[SourceForge/phpwiki.git] / lib / diff.php
1 <?php // -*-php-*-
2 rcs_id('$Id: diff.php,v 1.45 2004-01-25 03:57:15 rurban Exp $');
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 require_once('lib/HtmlElement.php');
13
14 class _HWLDF_WordAccumulator {
15     function _HWLDF_WordAccumulator () {
16         $this->_lines = array();
17         $this->_line = false;
18         $this->_group = false;
19         $this->_tag = '~begin';
20     }
21
22     function _flushGroup ($new_tag) {
23         if ($this->_group !== false) {
24             if (!$this->_line)
25                 $this->_line = HTML();
26             $this->_line->pushContent($this->_tag
27                                       ? new HtmlElement($this->_tag,
28                                                         $this->_group)
29                                       : $this->_group);
30         }
31         $this->_group = '';
32         $this->_tag = $new_tag;
33     }
34
35     function _flushLine ($new_tag) {
36         $this->_flushGroup($new_tag);
37         if ($this->_line)
38             $this->_lines[] = $this->_line;
39         $this->_line = HTML();
40     }
41
42     function addWords ($words, $tag = '') {
43         if ($tag != $this->_tag)
44             $this->_flushGroup($tag);
45
46         foreach ($words as $word) {
47             // new-line should only come as first char of word.
48             if (!$word)
49                 continue;
50             if ($word[0] == "\n") {
51                 $this->_group .= PrintXML(HTML::raw('&nbsp;'));
52                 $this->_flushLine($tag);
53                 $word = substr($word, 1);
54             }
55             assert(!strstr($word, "\n"));
56             $this->_group .= $word;
57         }
58     }
59
60     function getLines() {
61         $this->_flushLine('~done');
62         return $this->_lines;
63     }
64 }
65
66 class WordLevelDiff extends MappedDiff
67 {
68     function WordLevelDiff ($orig_lines, $final_lines) {
69         list ($orig_words, $orig_stripped) = $this->_split($orig_lines);
70         list ($final_words, $final_stripped) = $this->_split($final_lines);
71
72
73         $this->MappedDiff($orig_words, $final_words,
74                           $orig_stripped, $final_stripped);
75     }
76
77     function _split($lines) {
78         // FIXME: fix POSIX char class.
79         if (!preg_match_all('/ ( [^\S\n]+ | [[:alnum:]]+ | . ) (?: (?!< \n) [^\S\n])? /xs',
80                             implode("\n", $lines),
81                             $m)) {
82             return array(array(''), array(''));
83         }
84         return array($m[0], $m[1]);
85     }
86
87     function orig () {
88         $orig = new _HWLDF_WordAccumulator;
89
90         foreach ($this->edits as $edit) {
91             if ($edit->type == 'copy')
92                 $orig->addWords($edit->orig);
93             elseif ($edit->orig)
94                 $orig->addWords($edit->orig, 'del');
95         }
96         return $orig->getLines();
97     }
98
99     function final () {
100         $final = new _HWLDF_WordAccumulator;
101
102         foreach ($this->edits as $edit) {
103             if ($edit->type == 'copy')
104                 $final->addWords($edit->final);
105             elseif ($edit->final)
106                 $final->addWords($edit->final, 'ins');
107         }
108         return $final->getLines();
109     }
110 }
111
112
113 /**
114  * HTML unified diff formatter.
115  *
116  * This class formats a diff into a CSS-based
117  * unified diff format.
118  *
119  * Within groups of changed lines, diffs are highlit
120  * at the character-diff level.
121  */
122 class HtmlUnifiedDiffFormatter extends UnifiedDiffFormatter
123 {
124     function HtmlUnifiedDiffFormatter($context_lines = 4) {
125         $this->UnifiedDiffFormatter($context_lines);
126     }
127
128     function _start_diff() {
129         $this->_top = HTML::div(array('class' => 'diff'));
130     }
131     function _end_diff() {
132         $val = $this->_top;
133         unset($this->_top);
134         return $val;
135     }
136
137     function _start_block($header) {
138         $this->_block = HTML::div(array('class' => 'block'),
139                                   HTML::tt($header));
140     }
141
142     function _end_block() {
143         $this->_top->pushContent($this->_block);
144         unset($this->_block);
145     }
146
147     function _lines($lines, $class, $prefix = false, $elem = false) {
148         if (!$prefix)
149             $prefix = HTML::raw('&nbsp;');
150         $div = HTML::div(array('class' => 'difftext'));
151         foreach ($lines as $line) {
152             if ($elem)
153                 $line = new HtmlElement($elem, $line);
154             $div->pushContent(HTML::div(array('class' => $class),
155                                         HTML::tt(array('class' => 'prefix'),
156                                                  $prefix),
157                                         $line, HTML::raw('&nbsp;')));
158         }
159         $this->_block->pushContent($div);
160     }
161
162     function _context($lines) {
163         $this->_lines($lines, 'context');
164     }
165     function _deleted($lines) {
166         $this->_lines($lines, 'deleted', '-', 'del');
167     }
168
169     function _added($lines) {
170         $this->_lines($lines, 'added', '+', 'ins');
171     }
172
173     function _changed($orig, $final) {
174         $diff = new WordLevelDiff($orig, $final);
175         $this->_lines($diff->orig(), 'original', '-');
176         $this->_lines($diff->final(), 'final', '+');
177     }
178 }
179
180 /**
181  * HTML table-based unified diff formatter.
182  *
183  * This class formats a diff into a table-based
184  * unified diff format.  (Similar to what was produced
185  * by previous versions of PhpWiki.)
186  *
187  * Within groups of changed lines, diffs are highlit
188  * at the character-diff level.
189  */
190 class TableUnifiedDiffFormatter extends HtmlUnifiedDiffFormatter
191 {
192     function TableUnifiedDiffFormatter($context_lines = 4) {
193         $this->HtmlUnifiedDiffFormatter($context_lines);
194     }
195
196     function _start_diff() {
197         $this->_top = HTML::table(array('width' => '100%',
198                                         'class' => 'diff',
199                                         'cellspacing' => 1,
200                                         'cellpadding' => 1,
201                                         'border' => 1));
202     }
203
204     function _start_block($header) {
205         $this->_block = HTML::table(array('width' => '100%',
206                                           'class' => 'block',
207                                           'cellspacing' => 0,
208                                           'cellpadding' => 1,
209                                           'border' => 0),
210                                     HTML::tr(HTML::td(array('colspan' => 2),
211                                                       HTML::tt($header))));
212     }
213
214     function _end_block() {
215         $this->_top->pushContent(HTML::tr(HTML::td($this->_block)));
216         unset($this->_block);
217     }
218
219     function _lines($lines, $class, $prefix = false, $elem = false) {
220         if (!$prefix)
221             $prefix = HTML::raw('&nbsp;');
222         $prefix = HTML::td(array('class' => 'prefix',
223                                  'width' => "1%"), $prefix);
224         foreach ($lines as $line) {
225             if (! trim($line))
226                 $line = HTML::raw('&nbsp;');
227             elseif ($elem)
228                 $line = new HtmlElement($elem, $line);
229             $this->_block->pushContent(HTML::tr(array('valign' => 'top'),
230                                                 $prefix,
231                                                 HTML::td(array('class' => $class),
232                                                          $line)));
233         }
234     }
235 }
236
237
238 /////////////////////////////////////////////////////////////////
239
240 function PageInfoRow ($label, $rev, &$request)
241 {
242     global $Theme, $WikiNameRegexp;
243
244     $row = HTML::tr(HTML::td(array('align' => 'right'), $label));
245     if ($rev) {
246         $author = $rev->get('author');
247         $dbi = $request->getDbh();
248
249         $iswikipage = (isWikiWord($author) && $dbi->isWikiPage($author));
250         $authorlink = $iswikipage ? WikiLink($author) : $author;
251
252         $linked_version = WikiLink($rev, 'existing', $rev->getVersion());
253         $row->pushContent(HTML::td(fmt("version %s", $linked_version)),
254                           HTML::td($Theme->getLastModifiedMessage($rev,
255                                                                   false)),
256                           HTML::td(fmt("by %s", $authorlink)));
257     } else {
258         $row->pushContent(HTML::td(array('colspan' => '3'), _("None")));
259     }
260     return $row;
261 }
262
263 function showDiff (&$request) {
264     $pagename = $request->getArg('pagename');
265     if (is_array($versions = $request->getArg('versions'))) {
266         // Version selection from pageinfo.php display:
267         rsort($versions);
268         list ($version, $previous) = $versions;
269     }
270     else {
271         $version = $request->getArg('version');
272         $previous = $request->getArg('previous');
273     }
274
275     // abort if page doesn't exist
276     $dbi = $request->getDbh();
277     $page = $request->getPage();
278     $current = $page->getCurrentRevision();
279     if ($current->getVersion() < 1) {
280         $html = HTML(HTML::p(fmt("I'm sorry, there is no such page as %s.",
281                                  WikiLink($pagename, 'unknown'))));
282         include_once('lib/Template.php');
283         GeneratePage($html, sprintf(_("Diff: %s"), $pagename), false);
284         return; //early return
285     }
286
287     if ($version) {
288         if (!($new = $page->getRevision($version)))
289             NoSuchRevision($request, $page, $version);
290         $new_version = fmt("version %d", $version);
291     }
292     else {
293         $new = $current;
294         $new_version = _("current version");
295     }
296
297     if (preg_match('/^\d+$/', $previous)) {
298         if ( !($old = $page->getRevision($previous)) )
299             NoSuchRevision($request, $page, $previous);
300         $old_version = fmt("version %d", $previous);
301         $others = array('major', 'minor', 'author');
302     }
303     else {
304         switch ($previous) {
305         case 'author':
306             $old = $new;
307             while ($old = $page->getRevisionBefore($old)) {
308                 if ($old->get('author') != $new->get('author'))
309                     break;
310             }
311             $old_version = _("revision by previous author");
312             $others = array('major', 'minor');
313             break;
314         case 'minor':
315             $previous='minor';
316             $old = $page->getRevisionBefore($new);
317             $old_version = _("previous revision");
318             $others = array('major', 'author');
319             break;
320         case 'major':
321         default:
322             $old = $new;
323             while ($old && $old->get('is_minor_edit'))
324                 $old = $page->getRevisionBefore($old);
325             if ($old)
326                 $old = $page->getRevisionBefore($old);
327             $old_version = _("predecessor to the previous major change");
328             $others = array('minor', 'author');
329             break;
330         }
331     }
332
333     $new_link = WikiLink($new, '', $new_version);
334     $old_link = $old ? WikiLink($old, '', $old_version) : $old_version;
335     $page_link = WikiLink($page);
336
337     $html = HTML(HTML::p(fmt("Differences between %s and %s of %s.",
338                              $new_link, $old_link, $page_link)));
339
340     $otherdiffs = HTML::p(_("Other diffs:"));
341     $label = array('major' => _("Previous Major Revision"),
342                    'minor' => _("Previous Revision"),
343                    'author'=> _("Previous Author"));
344     foreach ($others as $other) {
345         $args = array('action' => 'diff', 'previous' => $other);
346         if ($version)
347             $args['version'] = $version;
348         if (count($otherdiffs->getContent()) > 1)
349             $otherdiffs->pushContent(", ");
350         else
351             $otherdiffs->pushContent(" ");
352         $otherdiffs->pushContent(Button($args, $label[$other]));
353     }
354     $html->pushContent($otherdiffs);
355
356
357     if ($old and $old->getVersion() == 0)
358         $old = false;
359
360     $html->pushContent(HTML::Table(PageInfoRow(_("Newer page:"), $new,
361                                                $request),
362                                    PageInfoRow(_("Older page:"), $old,
363                                                $request)));
364
365     if ($new && $old) {
366         $diff = new Diff($old->getContent(), $new->getContent());
367
368         if ($diff->isEmpty()) {
369             $html->pushContent(HTML::hr(),
370                                HTML::p('[', _("Versions are identical"),
371                                        ']'));
372         }
373         else {
374             // New CSS formatted unified diffs (ugly in NS4).
375             $fmt = new HtmlUnifiedDiffFormatter;
376
377             // Use this for old table-formatted diffs.
378             //$fmt = new TableUnifiedDiffFormatter;
379             $html->pushContent($fmt->format($diff));
380         }
381     }
382
383     include_once('lib/Template.php');
384     GeneratePage($html, sprintf(_("Diff: %s"), $pagename), $new);
385 }
386
387 // $Log: not supported by cvs2svn $
388 // Revision 1.44  2003/02/17 02:17:31  dairiki
389 // Fix so that action=diff will work when the most recent version
390 // of a page has been "deleted".
391 //
392 // Revision 1.43  2003/01/29 19:17:37  carstenklapp
393 // Bugfix for &nbsp showing on diff page.
394 //
395 // Revision 1.42  2003/01/11 23:05:04  carstenklapp
396 // Tweaked diff formatting.
397 //
398 // Revision 1.41  2003/01/08 02:23:02  carstenklapp
399 // Don't perform a diff when the page doesn't exist (such as a
400 // nonexistant calendar day/sub-page)
401 //
402
403 // Local Variables:
404 // mode: php
405 // tab-width: 8
406 // c-basic-offset: 4
407 // c-hanging-comment-ender-p: nil
408 // indent-tabs-mode: nil
409 // End:
410 ?>