2 rcs_id('$Id: diff.php,v 1.38 2002-03-21 21:24:49 lakka 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 .= NBSP;
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 = NBSP, $elem = false) {
147 foreach ($lines as $line) {
149 $line = new HtmlElement($elem, $line);
151 $this->_block->pushContent(HTML::div(array('class' => $class),
152 HTML::tt(array('class' => 'prefix'),
158 function _context($lines) {
159 $this->_lines($lines, 'context');
161 function _deleted($lines) {
162 $this->_lines($lines, 'deleted', '-', 'del');
165 function _added($lines) {
166 $this->_lines($lines, 'added', '+', 'ins');
169 function _changed($orig, $final) {
170 $diff = new WordLevelDiff($orig, $final);
171 $this->_lines($diff->orig(), 'original', '-');
172 $this->_lines($diff->final(), 'final', '+');
177 * HTML table-based unified diff formatter.
179 * This class formats a diff into a table-based
180 * unified diff format. (Similar to what was produced
181 * by previous versions of PhpWiki.)
183 * Within groups of changed lines, diffs are highlit
184 * at the character-diff level.
186 class TableUnifiedDiffFormatter extends HtmlUnifiedDiffFormatter
188 function TableUnifiedDiffFormatter($context_lines = 4) {
189 $this->HtmlUnifiedDiffFormatter($context_lines);
192 function _start_diff() {
193 $this->_top = HTML::table(array('width' => '100%',
200 function _start_block($header) {
201 $this->_block = HTML::table(array('width' => '100%',
206 HTML::tr(HTML::td(array('colspan' => 2),
207 HTML::tt($header))));
210 function _end_block() {
211 $this->_top->pushContent(HTML::tr(HTML::td($this->_block)));
212 unset($this->_block);
215 function _lines($lines, $class, $prefix = NBSP, $elem = false) {
216 $prefix = HTML::td(array('class' => 'prefix', 'width' => "1%"), $prefix);
217 foreach ($lines as $line) {
221 $line = new HtmlElement($elem, $line);
222 $this->_block->pushContent(HTML::tr(array('valign' => 'top'),
224 HTML::td(array('class' => $class),
231 /////////////////////////////////////////////////////////////////
233 function PageInfoRow ($label, $rev, &$request)
235 global $Theme, $WikiNameRegexp;
237 $row = HTML::tr(HTML::td(array('align' => 'right'), $label));
239 $author = $rev->get('author');
240 $dbi = $request->getDbh();
242 $iswikipage = (preg_match("/^$WikiNameRegexp\$/", $author) && $dbi->isWikiPage($author));
243 $authorlink = $iswikipage ? WikiLink($author) : $author;
245 $linked_version = WikiLink($rev, 'existing', $rev->getVersion());
246 $row->pushContent(HTML::td(fmt("version %s", $linked_version)),
247 HTML::td($Theme->getLastModifiedMessage($rev, false)),
248 HTML::td(fmt("by %s", $authorlink)));
250 $row->pushContent(HTML::td(array('colspan' => '3'), _("None")));
255 function showDiff (&$request) {
256 $pagename = $request->getArg('pagename');
257 if (is_array($versions = $request->getArg('versions'))) {
258 // Version selection from pageinfo.php display:
260 list ($version, $previous) = $versions;
263 $version = $request->getArg('version');
264 $previous = $request->getArg('previous');
267 $page = $request->getPage();
270 if (!($new = $page->getRevision($version)))
271 NoSuchRevision($request, $page, $version);
272 $new_version = fmt("version %d", $version);
275 $new = $page->getCurrentRevision();
276 $new_version = _("current version");
279 if (preg_match('/^\d+$/', $previous)) {
280 if ( !($old = $page->getRevision($previous)) )
281 NoSuchRevision($request, $page, $previous);
282 $old_version = fmt("version %d", $previous);
283 $others = array('major', 'minor', 'author');
289 while ($old = $page->getRevisionBefore($old)) {
290 if ($old->get('author') != $new->get('author'))
293 $old_version = _("revision by previous author");
294 $others = array('major', 'minor');
298 $old = $page->getRevisionBefore($new);
299 $old_version = _("previous revision");
300 $others = array('major', 'author');
305 while ($old && $old->get('is_minor_edit'))
306 $old = $page->getRevisionBefore($old);
308 $old = $page->getRevisionBefore($old);
309 $old_version = _("predecessor to the previous major change");
310 $others = array('minor', 'author');
315 $new_link = WikiLink($new, '', $new_version);
316 $old_link = $old ? WikiLink($old, '', $old_version) : $old_version;
317 $page_link = WikiLink($page);
319 $html = HTML(HTML::p(fmt("Differences between %s and %s of %s.",
320 $new_link, $old_link, $page_link)));
322 $otherdiffs = HTML::p(_("Other diffs:"));
323 $label = array('major' => _("Previous Major Revision"),
324 'minor' => _("Previous Revision"),
325 'author'=> _("Previous Author"));
326 foreach ($others as $other) {
327 $args = array('action' => 'diff', 'previous' => $other);
329 $args['version'] = $version;
330 if (count($otherdiffs->getContent()) > 1)
331 $otherdiffs->pushContent(", ");
333 $otherdiffs->pushContent(" ");
334 $otherdiffs->pushContent(Button($args, $label[$other]));
336 $html->pushContent($otherdiffs);
339 if ($old and $old->getVersion() == 0)
342 $html->pushContent(HTML::Table(PageInfoRow(_("Newer page:"), $new, $request),
343 PageInfoRow(_("Older page:"), $old, $request)));
346 $diff = new Diff($old->getContent(), $new->getContent());
348 if ($diff->isEmpty()) {
349 $html->pushContent(HTML::hr(),
350 HTML::p('[', _("Versions are identical"), ']'));
353 // New CSS formatted unified diffs (ugly in NS4).
354 $fmt = new HtmlUnifiedDiffFormatter;
356 // Use this for old table-formatted diffs.
357 //$fmt = new TableUnifiedDiffFormatter;
358 $html->pushContent($fmt->format($diff));
362 include_once('lib/Template.php');
363 GeneratePage($html, sprintf(_("Diff: %s"), $pagename), $new);
370 // c-hanging-comment-ender-p: nil
371 // indent-tabs-mode: nil