]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/poll.pm
inadvertent improvement
[git.ikiwiki.info.git] / IkiWiki / Plugin / poll.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::poll;
4 use warnings;
5 use strict;
6 use IkiWiki 3.00;
7 use Encode;
9 sub import {
10         hook(type => "getsetup", id => "poll", call => \&getsetup);
11         hook(type => "preprocess", id => "poll", call => \&preprocess);
12         hook(type => "sessioncgi", id => "poll", call => \&sessioncgi);
13 }
15 sub getsetup () {
16         return 
17                 plugin => {
18                         safe => 1,
19                         rebuild => undef,
20                         section => "widget",
21                 },
22 }
24 my %pagenum;
25 sub preprocess (@) {
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}};
35         my %choices;
36         my @choices;
37         my $total=0;
38         while (@_) {
39                 my $key=shift;
40                 my $value=shift;
42                 next unless $key =~ /^\d+/;
44                 my $num=$key;
45                 $key=shift;
46                 $value=shift;
48                 $choices{$key}=$num;
49                 push @choices, $key;
50                 $total+=$num;
51         }
53         my $ret="";
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";
58                 }
59                 my $percent=$total > 0 ? int($choices{$choice} / $total * 100) : 0;
60                 $ret.="<p>\n";
61                 if ($showpercent) {
62                         $ret.="$choice ($percent%)\n";
63                 }
64                 else {
65                         $ret.="$choice ($choices{$choice})\n";
66                 }
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";
74                         }
75                         if (defined $params{posttrail}) {
76                                 $ret.="<input type=\"hidden\" name=\"posttrail\" value=\"".linkpage($params{posttrail})."\" />\n";
77                         }
78                         $ret.="<input type=\"submit\" value=\"".gettext("vote")."\" />\n";
79                 }
80                 $ret.="</p>\n<hr class=poll align=left width=\"$percent%\"/>\n";
81                 if ($open && exists $config{cgiurl}) {
82                         $ret.="</form>\n";
83                 }
84         }
85         
86         if ($expandable && $open && exists $config{cgiurl}) {
87                 $ret.="<p>\n";
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";
94                 $ret.="</form>\n";
95                 $ret.="</p>\n";
96         }
98         if ($showtotal) {
99                 $ret.="<span>".gettext("Total votes:")." $total</span>\n";
100         }
101         return "<div class=poll>$ret</div>";
104 sub sessioncgi ($$) {
105         my $cgi=shift;
106         my $session=shift;
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");
111                 }
112                 my $num=$cgi->param('num');
113                 if (! defined $num) {
114                         error("no num specified");
115                 }
116                 my $page=IkiWiki::possibly_foolish_untaint($cgi->param('page'));
117                 if (! defined $page || ! exists $pagesources{$page}) {
118                         error("bad page name");
119                 }
121                 my $postvote=urlto($page);
122                 if (defined $cgi->param('postlink') && length $cgi->param('postlink')) {
123                         $postvote=urlto(bestlink($page, $cgi->param('postlink')));
124                 }
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);
130                         }
131                 }
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) {
138                         # Same vote; no-op.
139                         IkiWiki::redirect($cgi, $postvote);
140                         exit;
141                 }
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.
149                 my $edit=sub {
150                         my $escape=shift;
151                         my $params=shift;
152                         return "\\[[$prefix $params]]" if $escape;
153                         if (--$num == 0) {
154                                 if ($params=~s/(^|\s+)(\d+)\s+"?\Q$choice\E"?(\s+|$)/$1.($2+1)." \"$choice\"".$3/se) {
155                                 }
156                                 elsif ($params=~/expandable=(\w+)/
157                                     & &IkiWiki::yesno($1)) {
158                                         $choice=~s/["\]\n\r]//g;
159                                         $params.=" 1 \"$choice\""
160                                                 if length $choice;
161                                 }
162                                 if (defined $oldchoice) {
163                                         $params=~s/(^|\s+)(\d+)\s+"?\Q$oldchoice\E"?(\s+|$)/$1.($2-1 >=0 ? $2-1 : 0)." \"$oldchoice\"".$3/se;
164                                 }
165                         }
166                         return "[[$prefix $params]]";
167                 };
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);
175                 if ($config{rcs}) {
176                         IkiWiki::disable_commit_hook();
177                         IkiWiki::rcs_commit(
178                                 file => $pagesources{$page},
179                                 message => "poll vote ($choice)",
180                                 token => IkiWiki::rcs_prepedit($pagesources{$page}),
181                                 session => $session,
182                         );
183                         IkiWiki::enable_commit_hook();
184                         IkiWiki::rcs_update();
185                 }
186                 require IkiWiki::Render;
187                 IkiWiki::refresh();
188                 IkiWiki::saveindex();
190                 # Need to set cookie in same http response that does the
191                 # redir.
192                 eval q{use CGI::Cookie};
193                 error($@) if $@;
194                 my $cookie = CGI::Cookie->new(-name=> $session->name, -value=> $session->id);
195                 print $cgi->redirect(-cookie => $cookie, -url => $postvote);
196                 exit;
197         }