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 # use POST to avoid robots
49 $ret.="<form method=\"POST\" action=\"$config{cgiurl}\">\n";
51 my $percent=$total > 0 ? int($choices{$choice} / $total * 100) : 0;
54 $ret.="$choice ($percent%)\n";
57 $ret.="$choice ($choices{$choice})\n";
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";
66 $ret.="</p>\n<hr class=poll align=left width=\"$percent%\"/>\n";
67 if ($open && exists $config{cgiurl}) {
72 $ret.="<span>".gettext("Total votes:")." $total</span>\n";
74 return "<div class=poll>$ret</div>";
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");
84 my $num=$cgi->param('num');
86 error("no num specified");
88 my $page=IkiWiki::possibly_foolish_untaint($cgi->param('page'));
89 if (! defined $page || ! exists $pagesources{$page}) {
90 error("bad page name");
93 # Did they vote before? If so, let them change their vote,
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) {
100 IkiWiki::redirect($cgi, "$config{url}/".htmlpage($page));
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.
111 return "\\[[poll $params]]" if $escape;
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;
118 return "[[poll $params]]";
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);
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();
135 require IkiWiki::Render;
137 IkiWiki::saveindex();
139 # Need to set cookie in same http response that does the
141 eval q{use CGI::Cookie};
143 my $cookie = CGI::Cookie->new(-name=> $session->name, -value=> $session->id);
144 print $cgi->redirect(-cookie => $cookie,
145 -url => "$config{url}/".htmlpage($page));