]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/diff.php
Valid XHTML code for all themes
[SourceForge/phpwiki.git] / lib / diff.php
1 <?php // -*-php-*-
2 // rcs_id('$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  * HTML table-based unified diff formatter.
181  *
182  * This class formats a diff into a table-based
183  * unified diff format.  (Similar to what was produced
184  * by previous versions of PhpWiki.)
185  *
186  * Within groups of changed lines, diffs are highlit
187  * at the character-diff level.
188  */
189 class TableUnifiedDiffFormatter extends HtmlUnifiedDiffFormatter
190 {
191     function TableUnifiedDiffFormatter($context_lines = 4) {
192         $this->HtmlUnifiedDiffFormatter($context_lines);
193     }
194
195     function _start_diff() {
196         $this->_top = HTML::table(array('width' => '100%',
197                                         'class' => 'diff',
198                                         'cellspacing' => 1,
199                                         'cellpadding' => 1,
200                                         'border' => 1));
201     }
202
203     function _start_block($header) {
204         $this->_block = HTML::table(array('width' => '100%',
205                                           'class' => 'block',
206                                           'cellspacing' => 0,
207                                           'cellpadding' => 1,
208                                           'border' => 0),
209                                     HTML::tr(HTML::td(array('colspan' => 2),
210                                                       HTML::tt($header))));
211     }
212
213     function _end_block() {
214         $this->_top->pushContent(HTML::tr(HTML::td($this->_block)));
215         unset($this->_block);
216     }
217
218     function _lines($lines, $class, $prefix = false, $elem = false) {
219         if (!$prefix)
220             $prefix = HTML::raw('&nbsp;');
221         $prefix = HTML::td(array('class' => 'prefix',
222                                  'width' => "1%"), $prefix);
223         foreach ($lines as $line) {
224             if (! trim($line))
225                 $line = HTML::raw('&nbsp;');
226             elseif ($elem)
227                 $line = new HtmlElement($elem, $line);
228             $this->_block->pushContent(HTML::tr(array('valign' => 'top'),
229                                                 $prefix,
230                                                 HTML::td(array('class' => $class),
231                                                          $line)));
232         }
233     }
234 }
235
236 /////////////////////////////////////////////////////////////////
237
238 function PageInfoRow ($label, $rev, &$request, $is_current = false)
239 {
240     global $WikiTheme;
241
242     $row = HTML::tr(HTML::td(array('align' => 'right'), $label));
243     if ($rev) {
244         $author = $rev->get('author');
245         $dbi = $request->getDbh();
246
247         $iswikipage = (isWikiWord($author) && $dbi->isWikiPage($author));
248         $authorlink = $iswikipage ? WikiLink($author) : $author;
249         $version = $rev->getVersion();
250         $linked_version = WikiLink($rev, 'existing', $version);
251         if ($is_current)
252             $revertbutton = HTML();
253         else
254             $revertbutton = $WikiTheme->makeActionButton(array('action' => 'revert',
255                                                                'version' => $version),
256                                                          false, $rev);
257         $row->pushContent(HTML::td(fmt("version %s", $linked_version)),
258                           HTML::td($WikiTheme->getLastModifiedMessage($rev,
259                                                                       false)),
260                           HTML::td(fmt("by %s", $authorlink)),
261                           HTML::td($revertbutton)
262                           );
263     } else {
264         $row->pushContent(HTML::td(array('colspan' => '4'), _("None")));
265     }
266     return $row;
267 }
268
269 function showDiff (&$request) {
270     $pagename = $request->getArg('pagename');
271     if (is_array($versions = $request->getArg('versions'))) {
272         // Version selection from pageinfo.php display:
273         rsort($versions);
274         list ($version, $previous) = $versions;
275     }
276     else {
277         $version = $request->getArg('version');
278         $previous = $request->getArg('previous');
279     }
280  
281     // abort if page doesn't exist
282     $dbi = $request->getDbh();
283     $page = $request->getPage();
284     $current = $page->getCurrentRevision(false);
285     if ($current->getVersion() < 1) {
286         $html = HTML::div(array('class'=>'wikitext','id'=>'difftext'),
287                           HTML::p(fmt("I'm sorry, there is no such page as %s.",
288                                       WikiLink($pagename, 'unknown'))));
289         require_once('lib/Template.php');
290         GeneratePage($html, sprintf(_("Diff: %s"), $pagename), false);
291         return; //early return
292     }
293
294     if ($version) {
295         if (!($new = $page->getRevision($version)))
296             NoSuchRevision($request, $page, $version);
297         $new_version = fmt("version %d", $version);
298     }
299     else {
300         $new = $current;
301         $new_version = _("current version");
302     }
303
304     if (preg_match('/^\d+$/', $previous)) {
305         if ( !($old = $page->getRevision($previous)) )
306             NoSuchRevision($request, $page, $previous);
307         $old_version = fmt("version %d", $previous);
308         $others = array('major', 'minor', 'author');
309     }
310     else {
311         switch ($previous) {
312         case 'author':
313             $old = $new;
314             while ($old = $page->getRevisionBefore($old)) {
315                 if ($old->get('author') != $new->get('author'))
316                     break;
317             }
318             $old_version = _("revision by previous author");
319             $others = array('major', 'minor');
320             break;
321         case 'minor':
322             $previous='minor';
323             $old = $page->getRevisionBefore($new);
324             $old_version = _("previous revision");
325             $others = array('major', 'author');
326             break;
327         case 'major':
328         default:
329             $old = $new;
330             while ($old && $old->get('is_minor_edit'))
331                 $old = $page->getRevisionBefore($old);
332             if ($old)
333                 $old = $page->getRevisionBefore($old);
334             $old_version = _("predecessor to the previous major change");
335             $others = array('minor', 'author');
336             break;
337         }
338     }
339
340     $new_link = WikiLink($new, '', $new_version);
341     $old_link = $old ? WikiLink($old, '', $old_version) : $old_version;
342     $page_link = WikiLink($page);
343
344     $html = HTML::div(array('class'=>'wikitext','id'=>'difftext'),
345                      HTML::p(fmt("Differences between %s and %s of %s.",
346                                  $new_link, $old_link, $page_link)));
347
348     $otherdiffs = HTML::p(_("Other diffs:"));
349     $label = array('major' => _("Previous Major Revision"),
350                    'minor' => _("Previous Revision"),
351                    'author'=> _("Previous Author"));
352     foreach ($others as $other) {
353         $args = array('action' => 'diff', 'previous' => $other);
354         if ($version)
355             $args['version'] = $version;
356         if (count($otherdiffs->getContent()) > 1)
357             $otherdiffs->pushContent(", ");
358         else
359             $otherdiffs->pushContent(" ");
360         $otherdiffs->pushContent(Button($args, $label[$other]));
361     }
362     $html->pushContent($otherdiffs);
363
364     if ($old and $old->getVersion() == 0)
365         $old = false;
366
367     $html->pushContent(HTML::Table(PageInfoRow(_("Newer page:"), $new,
368                                                $request, empty($version)),
369                                    PageInfoRow(_("Older page:"), $old,
370                                                $request, false)));
371
372     if ($new && $old) {
373         $diff = new Diff($old->getContent(), $new->getContent());
374
375         if ($diff->isEmpty()) {
376             $html->pushContent(HTML::hr(),
377                                HTML::p(_("Content of versions "), $old->getVersion(),
378                                        _(" and "), $new->getVersion(), _(" is identical.")));
379             // If two consecutive versions have the same content, it is because the page was
380             // renamed, or metadata changed: ACL, owner, markup.
381             // We give the reason by printing the summary.
382             if (($new->getVersion() - $old->getVersion()) == 1) {
383                 $html->pushContent(HTML::p(_("Version "), $new->getVersion(), 
384                                            _(" was created because: "), $new->get('summary')));
385             }
386         } else {
387             $fmt = new HtmlUnifiedDiffFormatter;
388             $html->pushContent($fmt->format($diff));
389         }
390
391         $html->pushContent(HTML::hr(), HTML::h2($new_version));
392         require_once("lib/BlockParser.php");
393         $html->pushContent(TransformText($new,$new->get('markup'),$pagename));
394     }
395
396     require_once('lib/Template.php');
397     GeneratePage($html, sprintf(_("Diff: %s"), $pagename), $new);
398 }
399
400 // Local Variables:
401 // mode: php
402 // tab-width: 8
403 // c-basic-offset: 4
404 // c-hanging-comment-ender-p: nil
405 // indent-tabs-mode: nil
406 // End:
407 ?>