]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/poll.pm
uniq list for changed hook
[ikiwiki.git] / IkiWiki / Plugin / poll.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::poll;
3
4 use warnings;
5 use strict;
6 use IkiWiki 3.00;
7 use Encode;
8
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);
13 }
14
15 sub getsetup () {
16         return 
17                 plugin => {
18                         safe => 1,
19                         rebuild => undef,
20                         section => "widget",
21                 },
22 }
23
24 my %pagenum;
25 sub preprocess (@) {
26         my %params=(open => "yes", total => "yes", percent => "yes", @_);
27
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}}++;
32
33         my %choices;
34         my @choices;
35         my $total=0;
36         while (@_) {
37                 my $key=shift;
38                 my $value=shift;
39
40                 next unless $key =~ /^\d+/;
41
42                 my $num=$key;
43                 $key=shift;
44                 $value=shift;
45
46                 $choices{$key}=$num;
47                 push @choices, $key;
48                 $total+=$num;
49         }
50
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=\"".IkiWiki::cgiurl()."\">\n";
56                 }
57                 my $percent=$total > 0 ? int($choices{$choice} / $total * 100) : 0;
58                 $ret.="<p>\n";
59                 if ($showpercent) {
60                         $ret.="$choice ($percent%)\n";
61                 }
62                 else {
63                         $ret.="$choice ($choices{$choice})\n";
64                 }
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";
71                 }
72                 $ret.="</p>\n<hr class=poll align=left width=\"$percent%\"/>\n";
73                 if ($open && exists $config{cgiurl}) {
74                         $ret.="</form>\n";
75                 }
76         }
77         if ($showtotal) {
78                 $ret.="<span>".gettext("Total votes:")." $total</span>\n";
79         }
80         return "<div class=poll>$ret</div>";
81 }
82
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");
90                 }
91                 my $num=$cgi->param('num');
92                 if (! defined $num) {
93                         error("no num specified");
94                 }
95                 my $page=IkiWiki::possibly_foolish_untaint($cgi->param('page'));
96                 if (! defined $page || ! exists $pagesources{$page}) {
97                         error("bad page name");
98                 }
99
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));
107                         exit;
108                 }
109
110                 my $prefix=$config{prefix_directives} ? "!poll" : "poll";
111
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;
124                                 }
125                         }
126                         return "[[$prefix $params]]";
127                 };
128                 $content =~ s{(\\?)\[\[\Q$prefix\E\s+([^]]+)\s*\]\]}{$edit->($1, $2)}seg;
129
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(
138                                 file => $pagesources{$page},
139                                 message => "poll vote ($choice)",
140                                 token => IkiWiki::rcs_prepedit($pagesources{$page}),
141                                 session => $session,
142                         );
143                         IkiWiki::enable_commit_hook();
144                         IkiWiki::rcs_update();
145                 }
146                 require IkiWiki::Render;
147                 IkiWiki::refresh();
148                 IkiWiki::saveindex();
149
150                 # Need to set cookie in same http response that does the
151                 # redir.
152                 eval q{use CGI::Cookie};
153                 error($@) if $@;
154                 my $cookie = CGI::Cookie->new(-name=> $session->name, -value=> $session->id);
155                 print $cgi->redirect(-cookie => $cookie,
156                         -url => urlto($page));
157                 exit;
158         }
159 }
160
161 1