include choice in commit msg
[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;
7
8 sub import { #{{{
9         hook(type => "preprocess", id => "poll", call => \&preprocess);
10         hook(type => "cgi", id => "poll", call => \&cgi);
11 } # }}}
12
13 sub yesno ($) { #{{{
14         my $val=shift;
15         return (defined $val && lc($val) eq "yes");
16 } #}}}
17
18 my %pagenum;
19 sub preprocess (@) { #{{{
20         my %params=(open => "yes", total => "yes", percent => "yes", @_);
21
22         my $open=yesno($params{open});
23         my $showtotal=yesno($params{total});
24         my $showpercent=yesno($params{percent});
25         $pagenum{$params{page}}++;
26
27         my %choices;
28         my @choices;
29         my $total=0;
30         while (@_) {
31                 my $key=shift;
32                 my $value=shift;
33
34                 next unless $key =~ /^\d+/;
35
36                 my $num=$key;
37                 $key=shift;
38                 $value=shift;
39
40                 $choices{$key}=$num;
41                 push @choices, $key;
42                 $total+=$num;
43         }
44
45         my $ret="";
46         foreach my $choice (@choices) {
47                 if ($open && exists $config{cgiurl}) {
48                         $ret.="<form action=\"$config{cgiurl}\">\n";
49                 }
50                 my $percent=$total > 0 ? int($choices{$choice} / $total * 100) : 0;
51                 $ret.="<p>\n";
52                 if ($showpercent) {
53                         $ret.="$choice ($percent%)\n";
54                 }
55                 else {
56                         $ret.="$choice ($choices{$choice})\n";
57                 }
58                 if ($open && exists $config{cgiurl}) {
59                         $ret.="<input type=\"hidden\" name=\"do\" value=\"poll\" />\n";
60                         $ret.="<input type=\"hidden\" name=\"num\" value=\"$pagenum{$params{page}}\" />\n";
61                         $ret.="<input type=\"hidden\" name=\"page\" value=\"$params{page}\" />\n";
62                         $ret.="<input type=\"hidden\" name=\"choice\" value=\"$choice\" />\n";
63                         $ret.="<input type=\"submit\" value=\"vote\" />\n";
64                 }
65                 $ret.="</p>\n<hr class=poll align=left width=\"$percent%\"/>\n";
66                 if ($open && exists $config{cgiurl}) {
67                         $ret.="</form>\n";
68                 }
69         }
70         if ($showtotal) {
71                 $ret.="<span>Total votes: $total</span>\n";
72         }
73         return "<div class=poll>$ret</div>";
74 } # }}}
75
76 sub cgi ($) { #{{{
77         my $cgi=shift;
78         if (defined $cgi->param('do') && $cgi->param('do') eq "poll") {
79                 my $choice=$cgi->param('choice');
80                 if (! defined $choice) {
81                         error("no choice specified");
82                 }
83                 my $num=$cgi->param('num');
84                 if (! defined $num) {
85                         error("no num specified");
86                 }
87                 my $page=IkiWiki::possibly_foolish_untaint($cgi->param('page'));
88                 if (! defined $page || ! exists $pagesources{$page}) {
89                         error("bad page name");
90                 }
91
92                 # Did they vote before? If so, let them change their vote,
93                 # and check for dups.
94                 my $session=IkiWiki::cgi_getsession();
95                 my $choice_param="poll_choice_${page}_$num";
96                 my $oldchoice=$session->param($choice_param);
97                 if (defined $oldchoice && $oldchoice eq $choice) {
98                         # Same vote; no-op.
99                         IkiWiki::redirect($cgi, "$config{url}/".htmlpage($page));
100                         exit;
101                 }
102
103                 my $content=readfile(srcfile($pagesources{$page}));
104                 # Now parse the content, find the right poll,
105                 # and find the choice within it, and increment its number.
106                 # If they voted before, decrement that one.
107                 my $edit=sub {
108                         my $escape=shift;
109                         my $params=shift;
110                         return "\\[[poll $params]]" if $escape;
111                         if (--$num == 0) {
112                                 $params=~s/(^|\s+)(\d+)\s+"?\Q$choice\E"?(\s+|$)/$1.($2+1)." \"$choice\"".$3/se;
113                                 if (defined $oldchoice) {
114                                         $params=~s/(^|\s+)(\d+)\s+"?\Q$oldchoice\E"?(\s+|$)/$1.($2-1 >=0 ? $2-1 : 0)." \"$oldchoice\"".$3/se;
115                                 }
116                         }
117                         return "[[poll $params]]";
118                 };
119                 $content =~ s{(\\?)\[\[poll\s+([^]]+)\s*\]\]}{$edit->($1, $2)}seg;
120
121                 # Store their vote, update the page, and redirect to it.
122                 writefile($pagesources{$page}, $config{srcdir}, $content);
123                 $session->param($choice_param, $choice);
124                 IkiWiki::cgi_savesession($session);
125                 $oldchoice=$session->param($choice_param);
126                 if ($config{rcs}) {
127                         # prevent deadlock with post-commit hook
128                         IkiWiki::unlockwiki();
129                         IkiWiki::rcs_commit($pagesources{$page}, "poll vote ($choice)",
130                                 IkiWiki::rcs_prepedit($pagesources{$page}),
131                                 $session->param("name"), $ENV{REMOTE_ADDR});
132                 }
133                 else {
134                         require IkiWiki::Render;
135                         IkiWiki::refresh();
136                         IkiWiki::saveindex();
137                 }
138                 # Need to set cookie in same http response that does the
139                 # redir.
140                 eval q{use CGI::Cookie};
141                 error($@) if $@;
142                 my $cookie = CGI::Cookie->new(-name=> $session->name, -value=> $session->id);
143                 print $cgi->redirect(-cookie => $cookie,
144                         -url => "$config{url}/".htmlpage($page));
145                 exit;
146         }
147 } #}}}
148
149 1