]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/diff.php
omit want_content if not necessary
[SourceForge/phpwiki.git] / lib / diff.php
1 <?php // -*-php-*-
2 rcs_id('$Id: diff.php,v 1.54 2007-01-02 13:18:16 rurban 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,
28                                                         $this->_group)
29                                       : $this->_group);
30         }
31         $this->_group = '';
32         $this->_tag = $new_tag;
33     }
34
35     function _flushLine ($new_tag) {
36         $this->_flushGroup($new_tag);
37         if ($this->_line)
38             $this->_lines[] = $this->_line;
39         $this->_line = HTML();
40     }
41
42     function addWords ($words, $tag = '') {
43         if ($tag != $this->_tag)
44             $this->_flushGroup($tag);
45
46         foreach ($words as $word) {
47             // new-line should only come as first char of word.
48             if (!$word)
49                 continue;
50             if ($word[0] == "\n") {
51                 $this->_group .= " ";
52                 $this->_flushLine($tag);
53                 $word = substr($word, 1);
54             }
55             assert(!strstr($word, "\n"));
56             $this->_group .= $word;
57         }
58     }
59
60     function getLines() {
61         $this->_flushLine('~done');
62         return $this->_lines;
63     }
64 }
65
66 class WordLevelDiff extends MappedDiff
67 {
68     function WordLevelDiff ($orig_lines, $final_lines) {
69         list ($orig_words, $orig_stripped) = $this->_split($orig_lines);
70         list ($final_words, $final_stripped) = $this->_split($final_lines);
71
72
73         $this->MappedDiff($orig_words, $final_words,
74                           $orig_stripped, $final_stripped);
75     }
76
77     function _split($lines) {
78         // FIXME: fix POSIX char class.
79         if (!preg_match_all('/ ( [^\S\n]+ | [[:alnum:]]+ | . ) (?: (?!< \n) [^\S\n])? /xs',
80                             implode("\n", $lines),
81                             $m)) {
82             return array(array(''), array(''));
83         }
84         return array($m[0], $m[1]);
85     }
86
87     function orig () {
88         $orig = new _HWLDF_WordAccumulator;
89
90         foreach ($this->edits as $edit) {
91             if ($edit->type == 'copy')
92                 $orig->addWords($edit->orig);
93             elseif ($edit->orig)
94                 $orig->addWords($edit->orig, 'del');
95         }
96         return $orig->getLines();
97     }
98
99     function _final () {
100         $final = new _HWLDF_WordAccumulator;
101
102         foreach ($this->edits as $edit) {
103             if ($edit->type == 'copy')
104                 $final->addWords($edit->final);
105             elseif ($edit->final)
106                 $final->addWords($edit->final, 'ins');
107         }
108         return $final->getLines();
109     }
110 }
111
112
113 /**
114  * HTML unified diff formatter.
115  *
116  * This class formats a diff into a CSS-based
117  * unified diff format.
118  *
119  * Within groups of changed lines, diffs are highlit
120  * at the character-diff level.
121  */
122 class HtmlUnifiedDiffFormatter extends UnifiedDiffFormatter
123 {
124     function HtmlUnifiedDiffFormatter($context_lines = 4) {
125         $this->UnifiedDiffFormatter($context_lines);
126     }
127
128     function _start_diff() {
129         $this->_top = HTML::div(array('class' => 'diff'));
130     }
131     function _end_diff() {
132         $val = $this->_top;
133         unset($this->_top);
134         return $val;
135     }
136
137     function _start_block($header) {
138         $this->_block = HTML::div(array('class' => 'block'),
139                                   HTML::tt($header));
140     }
141
142     function _end_block() {
143         $this->_top->pushContent($this->_block);
144         unset($this->_block);
145     }
146
147     function _lines($lines, $class, $prefix = false, $elem = false) {
148         if (!$prefix)
149             $prefix = HTML::raw('&nbsp;');
150         $div = HTML::div(array('class' => 'difftext'));
151         foreach ($lines as $line) {
152             if ($elem)
153                 $line = new HtmlElement($elem, $line);
154             $div->pushContent(HTML::div(array('class' => $class),
155                                         HTML::tt(array('class' => 'prefix'),
156                                                  $prefix),
157                                         $line, HTML::raw('&nbsp;')));
158         }
159         $this->_block->pushContent($div);
160     }
161
162     function _context($lines) {
163         $this->_lines($lines, 'context');
164     }
165     function _deleted($lines) {
166         $this->_lines($lines, 'deleted', '-', 'del');
167     }
168
169     function _added($lines) {
170         $this->_lines($lines, 'added', '+', 'ins');
171     }
172
173     function _changed($orig, $final) {
174         $diff = new WordLevelDiff($orig, $final);
175         $this->_lines($diff->orig(), 'original', '-');
176         $this->_lines($diff->_final(), 'final', '+');
177     }
178 }
179
180 /**
181  * HTML table-based unified diff formatter.
182  *
183  * This class formats a diff into a table-based
184  * unified diff format.  (Similar to what was produced
185  * by previous versions of PhpWiki.)
186  *
187  * Within groups of changed lines, diffs are highlit
188  * at the character-diff level.
189  */
190 class TableUnifiedDiffFormatter extends HtmlUnifiedDiffFormatter
191 {
192     function TableUnifiedDiffFormatter($context_lines = 4) {
193         $this->HtmlUnifiedDiffFormatter($context_lines);
194     }
195
196     function _start_diff() {
197         $this->_top = HTML::table(array('width' => '100%',
198                                         'class' => 'diff',
199                                         'cellspacing' => 1,
200                                         'cellpadding' => 1,
201                                         'border' => 1));
202     }
203
204     function _start_block($header) {
205         $this->_block = HTML::table(array('width' => '100%',
206                                           'class' => 'block',
207                                           'cellspacing' => 0,
208                                           'cellpadding' => 1,
209                                           'border' => 0),
210                                     HTML::tr(HTML::td(array('colspan' => 2),
211                                                       HTML::tt($header))));
212     }
213
214     function _end_block() {
215         $this->_top->pushContent(HTML::tr(HTML::td($this->_block)));
216         unset($this->_block);
217     }
218
219     function _lines($lines, $class, $prefix = false, $elem = false) {
220         if (!$prefix)
221             $prefix = HTML::raw('&nbsp;');
222         $prefix = HTML::td(array('class' => 'prefix',
223                                  'width' => "1%"), $prefix);
224         foreach ($lines as $line) {
225             if (! trim($line))
226                 $line = HTML::raw('&nbsp;');
227             elseif ($elem)
228                 $line = new HtmlElement($elem, $line);
229             $this->_block->pushContent(HTML::tr(array('valign' => 'top'),
230                                                 $prefix,
231                                                 HTML::td(array('class' => $class),
232                                                          $line)));
233         }
234     }
235 }
236
237 /////////////////////////////////////////////////////////////////
238
239 function PageInfoRow ($label, $rev, &$request, $is_current = false)
240 {
241     global $WikiTheme;
242
243     $row = HTML::tr(HTML::td(array('align' => 'right'), $label));
244     if ($rev) {
245         $author = $rev->get('author');
246         $dbi = $request->getDbh();
247
248         $iswikipage = (isWikiWord($author) && $dbi->isWikiPage($author));
249         $authorlink = $iswikipage ? WikiLink($author) : $author;
250         $version = $rev->getVersion();
251         $linked_version = WikiLink($rev, 'existing', $version);
252         if ($is_current)
253             $revertbutton = HTML();
254         else
255             $revertbutton = $WikiTheme->makeActionButton(array('action' => 'revert',
256                                                                'version' => $version),
257                                                          false, $rev);
258         $row->pushContent(HTML::td(fmt("version %s", $linked_version)),
259                           HTML::td($WikiTheme->getLastModifiedMessage($rev,
260                                                                       false)),
261                           HTML::td(fmt("by %s", $authorlink)),
262                           HTML::td($revertbutton)
263                           );
264     } else {
265         $row->pushContent(HTML::td(array('colspan' => '4'), _("None")));
266     }
267     return $row;
268 }
269
270 function showDiff (&$request) {
271     $pagename = $request->getArg('pagename');
272     if (is_array($versions = $request->getArg('versions'))) {
273         // Version selection from pageinfo.php display:
274         rsort($versions);
275         list ($version, $previous) = $versions;
276     }
277     else {
278         $version = $request->getArg('version');
279         $previous = $request->getArg('previous');
280     }
281  
282     // abort if page doesn't exist
283     $dbi = $request->getDbh();
284     $page = $request->getPage();
285     $current = $page->getCurrentRevision(false);
286     if ($current->getVersion() < 1) {
287         $html = HTML::div(array('class'=>'wikitext','id'=>'difftext'),
288                           HTML::p(fmt("I'm sorry, there is no such page as %s.",
289                                       WikiLink($pagename, 'unknown'))));
290         include_once('lib/Template.php');
291         GeneratePage($html, sprintf(_("Diff: %s"), $pagename), false);
292         return; //early return
293     }
294
295     if ($version) {
296         if (!($new = $page->getRevision($version)))
297             NoSuchRevision($request, $page, $version);
298         $new_version = fmt("version %d", $version);
299     }
300     else {
301         $new = $current;
302         $new_version = _("current version");
303     }
304
305     if (preg_match('/^\d+$/', $previous)) {
306         if ( !($old = $page->getRevision($previous)) )
307             NoSuchRevision($request, $page, $previous);
308         $old_version = fmt("version %d", $previous);
309         $others = array('major', 'minor', 'author');
310     }
311     else {
312         switch ($previous) {
313         case 'author':
314             $old = $new;
315             while ($old = $page->getRevisionBefore($old)) {
316                 if ($old->get('author') != $new->get('author'))
317                     break;
318             }
319             $old_version = _("revision by previous author");
320             $others = array('major', 'minor');
321             break;
322         case 'minor':
323             $previous='minor';
324             $old = $page->getRevisionBefore($new);
325             $old_version = _("previous revision");
326             $others = array('major', 'author');
327             break;
328         case 'major':
329         default:
330             $old = $new;
331             while ($old && $old->get('is_minor_edit'))
332                 $old = $page->getRevisionBefore($old);
333             if ($old)
334                 $old = $page->getRevisionBefore($old);
335             $old_version = _("predecessor to the previous major change");
336             $others = array('minor', 'author');
337             break;
338         }
339     }
340
341     $new_link = WikiLink($new, '', $new_version);
342     $old_link = $old ? WikiLink($old, '', $old_version) : $old_version;
343     $page_link = WikiLink($page);
344
345     $html = HTML::div(array('class'=>'wikitext','id'=>'difftext'),
346                      HTML::p(fmt("Differences between %s and %s of %s.",
347                                  $new_link, $old_link, $page_link)));
348
349     $otherdiffs = HTML::p(_("Other diffs:"));
350     $label = array('major' => _("Previous Major Revision"),
351                    'minor' => _("Previous Revision"),
352                    'author'=> _("Previous Author"));
353     foreach ($others as $other) {
354         $args = array('action' => 'diff', 'previous' => $other);
355         if ($version)
356             $args['version'] = $version;
357         if (count($otherdiffs->getContent()) > 1)
358             $otherdiffs->pushContent(", ");
359         else
360             $otherdiffs->pushContent(" ");
361         $otherdiffs->pushContent(Button($args, $label[$other]));
362     }
363     $html->pushContent($otherdiffs);
364
365     if ($old and $old->getVersion() == 0)
366         $old = false;
367
368     $html->pushContent(HTML::Table(PageInfoRow(_("Newer page:"), $new,
369                                                $request, empty($version)),
370                                    PageInfoRow(_("Older page:"), $old,
371                                                $request, false)));
372
373     if ($new && $old) {
374         $diff = new Diff($old->getContent(), $new->getContent());
375
376         if ($diff->isEmpty()) {
377             $html->pushContent(HTML::hr(),
378                                HTML::p('[', _("Versions are identical"),
379                                        ']'));
380         }
381         else {
382             // New CSS formatted unified diffs (ugly in NS4).
383             $fmt = new HtmlUnifiedDiffFormatter;
384
385             // Use this for old table-formatted diffs.
386             //$fmt = new TableUnifiedDiffFormatter;
387             $html->pushContent($fmt->format($diff));
388         }
389
390         $html->pushContent(HTML::hr(), HTML::h1($new_version));
391         include_once("lib/BlockParser.php");
392         $html->pushContent(TransformText($new,$new->get('markup'),$page));
393     }
394
395     include_once('lib/Template.php');
396     GeneratePage($html, sprintf(_("Diff: %s"), $pagename), $new);
397 }
398
399 // $Log: not supported by cvs2svn $
400 // Revision 1.53  2006/12/02 13:58:27  rurban
401 // Fix MonoBook layout (id content forbidden here)
402 // Add new revision to the bottom of the diff as in mediawiki.
403 //
404 // Revision 1.52  2005/04/01 14:45:14  rurban
405 // fix dirty side-effect: dont printf too early bypassing ob_buffering.
406 // fixes MSIE.
407 //
408 // Revision 1.51  2005/02/04 15:26:57  rurban
409 // need div=content for blog
410 //
411 // Revision 1.50  2005/02/04 13:44:45  rurban
412 // prevent from php5 nameclash
413 //
414 // Revision 1.49  2004/11/21 11:59:19  rurban
415 // remove final \n to be ob_cache independent
416 //
417 // Revision 1.48  2004/06/14 11:31:36  rurban
418 // renamed global $Theme to $WikiTheme (gforge nameclash)
419 // inherit PageList default options from PageList
420 //   default sortby=pagename
421 // use options in PageList_Selectable (limit, sortby, ...)
422 // added action revert, with button at action=diff
423 // added option regex to WikiAdminSearchReplace
424 //
425 // Revision 1.47  2004/06/08 13:51:57  rurban
426 // some comments only
427 //
428 // Revision 1.46  2004/05/01 15:59:29  rurban
429 // nothing changed
430 //
431 // Revision 1.45  2004/01/25 03:57:15  rurban
432 // use isWikiWord()
433 //
434 // Revision 1.44  2003/02/17 02:17:31  dairiki
435 // Fix so that action=diff will work when the most recent version
436 // of a page has been "deleted".
437 //
438 // Revision 1.43  2003/01/29 19:17:37  carstenklapp
439 // Bugfix for &nbsp showing on diff page.
440 //
441 // Revision 1.42  2003/01/11 23:05:04  carstenklapp
442 // Tweaked diff formatting.
443 //
444 // Revision 1.41  2003/01/08 02:23:02  carstenklapp
445 // Don't perform a diff when the page doesn't exist (such as a
446 // nonexistant calendar day/sub-page)
447 //
448
449 // Local Variables:
450 // mode: php
451 // tab-width: 8
452 // c-basic-offset: 4
453 // c-hanging-comment-ender-p: nil
454 // indent-tabs-mode: nil
455 // End:
456 ?>