ready for review, I think
[ikiwiki.git] / IkiWiki / Plugin / poll.pm
blob6bc4579c2547a34691b0274bdeec7b054336a651
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::poll;
4 use warnings;
5 use strict;
6 use IkiWiki 3.00;
7 use Encode;
9 sub import {
10 hook(type => "getsetup", id => "poll", call => \&getsetup);
11 hook(type => "preprocess", id => "poll", call => \&preprocess);
12 hook(type => "sessioncgi", id => "poll", call => \&sessioncgi);
15 sub getsetup () {
16 return
17 plugin => {
18 safe => 1,
19 rebuild => undef,
20 section => "widget",
24 my %pagenum;
25 sub preprocess (@) {
26 my %params=(open => "yes", total => "yes", percent => "yes", @_);
28 my $open=IkiWiki::yesno($params{open});
29 my $showtotal=IkiWiki::yesno($params{total});
30 my $showpercent=IkiWiki::yesno($params{percent});
31 $pagenum{$params{page}}++;
33 my %choices;
34 my @choices;
35 my $total=0;
36 while (@_) {
37 my $key=shift;
38 my $value=shift;
40 next unless $key =~ /^\d+/;
42 my $num=$key;
43 $key=shift;
44 $value=shift;
46 $choices{$key}=$num;
47 push @choices, $key;
48 $total+=$num;
51 my $ret="";
52 foreach my $choice (@choices) {
53 if ($open && exists $config{cgiurl}) {
54 # use POST to avoid robots
55 $ret.="<form method=\"POST\" action=\"$config{cgiurl}\">\n";
57 my $percent=$total > 0 ? int($choices{$choice} / $total * 100) : 0;
58 $ret.="<p>\n";
59 if ($showpercent) {
60 $ret.="$choice ($percent%)\n";
62 else {
63 $ret.="$choice ($choices{$choice})\n";
65 if ($open && exists $config{cgiurl}) {
66 $ret.="<input type=\"hidden\" name=\"do\" value=\"poll\" />\n";
67 $ret.="<input type=\"hidden\" name=\"num\" value=\"$pagenum{$params{page}}\" />\n";
68 $ret.="<input type=\"hidden\" name=\"page\" value=\"$params{page}\" />\n";
69 $ret.="<input type=\"hidden\" name=\"choice\" value=\"$choice\" />\n";
70 $ret.="<input type=\"submit\" value=\"".gettext("vote")."\" />\n";
72 $ret.="</p>\n<hr class=poll align=left width=\"$percent%\"/>\n";
73 if ($open && exists $config{cgiurl}) {
74 $ret.="</form>\n";
77 if ($showtotal) {
78 $ret.="<span>".gettext("Total votes:")." $total</span>\n";
80 return "<div class=poll>$ret</div>";
83 sub sessioncgi ($$) {
84 my $cgi=shift;
85 my $session=shift;
86 if (defined $cgi->param('do') && $cgi->param('do') eq "poll") {
87 my $choice=decode_utf8($cgi->param('choice'));
88 if (! defined $choice) {
89 error("no choice specified");
91 my $num=$cgi->param('num');
92 if (! defined $num) {
93 error("no num specified");
95 my $page=IkiWiki::possibly_foolish_untaint($cgi->param('page'));
96 if (! defined $page || ! exists $pagesources{$page}) {
97 error("bad page name");
100 # Did they vote before? If so, let them change their vote,
101 # and check for dups.
102 my $choice_param="poll_choice_${page}_$num";
103 my $oldchoice=$session->param($choice_param);
104 if (defined $oldchoice && $oldchoice eq $choice) {
105 # Same vote; no-op.
106 IkiWiki::redirect($cgi, urlto($page, undef, 1));
107 exit;
110 my $prefix=$config{prefix_directives} ? "!poll" : "poll";
112 my $content=readfile(srcfile($pagesources{$page}));
113 # Now parse the content, find the right poll,
114 # and find the choice within it, and increment its number.
115 # If they voted before, decrement that one.
116 my $edit=sub {
117 my $escape=shift;
118 my $params=shift;
119 return "\\[[$prefix $params]]" if $escape;
120 if (--$num == 0) {
121 $params=~s/(^|\s+)(\d+)\s+"?\Q$choice\E"?(\s+|$)/$1.($2+1)." \"$choice\"".$3/se;
122 if (defined $oldchoice) {
123 $params=~s/(^|\s+)(\d+)\s+"?\Q$oldchoice\E"?(\s+|$)/$1.($2-1 >=0 ? $2-1 : 0)." \"$oldchoice\"".$3/se;
126 return "[[$prefix $params]]";
128 $content =~ s{(\\?)\[\[\Q$prefix\E\s+([^]]+)\s*\]\]}{$edit->($1, $2)}seg;
130 # Store their vote, update the page, and redirect to it.
131 writefile($pagesources{$page}, $config{srcdir}, $content);
132 $session->param($choice_param, $choice);
133 IkiWiki::cgi_savesession($session);
134 $oldchoice=$session->param($choice_param);
135 if ($config{rcs}) {
136 IkiWiki::disable_commit_hook();
137 IkiWiki::rcs_commit($pagesources{$page}, "poll vote ($choice)",
138 IkiWiki::rcs_prepedit($pagesources{$page}),
139 $session->param("name"), $ENV{REMOTE_ADDR});
140 IkiWiki::enable_commit_hook();
141 IkiWiki::rcs_update();
143 require IkiWiki::Render;
144 IkiWiki::refresh();
145 IkiWiki::saveindex();
147 # Need to set cookie in same http response that does the
148 # redir.
149 eval q{use CGI::Cookie};
150 error($@) if $@;
151 my $cookie = CGI::Cookie->new(-name=> $session->name, -value=> $session->id);
152 print $cgi->redirect(-cookie => $cookie,
153 -url => urlto($page, undef, 1));
154 exit;