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 if (defined $params{postlink}) {
73 $ret.="<input type=\"hidden\" name=\"postlink\" value=\"".linkpage($params{postlink})."\" />\n";
75 if (defined $params{posttrail}) {
76 $ret.="<input type=\"hidden\" name=\"posttrail\" value=\"".linkpage($params{posttrail})."\" />\n";
78 $ret.="<input type=\"submit\" value=\"".gettext("vote")."\" />\n";
80 $ret.="</p>\n<hr class=poll align=left width=\"$percent%\"/>\n";
81 if ($open && exists $config{cgiurl}) {
86 if ($expandable && $open && exists $config{cgiurl}) {
88 $ret.="<form method=\"POST\" action=\"".IkiWiki::cgiurl()."\">\n";
89 $ret.="<input type=\"hidden\" name=\"do\" value=\"poll\" />\n";
90 $ret.="<input type=\"hidden\" name=\"num\" value=\"$num\" />\n";
91 $ret.="<input type=\"hidden\" name=\"page\" value=\"$params{page}\" />\n";
92 $ret.=gettext("Write in").": <input name=\"choice\" size=50 />\n";
93 $ret.="<input type=\"submit\" value=\"".gettext("vote")."\" />\n";
99 $ret.="<span>".gettext("Total votes:")." $total</span>\n";
101 return "<div class=poll>$ret</div>";
104 sub sessioncgi ($$) {
107 if (defined $cgi->param('do') && $cgi->param('do') eq "poll") {
108 my $choice=decode_utf8(scalar $cgi->param('choice'));
109 if (! defined $choice || not length $choice) {
110 error("no choice specified");
112 my $num=$cgi->param('num');
113 if (! defined $num) {
114 error("no num specified");
116 my $page=IkiWiki::possibly_foolish_untaint($cgi->param('page'));
117 if (! defined $page || ! exists $pagesources{$page}) {
118 error("bad page name");
121 my $postvote=urlto($page);
122 if (defined $cgi->param('postlink') && length $cgi->param('postlink')) {
123 $postvote=urlto(bestlink($page, $cgi->param('postlink')));
125 elsif (defined $cgi->param('posttrail') && length $cgi->param('posttrail')) {
126 my $trailname=bestlink($page, $cgi->param('posttrail'));
127 my $trailnext=$pagestate{$page}{trail}{item}{$trailname}[1];
128 if (defined $trailnext) {
129 $postvote=urlto($trailnext);
133 # Did they vote before? If so, let them change their vote,
134 # and check for dups.
135 my $choice_param="poll_choice_${page}_$num";
136 my $oldchoice=$session->param($choice_param);
137 if (defined $oldchoice && $oldchoice eq $choice) {
139 IkiWiki::redirect($cgi, $postvote);
143 my $prefix=$config{prefix_directives} ? "!poll" : "poll";
145 my $content=readfile(srcfile($pagesources{$page}));
146 # Now parse the content, find the right poll,
147 # and find the choice within it, and increment its number.
148 # If they voted before, decrement that one.
152 return "\\[[$prefix $params]]" if $escape;
154 if ($params=~s/(^|\s+)(\d+)\s+"?\Q$choice\E"?(\s+|$)/$1.($2+1)." \"$choice\"".$3/se) {
156 elsif ($params=~/expandable=(\w+)/
157 & &IkiWiki::yesno($1)) {
158 $choice=~s/["\]\n\r]//g;
159 $params.=" 1 \"$choice\""
162 if (defined $oldchoice) {
163 $params=~s/(^|\s+)(\d+)\s+"?\Q$oldchoice\E"?(\s+|$)/$1.($2-1 >=0 ? $2-1 : 0)." \"$oldchoice\"".$3/se;
166 return "[[$prefix $params]]";
168 $content =~ s{(\\?)\[\[\Q$prefix\E\s+([^]]+)\s*\]\]}{$edit->($1, $2)}seg;
170 # Store their vote, update the page, and redirect.
171 writefile($pagesources{$page}, $config{srcdir}, $content);
172 $session->param($choice_param, $choice);
173 IkiWiki::cgi_savesession($session);
174 $oldchoice=$session->param($choice_param);
176 IkiWiki::disable_commit_hook();
178 file => $pagesources{$page},
179 message => "poll vote ($choice)",
180 token => IkiWiki::rcs_prepedit($pagesources{$page}),
183 IkiWiki::enable_commit_hook();
184 IkiWiki::rcs_update();
186 require IkiWiki::Render;
188 IkiWiki::saveindex();
190 # Need to set cookie in same http response that does the
192 eval q{use CGI::Cookie};
194 my $cookie = CGI::Cookie->new(-name=> $session->name, -value=> $session->id);
195 print $cgi->redirect(-cookie => $cookie, -url => $postvote);