]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/poll.pm
really add
[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 2.00;
7 use Encode;
9 sub import { #{{{
10         hook(type => "preprocess", id => "poll", call => \&preprocess);
11         hook(type => "sessioncgi", id => "poll", call => \&sessioncgi);
12 } # }}}
14 my %pagenum;
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}}++;
23         my %choices;
24         my @choices;
25         my $total=0;
26         while (@_) {
27                 my $key=shift;
28                 my $value=shift;
30                 next unless $key =~ /^\d+/;
32                 my $num=$key;
33                 $key=shift;
34                 $value=shift;
36                 $choices{$key}=$num;
37                 push @choices, $key;
38                 $total+=$num;
39         }
41         my $ret="";
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";
46                 }
47                 my $percent=$total > 0 ? int($choices{$choice} / $total * 100) : 0;
48                 $ret.="<p>\n";
49                 if ($showpercent) {
50                         $ret.="$choice ($percent%)\n";
51                 }
52                 else {
53                         $ret.="$choice ($choices{$choice})\n";
54                 }
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";
61                 }
62                 $ret.="</p>\n<hr class=poll align=left width=\"$percent%\"/>\n";
63                 if ($open && exists $config{cgiurl}) {
64                         $ret.="</form>\n";
65                 }
66         }
67         if ($showtotal) {
68                 $ret.="<span>".gettext("Total votes:")." $total</span>\n";
69         }
70         return "<div class=poll>$ret</div>";
71 } # }}}
73 sub sessioncgi ($$) { #{{{
74         my $cgi=shift;
75         my $session=shift;
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");
80                 }
81                 my $num=$cgi->param('num');
82                 if (! defined $num) {
83                         error("no num specified");
84                 }
85                 my $page=IkiWiki::possibly_foolish_untaint($cgi->param('page'));
86                 if (! defined $page || ! exists $pagesources{$page}) {
87                         error("bad page name");
88                 }
90                 # Did they vote before? If so, let them change their vote,
91                 # and check for dups.
92                 my $choice_param="poll_choice_${page}_$num";
93                 my $oldchoice=$session->param($choice_param);
94                 if (defined $oldchoice && $oldchoice eq $choice) {
95                         # Same vote; no-op.
96                         IkiWiki::redirect($cgi, "$config{url}/".htmlpage($page));
97                         exit;
98                 }
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.
106                 my $edit=sub {
107                         my $escape=shift;
108                         my $params=shift;
109                         return "\\[[$prefix $params]]" if $escape;
110                         if (--$num == 0) {
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;
114                                 }
115                         }
116                         return "[[$prefix $params]]";
117                 };
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);
125                 if ($config{rcs}) {
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();
132                 }
133                 require IkiWiki::Render;
134                 IkiWiki::refresh();
135                 IkiWiki::saveindex();
137                 # Need to set cookie in same http response that does the
138                 # redir.
139                 eval q{use CGI::Cookie};
140                 error($@) if $@;
141                 my $cookie = CGI::Cookie->new(-name=> $session->name, -value=> $session->id);
142                 print $cgi->redirect(-cookie => $cookie,
143                         -url => "$config{url}/".htmlpage($page));
144                 exit;
145         }
146 } #}}}