2 package IkiWiki::Plugin::poll;
10 hook(type => "preprocess", id => "poll", call => \&preprocess);
11 hook(type => "sessioncgi", id => "poll", call => \&sessioncgi);
15 sub preprocess (@) { #{{{
16 my %params=(open => "yes", total => "yes", percent => "yes", @_);
18 my $open=IkiWIki::yesno($params{open});
19 my $showtotal=IkiWiki::yesno($params{total});
20 my $showpercent=IkiWiki::yesno($params{percent});
21 $pagenum{$params{page}}++;
30 next unless $key =~ /^\d+/;
42 foreach my $choice (@choices) {
43 if ($open && exists $config{cgiurl}) {
44 # use POST to avoid robots
45 $ret.="<form method=\"POST\" action=\"$config{cgiurl}\">\n";
47 my $percent=$total > 0 ? int($choices{$choice} / $total * 100) : 0;
50 $ret.="$choice ($percent%)\n";
53 $ret.="$choice ($choices{$choice})\n";
55 if ($open && exists $config{cgiurl}) {
56 $ret.="<input type=\"hidden\" name=\"do\" value=\"poll\" />\n";
57 $ret.="<input type=\"hidden\" name=\"num\" value=\"$pagenum{$params{page}}\" />\n";
58 $ret.="<input type=\"hidden\" name=\"page\" value=\"$params{page}\" />\n";
59 $ret.="<input type=\"hidden\" name=\"choice\" value=\"$choice\" />\n";
60 $ret.="<input type=\"submit\" value=\"".gettext("vote")."\" />\n";
62 $ret.="</p>\n<hr class=poll align=left width=\"$percent%\"/>\n";
63 if ($open && exists $config{cgiurl}) {
68 $ret.="<span>".gettext("Total votes:")." $total</span>\n";
70 return "<div class=poll>$ret</div>";
73 sub sessioncgi ($$) { #{{{
76 if (defined $cgi->param('do') && $cgi->param('do') eq "poll") {
77 my $choice=decode_utf8($cgi->param('choice'));
78 if (! defined $choice) {
79 error("no choice specified");
81 my $num=$cgi->param('num');
83 error("no num specified");
85 my $page=IkiWiki::possibly_foolish_untaint($cgi->param('page'));
86 if (! defined $page || ! exists $pagesources{$page}) {
87 error("bad page name");
90 # Did they vote before? If so, let them change their vote,
92 my $choice_param="poll_choice_${page}_$num";
93 my $oldchoice=$session->param($choice_param);
94 if (defined $oldchoice && $oldchoice eq $choice) {
96 IkiWiki::redirect($cgi, "$config{url}/".htmlpage($page));
100 my $prefix=$config{prefix_directives} ? "!poll" : "poll";
102 my $content=readfile(srcfile($pagesources{$page}));
103 # Now parse the content, find the right poll,
104 # and find the choice within it, and increment its number.
105 # If they voted before, decrement that one.
109 return "\\[[$prefix $params]]" if $escape;
111 $params=~s/(^|\s+)(\d+)\s+"?\Q$choice\E"?(\s+|$)/$1.($2+1)." \"$choice\"".$3/se;
112 if (defined $oldchoice) {
113 $params=~s/(^|\s+)(\d+)\s+"?\Q$oldchoice\E"?(\s+|$)/$1.($2-1 >=0 ? $2-1 : 0)." \"$oldchoice\"".$3/se;
116 return "[[$prefix $params]]";
118 $content =~ s{(\\?)\[\[\Q$prefix\E\s+([^]]+)\s*\]\]}{$edit->($1, $2)}seg;
120 # Store their vote, update the page, and redirect to it.
121 writefile($pagesources{$page}, $config{srcdir}, $content);
122 $session->param($choice_param, $choice);
123 IkiWiki::cgi_savesession($session);
124 $oldchoice=$session->param($choice_param);
126 IkiWiki::disable_commit_hook();
127 IkiWiki::rcs_commit($pagesources{$page}, "poll vote ($choice)",
128 IkiWiki::rcs_prepedit($pagesources{$page}),
129 $session->param("name"), $ENV{REMOTE_ADDR});
130 IkiWiki::enable_commit_hook();
131 IkiWiki::rcs_update();
133 require IkiWiki::Render;
135 IkiWiki::saveindex();
137 # Need to set cookie in same http response that does the
139 eval q{use CGI::Cookie};
141 my $cookie = CGI::Cookie->new(-name=> $session->name, -value=> $session->id);
142 print $cgi->redirect(-cookie => $cookie,
143 -url => "$config{url}/".htmlpage($page));