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