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 {
14 function _HWLDF_WordAccumulator () {
15 $this->_lines = array();
17 $this->_group = false;
18 $this->_tag = '~begin';
21 function _flushGroup ($new_tag) {
22 if ($this->_group !== false) {
24 $this->_line = HTML();
25 $this->_line->pushContent($this->_tag
26 ? new HtmlElement($this->_tag,
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") {
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);
71 $this->MappedDiff($orig_words, $final_words,
72 $orig_stripped, $final_stripped);
75 function _split($lines) {
76 // FIXME: fix POSIX char class.
77 if (!preg_match_all('/ ( [^\S\n]+ | [[:alnum:]]+ | . ) (?: (?!< \n) [^\S\n])? /xs',
78 implode("\n", $lines),
80 return array(array(''), array(''));
82 return array($m[0], $m[1]);
86 $orig = new _HWLDF_WordAccumulator;
88 foreach ($this->edits as $edit) {
89 if ($edit->type == 'copy')
90 $orig->addWords($edit->orig);
92 $orig->addWords($edit->orig, 'del');
94 return $orig->getLines();
98 $final = new _HWLDF_WordAccumulator;
100 foreach ($this->edits as $edit) {
101 if ($edit->type == 'copy')
102 $final->addWords($edit->final);
103 elseif ($edit->final)
104 $final->addWords($edit->final, 'ins');
106 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 = false, $elem = false) {
146 $prefix = HTML::raw(' ');
147 $div = HTML::div(array('class' => 'difftext'));
148 foreach ($lines as $line) {
150 $line = new HtmlElement($elem, $line);
151 $div->pushContent(HTML::div(array('class' => $class),
152 HTML::tt(array('class' => 'prefix'),
154 $line, HTML::raw(' ')));
156 $this->_block->pushContent($div);
159 function _context($lines) {
160 $this->_lines($lines, 'context');
162 function _deleted($lines) {
163 $this->_lines($lines, 'deleted', '-', 'del');
166 function _added($lines) {
167 $this->_lines($lines, 'added', '+', 'ins');
170 function _changed($orig, $final) {
171 $diff = new WordLevelDiff($orig, $final);
172 $this->_lines($diff->orig(), 'original', '-');
173 $this->_lines($diff->_final(), 'final', '+');
177 /////////////////////////////////////////////////////////////////
179 function PageInfoRow ($label, $rev, &$request, $is_current = false)
183 $row = HTML::tr(HTML::td(array('align' => 'right'), $label));
185 $author = $rev->get('author');
186 $dbi = $request->getDbh();
188 $iswikipage = (isWikiWord($author) && $dbi->isWikiPage($author));
189 $authorlink = $iswikipage ? WikiLink($author) : $author;
190 $version = $rev->getVersion();
191 $linked_version = WikiLink($rev, 'existing', $version);
193 $revertbutton = HTML();
195 $revertbutton = $WikiTheme->makeActionButton(array('action' => 'revert',
196 'version' => $version),
198 $row->pushContent(HTML::td(fmt("version %s", $linked_version)),
199 HTML::td($WikiTheme->getLastModifiedMessage($rev,
201 HTML::td(fmt("by %s", $authorlink)),
202 HTML::td($revertbutton)
205 $row->pushContent(HTML::td(array('colspan' => '4'), _("None")));
210 function showDiff (&$request) {
211 $pagename = $request->getArg('pagename');
212 if (is_array($versions = $request->getArg('versions'))) {
213 // Version selection from pageinfo.php display:
215 list ($version, $previous) = $versions;
218 $version = $request->getArg('version');
219 $previous = $request->getArg('previous');
222 // abort if page doesn't exist
223 $dbi = $request->getDbh();
224 $page = $request->getPage();
225 $current = $page->getCurrentRevision(false);
226 if ($current->getVersion() < 1) {
227 $html = HTML::div(array('class'=>'wikitext','id'=>'difftext'),
228 HTML::p(fmt("I'm sorry, there is no such page as %s.",
229 WikiLink($pagename, 'unknown'))));
230 require_once 'lib/Template.php';
231 GeneratePage($html, sprintf(_("Diff: %s"), $pagename), false);
232 return; //early return
236 if (!($new = $page->getRevision($version)))
237 NoSuchRevision($request, $page, $version);
238 $new_version = fmt("version %d", $version);
242 $new_version = _("current version");
245 if (preg_match('/^\d+$/', $previous)) {
246 if ( !($old = $page->getRevision($previous)) )
247 NoSuchRevision($request, $page, $previous);
248 $old_version = fmt("version %d", $previous);
249 $others = array('major', 'minor', 'author');
255 while ($old = $page->getRevisionBefore($old)) {
256 if ($old->get('author') != $new->get('author'))
259 $old_version = _("revision by previous author");
260 $others = array('major', 'minor');
264 $old = $page->getRevisionBefore($new);
265 $old_version = _("previous revision");
266 $others = array('major', 'author');
271 while ($old && $old->get('is_minor_edit'))
272 $old = $page->getRevisionBefore($old);
274 $old = $page->getRevisionBefore($old);
275 $old_version = _("predecessor to the previous major change");
276 $others = array('minor', 'author');
281 $new_link = WikiLink($new, '', $new_version);
282 $old_link = $old ? WikiLink($old, '', $old_version) : $old_version;
283 $page_link = WikiLink($page);
285 $html = HTML::div(array('class'=>'wikitext','id'=>'difftext'),
286 HTML::p(fmt("Differences between %s and %s of %s.",
287 $new_link, $old_link, $page_link)));
289 $otherdiffs = HTML::p(_("Other diffs:"));
290 $label = array('major' => _("Previous Major Revision"),
291 'minor' => _("Previous Revision"),
292 'author'=> _("Previous Author"));
293 foreach ($others as $other) {
294 $args = array('action' => 'diff', 'previous' => $other);
296 $args['version'] = $version;
297 if (count($otherdiffs->getContent()) > 1)
298 $otherdiffs->pushContent(", ");
300 $otherdiffs->pushContent(" ");
301 $otherdiffs->pushContent(Button($args, $label[$other]));
303 $html->pushContent($otherdiffs);
305 if ($old and $old->getVersion() == 0)
308 $html->pushContent(HTML::Table(PageInfoRow(_("Newer page:"), $new,
309 $request, empty($version)),
310 PageInfoRow(_("Older page:"), $old,
314 $diff = new Diff($old->getContent(), $new->getContent());
316 if ($diff->isEmpty()) {
317 $html->pushContent(HTML::hr(),
318 HTML::p(sprintf(_('Content of versions %1$s and %2$s is identical.'),
320 $new->getVersion())));
321 // If two consecutive versions have the same content, it is because the page was
322 // renamed, or metadata changed: ACL, owner, markup.
323 // We give the reason by printing the summary.
324 if (($new->getVersion() - $old->getVersion()) == 1) {
325 $html->pushContent(HTML::p(sprintf(_('Version %1$s was created because: %2$s'),
327 $new->get('summary'))));
330 $fmt = new HtmlUnifiedDiffFormatter;
331 $html->pushContent($fmt->format($diff));
334 $html->pushContent(HTML::hr(), HTML::h2($new_version));
335 require_once 'lib/BlockParser.php';
336 $html->pushContent(TransformText($new,$new->get('markup'),$pagename));
339 require_once 'lib/Template.php';
340 GeneratePage($html, sprintf(_("Diff: %s"), $pagename), $new);
347 // c-hanging-comment-ender-p: nil
348 // indent-tabs-mode: nil