[ 新規 | 編集 | 差分 ] [ 検索 | 一覧 | FrontPage ] [ 履歴 (RSS) | 差分履歴 (RSS) ] [ ログイン ]
【PR】Amazon | SL-C3000 | SL-C1000(3/18発売!) | SL-C860 | SL-6000W | SL-6000L | SL-6000N

WalWiki/カスタマイズ/インクリメンタル差分

編集

一定時間内の同一IPアドレスからの編集を一回の編集とみなし、差分をインクリメンタルに取得します。

[incremental diff]

編集

ライブラリ追加

編集

Algorithm::Diff::Applylib/Algorithm/Diff/Apply.pm に設置します。

use Algorithm::Diff::Apply qw(apply_diff);      # yakty add [incremental diff]

グローバル変数定義

編集
# yakty add [incremental diff] start
my $info_UpdateTimeStamp = 'UpdateTimeStamp';
my $info_UpdateRemoteAddr = 'UpdateRemoteAddr';
# yakty add [incremental diff] end

15分以内の編集を同一と見なす。

my $force_update_interval = 15*60;                          # yakty add [incremental diff]

do_write()内の変更

編集
    $form{mymsg} =~ s/\x0D\x0A|\x0D|\x0A/\n/g;                             # Walrus add [unify new-line]
    my $isChanged;                                                         # yakty add [noupdate if nochange]
    $isChanged = &update_diff($form{mypage});                              # yakty add [incremental diff]
    # Making diff
    # yakty del [incremental diff] start
#    {
#        &open_diff;
##       my @msg1 = split(/\n/, $database{$form{mypage}});                  # Walrus del [unify new-line]
#        my @msg1 = split(/\x0D\x0A|\x0D|\x0A/, $database{$form{mypage}});  # Walrus add [unify new-line]
#        my @msg2 = split(/\n/, $form{mymsg});
#        $diffbase{$form{mypage}} = &difftext(\@msg1, \@msg2);
#        &close_diff;
#    }
    # yakty del [incremental diff] end

update_diff()追加

編集
# yakty add [incremental diff] start
sub update_diff {
    my ($page) = @_;
    my @msg1 = split(/\x0D\x0A|\x0D|\x0A/, $database{$page});  # Walrus add [unify new-line]
    my @msg2 = split(/\n/, $form{mymsg});
    my $skippage;

    &open_diff;

    my $timestamp = &get_info($page, $info_UpdateTimeStamp);
    if(@msg2 and $form{mycmd} eq 'write'
        and time - $timestamp <= $force_update_interval
        and (length($ENV{REMOTE_ADDR}) > 0 and $ENV{REMOTE_ADDR} eq &get_info($page, $info_UpdateRemoteAddr))){

        # reverse diff
        my @diffs = split(/\n/, $diffbase{$page});
        @msg1 = &reverse_diff(\@msg1, \@diffs);
        $skippage = $page;
    }

    my $diffmsg = &difftext(\@msg1, \@msg2);
    my $isChanged = 0;
    if($skippage eq $page
        or $diffmsg =~ /^[^=]/m){
        $isChanged = 1;
        $diffbase{$page} = $diffmsg;
    }

    &close_diff;

    &update_recent_diffs($diffmsg, $skippage);                 # yakty add [recent diffs]

    &set_info($page, $info_UpdateTimeStamp, '' . time);
    &set_info($page, $info_UpdateRemoteAddr, $ENV{REMOTE_ADDR});

    return $isChanged;
}
# yakty add [incremental diff] end

reverse_diff()追加

編集
# yakty add [incremental diff] [undo edit] start
sub reverse_diff {
    my ($msgref, $diffref) = @_;
    my $lineno = 0;
    my @patch;
    my $hunk = [];
    my $delta1 = 0;
    my $delta2 = 0;
    # make diff
    foreach (@$diffref){
        /^(.)(.*)/;
        if($1 eq '-'){
            push(@$hunk, ['+', $lineno+$delta1, $2]);
            $delta1++;
        } elsif($1 eq '+'){
            push(@$hunk, ['-', $lineno+$delta2, $2]);
            $delta2++;
        } else {
            push(@patch, $hunk) if scalar(@$hunk);
            $hunk = [];
            $lineno++;
        }
    }
    push(@patch, $hunk) if scalar(@$hunk);
    return &apply_diff($msgref, \@patch);
}
# yakty add [incremental diff] [undo edit] end

02939