2 rcs_id('$Id: Diff.php,v 1.3 2005-09-30 18:53:10 uckelman Exp $');
4 Copyright 1999, 2000, 2001, 2002, 2004 $ThePhpWikiProgrammingTeam
6 This file is part of PhpWiki.
8 PhpWiki is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 PhpWiki is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with PhpWiki; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * lib/diff.php converted to a plugin by electrawn,
24 * plugin cleaned up by rurban,
27 * Would make sense to see arbitrary diff's between any files or revisions.
30 require_once('lib/difflib.php');
39 function getDescription () {
40 return _("Display differences between revisions");
43 function getVersion() {
44 return preg_replace("/[Revision: $]/", '',
48 // Establish default values for each of this plugin's arguments.
49 // todo: makes only sense with more args.
50 function getDefaultArguments() {
51 return array('pagename' => '[pagename]',
55 'previous' => 'major', // author, minor or major
59 function PageInfoRow ($label, $rev, &$request) {
61 global $WikiTheme, $WikiNameRegexp;
63 $row = HTML::tr(HTML::td(array('align' => 'right'), $label));
65 $author = $rev->get('author');
66 $dbi = $request->getDbh();
68 $iswikipage = (isWikiWord($author) && $dbi->isWikiPage($author));
69 $authorlink = $iswikipage ? WikiLink($author) : $author;
71 $linked_version = WikiLink($rev, 'existing', $rev->getVersion());
72 $row->pushContent(HTML::td(fmt("version %s", $linked_version)),
73 HTML::td($WikiTheme->getLastModifiedMessage($rev,
75 HTML::td(fmt("by %s", $authorlink)));
77 $row->pushContent(HTML::td(array('colspan' => '3'), _("None")));
82 function run($dbi, $argstr, &$request, $basepage) {
83 extract($this->getArgs($argstr, $request));
84 if (is_array($versions)) {
85 // Version selection from pageinfo.php display:
87 list ($version, $previous) = $versions;
90 // abort if page doesn't exist
91 $page = $request->getPage($pagename);
92 $current = $page->getCurrentRevision();
93 if ($current->getVersion() < 1) {
94 $html = HTML(HTML::p(fmt("I'm sorry, there is no such page as %s.",
95 WikiLink($pagename, 'unknown'))));
96 return $html; //early return
100 if (!($new = $page->getRevision($version)))
101 NoSuchRevision($request, $page, $version);
102 $new_version = fmt("version %d", $version);
106 $new_version = _("current version");
109 if (preg_match('/^\d+$/', $previous)) {
110 if ( !($old = $page->getRevision($previous)) )
111 NoSuchRevision($request, $page, $previous);
112 $old_version = fmt("version %d", $previous);
113 $others = array('major', 'minor', 'author');
119 while ($old = $page->getRevisionBefore($old)) {
120 if ($old->get('author') != $new->get('author'))
123 $old_version = _("revision by previous author");
124 $others = array('major', 'minor');
128 $old = $page->getRevisionBefore($new);
129 $old_version = _("previous revision");
130 $others = array('major', 'author');
135 while ($old && $old->get('is_minor_edit'))
136 $old = $page->getRevisionBefore($old);
138 $old = $page->getRevisionBefore($old);
139 $old_version = _("predecessor to the previous major change");
140 $others = array('minor', 'author');
145 $new_link = WikiLink($new, '', $new_version);
146 $old_link = $old ? WikiLink($old, '', $old_version) : $old_version;
147 $page_link = WikiLink($page);
149 $html = HTML(HTML::p(fmt("Differences between %s and %s of %s.",
150 $new_link, $old_link, $page_link)));
152 $otherdiffs = HTML::p(_("Other diffs:"));
153 $label = array('major' => _("Previous Major Revision"),
154 'minor' => _("Previous Revision"),
155 'author'=> _("Previous Author"));
156 foreach ($others as $other) {
157 $args = array('pagename' => $pagename, 'previous' => $other);
159 $args['version'] = $version;
160 if (count($otherdiffs->getContent()) > 1)
161 $otherdiffs->pushContent(", ");
163 $otherdiffs->pushContent(" ");
164 $otherdiffs->pushContent(Button($args, $label[$other]));
166 $html->pushContent($otherdiffs);
169 if ($old and $old->getVersion() == 0)
172 $html->pushContent(HTML::Table($this->PageInfoRow(_("Newer page:"), $new,
174 $this->PageInfoRow(_("Older page:"), $old,
178 $diff = new Diff($old->getContent(), $new->getContent());
180 if ($diff->isEmpty()) {
181 $html->pushContent(HTML::hr(),
182 HTML::p('[', _("Versions are identical"),
186 // New CSS formatted unified diffs (ugly in NS4).
187 $fmt = new HtmlUnifiedDiffFormatter;
189 // Use this for old table-formatted diffs.
190 //$fmt = new TableUnifiedDiffFormatter;
191 $html->pushContent($fmt->format($diff));
195 //$html = HTML::tt(fmt('%s: %s', $salutation, WikiLink($name, 'auto')),
202 class _HWLDF_WordAccumulator {
203 function _HWLDF_WordAccumulator () {
204 $this->_lines = array();
205 $this->_line = false;
206 $this->_group = false;
207 $this->_tag = '~begin';
210 function _flushGroup ($new_tag) {
211 if ($this->_group !== false) {
213 $this->_line = HTML();
214 $this->_line->pushContent($this->_tag
215 ? new HtmlElement($this->_tag,
220 $this->_tag = $new_tag;
223 function _flushLine ($new_tag) {
224 $this->_flushGroup($new_tag);
226 $this->_lines[] = $this->_line;
227 $this->_line = HTML();
230 function addWords ($words, $tag = '') {
231 if ($tag != $this->_tag)
232 $this->_flushGroup($tag);
234 foreach ($words as $word) {
235 // new-line should only come as first char of word.
238 if ($word[0] == "\n") {
239 $this->_group .= PrintXML(HTML::raw(' '));
240 $this->_flushLine($tag);
241 $word = substr($word, 1);
243 assert(!strstr($word, "\n"));
244 $this->_group .= $word;
248 function getLines() {
249 $this->_flushLine('~done');
250 return $this->_lines;
254 class WordLevelDiff extends MappedDiff
256 function WordLevelDiff ($orig_lines, $final_lines) {
257 list ($orig_words, $orig_stripped) = $this->_split($orig_lines);
258 list ($final_words, $final_stripped) = $this->_split($final_lines);
261 $this->MappedDiff($orig_words, $final_words,
262 $orig_stripped, $final_stripped);
265 function _split($lines) {
266 // FIXME: fix POSIX char class.
267 if (!preg_match_all('/ ( [^\S\n]+ | [[:alnum:]]+ | . ) (?: (?!< \n) [^\S\n])? /xs',
268 implode("\n", $lines),
270 return array(array(''), array(''));
272 return array($m[0], $m[1]);
276 $orig = new _HWLDF_WordAccumulator;
278 foreach ($this->edits as $edit) {
279 if ($edit->type == 'copy')
280 $orig->addWords($edit->orig);
282 $orig->addWords($edit->orig, 'del');
284 return $orig->getLines();
288 $final = new _HWLDF_WordAccumulator;
290 foreach ($this->edits as $edit) {
291 if ($edit->type == 'copy')
292 $final->addWords($edit->final);
293 elseif ($edit->final)
294 $final->addWords($edit->final, 'ins');
296 return $final->getLines();
301 * HTML unified diff formatter.
303 * This class formats a diff into a CSS-based
304 * unified diff format.
306 * Within groups of changed lines, diffs are highlit
307 * at the character-diff level.
309 class HtmlUnifiedDiffFormatter extends UnifiedDiffFormatter
311 function HtmlUnifiedDiffFormatter($context_lines = 4) {
312 $this->UnifiedDiffFormatter($context_lines);
315 function _start_diff() {
316 $this->_top = HTML::div(array('class' => 'diff'));
318 function _end_diff() {
324 function _start_block($header) {
325 $this->_block = HTML::div(array('class' => 'block'),
329 function _end_block() {
330 $this->_top->pushContent($this->_block);
331 unset($this->_block);
334 function _lines($lines, $class, $prefix = false, $elem = false) {
336 $prefix = HTML::raw(' ');
337 $div = HTML::div(array('class' => 'difftext'));
338 foreach ($lines as $line) {
340 $line = new HtmlElement($elem, $line);
341 $div->pushContent(HTML::div(array('class' => $class),
342 HTML::tt(array('class' => 'prefix'),
344 $line, HTML::raw(' ')));
346 $this->_block->pushContent($div);
349 function _context($lines) {
350 $this->_lines($lines, 'context');
352 function _deleted($lines) {
353 $this->_lines($lines, 'deleted', '-', 'del');
356 function _added($lines) {
357 $this->_lines($lines, 'added', '+', 'ins');
360 function _changed($orig, $final) {
361 $diff = new WordLevelDiff($orig, $final);
362 $this->_lines($diff->orig(), 'original', '-');
363 $this->_lines($diff->_final(), 'final', '+');
368 * HTML table-based unified diff formatter.
370 * This class formats a diff into a table-based
371 * unified diff format. (Similar to what was produced
372 * by previous versions of PhpWiki.)
374 * Within groups of changed lines, diffs are highlit
375 * at the character-diff level.
377 class TableUnifiedDiffFormatter extends HtmlUnifiedDiffFormatter
379 function TableUnifiedDiffFormatter($context_lines = 4) {
380 $this->HtmlUnifiedDiffFormatter($context_lines);
383 function _start_diff() {
384 $this->_top = HTML::table(array('width' => '100%',
391 function _start_block($header) {
392 $this->_block = HTML::table(array('width' => '100%',
397 HTML::tr(HTML::td(array('colspan' => 2),
398 HTML::tt($header))));
401 function _end_block() {
402 $this->_top->pushContent(HTML::tr(HTML::td($this->_block)));
403 unset($this->_block);
406 function _lines($lines, $class, $prefix = false, $elem = false) {
408 $prefix = HTML::raw(' ');
409 $prefix = HTML::td(array('class' => 'prefix',
410 'width' => "1%"), $prefix);
411 foreach ($lines as $line) {
413 $line = HTML::raw(' ');
415 $line = new HtmlElement($elem, $line);
416 $this->_block->pushContent(HTML::tr(array('valign' => 'top'),
418 HTML::td(array('class' => $class),
424 // $Log: not supported by cvs2svn $
425 // Revision 1.2 2004/06/14 11:31:39 rurban
426 // renamed global $Theme to $WikiTheme (gforge nameclash)
427 // inherit PageList default options from PageList
428 // default sortby=pagename
429 // use options in PageList_Selectable (limit, sortby, ...)
430 // added action revert, with button at action=diff
431 // added option regex to WikiAdminSearchReplace
433 // Revision 1.1 2004/02/26 23:02:17 rurban
434 // lib/diff.php converted to a plugin by electrawn,
435 // plugin cleaned up by rurban,
438 // Would make sense to see arbitrary diff's between any files or revisions.
447 // c-hanging-comment-ender-p: nil
448 // indent-tabs-mode: nil