2 package IkiWiki::Plugin::poll;
10 hook(type => "preprocess", id => "poll", call => \&preprocess);
11 hook(type => "cgi", id => "poll", call => \&cgi);
16 return (defined $val && lc($val) eq "yes");
20 sub preprocess (@) { #{{{
21 my %params=(open => "yes", total => "yes", percent => "yes", @_);
23 my $open=yesno($params{open});
24 my $showtotal=yesno($params{total});
25 my $percent=yesno($params{percent});
26 $pagenum{$params{page}}++;
35 next unless $key =~ /^\d+/;
47 foreach my $choice (@choices) {
48 my $percent=$total > 0 ? int($choices{$choice} / $total * 100) : 0;
50 $ret.="$choice ($percent%) ";
53 $ret.="$choice ($choices{$choice}) ";
55 if ($open && exists $config{cgiurl}) {
56 my $url=URI->new($config{cgiurl});
59 "num" => $pagenum{$params{page}},
60 "page" => $params{page},
63 $ret.="<a class=pollbutton href=\"$url\">vote</a>";
65 $ret.="<br />\n<hr class=poll align=left width=\"$percent%\"/>\n";
68 $ret.="<span>Total votes: $total</span>\n";
70 return "<div class=poll>$ret</div>";
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");
80 my $num=$cgi->param('num');
82 error("no num specified");
84 my $page=IkiWiki::possibly_foolish_untaint($cgi->param('page'));
85 if (! defined $page || ! exists $pagesources{$page}) {
86 error("bad page name");
89 # Did they vote before? If so, let them change their vote,
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) {
96 IkiWiki::redirect($cgi, "$config{url}/".htmlpage($page));
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.
106 return "\\[[poll $params]]" if $escape;
107 return $params unless --$num == 0;
108 my @bits=split(' ', $params);
119 next unless defined $n && defined $c;
123 if (defined $oldchoice && $c eq $oldchoice) {
126 push @ret, $n, "\"$c\"";
128 return "[[poll ".join(" ", @ret)."]]";
130 $content =~ s{(\\?)\[\[poll\s+([^]]+)\s*\]\]}{$edit->($1, $2)}seg;
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);
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});
145 require IkiWiki::Render;
147 IkiWiki::saveindex();
149 # Need to set cookie in same http response that does the
151 eval q{use CGI::Cookie};
153 my $cookie = CGI::Cookie->new(-name=> $session->name, -value=> $session->id);
154 print $cgi->redirect(-cookie => $cookie,
155 -url => "$config{url}/".htmlpage($page));