]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/diff.php
Replaced NBSP constant with a new nbsp() function. The return value of HTML::nbsp...
[SourceForge/phpwiki.git] / lib / diff.php
1 <?php
2 rcs_id('$Id: diff.php,v 1.39 2002-10-29 01:12:24 carstenklapp 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, $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 .= "&nbsp;"; // FIXME to use HTML::nbsp()
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::nbsp();
149         foreach ($lines as $line) {
150             if ($elem)
151                 $line = new HtmlElement($elem, $line);
152             
153             $this->_block->pushContent(HTML::div(array('class' => $class),
154                                                  HTML::tt(array('class' => 'prefix'),
155                                                           $prefix),
156                                                  $line, HTML::nbsp()));
157         }
158     }
159
160     function _context($lines) {
161         $this->_lines($lines, 'context');
162     }
163     function _deleted($lines) {
164         $this->_lines($lines, 'deleted', '-', 'del');
165     }
166
167     function _added($lines) {
168         $this->_lines($lines, 'added', '+', 'ins');
169     }
170
171     function _changed($orig, $final) {
172         $diff = new WordLevelDiff($orig, $final);
173         $this->_lines($diff->orig(), 'original', '-');
174         $this->_lines($diff->final(), 'final', '+');
175     }
176 }
177
178 /**
179  * HTML table-based unified diff formatter.
180  *
181  * This class formats a diff into a table-based
182  * unified diff format.  (Similar to what was produced
183  * by previous versions of PhpWiki.)
184  *
185  * Within groups of changed lines, diffs are highlit
186  * at the character-diff level.
187  */
188 class TableUnifiedDiffFormatter extends HtmlUnifiedDiffFormatter
189 {
190     function TableUnifiedDiffFormatter($context_lines = 4) {
191         $this->HtmlUnifiedDiffFormatter($context_lines);
192     }
193
194     function _start_diff() {
195         $this->_top = HTML::table(array('width' => '100%',
196                                         'class' => 'diff',
197                                         'cellspacing' => 1,
198                                         'cellpadding' => 1,
199                                         'border' => 1));
200     }
201     
202     function _start_block($header) {
203         $this->_block = HTML::table(array('width' => '100%',
204                                           'class' => 'block',
205                                           'cellspacing' => 0,
206                                           'cellpadding' => 1,
207                                           'border' => 0),
208                                     HTML::tr(HTML::td(array('colspan' => 2),
209                                                       HTML::tt($header))));
210     }
211
212     function _end_block() {
213         $this->_top->pushContent(HTML::tr(HTML::td($this->_block)));
214         unset($this->_block);
215     }
216
217     function _lines($lines, $class, $prefix = false, $elem = false) {
218         if (!$prefix)
219             $prefix = HTML::nbsp();
220         $prefix = HTML::td(array('class' => 'prefix', 'width' => "1%"), $prefix);
221         foreach ($lines as $line) {
222             if (! trim($line))
223                 $line = HTML::nbsp();
224             elseif ($elem)
225                 $line = new HtmlElement($elem, $line);
226             $this->_block->pushContent(HTML::tr(array('valign' => 'top'), 
227                                                 $prefix,
228                                                 HTML::td(array('class' => $class),
229                                                          $line)));
230         }
231     }
232 }
233
234     
235 /////////////////////////////////////////////////////////////////
236
237 function PageInfoRow ($label, $rev, &$request)
238 {
239     global $Theme, $WikiNameRegexp;
240
241     $row = HTML::tr(HTML::td(array('align' => 'right'), $label));
242     if ($rev) {
243         $author = $rev->get('author');
244         $dbi = $request->getDbh();
245
246         $iswikipage = (preg_match("/^$WikiNameRegexp\$/", $author) && $dbi->isWikiPage($author));
247         $authorlink = $iswikipage ? WikiLink($author) : $author;
248
249         $linked_version = WikiLink($rev, 'existing', $rev->getVersion());
250         $row->pushContent(HTML::td(fmt("version %s", $linked_version)),
251                           HTML::td($Theme->getLastModifiedMessage($rev, false)),
252                           HTML::td(fmt("by %s", $authorlink)));
253     } else {
254         $row->pushContent(HTML::td(array('colspan' => '3'), _("None")));
255     }
256     return $row;
257 }
258
259 function showDiff (&$request) {
260     $pagename = $request->getArg('pagename');
261     if (is_array($versions = $request->getArg('versions'))) {
262         // Version selection from pageinfo.php display:
263         rsort($versions);
264         list ($version, $previous) = $versions;
265     }
266     else {
267         $version = $request->getArg('version');
268         $previous = $request->getArg('previous');
269     }
270     
271     $page = $request->getPage();
272     
273     if ($version) {
274         if (!($new = $page->getRevision($version)))
275             NoSuchRevision($request, $page, $version);
276         $new_version = fmt("version %d", $version);
277     }
278     else {
279         $new = $page->getCurrentRevision();
280         $new_version = _("current version");
281     }
282
283     if (preg_match('/^\d+$/', $previous)) {
284         if ( !($old = $page->getRevision($previous)) )
285             NoSuchRevision($request, $page, $previous);
286         $old_version = fmt("version %d", $previous);
287         $others = array('major', 'minor', 'author');
288     }
289     else {
290         switch ($previous) {
291         case 'author':
292             $old = $new;
293             while ($old = $page->getRevisionBefore($old)) {
294                 if ($old->get('author') != $new->get('author'))
295                     break;
296             }
297             $old_version = _("revision by previous author");
298             $others = array('major', 'minor');
299             break;
300         case 'minor':
301             $previous='minor';
302             $old = $page->getRevisionBefore($new);
303             $old_version = _("previous revision");
304             $others = array('major', 'author');
305             break;
306         case 'major':
307         default:
308             $old = $new;
309             while ($old && $old->get('is_minor_edit'))
310                 $old = $page->getRevisionBefore($old);
311             if ($old)
312                 $old = $page->getRevisionBefore($old);
313             $old_version = _("predecessor to the previous major change");
314             $others = array('minor', 'author');
315             break;
316         }
317     }
318
319     $new_link = WikiLink($new, '', $new_version);
320     $old_link = $old ? WikiLink($old, '', $old_version) : $old_version;
321     $page_link = WikiLink($page);
322
323     $html = HTML(HTML::p(fmt("Differences between %s and %s of %s.",
324                              $new_link, $old_link, $page_link)));
325
326     $otherdiffs = HTML::p(_("Other diffs:"));
327     $label = array('major' => _("Previous Major Revision"),
328                    'minor' => _("Previous Revision"),
329                    'author'=> _("Previous Author"));
330     foreach ($others as $other) {
331         $args = array('action' => 'diff', 'previous' => $other);
332         if ($version)
333             $args['version'] = $version;
334         if (count($otherdiffs->getContent()) > 1)
335             $otherdiffs->pushContent(", ");
336         else
337             $otherdiffs->pushContent(" ");
338         $otherdiffs->pushContent(Button($args, $label[$other]));
339     }
340     $html->pushContent($otherdiffs);
341         
342             
343     if ($old and $old->getVersion() == 0)
344         $old = false;
345     
346     $html->pushContent(HTML::Table(PageInfoRow(_("Newer page:"), $new, $request),
347                                    PageInfoRow(_("Older page:"), $old, $request)));
348     
349     if ($new && $old) {
350         $diff = new Diff($old->getContent(), $new->getContent());
351         
352         if ($diff->isEmpty()) {
353             $html->pushContent(HTML::hr(),
354                                HTML::p('[', _("Versions are identical"), ']'));
355         }
356         else {
357             // New CSS formatted unified diffs (ugly in NS4).
358             $fmt = new HtmlUnifiedDiffFormatter;
359
360             // Use this for old table-formatted diffs.
361             //$fmt = new TableUnifiedDiffFormatter;
362             $html->pushContent($fmt->format($diff));
363         }
364     }
365
366     include_once('lib/Template.php');
367     GeneratePage($html, sprintf(_("Diff: %s"), $pagename), $new);
368 }
369   
370 // Local Variables:
371 // mode: php
372 // tab-width: 8
373 // c-basic-offset: 4
374 // c-hanging-comment-ender-p: nil
375 // indent-tabs-mode: nil
376 // End:   
377 ?>