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);
26 my %params=(open => "yes", total => "yes", percent => "yes",
27 expandable => "no", @_);
29 my $open=IkiWiki::yesno($params{open});
30 my $showtotal=IkiWiki::yesno($params{total});
31 my $showpercent=IkiWiki::yesno($params{percent});
32 my $expandable=IkiWiki::yesno($params{expandable});
33 my $num=++$pagenum{$params{page}}{$params{destpage}};
42 next unless $key =~ /^\d+/;
54 foreach my $choice (@choices) {
55 if ($open && exists $config{cgiurl}) {
56 # use POST to avoid robots
57 $ret.="<form method=\"POST\" action=\"".IkiWiki::cgiurl()."\">\n";
59 my $percent=$total > 0 ? int($choices{$choice} / $total * 100) : 0;
62 $ret.="$choice ($percent%)\n";
65 $ret.="$choice ($choices{$choice})\n";
67 if ($open && exists $config{cgiurl}) {
68 $ret.="<input type=\"hidden\" name=\"do\" value=\"poll\" />\n";
69 $ret.="<input type=\"hidden\" name=\"num\" value=\"$num\" />\n";
70 $ret.="<input type=\"hidden\" name=\"page\" value=\"$params{page}\" />\n";
71 $ret.="<input type=\"hidden\" name=\"choice\" value=\"$choice\" />\n";
72 $ret.="<input type=\"submit\" value=\"".gettext("vote")."\" />\n";
74 $ret.="</p>\n<hr class=poll align=left width=\"$percent%\"/>\n";
75 if ($open && exists $config{cgiurl}) {
80 if ($expandable && $open && exists $config{cgiurl}) {
82 $ret.="<form method=\"POST\" action=\"".IkiWiki::cgiurl()."\">\n";
83 $ret.="<input type=\"hidden\" name=\"do\" value=\"poll\" />\n";
84 $ret.="<input type=\"hidden\" name=\"num\" value=\"$num\" />\n";
85 $ret.="<input type=\"hidden\" name=\"page\" value=\"$params{page}\" />\n";
86 $ret.=gettext("Write in").": <input name=\"choice\" size=50 />\n";
87 $ret.="<input type=\"submit\" value=\"".gettext("vote")."\" />\n";
93 $ret.="<span>".gettext("Total votes:")." $total</span>\n";
95 return "<div class=poll>$ret</div>";
101 if (defined $cgi->param('do') && $cgi->param('do') eq "poll") {
102 my $choice=decode_utf8(scalar $cgi->param('choice'));
103 if (! defined $choice || not length $choice) {
104 error("no choice specified");
106 my $num=$cgi->param('num');
107 if (! defined $num) {
108 error("no num specified");
110 my $page=IkiWiki::possibly_foolish_untaint($cgi->param('page'));
111 if (! defined $page || ! exists $pagesources{$page}) {
112 error("bad page name");
115 # Did they vote before? If so, let them change their vote,
116 # and check for dups.
117 my $choice_param="poll_choice_${page}_$num";
118 my $oldchoice=$session->param($choice_param);
119 if (defined $oldchoice && $oldchoice eq $choice) {
121 IkiWiki::redirect($cgi, urlto($page));
125 my $prefix=$config{prefix_directives} ? "!poll" : "poll";
127 my $content=readfile(srcfile($pagesources{$page}));
128 # Now parse the content, find the right poll,
129 # and find the choice within it, and increment its number.
130 # If they voted before, decrement that one.
134 return "\\[[$prefix $params]]" if $escape;
136 if ($params=~s/(^|\s+)(\d+)\s+"?\Q$choice\E"?(\s+|$)/$1.($2+1)." \"$choice\"".$3/se) {
138 elsif ($params=~/expandable=(\w+)/
139 & &IkiWiki::yesno($1)) {
140 $choice=~s/["\]\n\r]//g;
141 $params.=" 1 \"$choice\""
144 if (defined $oldchoice) {
145 $params=~s/(^|\s+)(\d+)\s+"?\Q$oldchoice\E"?(\s+|$)/$1.($2-1 >=0 ? $2-1 : 0)." \"$oldchoice\"".$3/se;
148 return "[[$prefix $params]]";
150 $content =~ s{(\\?)\[\[\Q$prefix\E\s+([^]]+)\s*\]\]}{$edit->($1, $2)}seg;
152 # Store their vote, update the page, and redirect to it.
153 writefile($pagesources{$page}, $config{srcdir}, $content);
154 $session->param($choice_param, $choice);
155 IkiWiki::cgi_savesession($session);
156 $oldchoice=$session->param($choice_param);
158 IkiWiki::disable_commit_hook();
160 file => $pagesources{$page},
161 message => "poll vote ($choice)",
162 token => IkiWiki::rcs_prepedit($pagesources{$page}),
165 IkiWiki::enable_commit_hook();
166 IkiWiki::rcs_update();
168 require IkiWiki::Render;
170 IkiWiki::saveindex();
172 # Need to set cookie in same http response that does the
174 eval q{use CGI::Cookie};
176 my $cookie = CGI::Cookie->new(-name=> $session->name, -value=> $session->id);
177 print $cgi->redirect(-cookie => $cookie,
178 -url => urlto($page));