2 package IkiWiki::Plugin::poll;
10 hook(type => "preprocess", id => "poll", call => \&preprocess);
11 hook(type => "sessioncgi", id => "poll", call => \&sessioncgi);
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 $showpercent=yesno($params{percent});
26 $pagenum{$params{page}}++;
35 next unless $key =~ /^\d+/;
47 foreach my $choice (@choices) {
48 if ($open && exists $config{cgiurl}) {
49 # use POST to avoid robots
50 $ret.="<form method=\"POST\" action=\"$config{cgiurl}\">\n";
52 my $percent=$total > 0 ? int($choices{$choice} / $total * 100) : 0;
55 $ret.="$choice ($percent%)\n";
58 $ret.="$choice ($choices{$choice})\n";
60 if ($open && exists $config{cgiurl}) {
61 $ret.="<input type=\"hidden\" name=\"do\" value=\"poll\" />\n";
62 $ret.="<input type=\"hidden\" name=\"num\" value=\"$pagenum{$params{page}}\" />\n";
63 $ret.="<input type=\"hidden\" name=\"page\" value=\"$params{page}\" />\n";
64 $ret.="<input type=\"hidden\" name=\"choice\" value=\"$choice\" />\n";
65 $ret.="<input type=\"submit\" value=\"".gettext("vote")."\" />\n";
67 $ret.="</p>\n<hr class=poll align=left width=\"$percent%\"/>\n";
68 if ($open && exists $config{cgiurl}) {
73 $ret.="<span>".gettext("Total votes:")." $total</span>\n";
75 return "<div class=poll>$ret</div>";
78 sub sessioncgi ($$) { #{{{
81 if (defined $cgi->param('do') && $cgi->param('do') eq "poll") {
82 my $choice=decode_utf8($cgi->param('choice'));
83 if (! defined $choice) {
84 error("no choice specified");
86 my $num=$cgi->param('num');
88 error("no num specified");
90 my $page=IkiWiki::possibly_foolish_untaint($cgi->param('page'));
91 if (! defined $page || ! exists $pagesources{$page}) {
92 error("bad page name");
95 # Did they vote before? If so, let them change their vote,
97 my $choice_param="poll_choice_${page}_$num";
98 my $oldchoice=$session->param($choice_param);
99 if (defined $oldchoice && $oldchoice eq $choice) {
101 IkiWiki::redirect($cgi, "$config{url}/".htmlpage($page));
105 my $prefix=$config{prefix_directives} ? "!poll" : "poll";
107 my $content=readfile(srcfile($pagesources{$page}));
108 # Now parse the content, find the right poll,
109 # and find the choice within it, and increment its number.
110 # If they voted before, decrement that one.
114 return "\\[[$prefix $params]]" if $escape;
116 $params=~s/(^|\s+)(\d+)\s+"?\Q$choice\E"?(\s+|$)/$1.($2+1)." \"$choice\"".$3/se;
117 if (defined $oldchoice) {
118 $params=~s/(^|\s+)(\d+)\s+"?\Q$oldchoice\E"?(\s+|$)/$1.($2-1 >=0 ? $2-1 : 0)." \"$oldchoice\"".$3/se;
121 return "[[$prefix $params]]";
123 $content =~ s{(\\?)\[\[\Q$prefix\E\s+([^]]+)\s*\]\]}{$edit->($1, $2)}seg;
125 # Store their vote, update the page, and redirect to it.
126 writefile($pagesources{$page}, $config{srcdir}, $content);
127 $session->param($choice_param, $choice);
128 IkiWiki::cgi_savesession($session);
129 $oldchoice=$session->param($choice_param);
131 IkiWiki::disable_commit_hook();
132 IkiWiki::rcs_commit($pagesources{$page}, "poll vote ($choice)",
133 IkiWiki::rcs_prepedit($pagesources{$page}),
134 $session->param("name"), $ENV{REMOTE_ADDR});
135 IkiWiki::enable_commit_hook();
136 IkiWiki::rcs_update();
138 require IkiWiki::Render;
140 IkiWiki::saveindex();
142 # Need to set cookie in same http response that does the
144 eval q{use CGI::Cookie};
146 my $cookie = CGI::Cookie->new(-name=> $session->name, -value=> $session->id);
147 print $cgi->redirect(-cookie => $cookie,
148 -url => "$config{url}/".htmlpage($page));