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