2 rcs_id('$Id: diff.php,v 1.28 2002-01-22 03:17:47 dairiki 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) {
24 $this->_line[] = ( $this->_tag
25 ? new HtmlElement($this->_tag, $this->_group)
29 $this->_tag = $new_tag;
32 function _flushLine ($new_tag) {
33 $this->_flushGroup($new_tag);
34 if ($this->_line !== false)
35 $this->_lines[] = $this->_line;
36 $this->_line = array();
39 function addWords ($words, $tag = '') {
40 if ($tag != $this->_tag)
41 $this->_flushGroup($tag);
43 foreach ($words as $word) {
44 // new-line should only come as first char of word.
47 if ($word[0] == "\n") {
48 $this->_group .= NBSP;
49 $this->_flushLine($tag);
50 $word = substr($word, 1);
52 assert(!strstr($word, "\n"));
53 $this->_group .= $word;
58 $this->_flushLine('~done');
63 class WordLevelDiff extends MappedDiff
65 function WordLevelDiff ($orig_lines, $final_lines) {
66 list ($orig_words, $orig_stripped) = $this->_split($orig_lines);
67 list ($final_words, $final_stripped) = $this->_split($final_lines);
70 $this->MappedDiff($orig_words, $final_words,
71 $orig_stripped, $final_stripped);
74 function _split($lines) {
75 // FIXME: fix POSIX char class.
76 if (!preg_match_all('/ ( [^\S\n]+ | [[:alnum:]]+ | . ) (?: (?!< \n) [^\S\n])? /xs',
77 implode("\n", $lines),
79 return array(array(''), array(''));
81 return array($m[0], $m[1]);
85 $orig = new _HWLDF_WordAccumulator;
87 foreach ($this->edits as $edit) {
88 if ($edit->type == 'copy')
89 $orig->addWords($edit->orig);
91 $orig->addWords($edit->orig, 'del');
93 return $orig->getLines();
97 $final = new _HWLDF_WordAccumulator;
99 foreach ($this->edits as $edit) {
100 if ($edit->type == 'copy')
101 $final->addWords($edit->final);
102 elseif ($edit->final)
103 $final->addWords($edit->final, 'ins');
105 return $final->getLines();
111 * HTML unified diff formatter.
113 * This class formats a diff into a CSS-based
114 * unified diff format.
116 * Within groups of changed lines, diffs are highlit
117 * at the character-diff level.
119 class HtmlUnifiedDiffFormatter extends UnifiedDiffFormatter
121 function HtmlUnifiedDiffFormatter($context_lines = 4) {
122 $this->UnifiedDiffFormatter($context_lines);
125 function _start_diff() {
126 $this->_top = HTML::div(array('class' => 'diff'));
128 function _end_diff() {
134 function _start_block($header) {
135 $this->_block = HTML::div(array('class' => 'block'),
139 function _end_block() {
140 $this->_top->pushContent($this->_block);
141 unset($this->_block);
144 function _lines($lines, $class, $prefix = NBSP, $elem = false) {
146 foreach ($lines as $line) {
148 $line = new HtmlElement($elem, $line);
149 $this->_block->pushContent(HTML::div(array('class' => $class),
150 HTML::tt(array('class' => 'prefix'),
156 function _context($lines) {
157 $this->_lines($lines, 'context');
159 function _deleted($lines) {
160 $this->_lines($lines, 'deleted', '-', 'del');
163 function _added($lines) {
164 $this->_lines($lines, 'added', '+', 'ins');
167 function _changed($orig, $final) {
168 $diff = new WordLevelDiff($orig, $final);
169 $this->_lines($diff->orig(), 'original', '-');
170 $this->_lines($diff->final(), 'final', '+');
175 * HTML table-based unified diff formatter.
177 * This class formats a diff into a table-based
178 * unified diff format. (Similar to what was produced
179 * by previous versions of PhpWiki.)
181 * Within groups of changed lines, diffs are highlit
182 * at the character-diff level.
184 class TableUnifiedDiffFormatter extends HtmlUnifiedDiffFormatter
186 function TableUnifiedDiffFormatter($context_lines = 4) {
187 $this->HtmlUnifiedDiffFormatter($context_lines);
190 function _start_diff() {
191 $this->_top = HTML::table(array('width' => '100%',
198 function _start_block($header) {
199 $this->_block = HTML::table(array('width' => '100%',
204 HTML::tr(HTML::td(array('colspan' => 2),
205 HTML::tt($header))));
208 function _end_block() {
209 $this->_top->pushContent(HTML::tr(HTML::td($this->_block)));
210 unset($this->_block);
213 function _lines($lines, $class, $prefix = NBSP, $elem = false) {
214 $prefix = HTML::td(array('class' => 'prefix', 'width' => "1%"), $prefix);
215 foreach ($lines as $line) {
219 $line = new HtmlElement($elem, $line);
220 $this->_block->pushContent(HTML::tr(array('valign' => 'top'),
222 HTML::td(array('class' => $class),
229 /////////////////////////////////////////////////////////////////
231 function PageInfoRow ($pagename, $label, $rev)
235 $row = HTML::tr(HTML::td(array('align' => 'right'), $label));
237 $url = WikiURL($pagename, array('version' => $rev->getVersion()));
238 $linked_version = HTML::a(array('href' => $url), $rev->getVersion());
239 $row->pushContent(HTML::td(fmt("version %s",$linked_version)),
240 HTML::td(fmt("last modified on %s",
241 $Theme->formatDateTime($rev->get('mtime')))),
242 HTML::td(fmt("by %s", $rev->get('author'))));
244 $row->pushContent(HTML::td(array('colspan' => '3'), _("None")));
249 function showDiff ($dbi, $request) {
250 $pagename = $request->getArg('pagename');
251 if (is_array($versions = $request->getArg('versions'))) {
252 // Version selection from pageinfo.php display:
254 list ($version, $previous) = $versions;
257 $version = $request->getArg('version');
258 $previous = $request->getArg('previous');
261 $page = $dbi->getPage($pagename);
263 if (!($new = $page->getRevision($version)))
264 NoSuchRevision($page, $version);
265 $new_version = fmt("version %d", $version);
268 $new = $page->getCurrentRevision();
269 $new_version = _("current version");
272 if (preg_match('/^\d+$/', $previous)) {
273 if ( !($old = $page->getRevision($previous)) )
274 NoSuchRevision($page, $previous);
275 $old_version = fmt("version %d", $previous);
276 $others = array('major', 'minor', 'author');
282 while ($old = $page->getRevisionBefore($old)) {
283 if (! $old->get('is_minor_edit'))
286 $old_version = _("previous major revision");
287 $others = array('minor', 'author');
291 while ($old = $page->getRevisionBefore($old)) {
292 if ($old->get('author') != $new->get('author'))
295 $old_version = _("revision by previous author");
296 $others = array('major', 'minor');
301 $old = $page->getRevisionBefore($new);
302 $old_version = _("previous revision");
303 $others = array('major', 'author');
308 $new_url = WikiURL($pagename, array('version' => $new->getVersion()));
309 $new_link = HTML::a(array('href' => $new_url), $new_version);
310 $old_url = WikiURL($pagename, array('version' => $old ? $old->getVersion() : 0));
311 $old_link = HTML::a(array('href' => $old_url), $old_version);
312 $page_link = LinkExistingWikiWord($pagename);
314 $html[] = HTML::p(fmt("Differences between %s and %s of %s.",
315 $new_link, $old_link, $page_link));
317 $otherdiffs = HTML::p(_("Other diffs:"));
318 $label = array('major' => _("Previous Major Revision"),
319 'minor' => _("Previous Revision"),
320 'author'=> _("Previous Author"));
321 foreach ($others as $other) {
322 $args = array('action' => 'diff', 'previous' => $other);
324 $args['version'] = $version;
325 if (count($otherdiffs->getContent()) > 1)
326 $otherdiffs->pushContent(", ");
328 $otherdiffs->pushContent(" ");
329 $otherdiffs->pushContent(HTML::a(array('href' => WikiURL($pagename, $args),
330 'class' => 'wikiaction'),
333 $html[] = $otherdiffs;
336 if ($old and $old->getVersion() == 0)
339 $html[] = HTML::Table(PageInfoRow($pagename, _("Newer page:"), $new),
340 PageInfoRow($pagename, _("Older page:"), $old));
343 $diff = new Diff($old->getContent(), $new->getContent());
345 if ($diff->isEmpty()) {
346 $html[] = HTML::hr();
347 $html[] = HTML::p('[', _("Versions are identical"), ']');
350 // New CSS formatted unified diffs (ugly in NS4).
351 $fmt = new HtmlUnifiedDiffFormatter;
353 // Use this for old table-formatted diffs.
354 //$fmt = new TableUnifiedDiffFormatter;
355 $html[] = $fmt->format($diff);
359 include_once('lib/Template.php');
360 echo GeneratePage('MESSAGE', $html,
361 sprintf(_("Diff: %s"), $pagename), $new);
368 // c-hanging-comment-ender-p: nil
369 // indent-tabs-mode: nil