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);
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 = false, $elem = false) {
148 $prefix = HTML::raw(' ');
149 $div = HTML::div(array('class' => 'difftext'));
150 foreach ($lines as $line) {
152 $line = new HtmlElement($elem, $line);
153 $div->pushContent(HTML::div(array('class' => $class),
154 HTML::tt(array('class' => 'prefix'),
156 $line, HTML::raw(' ')));
158 $this->_block->pushContent($div);
161 function _context($lines) {
162 $this->_lines($lines, 'context');
164 function _deleted($lines) {
165 $this->_lines($lines, 'deleted', '-', 'del');
168 function _added($lines) {
169 $this->_lines($lines, 'added', '+', 'ins');
172 function _changed($orig, $final) {
173 $diff = new WordLevelDiff($orig, $final);
174 $this->_lines($diff->orig(), 'original', '-');
175 $this->_lines($diff->_final(), 'final', '+');
179 /////////////////////////////////////////////////////////////////
181 function PageInfoRow ($label, $rev, &$request, $is_current = false)
185 $row = HTML::tr(HTML::td(array('align' => 'right'), $label));
187 $author = $rev->get('author');
188 $dbi = $request->getDbh();
190 $iswikipage = (isWikiWord($author) && $dbi->isWikiPage($author));
191 $authorlink = $iswikipage ? WikiLink($author) : $author;
192 $version = $rev->getVersion();
193 $linked_version = WikiLink($rev, 'existing', $version);
195 $revertbutton = HTML();
197 $revertbutton = $WikiTheme->makeActionButton(array('action' => 'revert',
198 'version' => $version),
200 $row->pushContent(HTML::td(fmt("version %s", $linked_version)),
201 HTML::td($WikiTheme->getLastModifiedMessage($rev,
203 HTML::td(fmt("by %s", $authorlink)),
204 HTML::td($revertbutton)
207 $row->pushContent(HTML::td(array('colspan' => '4'), _("None")));
212 function showDiff (&$request) {
213 $pagename = $request->getArg('pagename');
214 if (is_array($versions = $request->getArg('versions'))) {
215 // Version selection from pageinfo.php display:
217 list ($version, $previous) = $versions;
220 $version = $request->getArg('version');
221 $previous = $request->getArg('previous');
224 // abort if page doesn't exist
225 $dbi = $request->getDbh();
226 $page = $request->getPage();
227 $current = $page->getCurrentRevision(false);
228 if ($current->getVersion() < 1) {
229 $html = HTML::div(array('class'=>'wikitext','id'=>'difftext'),
230 HTML::p(fmt("I'm sorry, there is no such page as %s.",
231 WikiLink($pagename, 'unknown'))));
232 require_once('lib/Template.php');
233 GeneratePage($html, sprintf(_("Diff: %s"), $pagename), false);
234 return; //early return
238 if (!($new = $page->getRevision($version)))
239 NoSuchRevision($request, $page, $version);
240 $new_version = fmt("version %d", $version);
244 $new_version = _("current version");
247 if (preg_match('/^\d+$/', $previous)) {
248 if ( !($old = $page->getRevision($previous)) )
249 NoSuchRevision($request, $page, $previous);
250 $old_version = fmt("version %d", $previous);
251 $others = array('major', 'minor', 'author');
257 while ($old = $page->getRevisionBefore($old)) {
258 if ($old->get('author') != $new->get('author'))
261 $old_version = _("revision by previous author");
262 $others = array('major', 'minor');
266 $old = $page->getRevisionBefore($new);
267 $old_version = _("previous revision");
268 $others = array('major', 'author');
273 while ($old && $old->get('is_minor_edit'))
274 $old = $page->getRevisionBefore($old);
276 $old = $page->getRevisionBefore($old);
277 $old_version = _("predecessor to the previous major change");
278 $others = array('minor', 'author');
283 $new_link = WikiLink($new, '', $new_version);
284 $old_link = $old ? WikiLink($old, '', $old_version) : $old_version;
285 $page_link = WikiLink($page);
287 $html = HTML::div(array('class'=>'wikitext','id'=>'difftext'),
288 HTML::p(fmt("Differences between %s and %s of %s.",
289 $new_link, $old_link, $page_link)));
291 $otherdiffs = HTML::p(_("Other diffs:"));
292 $label = array('major' => _("Previous Major Revision"),
293 'minor' => _("Previous Revision"),
294 'author'=> _("Previous Author"));
295 foreach ($others as $other) {
296 $args = array('action' => 'diff', 'previous' => $other);
298 $args['version'] = $version;
299 if (count($otherdiffs->getContent()) > 1)
300 $otherdiffs->pushContent(", ");
302 $otherdiffs->pushContent(" ");
303 $otherdiffs->pushContent(Button($args, $label[$other]));
305 $html->pushContent($otherdiffs);
307 if ($old and $old->getVersion() == 0)
310 $html->pushContent(HTML::Table(PageInfoRow(_("Newer page:"), $new,
311 $request, empty($version)),
312 PageInfoRow(_("Older page:"), $old,
316 $diff = new Diff($old->getContent(), $new->getContent());
318 if ($diff->isEmpty()) {
319 $html->pushContent(HTML::hr(),
320 HTML::p(_("Content of versions "), $old->getVersion(),
321 _(" and "), $new->getVersion(), _(" is identical.")));
322 // If two consecutive versions have the same content, it is because the page was
323 // renamed, or metadata changed: ACL, owner, markup.
324 // We give the reason by printing the summary.
325 if (($new->getVersion() - $old->getVersion()) == 1) {
326 $html->pushContent(HTML::p(_("Version "), $new->getVersion(),
327 _(" was created because: "), $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