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);
15 sub getsetup () { #{{{
24 sub preprocess (@) { #{{{
25 my %params=(open => "yes", total => "yes", percent => "yes", @_);
27 my $open=IkiWiki::yesno($params{open});
28 my $showtotal=IkiWiki::yesno($params{total});
29 my $showpercent=IkiWiki::yesno($params{percent});
30 $pagenum{$params{page}}++;
39 next unless $key =~ /^\d+/;
51 foreach my $choice (@choices) {
52 if ($open && exists $config{cgiurl}) {
53 # use POST to avoid robots
54 $ret.="<form method=\"POST\" action=\"$config{cgiurl}\">\n";
56 my $percent=$total > 0 ? int($choices{$choice} / $total * 100) : 0;
59 $ret.="$choice ($percent%)\n";
62 $ret.="$choice ($choices{$choice})\n";
64 if ($open && exists $config{cgiurl}) {
65 $ret.="<input type=\"hidden\" name=\"do\" value=\"poll\" />\n";
66 $ret.="<input type=\"hidden\" name=\"num\" value=\"$pagenum{$params{page}}\" />\n";
67 $ret.="<input type=\"hidden\" name=\"page\" value=\"$params{page}\" />\n";
68 $ret.="<input type=\"hidden\" name=\"choice\" value=\"$choice\" />\n";
69 $ret.="<input type=\"submit\" value=\"".gettext("vote")."\" />\n";
71 $ret.="</p>\n<hr class=poll align=left width=\"$percent%\"/>\n";
72 if ($open && exists $config{cgiurl}) {
77 $ret.="<span>".gettext("Total votes:")." $total</span>\n";
79 return "<div class=poll>$ret</div>";
82 sub sessioncgi ($$) { #{{{
85 if (defined $cgi->param('do') && $cgi->param('do') eq "poll") {
86 my $choice=decode_utf8($cgi->param('choice'));
87 if (! defined $choice) {
88 error("no choice specified");
90 my $num=$cgi->param('num');
92 error("no num specified");
94 my $page=IkiWiki::possibly_foolish_untaint($cgi->param('page'));
95 if (! defined $page || ! exists $pagesources{$page}) {
96 error("bad page name");
99 # Did they vote before? If so, let them change their vote,
100 # and check for dups.
101 my $choice_param="poll_choice_${page}_$num";
102 my $oldchoice=$session->param($choice_param);
103 if (defined $oldchoice && $oldchoice eq $choice) {
105 IkiWiki::redirect($cgi, urlto($page, undef, 1));
109 my $prefix=$config{prefix_directives} ? "!poll" : "poll";
111 my $content=readfile(srcfile($pagesources{$page}));
112 # Now parse the content, find the right poll,
113 # and find the choice within it, and increment its number.
114 # If they voted before, decrement that one.
118 return "\\[[$prefix $params]]" if $escape;
120 $params=~s/(^|\s+)(\d+)\s+"?\Q$choice\E"?(\s+|$)/$1.($2+1)." \"$choice\"".$3/se;
121 if (defined $oldchoice) {
122 $params=~s/(^|\s+)(\d+)\s+"?\Q$oldchoice\E"?(\s+|$)/$1.($2-1 >=0 ? $2-1 : 0)." \"$oldchoice\"".$3/se;
125 return "[[$prefix $params]]";
127 $content =~ s{(\\?)\[\[\Q$prefix\E\s+([^]]+)\s*\]\]}{$edit->($1, $2)}seg;
129 # Store their vote, update the page, and redirect to it.
130 writefile($pagesources{$page}, $config{srcdir}, $content);
131 $session->param($choice_param, $choice);
132 IkiWiki::cgi_savesession($session);
133 $oldchoice=$session->param($choice_param);
135 IkiWiki::disable_commit_hook();
136 IkiWiki::rcs_commit($pagesources{$page}, "poll vote ($choice)",
137 IkiWiki::rcs_prepedit($pagesources{$page}),
138 $session->param("name"), $ENV{REMOTE_ADDR});
139 IkiWiki::enable_commit_hook();
140 IkiWiki::rcs_update();
142 require IkiWiki::Render;
144 IkiWiki::saveindex();
146 # Need to set cookie in same http response that does the
148 eval q{use CGI::Cookie};
150 my $cookie = CGI::Cookie->new(-name=> $session->name, -value=> $session->id);
151 print $cgi->redirect(-cookie => $cookie,
152 -url => urlto($page, undef, 1));