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 __construct()
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 __construct($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
141 function __construct($context_lines = 4)
143 parent::__construct($context_lines);
146 function _start_diff()
148 $this->_top = HTML::div(array('class' => 'diff'));
158 function _start_block($header)
160 $this->_block = HTML::div(array('class' => 'block'),
161 HTML::samp($header));
164 function _end_block()
166 $this->_top->pushContent($this->_block);
167 unset($this->_block);
170 function _lines($lines, $class, $prefix = false, $elem = false)
173 $prefix = HTML::raw(' ');
174 $div = HTML::div(array('class' => 'difftext'));
175 foreach ($lines as $line) {
177 $line = new HtmlElement($elem, $line);
178 $div->pushContent(HTML::div(array('class' => $class),
179 HTML::samp(array('class' => 'prefix'),
181 $line, HTML::raw(' ')));
183 $this->_block->pushContent($div);
186 function _context($lines)
188 $this->_lines($lines, 'context');
191 function _deleted($lines)
193 $this->_lines($lines, 'deleted', '-', 'del');
196 function _added($lines)
198 $this->_lines($lines, 'added', '+', 'ins');
201 function _changed($orig, $final)
203 $diff = new WordLevelDiff($orig, $final);
204 $this->_lines($diff->orig(), 'original', '-');
205 $this->_lines($diff->_final(), 'final', '+');
209 /////////////////////////////////////////////////////////////////
212 * @param string $label
213 * @param WikiDB_PageRevision $rev
214 * @param WikiRequest $request
215 * @param bool $is_current
216 * @return $this|HtmlElement
218 function PageInfoRow($label, $rev, &$request, $is_current = false)
222 $row = HTML::tr(HTML::td(array('class' => 'align-right'), $label));
224 $author = $rev->get('author');
225 $dbi = $request->getDbh();
227 $iswikipage = (isWikiWord($author) && $dbi->isWikiPage($author));
228 $authorlink = $iswikipage ? WikiLink($author) : $author;
229 $version = $rev->getVersion();
230 $linked_version = WikiLink($rev, 'existing', $version);
232 $revertbutton = HTML();
234 $revertbutton = $WikiTheme->makeActionButton(array('action' => 'revert',
235 'version' => $version),
237 $row->pushContent(HTML::td(fmt("version %s", $linked_version)),
238 HTML::td($WikiTheme->getLastModifiedMessage($rev,
240 HTML::td(fmt("by %s", $authorlink)),
241 HTML::td($revertbutton)
244 $row->pushContent(HTML::td(array('colspan' => '4'), _("None")));
250 * @param WikiRequest $request
252 function showDiff(&$request)
254 $pagename = $request->getArg('pagename');
255 if (is_array($versions = $request->getArg('versions'))) {
256 // Version selection from pageinfo.php display:
258 list ($version, $previous) = $versions;
260 $version = $request->getArg('version');
261 $previous = $request->getArg('previous');
264 // abort if page doesn't exist
265 $page = $request->getPage();
266 $current = $page->getCurrentRevision(false);
267 if ($current->getVersion() < 1) {
268 $html = HTML::div(array('class' => 'wikitext', 'id' => 'difftext'),
269 HTML::p(fmt("Page ā%sā does not exist.", WikiLink($pagename, 'unknown'))));
270 require_once 'lib/Template.php';
271 GeneratePage($html, sprintf(_("Diff: %s"), $pagename), false);
272 return; //early return
276 if (!($new = $page->getRevision($version)))
277 NoSuchRevision($request, $page, $version);
278 $new_version = fmt("version %d", $version);
281 $new_version = _("current version");
284 if (preg_match('/^\d+$/', $previous)) {
285 if (!($old = $page->getRevision($previous)))
286 NoSuchRevision($request, $page, $previous);
287 $old_version = fmt("version %d", $previous);
288 $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');
301 $old = $page->getRevisionBefore($new);
302 $old_version = _("previous revision");
303 $others = array('major', 'author');
307 while ($old && $old->get('is_minor_edit'))
308 $old = $page->getRevisionBefore($old);
310 $old = $page->getRevisionBefore($old);
311 $old_version = _("predecessor to the previous major change");
312 $others = array('minor', 'author');
317 $new_link = WikiLink($new, '', $new_version);
318 $old_link = $old ? WikiLink($old, '', $old_version) : $old_version;
319 $page_link = WikiLink($page);
321 $html = HTML::div(array('class' => 'wikitext', 'id' => 'difftext'),
322 HTML::p(fmt("Differences between %s and %s of %s.",
323 $new_link, $old_link, $page_link)));
325 $otherdiffs = HTML::p(_("Other diffs:"));
326 $label = array('major' => _("Previous Major Revision"),
327 'minor' => _("Previous Revision"),
328 'author' => _("Previous Author"));
329 foreach ($others as $other) {
330 $args = array('action' => 'diff', 'previous' => $other);
332 $args['version'] = $version;
333 if (count($otherdiffs->getContent()) > 1)
334 $otherdiffs->pushContent(", ");
336 $otherdiffs->pushContent(" ");
337 $otherdiffs->pushContent(Button($args, $label[$other]));
339 $html->pushContent($otherdiffs);
341 if ($old and $old->getVersion() == 0)
344 $html->pushContent(HTML::Table(PageInfoRow(_("Newer page:"), $new,
345 $request, empty($version)),
346 PageInfoRow(_("Older page:"), $old,
350 $diff = new Diff($old->getContent(), $new->getContent());
352 if ($diff->isEmpty()) {
353 $html->pushContent(HTML::hr(),
354 HTML::p(sprintf(_('Content of versions %1$s and %2$s is identical.'),
356 $new->getVersion())));
357 // If two consecutive versions have the same content, it is because the page was
358 // renamed, or metadata changed: ACL, owner, markup.
359 // We give the reason by printing the summary.
360 if (($new->getVersion() - $old->getVersion()) == 1) {
361 $html->pushContent(HTML::p(sprintf(_('Version %1$s was created because: %2$s'),
363 $new->get('summary'))));
366 $fmt = new HtmlUnifiedDiffFormatter;
367 $html->pushContent($fmt->format($diff));
370 $html->pushContent(HTML::hr(), HTML::h2($new_version));
371 require_once 'lib/BlockParser.php';
372 $html->pushContent(TransformText($new, $pagename));
375 require_once 'lib/Template.php';
376 GeneratePage($html, sprintf(_("Diff: %s"), $pagename), $new);
383 // c-hanging-comment-ender-p: nil
384 // indent-tabs-mode: nil