2 package IkiWiki::Plugin::poll;
9 hook(type => "preprocess", id => "poll", call => \&preprocess);
10 hook(type => "cgi", id => "poll", call => \&cgi);
15 return (defined $val && lc($val) eq "yes");
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}}++;
34 next unless $key =~ /^\d+/;
46 foreach my $choice (@choices) {
47 if ($open && exists $config{cgiurl}) {
48 $ret.="<form action=\"$config{cgiurl}\">\n";
50 my $percent=$total > 0 ? int($choices{$choice} / $total * 100) : 0;
53 $ret.="$choice ($percent%)\n";
56 $ret.="$choice ($choices{$choice})\n";
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";
65 $ret.="</p>\n<hr class=poll align=left width=\"$percent%\"/>\n";
66 if ($open && exists $config{cgiurl}) {
71 $ret.="<span>Total votes: $total</span>\n";
73 return "<div class=poll>$ret</div>";
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");
83 my $num=$cgi->param('num');
85 error("no num specified");
87 my $page=IkiWiki::possibly_foolish_untaint($cgi->param('page'));
88 if (! defined $page || ! exists $pagesources{$page}) {
89 error("bad page name");
92 # Did they vote before? If so, let them change their vote,
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) {
99 IkiWiki::redirect($cgi, "$config{url}/".htmlpage($page));
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.
110 return "\\[[poll $params]]" if $escape;
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;
117 return "[[poll $params]]";
119 $content =~ s{(\\?)\[\[poll\s+([^]]+)\s*\]\]}{$edit->($1, $2)}seg;
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);
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});
134 require IkiWiki::Render;
136 IkiWiki::saveindex();
138 # Need to set cookie in same http response that does the
140 eval q{use CGI::Cookie};
142 my $cookie = CGI::Cookie->new(-name=> $session->name, -value=> $session->id);
143 print $cgi->redirect(-cookie => $cookie,
144 -url => "$config{url}/".htmlpage($page));