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