]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/diff.php
Fixed SF bug #528567 (I think) - two or three functions were called with arguments
[SourceForge/phpwiki.git] / lib / diff.php
1 <?php
2 rcs_id('$Id: diff.php,v 1.38 2002-03-21 21:24:49 lakka 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, &$request)
234 {
235     global $Theme, $WikiNameRegexp;
236
237     $row = HTML::tr(HTML::td(array('align' => 'right'), $label));
238     if ($rev) {
239         $author = $rev->get('author');
240         $dbi = $request->getDbh();
241
242         $iswikipage = (preg_match("/^$WikiNameRegexp\$/", $author) && $dbi->isWikiPage($author));
243         $authorlink = $iswikipage ? WikiLink($author) : $author;
244
245         $linked_version = WikiLink($rev, 'existing', $rev->getVersion());
246         $row->pushContent(HTML::td(fmt("version %s", $linked_version)),
247                           HTML::td($Theme->getLastModifiedMessage($rev, false)),
248                           HTML::td(fmt("by %s", $authorlink)));
249     } else {
250         $row->pushContent(HTML::td(array('colspan' => '3'), _("None")));
251     }
252     return $row;
253 }
254
255 function showDiff (&$request) {
256     $pagename = $request->getArg('pagename');
257     if (is_array($versions = $request->getArg('versions'))) {
258         // Version selection from pageinfo.php display:
259         rsort($versions);
260         list ($version, $previous) = $versions;
261     }
262     else {
263         $version = $request->getArg('version');
264         $previous = $request->getArg('previous');
265     }
266     
267     $page = $request->getPage();
268     
269     if ($version) {
270         if (!($new = $page->getRevision($version)))
271             NoSuchRevision($request, $page, $version);
272         $new_version = fmt("version %d", $version);
273     }
274     else {
275         $new = $page->getCurrentRevision();
276         $new_version = _("current version");
277     }
278
279     if (preg_match('/^\d+$/', $previous)) {
280         if ( !($old = $page->getRevision($previous)) )
281             NoSuchRevision($request, $page, $previous);
282         $old_version = fmt("version %d", $previous);
283         $others = array('major', 'minor', 'author');
284     }
285     else {
286         switch ($previous) {
287         case 'author':
288             $old = $new;
289             while ($old = $page->getRevisionBefore($old)) {
290                 if ($old->get('author') != $new->get('author'))
291                     break;
292             }
293             $old_version = _("revision by previous author");
294             $others = array('major', 'minor');
295             break;
296         case 'minor':
297             $previous='minor';
298             $old = $page->getRevisionBefore($new);
299             $old_version = _("previous revision");
300             $others = array('major', 'author');
301             break;
302         case 'major':
303         default:
304             $old = $new;
305             while ($old && $old->get('is_minor_edit'))
306                 $old = $page->getRevisionBefore($old);
307             if ($old)
308                 $old = $page->getRevisionBefore($old);
309             $old_version = _("predecessor to the previous major change");
310             $others = array('minor', 'author');
311             break;
312         }
313     }
314
315     $new_link = WikiLink($new, '', $new_version);
316     $old_link = $old ? WikiLink($old, '', $old_version) : $old_version;
317     $page_link = WikiLink($page);
318
319     $html = HTML(HTML::p(fmt("Differences between %s and %s of %s.",
320                              $new_link, $old_link, $page_link)));
321
322     $otherdiffs = HTML::p(_("Other diffs:"));
323     $label = array('major' => _("Previous Major Revision"),
324                    'minor' => _("Previous Revision"),
325                    'author'=> _("Previous Author"));
326     foreach ($others as $other) {
327         $args = array('action' => 'diff', 'previous' => $other);
328         if ($version)
329             $args['version'] = $version;
330         if (count($otherdiffs->getContent()) > 1)
331             $otherdiffs->pushContent(", ");
332         else
333             $otherdiffs->pushContent(" ");
334         $otherdiffs->pushContent(Button($args, $label[$other]));
335     }
336     $html->pushContent($otherdiffs);
337         
338             
339     if ($old and $old->getVersion() == 0)
340         $old = false;
341     
342     $html->pushContent(HTML::Table(PageInfoRow(_("Newer page:"), $new, $request),
343                                    PageInfoRow(_("Older page:"), $old, $request)));
344     
345     if ($new && $old) {
346         $diff = new Diff($old->getContent(), $new->getContent());
347         
348         if ($diff->isEmpty()) {
349             $html->pushContent(HTML::hr(),
350                                HTML::p('[', _("Versions are identical"), ']'));
351         }
352         else {
353             // New CSS formatted unified diffs (ugly in NS4).
354             $fmt = new HtmlUnifiedDiffFormatter;
355
356             // Use this for old table-formatted diffs.
357             //$fmt = new TableUnifiedDiffFormatter;
358             $html->pushContent($fmt->format($diff));
359         }
360     }
361
362     include_once('lib/Template.php');
363     GeneratePage($html, sprintf(_("Diff: %s"), $pagename), $new);
364 }
365   
366 // Local Variables:
367 // mode: php
368 // tab-width: 8
369 // c-basic-offset: 4
370 // c-hanging-comment-ender-p: nil
371 // indent-tabs-mode: nil
372 // End:   
373 ?>