2 rcs_id('$Id: diff.php,v 1.55 2007-06-01 06:32:13 rurban 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');
12 require_once('lib/HtmlElement.php');
14 class _HWLDF_WordAccumulator {
15 function _HWLDF_WordAccumulator () {
16 $this->_lines = array();
18 $this->_group = false;
19 $this->_tag = '~begin';
22 function _flushGroup ($new_tag) {
23 if ($this->_group !== false) {
25 $this->_line = HTML();
26 $this->_line->pushContent($this->_tag
27 ? new HtmlElement($this->_tag,
32 $this->_tag = $new_tag;
35 function _flushLine ($new_tag) {
36 $this->_flushGroup($new_tag);
38 $this->_lines[] = $this->_line;
39 $this->_line = HTML();
42 function addWords ($words, $tag = '') {
43 if ($tag != $this->_tag)
44 $this->_flushGroup($tag);
46 foreach ($words as $word) {
47 // new-line should only come as first char of word.
50 if ($word[0] == "\n") {
52 $this->_flushLine($tag);
53 $word = substr($word, 1);
55 assert(!strstr($word, "\n"));
56 $this->_group .= $word;
61 $this->_flushLine('~done');
66 class WordLevelDiff extends MappedDiff
68 function WordLevelDiff ($orig_lines, $final_lines) {
69 list ($orig_words, $orig_stripped) = $this->_split($orig_lines);
70 list ($final_words, $final_stripped) = $this->_split($final_lines);
73 $this->MappedDiff($orig_words, $final_words,
74 $orig_stripped, $final_stripped);
77 function _split($lines) {
78 // FIXME: fix POSIX char class.
79 if (!preg_match_all('/ ( [^\S\n]+ | [[:alnum:]]+ | . ) (?: (?!< \n) [^\S\n])? /xs',
80 implode("\n", $lines),
82 return array(array(''), array(''));
84 return array($m[0], $m[1]);
88 $orig = new _HWLDF_WordAccumulator;
90 foreach ($this->edits as $edit) {
91 if ($edit->type == 'copy')
92 $orig->addWords($edit->orig);
94 $orig->addWords($edit->orig, 'del');
96 return $orig->getLines();
100 $final = new _HWLDF_WordAccumulator;
102 foreach ($this->edits as $edit) {
103 if ($edit->type == 'copy')
104 $final->addWords($edit->final);
105 elseif ($edit->final)
106 $final->addWords($edit->final, 'ins');
108 return $final->getLines();
114 * HTML unified diff formatter.
116 * This class formats a diff into a CSS-based
117 * unified diff format.
119 * Within groups of changed lines, diffs are highlit
120 * at the character-diff level.
122 class HtmlUnifiedDiffFormatter extends UnifiedDiffFormatter
124 function HtmlUnifiedDiffFormatter($context_lines = 4) {
125 $this->UnifiedDiffFormatter($context_lines);
128 function _start_diff() {
129 $this->_top = HTML::div(array('class' => 'diff'));
131 function _end_diff() {
137 function _start_block($header) {
138 $this->_block = HTML::div(array('class' => 'block'),
142 function _end_block() {
143 $this->_top->pushContent($this->_block);
144 unset($this->_block);
147 function _lines($lines, $class, $prefix = false, $elem = false) {
149 $prefix = HTML::raw(' ');
150 $div = HTML::div(array('class' => 'difftext'));
151 foreach ($lines as $line) {
153 $line = new HtmlElement($elem, $line);
154 $div->pushContent(HTML::div(array('class' => $class),
155 HTML::tt(array('class' => 'prefix'),
157 $line, HTML::raw(' ')));
159 $this->_block->pushContent($div);
162 function _context($lines) {
163 $this->_lines($lines, 'context');
165 function _deleted($lines) {
166 $this->_lines($lines, 'deleted', '-', 'del');
169 function _added($lines) {
170 $this->_lines($lines, 'added', '+', 'ins');
173 function _changed($orig, $final) {
174 $diff = new WordLevelDiff($orig, $final);
175 $this->_lines($diff->orig(), 'original', '-');
176 $this->_lines($diff->_final(), 'final', '+');
181 * HTML table-based unified diff formatter.
183 * This class formats a diff into a table-based
184 * unified diff format. (Similar to what was produced
185 * by previous versions of PhpWiki.)
187 * Within groups of changed lines, diffs are highlit
188 * at the character-diff level.
190 class TableUnifiedDiffFormatter extends HtmlUnifiedDiffFormatter
192 function TableUnifiedDiffFormatter($context_lines = 4) {
193 $this->HtmlUnifiedDiffFormatter($context_lines);
196 function _start_diff() {
197 $this->_top = HTML::table(array('width' => '100%',
204 function _start_block($header) {
205 $this->_block = HTML::table(array('width' => '100%',
210 HTML::tr(HTML::td(array('colspan' => 2),
211 HTML::tt($header))));
214 function _end_block() {
215 $this->_top->pushContent(HTML::tr(HTML::td($this->_block)));
216 unset($this->_block);
219 function _lines($lines, $class, $prefix = false, $elem = false) {
221 $prefix = HTML::raw(' ');
222 $prefix = HTML::td(array('class' => 'prefix',
223 'width' => "1%"), $prefix);
224 foreach ($lines as $line) {
226 $line = HTML::raw(' ');
228 $line = new HtmlElement($elem, $line);
229 $this->_block->pushContent(HTML::tr(array('valign' => 'top'),
231 HTML::td(array('class' => $class),
237 /////////////////////////////////////////////////////////////////
239 function PageInfoRow ($label, $rev, &$request, $is_current = false)
243 $row = HTML::tr(HTML::td(array('align' => 'right'), $label));
245 $author = $rev->get('author');
246 $dbi = $request->getDbh();
248 $iswikipage = (isWikiWord($author) && $dbi->isWikiPage($author));
249 $authorlink = $iswikipage ? WikiLink($author) : $author;
250 $version = $rev->getVersion();
251 $linked_version = WikiLink($rev, 'existing', $version);
253 $revertbutton = HTML();
255 $revertbutton = $WikiTheme->makeActionButton(array('action' => 'revert',
256 'version' => $version),
258 $row->pushContent(HTML::td(fmt("version %s", $linked_version)),
259 HTML::td($WikiTheme->getLastModifiedMessage($rev,
261 HTML::td(fmt("by %s", $authorlink)),
262 HTML::td($revertbutton)
265 $row->pushContent(HTML::td(array('colspan' => '4'), _("None")));
270 function showDiff (&$request) {
271 $pagename = $request->getArg('pagename');
272 if (is_array($versions = $request->getArg('versions'))) {
273 // Version selection from pageinfo.php display:
275 list ($version, $previous) = $versions;
278 $version = $request->getArg('version');
279 $previous = $request->getArg('previous');
282 // abort if page doesn't exist
283 $dbi = $request->getDbh();
284 $page = $request->getPage();
285 $current = $page->getCurrentRevision(false);
286 if ($current->getVersion() < 1) {
287 $html = HTML::div(array('class'=>'wikitext','id'=>'difftext'),
288 HTML::p(fmt("I'm sorry, there is no such page as %s.",
289 WikiLink($pagename, 'unknown'))));
290 require_once('lib/Template.php');
291 GeneratePage($html, sprintf(_("Diff: %s"), $pagename), false);
292 return; //early return
296 if (!($new = $page->getRevision($version)))
297 NoSuchRevision($request, $page, $version);
298 $new_version = fmt("version %d", $version);
302 $new_version = _("current version");
305 if (preg_match('/^\d+$/', $previous)) {
306 if ( !($old = $page->getRevision($previous)) )
307 NoSuchRevision($request, $page, $previous);
308 $old_version = fmt("version %d", $previous);
309 $others = array('major', 'minor', 'author');
315 while ($old = $page->getRevisionBefore($old)) {
316 if ($old->get('author') != $new->get('author'))
319 $old_version = _("revision by previous author");
320 $others = array('major', 'minor');
324 $old = $page->getRevisionBefore($new);
325 $old_version = _("previous revision");
326 $others = array('major', 'author');
331 while ($old && $old->get('is_minor_edit'))
332 $old = $page->getRevisionBefore($old);
334 $old = $page->getRevisionBefore($old);
335 $old_version = _("predecessor to the previous major change");
336 $others = array('minor', 'author');
341 $new_link = WikiLink($new, '', $new_version);
342 $old_link = $old ? WikiLink($old, '', $old_version) : $old_version;
343 $page_link = WikiLink($page);
345 $html = HTML::div(array('class'=>'wikitext','id'=>'difftext'),
346 HTML::p(fmt("Differences between %s and %s of %s.",
347 $new_link, $old_link, $page_link)));
349 $otherdiffs = HTML::p(_("Other diffs:"));
350 $label = array('major' => _("Previous Major Revision"),
351 'minor' => _("Previous Revision"),
352 'author'=> _("Previous Author"));
353 foreach ($others as $other) {
354 $args = array('action' => 'diff', 'previous' => $other);
356 $args['version'] = $version;
357 if (count($otherdiffs->getContent()) > 1)
358 $otherdiffs->pushContent(", ");
360 $otherdiffs->pushContent(" ");
361 $otherdiffs->pushContent(Button($args, $label[$other]));
363 $html->pushContent($otherdiffs);
365 if ($old and $old->getVersion() == 0)
368 $html->pushContent(HTML::Table(PageInfoRow(_("Newer page:"), $new,
369 $request, empty($version)),
370 PageInfoRow(_("Older page:"), $old,
374 $diff = new Diff($old->getContent(), $new->getContent());
376 if ($diff->isEmpty()) {
377 $html->pushContent(HTML::hr(),
378 HTML::p('[', _("Versions are identical"),
382 // New CSS formatted unified diffs (ugly in NS4).
383 $fmt = new HtmlUnifiedDiffFormatter;
385 // Use this for old table-formatted diffs.
386 //$fmt = new TableUnifiedDiffFormatter;
387 $html->pushContent($fmt->format($diff));
390 $html->pushContent(HTML::hr(), HTML::h1($new_version));
391 require_once("lib/BlockParser.php");
392 $html->pushContent(TransformText($new,$new->get('markup'),$pagename));
395 require_once('lib/Template.php');
396 GeneratePage($html, sprintf(_("Diff: %s"), $pagename), $new);
399 // $Log: not supported by cvs2svn $
400 // Revision 1.54 2007/01/02 13:18:16 rurban
401 // omit want_content if not necessary
403 // Revision 1.53 2006/12/02 13:58:27 rurban
404 // Fix MonoBook layout (id content forbidden here)
405 // Add new revision to the bottom of the diff as in mediawiki.
407 // Revision 1.52 2005/04/01 14:45:14 rurban
408 // fix dirty side-effect: dont printf too early bypassing ob_buffering.
411 // Revision 1.51 2005/02/04 15:26:57 rurban
412 // need div=content for blog
414 // Revision 1.50 2005/02/04 13:44:45 rurban
415 // prevent from php5 nameclash
417 // Revision 1.49 2004/11/21 11:59:19 rurban
418 // remove final \n to be ob_cache independent
420 // Revision 1.48 2004/06/14 11:31:36 rurban
421 // renamed global $Theme to $WikiTheme (gforge nameclash)
422 // inherit PageList default options from PageList
423 // default sortby=pagename
424 // use options in PageList_Selectable (limit, sortby, ...)
425 // added action revert, with button at action=diff
426 // added option regex to WikiAdminSearchReplace
428 // Revision 1.47 2004/06/08 13:51:57 rurban
429 // some comments only
431 // Revision 1.46 2004/05/01 15:59:29 rurban
434 // Revision 1.45 2004/01/25 03:57:15 rurban
437 // Revision 1.44 2003/02/17 02:17:31 dairiki
438 // Fix so that action=diff will work when the most recent version
439 // of a page has been "deleted".
441 // Revision 1.43 2003/01/29 19:17:37 carstenklapp
442 // Bugfix for   showing on diff page.
444 // Revision 1.42 2003/01/11 23:05:04 carstenklapp
445 // Tweaked diff formatting.
447 // Revision 1.41 2003/01/08 02:23:02 carstenklapp
448 // Don't perform a diff when the page doesn't exist (such as a
449 // nonexistant calendar day/sub-page)
456 // c-hanging-comment-ender-p: nil
457 // indent-tabs-mode: nil