summaryrefslogtreecommitdiff
path: root/IkiWiki
diff options
context:
space:
mode:
authorjoey <joey@0fa5a96a-9a0e-0410-b3b2-a0fd24251071>2007-08-22 21:06:13 +0000
committerjoey <joey@0fa5a96a-9a0e-0410-b3b2-a0fd24251071>2007-08-22 21:06:13 +0000
commit2c5fbe844b3137b2e3f84d5f1d1ea9ef37564852 (patch)
treee48b6030fe82796a6c899f85d47dbe17776971a2 /IkiWiki
parent771608a4d43438931f2f1a3dddd07810a006d435 (diff)
* Call the formbuilder hook for the edit page.
* Call decode_form_utf8 before running formbuilder_setup hooks. * Add editdiff plugin contributed by Jeremie Koenig. * Fix it to not leak path info.
Diffstat (limited to 'IkiWiki')
-rw-r--r--IkiWiki/CGI.pm72
-rw-r--r--IkiWiki/Plugin/editdiff.pm69
2 files changed, 103 insertions, 38 deletions
diff --git a/IkiWiki/CGI.pm b/IkiWiki/CGI.pm
index e8df1fe11..c800ddf6e 100644
--- a/IkiWiki/CGI.pm
+++ b/IkiWiki/CGI.pm
@@ -20,6 +20,24 @@ sub printheader ($) { #{{{
}
} #}}}
+
+sub showform ($$$$) { #{{{
+ my $form=shift;
+ my $buttons=shift;
+ my $session=shift;
+ my $cgi=shift;
+
+ if (exists $hooks{formbuilder}) {
+ run_hooks(formbuilder => sub {
+ shift->(form => $form, cgi => $cgi, session => $session,
+ buttons => $buttons);
+ });
+ }
+ else {
+ printheader($session);
+ print misctemplate($form->title, $form->render(submit => $buttons));
+ }
+}
sub redirect ($$) { #{{{
my $q=shift;
@@ -156,26 +174,18 @@ sub cgi_signin ($$) { #{{{
$form->field(name => "do", type => "hidden", value => "signin",
force => 1);
+ decode_form_utf8($form);
+
run_hooks(formbuilder_setup => sub {
shift->(form => $form, cgi => $q, session => $session,
buttons => $buttons);
});
-
- decode_form_utf8($form);
- if (exists $hooks{formbuilder}) {
- run_hooks(formbuilder => sub {
- shift->(form => $form, cgi => $q, session => $session,
- buttons => $buttons);
- });
- }
- else {
- if ($form->submitted) {
- $form->validate;
- }
- printheader($session);
- print misctemplate($form->title, $form->render(submit => $buttons));
+ if ($form->submitted) {
+ $form->validate;
}
+
+ showform($form, $buttons, $session, $q);
} #}}}
sub cgi_postsignin ($$) { #{{{
@@ -228,6 +238,8 @@ sub cgi_prefs ($$) { #{{{
);
my $buttons=["Save Preferences", "Logout", "Cancel"];
+ decode_form_utf8($form);
+
run_hooks(formbuilder_setup => sub {
shift->(form => $form, cgi => $q, session => $session,
buttons => $buttons);
@@ -257,8 +269,6 @@ sub cgi_prefs ($$) { #{{{
}
}
- decode_form_utf8($form);
-
if ($form->submitted eq 'Logout') {
$session->delete();
redirect($q, $config{url});
@@ -284,16 +294,7 @@ sub cgi_prefs ($$) { #{{{
$form->text(gettext("Preferences saved."));
}
- if (exists $hooks{formbuilder}) {
- run_hooks(formbuilder => sub {
- shift->(form => $form, cgi => $q, session => $session,
- buttons => $buttons);
- });
- }
- else {
- printheader($session);
- print misctemplate($form->title, $form->render(submit => $buttons));
- }
+ showform($form, $buttons, $session, $q);
} #}}}
sub cgi_editpage ($$) { #{{{
@@ -323,13 +324,13 @@ sub cgi_editpage ($$) { #{{{
wikiname => $config{wikiname},
);
+ decode_form_utf8($form);
+
run_hooks(formbuilder_setup => sub {
shift->(form => $form, cgi => $q, session => $session,
buttons => \@buttons);
});
- decode_form_utf8($form);
-
# This untaint is safe because titlepage removes any problematic
# characters.
my ($page)=$form->field('page');
@@ -495,8 +496,7 @@ sub cgi_editpage ($$) { #{{{
$form->title(sprintf(gettext("editing %s"), pagetitle($page)));
}
- printheader($session);
- print misctemplate($form->title, $form->render(submit => \@buttons));
+ showform($form, \@buttons, $session, $q);
}
else {
# save page
@@ -512,8 +512,7 @@ sub cgi_editpage ($$) { #{{{
$form->field(name => "page", type => 'hidden');
$form->field(name => "type", type => 'hidden');
$form->title(sprintf(gettext("editing %s"), $page));
- printheader($session);
- print misctemplate($form->title, $form->render(submit => \@buttons));
+ showform($form, \@buttons, $session, $q);
return;
}
elsif ($form->field("do") eq "create" && $exists) {
@@ -527,8 +526,7 @@ sub cgi_editpage ($$) { #{{{
value => readfile("$config{srcdir}/$file").
"\n\n\n".$form->field("editcontent"),
force => 1);
- printheader($session);
- print misctemplate($form->title, $form->render(submit => \@buttons));
+ showform($form, \@buttons, $session, $q);
return;
}
@@ -550,8 +548,7 @@ sub cgi_editpage ($$) { #{{{
$form->field(name => "page", type => 'hidden');
$form->field(name => "type", type => 'hidden');
$form->title(sprintf(gettext("editing %s"), $page));
- printheader($session);
- print misctemplate($form->title, $form->render(submit => \@buttons));
+ showform($form, \@buttons, $session, $q);
return;
}
@@ -595,8 +592,7 @@ sub cgi_editpage ($$) { #{{{
$form->field(name => "page", type => 'hidden');
$form->field(name => "type", type => 'hidden');
$form->title(sprintf(gettext("editing %s"), $page));
- printheader($session);
- print misctemplate($form->title, $form->render(submit => \@buttons));
+ showform($form, \@buttons, $session, $q);
return;
}
else {
diff --git a/IkiWiki/Plugin/editdiff.pm b/IkiWiki/Plugin/editdiff.pm
new file mode 100644
index 000000000..46eb1168e
--- /dev/null
+++ b/IkiWiki/Plugin/editdiff.pm
@@ -0,0 +1,69 @@
+#!/usr/bin/perl
+# This plugin adds a "Diff" button to the page edit form.
+package IkiWiki::Plugin::editdiff;
+
+use warnings;
+use strict;
+use IkiWiki 2.00;
+use HTML::Entities;
+use IPC::Open2;
+
+sub import { #{{{
+ hook(type => "formbuilder_setup", id => "editdiff",
+ call => \&formbuilder_setup);
+} #}}}
+
+sub diff ($$) { #{{{
+ 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;
+ my $ret;
+ 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>";
+} #}}}
+
+sub formbuilder_setup { #{{{
+ my %params=@_;
+ my $form=$params{form};
+ my $page=$form->field("page");
+
+ return if $form->title ne "editpage"
+ || $form->field("do") ne "edit";
+
+ $page = IkiWiki::titlepage(IkiWiki::possibly_foolish_untaint($page));
+ 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);
+ }
+} #}}}
+
+1