2 rcs_id('$Id: diff.php,v 1.43 2003-01-29 19:17:37 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,
32 $this->_tag = $new_tag;
35 function _flushLine ($new_tag) {
36 $this->_flushGroup($new_tag);
38 $this->_lines[] = $this->_line;
39 $this->_line = HTML();
42 function addWords ($words, $tag = '') {
43 if ($tag != $this->_tag)
44 $this->_flushGroup($tag);
46 foreach ($words as $word) {
47 // new-line should only come as first char of word.
50 if ($word[0] == "\n") {
51 $this->_group .= PrintXML(HTML::raw(' '));
52 $this->_flushLine($tag);
53 $word = substr($word, 1);
55 assert(!strstr($word, "\n"));
56 $this->_group .= $word;
61 $this->_flushLine('~done');
66 class WordLevelDiff extends MappedDiff
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);
73 $this->MappedDiff($orig_words, $final_words,
74 $orig_stripped, $final_stripped);
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),
82 return array(array(''), array(''));
84 return array($m[0], $m[1]);
88 $orig = new _HWLDF_WordAccumulator;
90 foreach ($this->edits as $edit) {
91 if ($edit->type == 'copy')
92 $orig->addWords($edit->orig);
94 $orig->addWords($edit->orig, 'del');
96 return $orig->getLines();
100 $final = new _HWLDF_WordAccumulator;
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');
108 return $final->getLines();
114 * HTML unified diff formatter.
116 * This class formats a diff into a CSS-based
117 * unified diff format.
119 * Within groups of changed lines, diffs are highlit
120 * at the character-diff level.
122 class HtmlUnifiedDiffFormatter extends UnifiedDiffFormatter
124 function HtmlUnifiedDiffFormatter($context_lines = 4) {
125 $this->UnifiedDiffFormatter($context_lines);
128 function _start_diff() {
129 $this->_top = HTML::div(array('class' => 'diff'));
131 function _end_diff() {
137 function _start_block($header) {
138 $this->_block = HTML::div(array('class' => 'block'),
142 function _end_block() {
143 $this->_top->pushContent($this->_block);
144 unset($this->_block);
147 function _lines($lines, $class, $prefix = false, $elem = false) {
149 $prefix = HTML::raw(' ');
150 $div = HTML::div(array('class' => 'difftext'));
151 foreach ($lines as $line) {
153 $line = new HtmlElement($elem, $line);
154 $div->pushContent(HTML::div(array('class' => $class),
155 HTML::tt(array('class' => 'prefix'),
157 $line, HTML::raw(' ')));
159 $this->_block->pushContent($div);
162 function _context($lines) {
163 $this->_lines($lines, 'context');
165 function _deleted($lines) {
166 $this->_lines($lines, 'deleted', '-', 'del');
169 function _added($lines) {
170 $this->_lines($lines, 'added', '+', 'ins');
173 function _changed($orig, $final) {
174 $diff = new WordLevelDiff($orig, $final);
175 $this->_lines($diff->orig(), 'original', '-');
176 $this->_lines($diff->final(), 'final', '+');
181 * HTML table-based unified diff formatter.
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.)
187 * Within groups of changed lines, diffs are highlit
188 * at the character-diff level.
190 class TableUnifiedDiffFormatter extends HtmlUnifiedDiffFormatter
192 function TableUnifiedDiffFormatter($context_lines = 4) {
193 $this->HtmlUnifiedDiffFormatter($context_lines);
196 function _start_diff() {
197 $this->_top = HTML::table(array('width' => '100%',
204 function _start_block($header) {
205 $this->_block = HTML::table(array('width' => '100%',
210 HTML::tr(HTML::td(array('colspan' => 2),
211 HTML::tt($header))));
214 function _end_block() {
215 $this->_top->pushContent(HTML::tr(HTML::td($this->_block)));
216 unset($this->_block);
219 function _lines($lines, $class, $prefix = false, $elem = false) {
221 $prefix = HTML::raw(' ');
222 $prefix = HTML::td(array('class' => 'prefix',
223 'width' => "1%"), $prefix);
224 foreach ($lines as $line) {
226 $line = HTML::raw(' ');
228 $line = new HtmlElement($elem, $line);
229 $this->_block->pushContent(HTML::tr(array('valign' => 'top'),
231 HTML::td(array('class' => $class),
238 /////////////////////////////////////////////////////////////////
240 function PageInfoRow ($label, $rev, &$request)
242 global $Theme, $WikiNameRegexp;
244 $row = HTML::tr(HTML::td(array('align' => 'right'), $label));
246 $author = $rev->get('author');
247 $dbi = $request->getDbh();
249 $iswikipage = (preg_match("/^$WikiNameRegexp\$/", $author)
250 && $dbi->isWikiPage($author));
251 $authorlink = $iswikipage ? WikiLink($author) : $author;
253 $linked_version = WikiLink($rev, 'existing', $rev->getVersion());
254 $row->pushContent(HTML::td(fmt("version %s", $linked_version)),
255 HTML::td($Theme->getLastModifiedMessage($rev,
257 HTML::td(fmt("by %s", $authorlink)));
259 $row->pushContent(HTML::td(array('colspan' => '3'), _("None")));
264 function showDiff (&$request) {
265 $pagename = $request->getArg('pagename');
266 if (is_array($versions = $request->getArg('versions'))) {
267 // Version selection from pageinfo.php display:
269 list ($version, $previous) = $versions;
272 $version = $request->getArg('version');
273 $previous = $request->getArg('previous');
276 // abort if page doesn't exist
277 $dbi = $request->getDbh();
278 if (! $dbi->isWikiPage($pagename)) {
279 $html = HTML(HTML::p(fmt("I'm sorry, there is no such page as %s.",
280 WikiLink($pagename, 'unknown'))));
281 include_once('lib/Template.php');
282 GeneratePage($html, sprintf(_("Diff: %s"), $pagename), false);
283 return; //early return
286 $page = $request->getPage();
289 if (!($new = $page->getRevision($version)))
290 NoSuchRevision($request, $page, $version);
291 $new_version = fmt("version %d", $version);
294 $new = $page->getCurrentRevision();
295 $new_version = _("current version");
298 if (preg_match('/^\d+$/', $previous)) {
299 if ( !($old = $page->getRevision($previous)) )
300 NoSuchRevision($request, $page, $previous);
301 $old_version = fmt("version %d", $previous);
302 $others = array('major', 'minor', 'author');
308 while ($old = $page->getRevisionBefore($old)) {
309 if ($old->get('author') != $new->get('author'))
312 $old_version = _("revision by previous author");
313 $others = array('major', 'minor');
317 $old = $page->getRevisionBefore($new);
318 $old_version = _("previous revision");
319 $others = array('major', 'author');
324 while ($old && $old->get('is_minor_edit'))
325 $old = $page->getRevisionBefore($old);
327 $old = $page->getRevisionBefore($old);
328 $old_version = _("predecessor to the previous major change");
329 $others = array('minor', 'author');
334 $new_link = WikiLink($new, '', $new_version);
335 $old_link = $old ? WikiLink($old, '', $old_version) : $old_version;
336 $page_link = WikiLink($page);
338 $html = HTML(HTML::p(fmt("Differences between %s and %s of %s.",
339 $new_link, $old_link, $page_link)));
341 $otherdiffs = HTML::p(_("Other diffs:"));
342 $label = array('major' => _("Previous Major Revision"),
343 'minor' => _("Previous Revision"),
344 'author'=> _("Previous Author"));
345 foreach ($others as $other) {
346 $args = array('action' => 'diff', 'previous' => $other);
348 $args['version'] = $version;
349 if (count($otherdiffs->getContent()) > 1)
350 $otherdiffs->pushContent(", ");
352 $otherdiffs->pushContent(" ");
353 $otherdiffs->pushContent(Button($args, $label[$other]));
355 $html->pushContent($otherdiffs);
358 if ($old and $old->getVersion() == 0)
361 $html->pushContent(HTML::Table(PageInfoRow(_("Newer page:"), $new,
363 PageInfoRow(_("Older page:"), $old,
367 $diff = new Diff($old->getContent(), $new->getContent());
369 if ($diff->isEmpty()) {
370 $html->pushContent(HTML::hr(),
371 HTML::p('[', _("Versions are identical"),
375 // New CSS formatted unified diffs (ugly in NS4).
376 $fmt = new HtmlUnifiedDiffFormatter;
378 // Use this for old table-formatted diffs.
379 //$fmt = new TableUnifiedDiffFormatter;
380 $html->pushContent($fmt->format($diff));
384 include_once('lib/Template.php');
385 GeneratePage($html, sprintf(_("Diff: %s"), $pagename), $new);
388 // $Log: not supported by cvs2svn $
389 // Revision 1.42 2003/01/11 23:05:04 carstenklapp
390 // Tweaked diff formatting.
392 // Revision 1.41 2003/01/08 02:23:02 carstenklapp
393 // Don't perform a diff when the page doesn't exist (such as a
394 // nonexistant calendar day/sub-page)
401 // c-hanging-comment-ender-p: nil
402 // indent-tabs-mode: nil