]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/diff.php
Fix MonoBook layout (id content forbidden here)
[SourceForge/phpwiki.git] / lib / diff.php
1 <?php // -*-php-*-
2 rcs_id('$Id: diff.php,v 1.53 2006-12-02 13:58:27 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
240 function PageInfoRow ($label, $rev, &$request, $is_current = false)
241 {
242     global $WikiTheme;
243
244     $row = HTML::tr(HTML::td(array('align' => 'right'), $label));
245     if ($rev) {
246         $author = $rev->get('author');
247         $dbi = $request->getDbh();
248
249         $iswikipage = (isWikiWord($author) && $dbi->isWikiPage($author));
250         $authorlink = $iswikipage ? WikiLink($author) : $author;
251         $version = $rev->getVersion();
252         $linked_version = WikiLink($rev, 'existing', $version);
253         if ($is_current)
254             $revertbutton = HTML();
255         else
256             $revertbutton = $WikiTheme->makeActionButton(array('action' => 'revert',
257                                                                'version' => $version),
258                                                          false, $rev);
259         $row->pushContent(HTML::td(fmt("version %s", $linked_version)),
260                           HTML::td($WikiTheme->getLastModifiedMessage($rev,
261                                                                       false)),
262                           HTML::td(fmt("by %s", $authorlink)),
263                           HTML::td($revertbutton)
264                           );
265     } else {
266         $row->pushContent(HTML::td(array('colspan' => '4'), _("None")));
267     }
268     return $row;
269 }
270
271 function showDiff (&$request) {
272     $pagename = $request->getArg('pagename');
273     if (is_array($versions = $request->getArg('versions'))) {
274         // Version selection from pageinfo.php display:
275         rsort($versions);
276         list ($version, $previous) = $versions;
277     }
278     else {
279         $version = $request->getArg('version');
280         $previous = $request->getArg('previous');
281     }
282  
283     // abort if page doesn't exist
284     $dbi = $request->getDbh();
285     $page = $request->getPage();
286     $current = $page->getCurrentRevision();
287     if ($current->getVersion() < 1) {
288         $html = HTML::div(array('class'=>'wikitext','id'=>'difftext'),
289                           HTML::p(fmt("I'm sorry, there is no such page as %s.",
290                                       WikiLink($pagename, 'unknown'))));
291         include_once('lib/Template.php');
292         GeneratePage($html, sprintf(_("Diff: %s"), $pagename), false);
293         return; //early return
294     }
295
296     if ($version) {
297         if (!($new = $page->getRevision($version)))
298             NoSuchRevision($request, $page, $version);
299         $new_version = fmt("version %d", $version);
300     }
301     else {
302         $new = $current;
303         $new_version = _("current version");
304     }
305
306     if (preg_match('/^\d+$/', $previous)) {
307         if ( !($old = $page->getRevision($previous)) )
308             NoSuchRevision($request, $page, $previous);
309         $old_version = fmt("version %d", $previous);
310         $others = array('major', 'minor', 'author');
311     }
312     else {
313         switch ($previous) {
314         case 'author':
315             $old = $new;
316             while ($old = $page->getRevisionBefore($old)) {
317                 if ($old->get('author') != $new->get('author'))
318                     break;
319             }
320             $old_version = _("revision by previous author");
321             $others = array('major', 'minor');
322             break;
323         case 'minor':
324             $previous='minor';
325             $old = $page->getRevisionBefore($new);
326             $old_version = _("previous revision");
327             $others = array('major', 'author');
328             break;
329         case 'major':
330         default:
331             $old = $new;
332             while ($old && $old->get('is_minor_edit'))
333                 $old = $page->getRevisionBefore($old);
334             if ($old)
335                 $old = $page->getRevisionBefore($old);
336             $old_version = _("predecessor to the previous major change");
337             $others = array('minor', 'author');
338             break;
339         }
340     }
341
342     $new_link = WikiLink($new, '', $new_version);
343     $old_link = $old ? WikiLink($old, '', $old_version) : $old_version;
344     $page_link = WikiLink($page);
345
346     $html = HTML::div(array('class'=>'wikitext','id'=>'difftext'),
347                      HTML::p(fmt("Differences between %s and %s of %s.",
348                                  $new_link, $old_link, $page_link)));
349
350     $otherdiffs = HTML::p(_("Other diffs:"));
351     $label = array('major' => _("Previous Major Revision"),
352                    'minor' => _("Previous Revision"),
353                    'author'=> _("Previous Author"));
354     foreach ($others as $other) {
355         $args = array('action' => 'diff', 'previous' => $other);
356         if ($version)
357             $args['version'] = $version;
358         if (count($otherdiffs->getContent()) > 1)
359             $otherdiffs->pushContent(", ");
360         else
361             $otherdiffs->pushContent(" ");
362         $otherdiffs->pushContent(Button($args, $label[$other]));
363     }
364     $html->pushContent($otherdiffs);
365
366     if ($old and $old->getVersion() == 0)
367         $old = false;
368
369     $html->pushContent(HTML::Table(PageInfoRow(_("Newer page:"), $new,
370                                                $request, empty($version)),
371                                    PageInfoRow(_("Older page:"), $old,
372                                                $request, false)));
373
374     if ($new && $old) {
375         $diff = new Diff($old->getContent(), $new->getContent());
376
377         if ($diff->isEmpty()) {
378             $html->pushContent(HTML::hr(),
379                                HTML::p('[', _("Versions are identical"),
380                                        ']'));
381         }
382         else {
383             // New CSS formatted unified diffs (ugly in NS4).
384             $fmt = new HtmlUnifiedDiffFormatter;
385
386             // Use this for old table-formatted diffs.
387             //$fmt = new TableUnifiedDiffFormatter;
388             $html->pushContent($fmt->format($diff));
389         }
390
391         
392         $html->pushContent(HTML::hr(), HTML::h1($new_version));
393         include_once("lib/BlockParser.php");
394         $html->pushContent(TransformText($new,$new->get('markup'),$page));
395     }
396
397     include_once('lib/Template.php');
398     GeneratePage($html, sprintf(_("Diff: %s"), $pagename), $new);
399 }
400
401 // $Log: not supported by cvs2svn $
402 // Revision 1.52  2005/04/01 14:45:14  rurban
403 // fix dirty side-effect: dont printf too early bypassing ob_buffering.
404 // fixes MSIE.
405 //
406 // Revision 1.51  2005/02/04 15:26:57  rurban
407 // need div=content for blog
408 //
409 // Revision 1.50  2005/02/04 13:44:45  rurban
410 // prevent from php5 nameclash
411 //
412 // Revision 1.49  2004/11/21 11:59:19  rurban
413 // remove final \n to be ob_cache independent
414 //
415 // Revision 1.48  2004/06/14 11:31:36  rurban
416 // renamed global $Theme to $WikiTheme (gforge nameclash)
417 // inherit PageList default options from PageList
418 //   default sortby=pagename
419 // use options in PageList_Selectable (limit, sortby, ...)
420 // added action revert, with button at action=diff
421 // added option regex to WikiAdminSearchReplace
422 //
423 // Revision 1.47  2004/06/08 13:51:57  rurban
424 // some comments only
425 //
426 // Revision 1.46  2004/05/01 15:59:29  rurban
427 // nothing changed
428 //
429 // Revision 1.45  2004/01/25 03:57:15  rurban
430 // use isWikiWord()
431 //
432 // Revision 1.44  2003/02/17 02:17:31  dairiki
433 // Fix so that action=diff will work when the most recent version
434 // of a page has been "deleted".
435 //
436 // Revision 1.43  2003/01/29 19:17:37  carstenklapp
437 // Bugfix for &nbsp showing on diff page.
438 //
439 // Revision 1.42  2003/01/11 23:05:04  carstenklapp
440 // Tweaked diff formatting.
441 //
442 // Revision 1.41  2003/01/08 02:23:02  carstenklapp
443 // Don't perform a diff when the page doesn't exist (such as a
444 // nonexistant calendar day/sub-page)
445 //
446
447 // Local Variables:
448 // mode: php
449 // tab-width: 8
450 // c-basic-offset: 4
451 // c-hanging-comment-ender-p: nil
452 // indent-tabs-mode: nil
453 // End:
454 ?>