2007-08-22 23:06:13 +02:00
|
|
|
#!/usr/bin/perl
|
|
|
|
# This plugin adds a "Diff" button to the page edit form.
|
|
|
|
package IkiWiki::Plugin::editdiff;
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
2008-12-23 22:34:19 +01:00
|
|
|
use IkiWiki 3.00;
|
2007-08-22 23:06:13 +02:00
|
|
|
use HTML::Entities;
|
|
|
|
use IPC::Open2;
|
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub import {
|
2008-08-03 22:40:12 +02:00
|
|
|
hook(type => "getsetup", id => "editdiff", call => \&getsetup);
|
2007-08-22 23:06:13 +02:00
|
|
|
hook(type => "formbuilder_setup", id => "editdiff",
|
|
|
|
call => \&formbuilder_setup);
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2007-08-22 23:06:13 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub getsetup () {
|
2008-08-03 22:40:12 +02:00
|
|
|
return
|
|
|
|
plugin => {
|
|
|
|
safe => 1,
|
|
|
|
rebuild => 0,
|
2010-02-12 10:22:15 +01:00
|
|
|
section => "web",
|
2008-08-03 22:40:12 +02:00
|
|
|
},
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-08-03 22:40:12 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub diff ($$) {
|
2007-08-22 23:06:13 +02:00
|
|
|
my $orig=shift;
|
|
|
|
my $content=shift;
|
|
|
|
|
|
|
|
my $sigpipe=0;
|
|
|
|
$SIG{PIPE} = sub { $sigpipe=1; };
|
|
|
|
|
|
|
|
my $pid = open2(*DIFFOUT, *DIFFIN, 'diff', '-u', $orig, '-');
|
|
|
|
binmode($_, ':utf8') foreach (*DIFFIN, *DIFFOUT);
|
|
|
|
|
|
|
|
print DIFFIN $content;
|
|
|
|
close DIFFIN;
|
2008-05-21 22:54:34 +02:00
|
|
|
my $ret='';
|
2007-08-22 23:06:13 +02:00
|
|
|
while (<DIFFOUT>) {
|
|
|
|
if (defined $ret) {
|
|
|
|
$ret.=$_;
|
|
|
|
}
|
|
|
|
elsif (/^\@\@/) {
|
|
|
|
$ret=$_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close DIFFOUT;
|
|
|
|
waitpid $pid, 0;
|
|
|
|
|
|
|
|
$SIG{PIPE}="default";
|
|
|
|
return "couldn't run diff\n" if $sigpipe;
|
|
|
|
|
|
|
|
return "<pre>".encode_entities($ret)."</pre>";
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2007-08-22 23:06:13 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub formbuilder_setup {
|
2007-08-22 23:06:13 +02:00
|
|
|
my %params=@_;
|
|
|
|
my $form=$params{form};
|
|
|
|
|
2008-09-17 20:34:02 +02:00
|
|
|
return if ! defined $form->field("do") || $form->field("do") ne "edit";
|
2007-08-22 23:06:13 +02:00
|
|
|
|
2008-07-06 22:56:50 +02:00
|
|
|
my $page=$form->field("page");
|
2008-07-06 21:52:04 +02:00
|
|
|
$page = IkiWiki::possibly_foolish_untaint($page);
|
2007-08-22 23:06:13 +02:00
|
|
|
return unless exists $pagesources{$page};
|
|
|
|
|
|
|
|
push @{$params{buttons}}, "Diff";
|
|
|
|
|
|
|
|
if ($form->submitted eq "Diff") {
|
|
|
|
my $content=$form->field('editcontent');
|
|
|
|
$content=~s/\r\n/\n/g;
|
|
|
|
$content=~s/\r/\n/g;
|
|
|
|
|
|
|
|
my $diff = diff(srcfile($pagesources{$page}), $content);
|
|
|
|
$form->tmpl_param("page_preview", $diff);
|
|
|
|
}
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2007-08-22 23:06:13 +02:00
|
|
|
|
|
|
|
1
|