]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/diff.php
Allow bold, italics or underlined for numbers
[SourceForge/phpwiki.git] / lib / diff.php
1 <?php
2
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 class _HWLDF_WordAccumulator
14 {
15     function _HWLDF_WordAccumulator()
16     {
17         $this->_lines = array();
18         $this->_line = false;
19         $this->_group = false;
20         $this->_tag = '~begin';
21     }
22
23     function _flushGroup($new_tag)
24     {
25         if ($this->_group !== false) {
26             if (!$this->_line)
27                 $this->_line = HTML();
28             $this->_line->pushContent($this->_tag
29                 ? new HtmlElement($this->_tag,
30                     $this->_group)
31                 : $this->_group);
32         }
33         $this->_group = '';
34         $this->_tag = $new_tag;
35     }
36
37     function _flushLine($new_tag)
38     {
39         $this->_flushGroup($new_tag);
40         if ($this->_line)
41             $this->_lines[] = $this->_line;
42         $this->_line = HTML();
43     }
44
45     function addWords($words, $tag = '')
46     {
47         if ($tag != $this->_tag)
48             $this->_flushGroup($tag);
49
50         foreach ($words as $word) {
51             // new-line should only come as first char of word.
52             if ($word === "")
53                 continue;
54             if ($word[0] == "\n") {
55                 $this->_group .= " ";
56                 $this->_flushLine($tag);
57                 $word = substr($word, 1);
58             }
59             assert(!strstr($word, "\n"));
60             $this->_group .= $word;
61         }
62     }
63
64     function getLines()
65     {
66         $this->_flushLine('~done');
67         return $this->_lines;
68     }
69 }
70
71 class WordLevelDiff extends MappedDiff
72 {
73     function WordLevelDiff($orig_lines, $final_lines)
74     {
75         list ($orig_words, $orig_stripped) = $this->_split($orig_lines);
76         list ($final_words, $final_stripped) = $this->_split($final_lines);
77
78         $this->MappedDiff($orig_words, $final_words,
79             $orig_stripped, $final_stripped);
80     }
81
82     function _split($lines)
83     {
84         // FIXME: fix POSIX char class.
85         if (!preg_match_all('/ ( [^\S\n]+ | [[:alnum:]]+ | . ) (?: (?!< \n) [^\S\n])? /xs',
86             implode("\n", $lines),
87             $m)
88         ) {
89             return array(array(''), array(''));
90         }
91         return array($m[0], $m[1]);
92     }
93
94     function orig()
95     {
96         $orig = new _HWLDF_WordAccumulator;
97
98         foreach ($this->edits as $edit) {
99             if ($edit->type == 'copy')
100                 $orig->addWords($edit->orig);
101             elseif ($edit->orig)
102                 $orig->addWords($edit->orig, 'del');
103         }
104         return $orig->getLines();
105     }
106
107     function _final()
108     {
109         $final = new _HWLDF_WordAccumulator;
110
111         foreach ($this->edits as $edit) {
112             if ($edit->type == 'copy')
113                 $final->addWords($edit->final);
114             elseif ($edit->final)
115                 $final->addWords($edit->final, 'ins');
116         }
117         return $final->getLines();
118     }
119 }
120
121 /**
122  * HTML unified diff formatter.
123  *
124  * This class formats a diff into a CSS-based
125  * unified diff format.
126  *
127  * Within groups of changed lines, diffs are highlit
128  * at the character-diff level.
129  */
130 class HtmlUnifiedDiffFormatter extends UnifiedDiffFormatter
131 {
132     function HtmlUnifiedDiffFormatter($context_lines = 4)
133     {
134         $this->UnifiedDiffFormatter($context_lines);
135     }
136
137     function _start_diff()
138     {
139         $this->_top = HTML::div(array('class' => 'diff'));
140     }
141
142     function _end_diff()
143     {
144         $val = $this->_top;
145         unset($this->_top);
146         return $val;
147     }
148
149     function _start_block($header)
150     {
151         $this->_block = HTML::div(array('class' => 'block'),
152             HTML::tt($header));
153     }
154
155     function _end_block()
156     {
157         $this->_top->pushContent($this->_block);
158         unset($this->_block);
159     }
160
161     function _lines($lines, $class, $prefix = false, $elem = false)
162     {
163         if (!$prefix)
164             $prefix = HTML::raw('&nbsp;');
165         $div = HTML::div(array('class' => 'difftext'));
166         foreach ($lines as $line) {
167             if ($elem)
168                 $line = new HtmlElement($elem, $line);
169             $div->pushContent(HTML::div(array('class' => $class),
170                 HTML::tt(array('class' => 'prefix'),
171                     $prefix),
172                 $line, HTML::raw('&nbsp;')));
173         }
174         $this->_block->pushContent($div);
175     }
176
177     function _context($lines)
178     {
179         $this->_lines($lines, 'context');
180     }
181
182     function _deleted($lines)
183     {
184         $this->_lines($lines, 'deleted', '-', 'del');
185     }
186
187     function _added($lines)
188     {
189         $this->_lines($lines, 'added', '+', 'ins');
190     }
191
192     function _changed($orig, $final)
193     {
194         $diff = new WordLevelDiff($orig, $final);
195         $this->_lines($diff->orig(), 'original', '-');
196         $this->_lines($diff->_final(), 'final', '+');
197     }
198 }
199
200 /////////////////////////////////////////////////////////////////
201
202 function PageInfoRow($label, $rev, &$request, $is_current = false)
203 {
204     global $WikiTheme;
205
206     $row = HTML::tr(HTML::td(array('align' => 'right'), $label));
207     if ($rev) {
208         $author = $rev->get('author');
209         $dbi = $request->getDbh();
210
211         $iswikipage = (isWikiWord($author) && $dbi->isWikiPage($author));
212         $authorlink = $iswikipage ? WikiLink($author) : $author;
213         $version = $rev->getVersion();
214         $linked_version = WikiLink($rev, 'existing', $version);
215         if ($is_current)
216             $revertbutton = HTML();
217         else
218             $revertbutton = $WikiTheme->makeActionButton(array('action' => 'revert',
219                     'version' => $version),
220                 false, $rev);
221         $row->pushContent(HTML::td(fmt("version %s", $linked_version)),
222             HTML::td($WikiTheme->getLastModifiedMessage($rev,
223                 false)),
224             HTML::td(fmt("by %s", $authorlink)),
225             HTML::td($revertbutton)
226         );
227     } else {
228         $row->pushContent(HTML::td(array('colspan' => '4'), _("None")));
229     }
230     return $row;
231 }
232
233 function showDiff(&$request)
234 {
235     $pagename = $request->getArg('pagename');
236     if (is_array($versions = $request->getArg('versions'))) {
237         // Version selection from pageinfo.php display:
238         rsort($versions);
239         list ($version, $previous) = $versions;
240     } else {
241         $version = $request->getArg('version');
242         $previous = $request->getArg('previous');
243     }
244
245     // abort if page doesn't exist
246     $dbi = $request->getDbh();
247     $page = $request->getPage();
248     $current = $page->getCurrentRevision(false);
249     if ($current->getVersion() < 1) {
250         $html = HTML::div(array('class' => 'wikitext', 'id' => 'difftext'),
251             HTML::p(fmt("Page ā€œ%sā€ does not exist.", WikiLink($pagename, 'unknown'))));
252         require_once 'lib/Template.php';
253         GeneratePage($html, sprintf(_("Diff: %s"), $pagename), false);
254         return; //early return
255     }
256
257     if ($version) {
258         if (!($new = $page->getRevision($version)))
259             NoSuchRevision($request, $page, $version);
260         $new_version = fmt("version %d", $version);
261     } else {
262         $new = $current;
263         $new_version = _("current version");
264     }
265
266     if (preg_match('/^\d+$/', $previous)) {
267         if (!($old = $page->getRevision($previous)))
268             NoSuchRevision($request, $page, $previous);
269         $old_version = fmt("version %d", $previous);
270         $others = array('major', 'minor', 'author');
271     } else {
272         switch ($previous) {
273             case 'author':
274                 $old = $new;
275                 while ($old = $page->getRevisionBefore($old)) {
276                     if ($old->get('author') != $new->get('author'))
277                         break;
278                 }
279                 $old_version = _("revision by previous author");
280                 $others = array('major', 'minor');
281                 break;
282             case 'minor':
283                 $previous = 'minor';
284                 $old = $page->getRevisionBefore($new);
285                 $old_version = _("previous revision");
286                 $others = array('major', 'author');
287                 break;
288             case 'major':
289             default:
290                 $old = $new;
291                 while ($old && $old->get('is_minor_edit'))
292                     $old = $page->getRevisionBefore($old);
293                 if ($old)
294                     $old = $page->getRevisionBefore($old);
295                 $old_version = _("predecessor to the previous major change");
296                 $others = array('minor', 'author');
297                 break;
298         }
299     }
300
301     $new_link = WikiLink($new, '', $new_version);
302     $old_link = $old ? WikiLink($old, '', $old_version) : $old_version;
303     $page_link = WikiLink($page);
304
305     $html = HTML::div(array('class' => 'wikitext', 'id' => 'difftext'),
306         HTML::p(fmt("Differences between %s and %s of %s.",
307             $new_link, $old_link, $page_link)));
308
309     $otherdiffs = HTML::p(_("Other diffs:"));
310     $label = array('major' => _("Previous Major Revision"),
311         'minor' => _("Previous Revision"),
312         'author' => _("Previous Author"));
313     foreach ($others as $other) {
314         $args = array('action' => 'diff', 'previous' => $other);
315         if ($version)
316             $args['version'] = $version;
317         if (count($otherdiffs->getContent()) > 1)
318             $otherdiffs->pushContent(", ");
319         else
320             $otherdiffs->pushContent(" ");
321         $otherdiffs->pushContent(Button($args, $label[$other]));
322     }
323     $html->pushContent($otherdiffs);
324
325     if ($old and $old->getVersion() == 0)
326         $old = false;
327
328     $html->pushContent(HTML::Table(PageInfoRow(_("Newer page:"), $new,
329             $request, empty($version)),
330         PageInfoRow(_("Older page:"), $old,
331             $request, false)));
332
333     if ($new && $old) {
334         $diff = new Diff($old->getContent(), $new->getContent());
335
336         if ($diff->isEmpty()) {
337             $html->pushContent(HTML::hr(),
338                 HTML::p(sprintf(_('Content of versions %1$s and %2$s is identical.'),
339                     $old->getVersion(),
340                     $new->getVersion())));
341             // If two consecutive versions have the same content, it is because the page was
342             // renamed, or metadata changed: ACL, owner, markup.
343             // We give the reason by printing the summary.
344             if (($new->getVersion() - $old->getVersion()) == 1) {
345                 $html->pushContent(HTML::p(sprintf(_('Version %1$s was created because: %2$s'),
346                     $new->getVersion(),
347                     $new->get('summary'))));
348             }
349         } else {
350             $fmt = new HtmlUnifiedDiffFormatter;
351             $html->pushContent($fmt->format($diff));
352         }
353
354         $html->pushContent(HTML::hr(), HTML::h2($new_version));
355         require_once 'lib/BlockParser.php';
356         $html->pushContent(TransformText($new, $pagename));
357     }
358
359     require_once 'lib/Template.php';
360     GeneratePage($html, 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: