2 package IkiWiki::Plugin::poll;
9 hook(type => "preprocess", id => "poll", call => \&preprocess);
10 hook(type => "sessioncgi", id => "poll", call => \&sessioncgi);
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>";
77 sub sessioncgi ($$) { #{{{
80 if (defined $cgi->param('do') && $cgi->param('do') eq "poll") {
81 my $choice=$cgi->param('choice');
82 if (! defined $choice) {
83 error("no choice specified");
85 my $num=$cgi->param('num');
87 error("no num specified");
89 my $page=IkiWiki::possibly_foolish_untaint($cgi->param('page'));
90 if (! defined $page || ! exists $pagesources{$page}) {
91 error("bad page name");
94 # Did they vote before? If so, let them change their vote,
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 $prefix=$config{prefix_directives} ? "!poll" : "poll";
106 my $content=readfile(srcfile($pagesources{$page}));
107 # Now parse the content, find the right poll,
108 # and find the choice within it, and increment its number.
109 # If they voted before, decrement that one.
113 return "\\[[$prefix $params]]" if $escape;
115 $params=~s/(^|\s+)(\d+)\s+"?\Q$choice\E"?(\s+|$)/$1.($2+1)." \"$choice\"".$3/se;
116 if (defined $oldchoice) {
117 $params=~s/(^|\s+)(\d+)\s+"?\Q$oldchoice\E"?(\s+|$)/$1.($2-1 >=0 ? $2-1 : 0)." \"$oldchoice\"".$3/se;
120 return "[[$prefix $params]]";
122 $content =~ s{(\\?)\[\[\Q$prefix\E\s+([^]]+)\s*\]\]}{$edit->($1, $2)}seg;
124 # Store their vote, update the page, and redirect to it.
125 writefile($pagesources{$page}, $config{srcdir}, $content);
126 $session->param($choice_param, $choice);
127 IkiWiki::cgi_savesession($session);
128 $oldchoice=$session->param($choice_param);
130 IkiWiki::disable_commit_hook();
131 IkiWiki::rcs_commit($pagesources{$page}, "poll vote ($choice)",
132 IkiWiki::rcs_prepedit($pagesources{$page}),
133 $session->param("name"), $ENV{REMOTE_ADDR});
134 IkiWiki::enable_commit_hook();
135 IkiWiki::rcs_update();
137 require IkiWiki::Render;
139 IkiWiki::saveindex();
141 # Need to set cookie in same http response that does the
143 eval q{use CGI::Cookie};
145 my $cookie = CGI::Cookie->new(-name=> $session->name, -value=> $session->id);
146 print $cgi->redirect(-cookie => $cookie,
147 -url => "$config{url}/".htmlpage($page));