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