<?php
// PukiWiki - Yet another WikiWikiWeb clone.
-// $Id: diff.php,v 1.3 2005/04/29 09:10:11 henoheno Exp $
-// Copywrite (C) 2003-2005 PukiWiki Developers Team
+// $Id: diff.php,v 1.10 2011/01/25 15:01:01 henoheno Exp $
+// Copyright (C)
+// 2003-2005, 2007 PukiWiki Developers Team
+// 2001-2002 Originally written by yu-ji
// License: GPL v2 or (at your option) any later version
//
-//¾×ÆÍ»þ¤ËÂбþɽ¤ò½Ð¤¹
-define('DIFF_SHOW_TABLE',TRUE);
-// º¹Ê¬¤ÎºîÀ®
+// Show more information when it conflicts
+define('PKWK_DIFF_SHOW_CONFLICT_DETAIL', 1);
+
+// Create diff-style data between arrays
function do_diff($strlines1, $strlines2)
{
$obj = new line_diff();
return $str;
}
-// º¹Ê¬¤ÎºîÀ®(¹¹¿·¤Î¾×ÆÍ)
+// Visualize diff-style-text to text-with-CSS
+// '+Added' => '<span added>Added</span>'
+// '-Removed' => '<span removed>Removed</span>'
+// ' Nothing' => 'Nothing'
+function diff_style_to_css($str = '')
+{
+ // Cut diff markers ('+' or '-' or ' ')
+ $str = preg_replace('/^\-(.*)$/m', '<span class="diff_removed">$1</span>', $str);
+ $str = preg_replace('/^\+(.*)$/m', '<span class="diff_added" >$1</span>', $str);
+ return preg_replace('/^ (.*)$/m', '$1', $str);
+}
+
+// Merge helper (when it conflicts)
function do_update_diff($pagestr, $poststr, $original)
{
$obj = new line_diff();
$arr = $obj->arr_compare('all', $diff1, $diff2);
- if (DIFF_SHOW_TABLE)
- {
+ if (PKWK_DIFF_SHOW_CONFLICT_DETAIL) {
global $do_update_diff_table;
-
- $do_update_diff_table = <<<EOD
+ $table = array();
+ $table[] = <<<EOD
<p>l : between backup data and stored page data.<br />
r : between backup data and your post data.</p>
<table class="style_table">
</tr>
EOD;
$tags = array('th', 'th', 'td');
- foreach ($arr as $_obj)
- {
- $do_update_diff_table .= '<tr>';
+ foreach ($arr as $_obj) {
+ $table[] = ' <tr>';
$params = array($_obj->get('left'), $_obj->get('right'), $_obj->text());
- foreach ($params as $key=>$text) {
- $text = htmlspecialchars($text);
- if (trim($text) == '') $text = ' ';
- $do_update_diff_table .= "<{$tags[$key]} class=\"style_{$tags[$key]}\">$text</{$tags[$key]}>";
+ foreach ($params as $key => $text) {
+ $text = htmlsc(rtrim($text));
+ if (empty($text)) $text = ' ';
+ $table[] =
+ ' <' . $tags[$key] . ' class="style_' . $tags[$key] . '">' .
+ $text .
+ '</' . $tags[$key] . '>';
}
- $do_update_diff_table .= '</tr>'."\n";
+ $table[] = ' </tr>';
}
- $do_update_diff_table .= '</table>'."\n";
+ $table[] = '</table>';
+
+ $do_update_diff_table = implode("\n", $table) . "\n";
+ unset($table);
}
- $body = '';
+ $body = array();
foreach ($arr as $_obj) {
- if ($_obj->get('left') != '-' && $_obj->get('right') != '-')
- $body .= $_obj->text();
+ if ($_obj->get('left') != '-' && $_obj->get('right') != '-') {
+ $body[] = $_obj->text();
+ }
}
- $auto = 1;
-
- return array(rtrim($body) . "\n", $auto);
+ return array(rtrim(implode('', $body)) . "\n", 1);
}
-/*
-line_diff¥¯¥é¥¹
-
-°Ê²¼¤Î¾ðÊó¤ò»²¹Í¤Ë¤·¤ÆºîÀ®¤·¤Þ¤·¤¿¡£
-
-S. Wu, <A HREF="http://www.cs.arizona.edu/people/gene/vita.html">
-E. Myers,</A> U. Manber, and W. Miller,
-<A HREF="http://www.cs.arizona.edu/people/gene/PAPERS/np_diff.ps">
-"An O(NP) Sequence Comparison Algorithm,"</A>
-Information Processing Letters 35, 6 (1990), 317-323.
-
-*/
+// References of this class:
+// S. Wu, <A HREF="http://www.cs.arizona.edu/people/gene/vita.html">
+// E. Myers,</A> U. Manber, and W. Miller,
+// <A HREF="http://www.cs.arizona.edu/people/gene/PAPERS/np_diff.ps">
+// "An O(NP) Sequence Comparison Algorithm,"</A>
+// Information Processing Letters 35, 6 (1990), 317-323.
class line_diff
{
var $arr1, $arr2, $m, $n, $pos, $key, $plus, $minus, $equal, $reverse;
$this->key = $key;
$this->arr1 = array();
$this->arr2 = array();
- $str1 = preg_replace("/\r/",'',$str1);
- $str2 = preg_replace("/\r/",'',$str2);
+ $str1 = str_replace("\r", '', $str1);
+ $str2 = str_replace("\r", '', $str2);
foreach (explode("\n", $str1) as $line) {
$this->arr1[] = new DiffLine($line);
}
$this->m = count($this->arr1);
$this->n = count($this->arr2);
- if ($this->m == 0 || $this->n == 0) { // no need compare.
+ if ($this->m == 0 || $this->n == 0) { // No need to compare
$this->result = array(array('x'=>0, 'y'=>0));
return;
}
- // sentinel
+ // Sentinel
array_unshift($this->arr1, new DiffLine(''));
$this->m++;
array_unshift($this->arr2, new DiffLine(''));
}
$fp[$delta] = $this->snake($delta, $fp[$delta - 1], $fp[$delta + 1]);
if ($fp[$delta] >= $this->n) {
- $this->pos = $this->path[$delta]; // ·ÐÏ©¤ò·èÄê
+ $this->pos = $this->path[$delta]; // 経路を決定
return;
}
}
$_k = $k + 1;
$y = $y2;
}
- $this->path[$k] = $this->path[$_k];// ¤³¤³¤Þ¤Ç¤Î·ÐÏ©¤ò¥³¥Ô¡¼
+ $this->path[$k] = $this->path[$_k];// ã\81\93ã\81\93ã\81¾ã\81§ã\81®çµ\8cè·¯ã\82\92ã\82³ã\83\94ã\83¼
$x = $y - $k;
while ((($x + 1) < $this->m) && (($y + 1) < $this->n)
and $this->arr1[$x + 1]->compare($this->arr2[$y + 1]))
{
++$x; ++$y;
- $this->path[$k][] = array('x'=>$x, 'y'=>$y); // ·ÐÏ©¤òÄɲÃ
+ $this->path[$k][] = array('x'=>$x, 'y'=>$y); // 経路を追加
}
return $y;
}
function toArray()
{
$arr = array();
- if ($this->reverse) { //¸È©¤Ê¡Ä
+ if ($this->reverse) { // 姑息な…
$_x = 'y'; $_y = 'x'; $_m = $this->n; $arr1 =& $this->arr2; $arr2 =& $this->arr1;
} else {
$_x = 'x'; $_y = 'y'; $_m = $this->m; $arr1 =& $this->arr1; $arr2 =& $this->arr2;
function DiffLine($text)
{
- $this->text = "$text\n";
+ $this->text = $text . "\n";
$this->status = array();
}