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';
13 class _HWLDF_WordAccumulator
15 function _HWLDF_WordAccumulator()
17 $this->_lines = array();
19 $this->_group = false;
20 $this->_tag = '~begin';
23 function _flushGroup($new_tag)
25 if ($this->_group !== false) {
27 $this->_line = HTML();
28 $this->_line->pushContent($this->_tag
29 ? new HtmlElement($this->_tag,
34 $this->_tag = $new_tag;
37 function _flushLine($new_tag)
39 $this->_flushGroup($new_tag);
41 $this->_lines[] = $this->_line;
42 $this->_line = HTML();
45 function addWords($words, $tag = '')
47 if ($tag != $this->_tag)
48 $this->_flushGroup($tag);
50 foreach ($words as $word) {
51 // new-line should only come as first char of word.
54 if ($word[0] == "\n") {
56 $this->_flushLine($tag);
57 $word = substr($word, 1);
59 assert(!strstr($word, "\n"));
60 $this->_group .= $word;
66 $this->_flushLine('~done');
71 class WordLevelDiff extends MappedDiff
73 function WordLevelDiff($orig_lines, $final_lines)
75 list ($orig_words, $orig_stripped) = $this->_split($orig_lines);
76 list ($final_words, $final_stripped) = $this->_split($final_lines);
78 $this->MappedDiff($orig_words, $final_words,
79 $orig_stripped, $final_stripped);
82 function _split($lines)
84 // FIXME: fix POSIX char class.
85 if (!preg_match_all('/ ( [^\S\n]+ | [[:alnum:]]+ | . ) (?: (?!< \n) [^\S\n])? /xs',
86 implode("\n", $lines),
89 return array(array(''), array(''));
91 return array($m[0], $m[1]);
96 $orig = new _HWLDF_WordAccumulator;
98 foreach ($this->edits as $edit) {
99 if ($edit->type == 'copy')
100 $orig->addWords($edit->orig);
102 $orig->addWords($edit->orig, 'del');
104 return $orig->getLines();
109 $final = new _HWLDF_WordAccumulator;
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');
117 return $final->getLines();
122 * HTML unified diff formatter.
124 * This class formats a diff into a CSS-based
125 * unified diff format.
127 * Within groups of changed lines, diffs are highlit
128 * at the character-diff level.
130 class HtmlUnifiedDiffFormatter extends UnifiedDiffFormatter
132 function HtmlUnifiedDiffFormatter($context_lines = 4)
134 $this->UnifiedDiffFormatter($context_lines);
137 function _start_diff()
139 $this->_top = HTML::div(array('class' => 'diff'));
149 function _start_block($header)
151 $this->_block = HTML::div(array('class' => 'block'),
155 function _end_block()
157 $this->_top->pushContent($this->_block);
158 unset($this->_block);
161 function _lines($lines, $class, $prefix = false, $elem = false)
164 $prefix = HTML::raw(' ');
165 $div = HTML::div(array('class' => 'difftext'));
166 foreach ($lines as $line) {
168 $line = new HtmlElement($elem, $line);
169 $div->pushContent(HTML::div(array('class' => $class),
170 HTML::tt(array('class' => 'prefix'),
172 $line, HTML::raw(' ')));
174 $this->_block->pushContent($div);
177 function _context($lines)
179 $this->_lines($lines, 'context');
182 function _deleted($lines)
184 $this->_lines($lines, 'deleted', '-', 'del');
187 function _added($lines)
189 $this->_lines($lines, 'added', '+', 'ins');
192 function _changed($orig, $final)
194 $diff = new WordLevelDiff($orig, $final);
195 $this->_lines($diff->orig(), 'original', '-');
196 $this->_lines($diff->_final(), 'final', '+');
200 /////////////////////////////////////////////////////////////////
202 function PageInfoRow($label, $rev, &$request, $is_current = false)
206 $row = HTML::tr(HTML::td(array('align' => 'right'), $label));
208 $author = $rev->get('author');
209 $dbi = $request->getDbh();
211 $iswikipage = (isWikiWord($author) && $dbi->isWikiPage($author));
212 $authorlink = $iswikipage ? WikiLink($author) : $author;
213 $version = $rev->getVersion();
214 $linked_version = WikiLink($rev, 'existing', $version);
216 $revertbutton = HTML();
218 $revertbutton = $WikiTheme->makeActionButton(array('action' => 'revert',
219 'version' => $version),
221 $row->pushContent(HTML::td(fmt("version %s", $linked_version)),
222 HTML::td($WikiTheme->getLastModifiedMessage($rev,
224 HTML::td(fmt("by %s", $authorlink)),
225 HTML::td($revertbutton)
228 $row->pushContent(HTML::td(array('colspan' => '4'), _("None")));
233 function showDiff(&$request)
235 $pagename = $request->getArg('pagename');
236 if (is_array($versions = $request->getArg('versions'))) {
237 // Version selection from pageinfo.php display:
239 list ($version, $previous) = $versions;
241 $version = $request->getArg('version');
242 $previous = $request->getArg('previous');
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
258 if (!($new = $page->getRevision($version)))
259 NoSuchRevision($request, $page, $version);
260 $new_version = fmt("version %d", $version);
263 $new_version = _("current version");
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');
275 while ($old = $page->getRevisionBefore($old)) {
276 if ($old->get('author') != $new->get('author'))
279 $old_version = _("revision by previous author");
280 $others = array('major', 'minor');
284 $old = $page->getRevisionBefore($new);
285 $old_version = _("previous revision");
286 $others = array('major', 'author');
291 while ($old && $old->get('is_minor_edit'))
292 $old = $page->getRevisionBefore($old);
294 $old = $page->getRevisionBefore($old);
295 $old_version = _("predecessor to the previous major change");
296 $others = array('minor', 'author');
301 $new_link = WikiLink($new, '', $new_version);
302 $old_link = $old ? WikiLink($old, '', $old_version) : $old_version;
303 $page_link = WikiLink($page);
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)));
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);
316 $args['version'] = $version;
317 if (count($otherdiffs->getContent()) > 1)
318 $otherdiffs->pushContent(", ");
320 $otherdiffs->pushContent(" ");
321 $otherdiffs->pushContent(Button($args, $label[$other]));
323 $html->pushContent($otherdiffs);
325 if ($old and $old->getVersion() == 0)
328 $html->pushContent(HTML::Table(PageInfoRow(_("Newer page:"), $new,
329 $request, empty($version)),
330 PageInfoRow(_("Older page:"), $old,
334 $diff = new Diff($old->getContent(), $new->getContent());
336 if ($diff->isEmpty()) {
337 $html->pushContent(HTML::hr(),
338 HTML::p(sprintf(_('Content of versions %1$s and %2$s is identical.'),
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'),
347 $new->get('summary'))));
350 $fmt = new HtmlUnifiedDiffFormatter;
351 $html->pushContent($fmt->format($diff));
354 $html->pushContent(HTML::hr(), HTML::h2($new_version));
355 require_once 'lib/BlockParser.php';
356 $html->pushContent(TransformText($new, $new->get('markup'), $pagename));
359 require_once 'lib/Template.php';
360 GeneratePage($html, sprintf(_("Diff: %s"), $pagename), $new);
367 // c-hanging-comment-ender-p: nil
368 // indent-tabs-mode: nil