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