From: Tim Düsterhus Date: Thu, 12 Feb 2015 21:22:00 +0000 (+0100) Subject: Add word diff to EditHistory X-Git-Tag: 3.0.0_RC_2~30 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=948b2593189a71aeb8c0add8917c714a9bf0959b;p=GitHub%2FWoltLab%2FWCF.git Add word diff to EditHistory --- diff --git a/com.woltlab.wcf/templates/editHistory.tpl b/com.woltlab.wcf/templates/editHistory.tpl index bbd1b9aa45..f5feb3ce82 100644 --- a/com.woltlab.wcf/templates/editHistory.tpl +++ b/com.woltlab.wcf/templates/editHistory.tpl @@ -19,7 +19,7 @@
{assign var='prevType' value=''} -{foreach from=$diff->getRawDiff() item='line'} +{foreach from=$diff item='line'} {if $line[0] !== $prevType}
@@ -44,9 +44,9 @@ {/if} {/if} -{if $line[0] === ' '}{$line[1]}
{/if} -{if $line[0] === '-'}{$line[1]}
{/if} -{if $line[0] === '+'}{$line[1]}
{/if} +{if $line[0] === ' '}{@$line[1]}
{/if} +{if $line[0] === '-'}{@$line[1]}
{/if} +{if $line[0] === '+'}{@$line[1]}
{/if} {assign var='prevType' value=$line[0]} {/foreach}
diff --git a/wcfsetup/install/files/lib/page/EditHistoryPage.class.php b/wcfsetup/install/files/lib/page/EditHistoryPage.class.php index 393b059377..3c856aaccf 100644 --- a/wcfsetup/install/files/lib/page/EditHistoryPage.class.php +++ b/wcfsetup/install/files/lib/page/EditHistoryPage.class.php @@ -161,7 +161,44 @@ class EditHistoryPage extends AbstractPage { if ($this->old && $this->new) { $a = explode("\n", StringUtil::unifyNewlines($this->old->getMessage())); $b = explode("\n", StringUtil::unifyNewlines($this->new->getMessage())); - $this->diff = new Diff($a, $b); + $diff = new Diff($a, $b); + $this->diff = $diff->getRawDiff(); + + // create word diff for small changes (only one consecutive paragraph modified) + for ($i = 0, $max = count($this->diff); $i < $max;) { + $previousIsNotRemoved = !isset($this->diff[$i - 1][0]) || $this->diff[$i - 1][0] !== Diff::REMOVED; + $currentIsRemoved = $this->diff[$i][0] === Diff::REMOVED; + $nextIsAdded = isset($this->diff[$i + 1][0]) && $this->diff[$i + 1][0] === Diff::ADDED; + $afterNextIsNotAdded = !isset($this->diff[$i + 2][0]) || $this->diff[$i + 2][0] !== Diff::ADDED; + + if ($previousIsNotRemoved && $currentIsRemoved && $nextIsAdded && $afterNextIsNotAdded) { + $a = preg_split('/(\\W)/u', $this->diff[$i][1], -1, PREG_SPLIT_DELIM_CAPTURE); + $b = preg_split('/(\\W)/u', $this->diff[$i + 1][1], -1, PREG_SPLIT_DELIM_CAPTURE); + + $diff = new Diff($a, $b); + $this->diff[$i][1] = ''; + $this->diff[$i + 1][1] = ''; + foreach ($diff->getRawDiff() as $entry) { + $entry[1] = StringUtil::encodeHTML($entry[1]); + + if ($entry[0] === Diff::SAME) { + $this->diff[$i][1] .= $entry[1]; + $this->diff[$i + 1][1] .= $entry[1]; + } + else if ($entry[0] === Diff::REMOVED) { + $this->diff[$i][1] .= ''.$entry[1].''; + } + else if ($entry[0] === Diff::ADDED) { + $this->diff[$i + 1][1] .= ''.$entry[1].''; + } + } + $i += 2; + } + else { + $this->diff[$i][1] = StringUtil::encodeHTML($this->diff[$i][1]); + $i++; + } + } } // set default values diff --git a/wcfsetup/install/files/lib/util/Diff.class.php b/wcfsetup/install/files/lib/util/Diff.class.php index e5ae6aa48f..e7691c59cc 100644 --- a/wcfsetup/install/files/lib/util/Diff.class.php +++ b/wcfsetup/install/files/lib/util/Diff.class.php @@ -199,7 +199,7 @@ class Diff { $this->d[] = [self::REMOVED, $this->a[$positionA++]]; } - // find next matching item in b, every item in between must be removed + // find next matching item in b, every item in between must be added while ($positionB < $this->sizeB && $this->b[$positionB] !== $item) { $this->d[] = [self::ADDED, $this->b[$positionB++]]; }