2 rcs_id('$Id: diff.php,v 1.19 2001-12-14 20:15:02 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 echo "<div class='diff'>\n";
31 function _end_diff() {
35 function _start_block($header) {
36 echo "<div class='block'>\n";
37 echo QElement('tt', $header);
40 function _end_block() {
44 function _lines($lines, $class, $prefix = ' ', $elem = false) {
45 foreach ($lines as $line) {
49 $line = QElement($elem, $line);
51 echo Element('div', array('class' => $class),
52 Element('tt', array('class' => 'prefix'), $prefix)
57 function _context($lines) {
58 $this->_lines($lines, 'context');
60 function _deleted($lines) {
61 $this->_lines($lines, 'deleted', '-', 'del');
63 function _added($lines) {
64 $this->_lines($lines, 'added', '+', 'ins');
67 function _pack_in_span($chars, $elem) {
70 foreach ($chars as $c) {
77 $end_span = "</$elem>";
79 $packed .= htmlspecialchars($c);
81 return $packed . $end_span;
84 function _split_to_chars($lines) {
85 // Split into characters --- there must be a better way ...
86 $joined = implode("\n", $lines);
88 for ($i = 0; $i < strlen($joined); $i++)
89 $split[$i] = $joined[$i];
94 function _changed($orig, $final) {
95 // Compute character-wise diff in changed region.
96 $diff = new Diff($this->_split_to_chars($orig),
97 $this->_split_to_chars($final));
100 foreach ($diff->edits as $edit) {
101 switch ($edit->type) {
103 $packed = implode('', $edit->orig);
108 $final .= $this->_pack_in_span($edit->final, 'ins');
111 $orig .= $this->_pack_in_span($edit->orig, 'del');
114 $orig .= $this->_pack_in_span($edit->orig, 'del');
115 $final .= $this->_pack_in_span($edit->final, 'ins');
120 $this->_lines(explode("\n", $orig), 'changed', '-');
121 $this->_lines(explode("\n", $final), 'changed', '+');
126 * HTML table-based unified diff formatter.
128 * This class formats a diff into a table-based
129 * unified diff format. (Similar to what was produced
130 * by previous versions of PhpWiki.)
132 * Within groups of changed lines, diffs are highlit
133 * at the character-diff level.
135 class TableUnifiedDiffFormatter extends HtmlUnifiedDiffFormatter
137 function TableUnifiedDiffFormatter($context_lines = 4) {
138 $this->HtmlUnifiedDiffFormatter($context_lines);
141 function _start_diff() {
142 echo "\n<table width='100%' class='diff'";
143 echo " cellspacing='1' cellpadding='1' border='1'>\n";
145 function _end_diff() {
149 function _start_block($header) {
150 echo "<tr><td><table width='100%' class='block'";
151 echo " cellspacing='0' cellpadding='1' border='0'>\n";
153 Element('td', array('colspan' => 2),
154 QElement('tt', $header))) . "\n";
157 function _end_block() {
158 echo "</table></td></tr>\n";
161 function _lines($lines, $class, $prefix = ' ', $elem = false) {
162 $prefix = Element('td', array('class' => 'prefix', 'width' => "1%"), $prefix);
163 foreach ($lines as $line) {
167 $line = QElement($elem, $line);
169 echo Element('tr', array('valign' => 'top'),
170 $prefix . Element('td', array('class' => $class),
177 /////////////////////////////////////////////////////////////////
179 function PageInfoRow ($pagename, $label, $rev)
181 global $datetimeformat;
183 $cols = QElement('td', array('align' => 'right'), $label);
186 $url = WikiURL($pagename, array('version' => $rev->getVersion()));
187 $linked_version = QElement('a', array('href' => $url), $rev->getVersion());
188 $cols .= Element('td',
189 gettext("version") . " " . $linked_version);
191 $cols .= QElement('td',
192 sprintf(gettext ("last modified on %s"),
193 strftime($datetimeformat, $rev->get('mtime'))));
194 $cols .= QElement('td',
195 sprintf(gettext ("by %s"), $rev->get('author')));
197 $cols .= QElement('td', array('colspan' => '3'),
200 return Element('tr', $cols);
203 function showDiff ($dbi, $request) {
204 $pagename = $request->getArg('pagename');
205 if (is_array($versions = $request->getArg('versions'))) {
206 // Version selection from pageinfo.php display:
208 list ($version, $previous) = $versions;
211 $version = $request->getArg('version');
212 $previous = $request->getArg('previous');
215 $page = $dbi->getPage($pagename);
217 if (!($new = $page->getRevision($version)))
218 NoSuchRevision($page, $version);
219 $new_version = sprintf(gettext("version %d"), $version);
222 $new = $page->getCurrentRevision();
223 $new_version = gettext('current version');
226 if (preg_match('/^\d+$/', $previous)) {
227 if ( !($old = $page->getRevision($previous)) )
228 NoSuchRevision($page, $previous);
229 $old_version = sprintf(gettext("version %d"), $previous);
230 $others = array('major', 'minor', 'author');
236 while ($old = $page->getRevisionBefore($old)) {
237 if (! $old->get('is_minor_edit'))
240 $old_version = gettext("previous major revision");
241 $others = array('minor', 'author');
245 while ($old = $page->getRevisionBefore($old)) {
246 if ($old->get('author') != $new->get('author'))
249 $old_version = gettext("revision by previous author");
250 $others = array('major', 'minor');
255 $old = $page->getRevisionBefore($new);
256 $old_version = gettext("previous revision");
257 $others = array('major', 'author');
262 $new_url = WikiURL($pagename, array('version' => $new->getVersion()));
263 $new_link = QElement('a', array('href' => $new_url), $new_version);
264 $old_url = WikiURL($pagename, array('version' => $old ? $old->getVersion() : 0));
265 $old_link = QElement('a', array('href' => $old_url), $old_version);
266 $page_link = LinkExistingWikiWord($pagename);
269 sprintf(htmlspecialchars(gettext("Differences between %s and %s of %s.")),
270 $new_link, $old_link, $page_link));
273 $label = array('major' => gettext("Previous Major Revision"),
274 'minor' => gettext("Previous Revision"),
275 'author'=> gettext("Previous Author"));
276 foreach ($others as $other) {
277 $args = array('action' => 'diff', 'previous' => $other);
279 $args['version'] = $version;
280 $otherdiffs .= ' ' . QElement('a', array('href' => WikiURL($pagename, $args),
281 'class' => 'wikiaction'),
284 $html .= Element('p',
285 htmlspecialchars(gettext("Other diffs:"))
289 if ($old and $old->getVersion() == 0)
292 $html .= Element('table',
293 PageInfoRow($pagename, gettext ("Newer page:"), $new)
294 . PageInfoRow($pagename, gettext ("Older page:"), $old));
298 $diff = new Diff($old->getContent(), $new->getContent());
300 if ($diff->isEmpty()) {
301 $html .= Element('hr');
302 $html .= QElement('p', '[' . gettext ("Versions are identical") . ']');
305 // New CSS formatted unified diffs (ugly in NS4).
306 $fmt = new HtmlUnifiedDiffFormatter;
308 // Use this for old table-formatted diffs.
309 //$fmt = new TableUnifiedDiffFormatter;
310 $html .= $fmt->format($diff);
314 include_once('lib/Template.php');
315 echo GeneratePage('MESSAGE', $html,
316 sprintf(gettext ("Diff: %s"), $pagename));
323 // c-hanging-comment-ender-p: nil
324 // indent-tabs-mode: nil