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', '+');
180 * HTML table-based unified diff formatter.
182 * This class formats a diff into a table-based
183 * unified diff format. (Similar to what was produced
184 * by previous versions of PhpWiki.)
186 * Within groups of changed lines, diffs are highlit
187 * at the character-diff level.
189 class TableUnifiedDiffFormatter extends HtmlUnifiedDiffFormatter
191 function TableUnifiedDiffFormatter($context_lines = 4) {
192 $this->HtmlUnifiedDiffFormatter($context_lines);
195 function _start_diff() {
196 $this->_top = HTML::table(array('width' => '100%',
203 function _start_block($header) {
204 $this->_block = HTML::table(array('width' => '100%',
209 HTML::tr(HTML::td(array('colspan' => 2),
210 HTML::tt($header))));
213 function _end_block() {
214 $this->_top->pushContent(HTML::tr(HTML::td($this->_block)));
215 unset($this->_block);
218 function _lines($lines, $class, $prefix = false, $elem = false) {
220 $prefix = HTML::raw(' ');
221 $prefix = HTML::td(array('class' => 'prefix',
222 'width' => "1%"), $prefix);
223 foreach ($lines as $line) {
225 $line = HTML::raw(' ');
227 $line = new HtmlElement($elem, $line);
228 $this->_block->pushContent(HTML::tr(array('valign' => 'top'),
230 HTML::td(array('class' => $class),
236 /////////////////////////////////////////////////////////////////
238 function PageInfoRow ($label, $rev, &$request, $is_current = false)
242 $row = HTML::tr(HTML::td(array('align' => 'right'), $label));
244 $author = $rev->get('author');
245 $dbi = $request->getDbh();
247 $iswikipage = (isWikiWord($author) && $dbi->isWikiPage($author));
248 $authorlink = $iswikipage ? WikiLink($author) : $author;
249 $version = $rev->getVersion();
250 $linked_version = WikiLink($rev, 'existing', $version);
252 $revertbutton = HTML();
254 $revertbutton = $WikiTheme->makeActionButton(array('action' => 'revert',
255 'version' => $version),
257 $row->pushContent(HTML::td(fmt("version %s", $linked_version)),
258 HTML::td($WikiTheme->getLastModifiedMessage($rev,
260 HTML::td(fmt("by %s", $authorlink)),
261 HTML::td($revertbutton)
264 $row->pushContent(HTML::td(array('colspan' => '4'), _("None")));
269 function showDiff (&$request) {
270 $pagename = $request->getArg('pagename');
271 if (is_array($versions = $request->getArg('versions'))) {
272 // Version selection from pageinfo.php display:
274 list ($version, $previous) = $versions;
277 $version = $request->getArg('version');
278 $previous = $request->getArg('previous');
281 // abort if page doesn't exist
282 $dbi = $request->getDbh();
283 $page = $request->getPage();
284 $current = $page->getCurrentRevision(false);
285 if ($current->getVersion() < 1) {
286 $html = HTML::div(array('class'=>'wikitext','id'=>'difftext'),
287 HTML::p(fmt("I'm sorry, there is no such page as %s.",
288 WikiLink($pagename, 'unknown'))));
289 require_once('lib/Template.php');
290 GeneratePage($html, sprintf(_("Diff: %s"), $pagename), false);
291 return; //early return
295 if (!($new = $page->getRevision($version)))
296 NoSuchRevision($request, $page, $version);
297 $new_version = fmt("version %d", $version);
301 $new_version = _("current version");
304 if (preg_match('/^\d+$/', $previous)) {
305 if ( !($old = $page->getRevision($previous)) )
306 NoSuchRevision($request, $page, $previous);
307 $old_version = fmt("version %d", $previous);
308 $others = array('major', 'minor', 'author');
314 while ($old = $page->getRevisionBefore($old)) {
315 if ($old->get('author') != $new->get('author'))
318 $old_version = _("revision by previous author");
319 $others = array('major', 'minor');
323 $old = $page->getRevisionBefore($new);
324 $old_version = _("previous revision");
325 $others = array('major', 'author');
330 while ($old && $old->get('is_minor_edit'))
331 $old = $page->getRevisionBefore($old);
333 $old = $page->getRevisionBefore($old);
334 $old_version = _("predecessor to the previous major change");
335 $others = array('minor', 'author');
340 $new_link = WikiLink($new, '', $new_version);
341 $old_link = $old ? WikiLink($old, '', $old_version) : $old_version;
342 $page_link = WikiLink($page);
344 $html = HTML::div(array('class'=>'wikitext','id'=>'difftext'),
345 HTML::p(fmt("Differences between %s and %s of %s.",
346 $new_link, $old_link, $page_link)));
348 $otherdiffs = HTML::p(_("Other diffs:"));
349 $label = array('major' => _("Previous Major Revision"),
350 'minor' => _("Previous Revision"),
351 'author'=> _("Previous Author"));
352 foreach ($others as $other) {
353 $args = array('action' => 'diff', 'previous' => $other);
355 $args['version'] = $version;
356 if (count($otherdiffs->getContent()) > 1)
357 $otherdiffs->pushContent(", ");
359 $otherdiffs->pushContent(" ");
360 $otherdiffs->pushContent(Button($args, $label[$other]));
362 $html->pushContent($otherdiffs);
364 if ($old and $old->getVersion() == 0)
367 $html->pushContent(HTML::Table(PageInfoRow(_("Newer page:"), $new,
368 $request, empty($version)),
369 PageInfoRow(_("Older page:"), $old,
373 $diff = new Diff($old->getContent(), $new->getContent());
375 if ($diff->isEmpty()) {
376 $html->pushContent(HTML::hr(),
377 HTML::p(_("Content of versions "), $old->getVersion(),
378 _(" and "), $new->getVersion(), _(" is identical.")));
379 // If two consecutive versions have the same content, it is because the page was
380 // renamed, or metadata changed: ACL, owner, markup.
381 // We give the reason by printing the summary.
382 if (($new->getVersion() - $old->getVersion()) == 1) {
383 $html->pushContent(HTML::p(_("Version "), $new->getVersion(),
384 _(" was created because: "), $new->get('summary')));
387 $fmt = new HtmlUnifiedDiffFormatter;
388 $html->pushContent($fmt->format($diff));
391 $html->pushContent(HTML::hr(), HTML::h2($new_version));
392 require_once("lib/BlockParser.php");
393 $html->pushContent(TransformText($new,$new->get('markup'),$pagename));
396 require_once('lib/Template.php');
397 GeneratePage($html, sprintf(_("Diff: %s"), $pagename), $new);
404 // c-hanging-comment-ender-p: nil
405 // indent-tabs-mode: nil