2 package IkiWiki::Plugin::poll;
10 hook(type => "getsetup", id => "poll", call => \&getsetup);
11 hook(type => "preprocess", id => "poll", call => \&preprocess);
12 hook(type => "sessioncgi", id => "poll", call => \&sessioncgi);
26 my %params=(open => "yes", total => "yes", percent => "yes", @_);
28 my $open=IkiWiki::yesno($params{open});
29 my $showtotal=IkiWiki::yesno($params{total});
30 my $showpercent=IkiWiki::yesno($params{percent});
31 $pagenum{$params{page}}++;
40 next unless $key =~ /^\d+/;
52 foreach my $choice (@choices) {
53 if ($open && exists $config{cgiurl}) {
54 # use POST to avoid robots
55 $ret.="<form method=\"POST\" action=\"$config{cgiurl}\">\n";
57 my $percent=$total > 0 ? int($choices{$choice} / $total * 100) : 0;
60 $ret.="$choice ($percent%)\n";
63 $ret.="$choice ($choices{$choice})\n";
65 if ($open && exists $config{cgiurl}) {
66 $ret.="<input type=\"hidden\" name=\"do\" value=\"poll\" />\n";
67 $ret.="<input type=\"hidden\" name=\"num\" value=\"$pagenum{$params{page}}\" />\n";
68 $ret.="<input type=\"hidden\" name=\"page\" value=\"$params{page}\" />\n";
69 $ret.="<input type=\"hidden\" name=\"choice\" value=\"$choice\" />\n";
70 $ret.="<input type=\"submit\" value=\"".gettext("vote")."\" />\n";
72 $ret.="</p>\n<hr class=poll align=left width=\"$percent%\"/>\n";
73 if ($open && exists $config{cgiurl}) {
78 $ret.="<span>".gettext("Total votes:")." $total</span>\n";
80 return "<div class=poll>$ret</div>";
86 if (defined $cgi->param('do') && $cgi->param('do') eq "poll") {
87 my $choice=decode_utf8($cgi->param('choice'));
88 if (! defined $choice) {
89 error("no choice specified");
91 my $num=$cgi->param('num');
93 error("no num specified");
95 my $page=IkiWiki::possibly_foolish_untaint($cgi->param('page'));
96 if (! defined $page || ! exists $pagesources{$page}) {
97 error("bad page name");
100 # Did they vote before? If so, let them change their vote,
101 # and check for dups.
102 my $choice_param="poll_choice_${page}_$num";
103 my $oldchoice=$session->param($choice_param);
104 if (defined $oldchoice && $oldchoice eq $choice) {
106 IkiWiki::redirect($cgi, urlto($page, undef, 1));
110 my $prefix=$config{prefix_directives} ? "!poll" : "poll";
112 my $content=readfile(srcfile($pagesources{$page}));
113 # Now parse the content, find the right poll,
114 # and find the choice within it, and increment its number.
115 # If they voted before, decrement that one.
119 return "\\[[$prefix $params]]" if $escape;
121 $params=~s/(^|\s+)(\d+)\s+"?\Q$choice\E"?(\s+|$)/$1.($2+1)." \"$choice\"".$3/se;
122 if (defined $oldchoice) {
123 $params=~s/(^|\s+)(\d+)\s+"?\Q$oldchoice\E"?(\s+|$)/$1.($2-1 >=0 ? $2-1 : 0)." \"$oldchoice\"".$3/se;
126 return "[[$prefix $params]]";
128 $content =~ s{(\\?)\[\[\Q$prefix\E\s+([^]]+)\s*\]\]}{$edit->($1, $2)}seg;
130 # Store their vote, update the page, and redirect to it.
131 writefile($pagesources{$page}, $config{srcdir}, $content);
132 $session->param($choice_param, $choice);
133 IkiWiki::cgi_savesession($session);
134 $oldchoice=$session->param($choice_param);
136 IkiWiki::disable_commit_hook();
137 IkiWiki::rcs_commit($pagesources{$page}, "poll vote ($choice)",
138 IkiWiki::rcs_prepedit($pagesources{$page}),
139 $session->param("name"), $ENV{REMOTE_ADDR});
140 IkiWiki::enable_commit_hook();
141 IkiWiki::rcs_update();
143 require IkiWiki::Render;
145 IkiWiki::saveindex();
147 # Need to set cookie in same http response that does the
149 eval q{use CGI::Cookie};
151 my $cookie = CGI::Cookie->new(-name=> $session->name, -value=> $session->id);
152 print $cgi->redirect(-cookie => $cookie,
153 -url => urlto($page, undef, 1));