2 rcs_id('$Id: diff.php,v 1.40 2002-10-31 03:28:30 carstenklapp Exp $');
5 // PhpWiki diff output code.
7 // Copyright (C) 2000, 2001 Geoffrey T. Dairiki <dairiki@dairiki.org>
8 // You may copy this code freely under the conditions of the GPL.
11 require_once('lib/difflib.php');
12 require_once('lib/HtmlElement.php');
14 class _HWLDF_WordAccumulator {
15 function _HWLDF_WordAccumulator () {
16 $this->_lines = array();
18 $this->_group = false;
19 $this->_tag = '~begin';
22 function _flushGroup ($new_tag) {
23 if ($this->_group !== false) {
25 $this->_line = HTML();
26 $this->_line->pushContent( $this->_tag
27 ? new HtmlElement($this->_tag, $this->_group)
31 $this->_tag = $new_tag;
34 function _flushLine ($new_tag) {
35 $this->_flushGroup($new_tag);
37 $this->_lines[] = $this->_line;
38 $this->_line = HTML();
41 function addWords ($words, $tag = '') {
42 if ($tag != $this->_tag)
43 $this->_flushGroup($tag);
45 foreach ($words as $word) {
46 // new-line should only come as first char of word.
49 if ($word[0] == "\n") {
50 $this->_group .= " "; // FIXME to use HTML::raw(' ')
51 $this->_flushLine($tag);
52 $word = substr($word, 1);
54 assert(!strstr($word, "\n"));
55 $this->_group .= $word;
60 $this->_flushLine('~done');
65 class WordLevelDiff extends MappedDiff
67 function WordLevelDiff ($orig_lines, $final_lines) {
68 list ($orig_words, $orig_stripped) = $this->_split($orig_lines);
69 list ($final_words, $final_stripped) = $this->_split($final_lines);
72 $this->MappedDiff($orig_words, $final_words,
73 $orig_stripped, $final_stripped);
76 function _split($lines) {
77 // FIXME: fix POSIX char class.
78 if (!preg_match_all('/ ( [^\S\n]+ | [[:alnum:]]+ | . ) (?: (?!< \n) [^\S\n])? /xs',
79 implode("\n", $lines),
81 return array(array(''), array(''));
83 return array($m[0], $m[1]);
87 $orig = new _HWLDF_WordAccumulator;
89 foreach ($this->edits as $edit) {
90 if ($edit->type == 'copy')
91 $orig->addWords($edit->orig);
93 $orig->addWords($edit->orig, 'del');
95 return $orig->getLines();
99 $final = new _HWLDF_WordAccumulator;
101 foreach ($this->edits as $edit) {
102 if ($edit->type == 'copy')
103 $final->addWords($edit->final);
104 elseif ($edit->final)
105 $final->addWords($edit->final, 'ins');
107 return $final->getLines();
113 * HTML unified diff formatter.
115 * This class formats a diff into a CSS-based
116 * unified diff format.
118 * Within groups of changed lines, diffs are highlit
119 * at the character-diff level.
121 class HtmlUnifiedDiffFormatter extends UnifiedDiffFormatter
123 function HtmlUnifiedDiffFormatter($context_lines = 4) {
124 $this->UnifiedDiffFormatter($context_lines);
127 function _start_diff() {
128 $this->_top = HTML::div(array('class' => 'diff'));
130 function _end_diff() {
136 function _start_block($header) {
137 $this->_block = HTML::div(array('class' => 'block'),
141 function _end_block() {
142 $this->_top->pushContent($this->_block);
143 unset($this->_block);
146 function _lines($lines, $class, $prefix = false, $elem = false) {
148 $prefix = HTML::raw(' ');
149 foreach ($lines as $line) {
151 $line = new HtmlElement($elem, $line);
153 $this->_block->pushContent(HTML::div(array('class' => $class),
154 HTML::tt(array('class' => 'prefix'),
156 $line, HTML::raw(' ')));
160 function _context($lines) {
161 $this->_lines($lines, 'context');
163 function _deleted($lines) {
164 $this->_lines($lines, 'deleted', '-', 'del');
167 function _added($lines) {
168 $this->_lines($lines, 'added', '+', 'ins');
171 function _changed($orig, $final) {
172 $diff = new WordLevelDiff($orig, $final);
173 $this->_lines($diff->orig(), 'original', '-');
174 $this->_lines($diff->final(), 'final', '+');
179 * HTML table-based unified diff formatter.
181 * This class formats a diff into a table-based
182 * unified diff format. (Similar to what was produced
183 * by previous versions of PhpWiki.)
185 * Within groups of changed lines, diffs are highlit
186 * at the character-diff level.
188 class TableUnifiedDiffFormatter extends HtmlUnifiedDiffFormatter
190 function TableUnifiedDiffFormatter($context_lines = 4) {
191 $this->HtmlUnifiedDiffFormatter($context_lines);
194 function _start_diff() {
195 $this->_top = HTML::table(array('width' => '100%',
202 function _start_block($header) {
203 $this->_block = HTML::table(array('width' => '100%',
208 HTML::tr(HTML::td(array('colspan' => 2),
209 HTML::tt($header))));
212 function _end_block() {
213 $this->_top->pushContent(HTML::tr(HTML::td($this->_block)));
214 unset($this->_block);
217 function _lines($lines, $class, $prefix = false, $elem = false) {
219 $prefix = HTML::raw(' ');
220 $prefix = HTML::td(array('class' => 'prefix', 'width' => "1%"), $prefix);
221 foreach ($lines as $line) {
223 $line = HTML::raw(' ');
225 $line = new HtmlElement($elem, $line);
226 $this->_block->pushContent(HTML::tr(array('valign' => 'top'),
228 HTML::td(array('class' => $class),
235 /////////////////////////////////////////////////////////////////
237 function PageInfoRow ($label, $rev, &$request)
239 global $Theme, $WikiNameRegexp;
241 $row = HTML::tr(HTML::td(array('align' => 'right'), $label));
243 $author = $rev->get('author');
244 $dbi = $request->getDbh();
246 $iswikipage = (preg_match("/^$WikiNameRegexp\$/", $author) && $dbi->isWikiPage($author));
247 $authorlink = $iswikipage ? WikiLink($author) : $author;
249 $linked_version = WikiLink($rev, 'existing', $rev->getVersion());
250 $row->pushContent(HTML::td(fmt("version %s", $linked_version)),
251 HTML::td($Theme->getLastModifiedMessage($rev, false)),
252 HTML::td(fmt("by %s", $authorlink)));
254 $row->pushContent(HTML::td(array('colspan' => '3'), _("None")));
259 function showDiff (&$request) {
260 $pagename = $request->getArg('pagename');
261 if (is_array($versions = $request->getArg('versions'))) {
262 // Version selection from pageinfo.php display:
264 list ($version, $previous) = $versions;
267 $version = $request->getArg('version');
268 $previous = $request->getArg('previous');
271 $page = $request->getPage();
274 if (!($new = $page->getRevision($version)))
275 NoSuchRevision($request, $page, $version);
276 $new_version = fmt("version %d", $version);
279 $new = $page->getCurrentRevision();
280 $new_version = _("current version");
283 if (preg_match('/^\d+$/', $previous)) {
284 if ( !($old = $page->getRevision($previous)) )
285 NoSuchRevision($request, $page, $previous);
286 $old_version = fmt("version %d", $previous);
287 $others = array('major', 'minor', 'author');
293 while ($old = $page->getRevisionBefore($old)) {
294 if ($old->get('author') != $new->get('author'))
297 $old_version = _("revision by previous author");
298 $others = array('major', 'minor');
302 $old = $page->getRevisionBefore($new);
303 $old_version = _("previous revision");
304 $others = array('major', 'author');
309 while ($old && $old->get('is_minor_edit'))
310 $old = $page->getRevisionBefore($old);
312 $old = $page->getRevisionBefore($old);
313 $old_version = _("predecessor to the previous major change");
314 $others = array('minor', 'author');
319 $new_link = WikiLink($new, '', $new_version);
320 $old_link = $old ? WikiLink($old, '', $old_version) : $old_version;
321 $page_link = WikiLink($page);
323 $html = HTML(HTML::p(fmt("Differences between %s and %s of %s.",
324 $new_link, $old_link, $page_link)));
326 $otherdiffs = HTML::p(_("Other diffs:"));
327 $label = array('major' => _("Previous Major Revision"),
328 'minor' => _("Previous Revision"),
329 'author'=> _("Previous Author"));
330 foreach ($others as $other) {
331 $args = array('action' => 'diff', 'previous' => $other);
333 $args['version'] = $version;
334 if (count($otherdiffs->getContent()) > 1)
335 $otherdiffs->pushContent(", ");
337 $otherdiffs->pushContent(" ");
338 $otherdiffs->pushContent(Button($args, $label[$other]));
340 $html->pushContent($otherdiffs);
343 if ($old and $old->getVersion() == 0)
346 $html->pushContent(HTML::Table(PageInfoRow(_("Newer page:"), $new, $request),
347 PageInfoRow(_("Older page:"), $old, $request)));
350 $diff = new Diff($old->getContent(), $new->getContent());
352 if ($diff->isEmpty()) {
353 $html->pushContent(HTML::hr(),
354 HTML::p('[', _("Versions are identical"), ']'));
357 // New CSS formatted unified diffs (ugly in NS4).
358 $fmt = new HtmlUnifiedDiffFormatter;
360 // Use this for old table-formatted diffs.
361 //$fmt = new TableUnifiedDiffFormatter;
362 $html->pushContent($fmt->format($diff));
366 include_once('lib/Template.php');
367 GeneratePage($html, sprintf(_("Diff: %s"), $pagename), $new);
374 // c-hanging-comment-ender-p: nil
375 // indent-tabs-mode: nil