2 rcs_id('$Id: diff.php,v 1.27 2002-01-21 06:55:47 dairiki Exp $');
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');
14 * HTML unified diff formatter.
16 * This class formats a diff into a CSS-based
17 * unified diff format.
19 * Within groups of changed lines, diffs are highlit
20 * at the character-diff level.
22 class HtmlUnifiedDiffFormatter extends UnifiedDiffFormatter
24 function HtmlUnifiedDiffFormatter($context_lines = 4) {
25 $this->UnifiedDiffFormatter($context_lines);
28 function _start_diff() {
29 $this->_top = HTML::div(array('class' => 'diff'));
31 function _end_diff() {
37 function _start_block($header) {
38 $this->_block = HTML::div(array('class' => 'block'),
42 function _end_block() {
43 $this->_top->pushContent($this->_block);
47 function _lines($lines, $class, $prefix = false, $elem = false) {
49 $prefix = new RawXml(' ');
51 foreach ($lines as $line) {
53 $line = new HtmlElement($elem, $line);
54 $this->_block->pushContent(HTML::div(array('class' => $class),
55 HTML::tt(array('class' => 'prefix'),
58 new RawXml(' ')));
62 function _context($lines) {
63 $this->_lines($lines, 'context');
65 function _deleted($lines) {
66 $this->_lines($lines, 'deleted', '-', 'del');
69 function _added($lines) {
70 $this->_lines($lines, 'added', '+', 'ins');
73 function _pack($bits, $tag) {
74 $packed = htmlspecialchars(implode("", $bits));
76 . str_replace("\n", "<tt> </tt></$tag>\n<$tag>",
81 function _split($lines) {
82 preg_match_all('/ ( [^\S\n]+ | [[:alnum:]]+ | . ) (?: (?!< \n) [^\S\n])? /xs',
83 implode("\n", $lines),
85 return array($m[0], $m[1]);
88 function _explode_raw($lines) {
89 $split = explode("\n", $lines);
90 foreach ($split as $key => $val)
91 $split[$key] = new RawXml($split[$key]);
95 function _changed($orig, $final) {
96 list ($orig_words, $orig_stripped) = $this->_split($orig);
97 list ($final_words, $final_stripped) = $this->_split($final);
99 // Compute character-wise diff in changed region.
100 $diff = new MappedDiff($orig_words, $final_words,
101 $orig_stripped, $final_stripped);
104 foreach ($diff->edits as $edit) {
105 if ($edit->type == 'copy') {
106 $orig .= implode('', $edit->orig);
107 $final .= implode('', $edit->final);
111 $orig .= $this->_pack($edit->orig, 'del');
113 $final .= $this->_pack($edit->final, 'ins');
117 // FIXME: this is a hack
118 $this->_lines($this->_explode_raw($orig), 'original', '-');
119 $this->_lines($this->_explode_raw($final), 'final', '+');
124 * HTML table-based unified diff formatter.
126 * This class formats a diff into a table-based
127 * unified diff format. (Similar to what was produced
128 * by previous versions of PhpWiki.)
130 * Within groups of changed lines, diffs are highlit
131 * at the character-diff level.
133 class TableUnifiedDiffFormatter extends HtmlUnifiedDiffFormatter
135 function TableUnifiedDiffFormatter($context_lines = 4) {
136 $this->HtmlUnifiedDiffFormatter($context_lines);
139 function _start_diff() {
140 $this->_top = HTML::table(array('width' => '100%',
147 function _start_block($header) {
148 $this->_block = HTML::table(array('width' => '100%',
153 HTML::tr(HTML::td(array('colspan' => 2),
154 HTML::tt($header))));
157 function _end_block() {
158 $this->_top->pushContent(HTML::tr(HTML::td($this->_block)));
159 unset($this->_block);
162 function _lines($lines, $class, $prefix = ' ', $elem = false) {
163 $prefix = HTML::td(array('class' => 'prefix', 'width' => "1%"), $prefix);
164 foreach ($lines as $line) {
166 $line = new RawXml(' ');
168 $line = new HtmlElement($elem, $line);
169 $this->_block->pushContent(HTML::tr(array('valign' => 'top'),
171 HTML::td(array('class' => $class),
178 /////////////////////////////////////////////////////////////////
180 function PageInfoRow ($pagename, $label, $rev)
184 $row = HTML::tr(HTML::td(array('align' => 'right'), $label));
186 $url = WikiURL($pagename, array('version' => $rev->getVersion()));
187 $linked_version = HTML::a(array('href' => $url), $rev->getVersion());
188 $row->pushContent(HTML::td(fmt("version %s",$linked_version)),
189 HTML::td(fmt("last modified on %s",
190 $Theme->formatDateTime($rev->get('mtime')))),
191 HTML::td(fmt("by %s", $rev->get('author'))));
193 $row->pushContent(HTML::td(array('colspan' => '3'), _("None")));
198 function showDiff ($dbi, $request) {
199 $pagename = $request->getArg('pagename');
200 if (is_array($versions = $request->getArg('versions'))) {
201 // Version selection from pageinfo.php display:
203 list ($version, $previous) = $versions;
206 $version = $request->getArg('version');
207 $previous = $request->getArg('previous');
210 $page = $dbi->getPage($pagename);
212 if (!($new = $page->getRevision($version)))
213 NoSuchRevision($page, $version);
214 $new_version = fmt("version %d", $version);
217 $new = $page->getCurrentRevision();
218 $new_version = _("current version");
221 if (preg_match('/^\d+$/', $previous)) {
222 if ( !($old = $page->getRevision($previous)) )
223 NoSuchRevision($page, $previous);
224 $old_version = fmt("version %d", $previous);
225 $others = array('major', 'minor', 'author');
231 while ($old = $page->getRevisionBefore($old)) {
232 if (! $old->get('is_minor_edit'))
235 $old_version = _("previous major revision");
236 $others = array('minor', 'author');
240 while ($old = $page->getRevisionBefore($old)) {
241 if ($old->get('author') != $new->get('author'))
244 $old_version = _("revision by previous author");
245 $others = array('major', 'minor');
250 $old = $page->getRevisionBefore($new);
251 $old_version = _("previous revision");
252 $others = array('major', 'author');
257 $new_url = WikiURL($pagename, array('version' => $new->getVersion()));
258 $new_link = HTML::a(array('href' => $new_url), $new_version);
259 $old_url = WikiURL($pagename, array('version' => $old ? $old->getVersion() : 0));
260 $old_link = HTML::a(array('href' => $old_url), $old_version);
262 $page_link = $Theme->linkExistingWikiWord($pagename);
264 $html[] = HTML::p(fmt("Differences between %s and %s of %s.",
265 $new_link, $old_link, $page_link));
267 $otherdiffs = HTML::p(_("Other diffs:"));
268 $label = array('major' => _("Previous Major Revision"),
269 'minor' => _("Previous Revision"),
270 'author'=> _("Previous Author"));
271 foreach ($others as $other) {
272 $args = array('action' => 'diff', 'previous' => $other);
274 $args['version'] = $version;
275 if (count($otherdiffs->getContent()) > 1)
276 $otherdiffs->pushContent(", ");
278 $otherdiffs->pushContent(" ");
279 $otherdiffs->pushContent(HTML::a(array('href' => WikiURL($pagename, $args),
280 'class' => 'wikiaction'),
283 $html[] = $otherdiffs;
286 if ($old and $old->getVersion() == 0)
289 $html[] = HTML::Table(PageInfoRow($pagename, _("Newer page:"), $new),
290 PageInfoRow($pagename, _("Older page:"), $old));
293 $diff = new Diff($old->getContent(), $new->getContent());
295 if ($diff->isEmpty()) {
296 $html[] = HTML::hr();
297 $html[] = HTML::p('[', _("Versions are identical"), ']');
300 // New CSS formatted unified diffs (ugly in NS4).
301 $fmt = new HtmlUnifiedDiffFormatter;
303 // Use this for old table-formatted diffs.
304 //$fmt = new TableUnifiedDiffFormatter;
305 $html[] = $fmt->format($diff);
309 include_once('lib/Template.php');
310 echo GeneratePage('MESSAGE', $html,
311 sprintf(_("Diff: %s"), $pagename), $new);
318 // c-hanging-comment-ender-p: nil
319 // indent-tabs-mode: nil