]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/Diff.php
renamed global $Theme to $WikiTheme (gforge nameclash)
[SourceForge/phpwiki.git] / lib / plugin / Diff.php
1 <?php // -*-php-*-
2 rcs_id('$Id: Diff.php,v 1.2 2004-06-14 11:31:39 rurban Exp $');
3 /**
4  Copyright 1999, 2000, 2001, 2002, 2004 $ThePhpWikiProgrammingTeam
5
6  This file is part of PhpWiki.
7
8  PhpWiki is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12
13  PhpWiki is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  GNU General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with PhpWiki; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22 /**
23  * lib/diff.php converted to a plugin by electrawn, 
24  * plugin cleaned up by rurban,
25  * code by dairiki
26  *
27  * Would make sense to see arbitrary diff's between any files or revisions.
28  */
29
30 require_once('lib/difflib.php');
31
32 class WikiPlugin_Diff
33 extends WikiPlugin {
34
35     function getName () {
36         return _("Diff");
37     }
38
39     function getDescription () {
40         return _("Display differences between revisions");
41     }
42
43     function getVersion() {
44         return preg_replace("/[Revision: $]/", '',
45                             "\$Revision: 1.2 $");
46     }
47
48     // Establish default values for each of this plugin's arguments.
49     // todo: makes only sense with more args.
50     function getDefaultArguments() {
51         return array('pagename' => '[pagename]',
52                      'name'     => _("World"),
53                      'versions' => false,
54                      'version'  => false,
55                      'previous' => 'major', // author, minor or major
56                      );
57     }
58
59     function PageInfoRow ($label, $rev, &$request) {
60
61         global $WikiTheme, $WikiNameRegexp;
62
63         $row = HTML::tr(HTML::td(array('align' => 'right'), $label));
64         if ($rev) {
65             $author = $rev->get('author');
66             $dbi = $request->getDbh();
67
68             $iswikipage = (isWikiWord($author) && $dbi->isWikiPage($author));
69             $authorlink = $iswikipage ? WikiLink($author) : $author;
70             
71             $linked_version = WikiLink($rev, 'existing', $rev->getVersion());
72             $row->pushContent(HTML::td(fmt("version %s", $linked_version)),
73                               HTML::td($WikiTheme->getLastModifiedMessage($rev,
74                                                                       false)),
75                               HTML::td(fmt("by %s", $authorlink)));
76         } else {
77             $row->pushContent(HTML::td(array('colspan' => '3'), _("None")));
78         }
79         return $row;
80     }
81     
82     function run($dbi, $argstr, &$request, $basepage) {
83         extract($this->getArgs($argstr, $request));
84         if (is_array($versions)) {
85             // Version selection from pageinfo.php display:
86             rsort($versions);
87             list ($version, $previous) = $versions;
88         }
89
90         // abort if page doesn't exist
91         $page = $request->getPage($pagename);
92         $current = $page->getCurrentRevision();
93         if ($current->getVersion() < 1) {
94             $html = HTML(HTML::p(fmt("I'm sorry, there is no such page as %s.",
95                                      WikiLink($pagename, 'unknown'))));
96             return $html; //early return
97         }
98
99         if ($version) {
100             if (!($new = $page->getRevision($version)))
101                 NoSuchRevision($request, $page, $version);
102             $new_version = fmt("version %d", $version);
103         }
104         else {
105             $new = $current;
106             $new_version = _("current version");
107         }
108
109         if (preg_match('/^\d+$/', $previous)) {
110             if ( !($old = $page->getRevision($previous)) )
111                 NoSuchRevision($request, $page, $previous);
112             $old_version = fmt("version %d", $previous);
113             $others = array('major', 'minor', 'author');
114         }
115         else {
116             switch ($previous) {
117             case 'author':
118                 $old = $new;
119                 while ($old = $page->getRevisionBefore($old)) {
120                     if ($old->get('author') != $new->get('author'))
121                         break;
122                 }
123                 $old_version = _("revision by previous author");
124                 $others = array('major', 'minor');
125                 break;
126             case 'minor':
127                 $previous='minor';
128                 $old = $page->getRevisionBefore($new);
129                 $old_version = _("previous revision");
130                 $others = array('major', 'author');
131                 break;
132             case 'major':
133             default:
134                 $old = $new;
135                 while ($old && $old->get('is_minor_edit'))
136                     $old = $page->getRevisionBefore($old);
137                 if ($old)
138                     $old = $page->getRevisionBefore($old);
139                 $old_version = _("predecessor to the previous major change");
140                 $others = array('minor', 'author');
141                 break;
142             }
143         }
144         
145         $new_link = WikiLink($new, '', $new_version);
146         $old_link = $old ? WikiLink($old, '', $old_version) : $old_version;
147         $page_link = WikiLink($page);
148         
149         $html = HTML(HTML::p(fmt("Differences between %s and %s of %s.",
150                                  $new_link, $old_link, $page_link)));
151
152         $otherdiffs = HTML::p(_("Other diffs:"));
153         $label = array('major' => _("Previous Major Revision"),
154                        'minor' => _("Previous Revision"),
155                        'author'=> _("Previous Author"));
156         foreach ($others as $other) {
157             $args = array('pagename' => $pagename, 'previous' => $other);
158             if ($version)
159                 $args['version'] = $version;
160             if (count($otherdiffs->getContent()) > 1)
161                 $otherdiffs->pushContent(", ");
162             else
163                 $otherdiffs->pushContent(" ");
164             $otherdiffs->pushContent(Button($args, $label[$other]));
165         }
166         $html->pushContent($otherdiffs);
167
168         
169         if ($old and $old->getVersion() == 0)
170             $old = false;
171
172         $html->pushContent(HTML::Table($this->PageInfoRow(_("Newer page:"), $new,
173                                                           $request),
174                                        $this->PageInfoRow(_("Older page:"), $old,
175                                                           $request)));
176
177         if ($new && $old) {
178             $diff = new Diff($old->getContent(), $new->getContent());
179             
180             if ($diff->isEmpty()) {
181                 $html->pushContent(HTML::hr(),
182                                    HTML::p('[', _("Versions are identical"),
183                                            ']'));
184             }
185             else {
186                 // New CSS formatted unified diffs (ugly in NS4).
187                 $fmt = new HtmlUnifiedDiffFormatter;
188
189                 // Use this for old table-formatted diffs.
190                 //$fmt = new TableUnifiedDiffFormatter;
191                 $html->pushContent($fmt->format($diff));
192             }
193         }
194
195         //$html = HTML::tt(fmt('%s: %s', $salutation, WikiLink($name, 'auto')),
196         //                 THE_END);
197         
198         return $html;
199     }
200 };
201
202 class _HWLDF_WordAccumulator {
203     function _HWLDF_WordAccumulator () {
204         $this->_lines = array();
205         $this->_line = false;
206         $this->_group = false;
207         $this->_tag = '~begin';
208     }
209
210     function _flushGroup ($new_tag) {
211         if ($this->_group !== false) {
212             if (!$this->_line)
213                 $this->_line = HTML();
214             $this->_line->pushContent($this->_tag
215                                       ? new HtmlElement($this->_tag,
216                                                         $this->_group)
217                                       : $this->_group);
218         }
219         $this->_group = '';
220         $this->_tag = $new_tag;
221     }
222
223     function _flushLine ($new_tag) {
224         $this->_flushGroup($new_tag);
225         if ($this->_line)
226             $this->_lines[] = $this->_line;
227         $this->_line = HTML();
228     }
229
230     function addWords ($words, $tag = '') {
231         if ($tag != $this->_tag)
232             $this->_flushGroup($tag);
233
234         foreach ($words as $word) {
235             // new-line should only come as first char of word.
236             if (!$word)
237                 continue;
238             if ($word[0] == "\n") {
239                 $this->_group .= PrintXML(HTML::raw('&nbsp;'));
240                 $this->_flushLine($tag);
241                 $word = substr($word, 1);
242             }
243             assert(!strstr($word, "\n"));
244             $this->_group .= $word;
245         }
246     }
247
248     function getLines() {
249         $this->_flushLine('~done');
250         return $this->_lines;
251     }
252 }
253
254 class WordLevelDiff extends MappedDiff
255 {
256     function WordLevelDiff ($orig_lines, $final_lines) {
257         list ($orig_words, $orig_stripped) = $this->_split($orig_lines);
258         list ($final_words, $final_stripped) = $this->_split($final_lines);
259
260
261         $this->MappedDiff($orig_words, $final_words,
262                           $orig_stripped, $final_stripped);
263     }
264
265     function _split($lines) {
266         // FIXME: fix POSIX char class.
267         if (!preg_match_all('/ ( [^\S\n]+ | [[:alnum:]]+ | . ) (?: (?!< \n) [^\S\n])? /xs',
268                             implode("\n", $lines),
269                             $m)) {
270             return array(array(''), array(''));
271         }
272         return array($m[0], $m[1]);
273     }
274
275     function orig () {
276         $orig = new _HWLDF_WordAccumulator;
277
278         foreach ($this->edits as $edit) {
279             if ($edit->type == 'copy')
280                 $orig->addWords($edit->orig);
281             elseif ($edit->orig)
282                 $orig->addWords($edit->orig, 'del');
283         }
284         return $orig->getLines();
285     }
286
287     function final () {
288         $final = new _HWLDF_WordAccumulator;
289
290         foreach ($this->edits as $edit) {
291             if ($edit->type == 'copy')
292                 $final->addWords($edit->final);
293             elseif ($edit->final)
294                 $final->addWords($edit->final, 'ins');
295         }
296         return $final->getLines();
297     }
298 }
299
300 /**
301  * HTML unified diff formatter.
302  *
303  * This class formats a diff into a CSS-based
304  * unified diff format.
305  *
306  * Within groups of changed lines, diffs are highlit
307  * at the character-diff level.
308  */
309 class HtmlUnifiedDiffFormatter extends UnifiedDiffFormatter
310 {
311     function HtmlUnifiedDiffFormatter($context_lines = 4) {
312         $this->UnifiedDiffFormatter($context_lines);
313     }
314
315     function _start_diff() {
316         $this->_top = HTML::div(array('class' => 'diff'));
317     }
318     function _end_diff() {
319         $val = $this->_top;
320         unset($this->_top);
321         return $val;
322     }
323
324     function _start_block($header) {
325         $this->_block = HTML::div(array('class' => 'block'),
326                                   HTML::tt($header));
327     }
328
329     function _end_block() {
330         $this->_top->pushContent($this->_block);
331         unset($this->_block);
332     }
333
334     function _lines($lines, $class, $prefix = false, $elem = false) {
335         if (!$prefix)
336             $prefix = HTML::raw('&nbsp;');
337         $div = HTML::div(array('class' => 'difftext'));
338         foreach ($lines as $line) {
339             if ($elem)
340                 $line = new HtmlElement($elem, $line);
341             $div->pushContent(HTML::div(array('class' => $class),
342                                         HTML::tt(array('class' => 'prefix'),
343                                                  $prefix),
344                                         $line, HTML::raw('&nbsp;')));
345         }
346         $this->_block->pushContent($div);
347     }
348
349     function _context($lines) {
350         $this->_lines($lines, 'context');
351     }
352     function _deleted($lines) {
353         $this->_lines($lines, 'deleted', '-', 'del');
354     }
355
356     function _added($lines) {
357         $this->_lines($lines, 'added', '+', 'ins');
358     }
359
360     function _changed($orig, $final) {
361         $diff = new WordLevelDiff($orig, $final);
362         $this->_lines($diff->orig(), 'original', '-');
363         $this->_lines($diff->final(), 'final', '+');
364     }
365 }
366
367 /**
368  * HTML table-based unified diff formatter.
369  *
370  * This class formats a diff into a table-based
371  * unified diff format.  (Similar to what was produced
372  * by previous versions of PhpWiki.)
373  *
374  * Within groups of changed lines, diffs are highlit
375  * at the character-diff level.
376  */
377 class TableUnifiedDiffFormatter extends HtmlUnifiedDiffFormatter
378 {
379     function TableUnifiedDiffFormatter($context_lines = 4) {
380         $this->HtmlUnifiedDiffFormatter($context_lines);
381     }
382
383     function _start_diff() {
384         $this->_top = HTML::table(array('width' => '100%',
385                                         'class' => 'diff',
386                                         'cellspacing' => 1,
387                                         'cellpadding' => 1,
388                                         'border' => 1));
389     }
390
391     function _start_block($header) {
392         $this->_block = HTML::table(array('width' => '100%',
393                                           'class' => 'block',
394                                           'cellspacing' => 0,
395                                           'cellpadding' => 1,
396                                           'border' => 0),
397                                     HTML::tr(HTML::td(array('colspan' => 2),
398                                                       HTML::tt($header))));
399     }
400
401     function _end_block() {
402         $this->_top->pushContent(HTML::tr(HTML::td($this->_block)));
403         unset($this->_block);
404     }
405
406     function _lines($lines, $class, $prefix = false, $elem = false) {
407         if (!$prefix)
408             $prefix = HTML::raw('&nbsp;');
409         $prefix = HTML::td(array('class' => 'prefix',
410                                  'width' => "1%"), $prefix);
411         foreach ($lines as $line) {
412             if (! trim($line))
413                 $line = HTML::raw('&nbsp;');
414             elseif ($elem)
415                 $line = new HtmlElement($elem, $line);
416             $this->_block->pushContent(HTML::tr(array('valign' => 'top'),
417                                                 $prefix,
418                                                 HTML::td(array('class' => $class),
419                                                          $line)));
420         }
421     }
422 }
423
424 // $Log: not supported by cvs2svn $
425 // Revision 1.1  2004/02/26 23:02:17  rurban
426 // lib/diff.php converted to a plugin by electrawn,
427 // plugin cleaned up by rurban,
428 // code by dairiki
429 //
430 // Would make sense to see arbitrary diff's between any files or revisions.
431 //
432 //
433
434 // For emacs users
435 // Local Variables:
436 // mode: php
437 // tab-width: 8
438 // c-basic-offset: 4
439 // c-hanging-comment-ender-p: nil
440 // indent-tabs-mode: nil
441 // End:
442 ?>