2 package IkiWiki::Plugin::poll;
10 hook(type => "preprocess", id => "poll", call => \&preprocess);
11 hook(type => "cgi", id => "poll", call => \&cgi);
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 my $percent=$total > 0 ? int($choices{$choice} / $total * 100) : 0;
50 $ret.="$choice ($percent%) ";
53 $ret.="$choice ($choices{$choice}) ";
55 if ($open && exists $config{cgiurl}) {
56 my $url=URI->new($config{cgiurl});
59 "num" => $pagenum{$params{page}},
60 "page" => $params{page},
63 $ret.="<a class=pollbutton href=\"$url\">vote</a>";
65 $ret.="<br />\n<hr class=poll align=left width=\"$percent%\"/>\n";
68 $ret.="<span>Total votes: $total</span>\n";
70 return "<div class=poll>$ret</div>";
75 if (defined $cgi->param('do') && $cgi->param('do') eq "poll") {
76 my $choice=$cgi->param('choice');
77 if (! defined $choice) {
78 error("no choice specified");
80 my $num=$cgi->param('num');
82 error("no num specified");
84 my $page=IkiWiki::possibly_foolish_untaint($cgi->param('page'));
85 if (! defined $page || ! exists $pagesources{$page}) {
86 error("bad page name");
89 # Did they vote before? If so, let them change their vote,
91 my $session=IkiWiki::cgi_getsession();
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 $content=readfile(srcfile($pagesources{$page}));
101 # Now parse the content, find the right poll,
102 # and find the choice within it, and increment its number.
103 # If they voted before, decrement that one.
107 return "\\[[poll $params]]" if $escape;
109 $params=~s/(^|\s+)(\d+)\s+"?\Q$choice\E"?(\s+|$)/$1.($2+1)." \"$choice\"".$3/se;
110 if (defined $oldchoice) {
111 $params=~s/(^|\s+)(\d+)\s+"?\Q$oldchoice\E"?(\s+|$)/$1.($2-1 >=0 ? $2-1 : 0)." \"$oldchoice\"".$3/se;
114 return "[[poll $params]]";
116 $content =~ s{(\\?)\[\[poll\s+([^]]+)\s*\]\]}{$edit->($1, $2)}seg;
118 # Store their vote, update the page, and redirect to it.
119 writefile($pagesources{$page}, $config{srcdir}, $content);
120 $session->param($choice_param, $choice);
121 IkiWiki::cgi_savesession($session);
122 $oldchoice=$session->param($choice_param);
124 # prevent deadlock with post-commit hook
125 IkiWiki::unlockwiki();
126 IkiWiki::rcs_commit($pagesources{$page}, "poll vote",
127 IkiWiki::rcs_prepedit($pagesources{$page}),
128 $session->param("name"), $ENV{REMOTE_ADDR});
131 require IkiWiki::Render;
133 IkiWiki::saveindex();
135 # Need to set cookie in same http response that does the
137 eval q{use CGI::Cookie};
139 my $cookie = CGI::Cookie->new(-name=> $session->name, -value=> $session->id);
140 print $cgi->redirect(-cookie => $cookie,
141 -url => "$config{url}/".htmlpage($page));