]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/poll.pm
web commit by http://jcflack.myopenid.com/
[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 sub yesno ($) { #{{{
15         my $val=shift;
16         return (defined $val && lc($val) eq "yes");
17 } #}}}
19 my %pagenum;
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}}++;
28         my %choices;
29         my @choices;
30         my $total=0;
31         while (@_) {
32                 my $key=shift;
33                 my $value=shift;
35                 next unless $key =~ /^\d+/;
37                 my $num=$key;
38                 $key=shift;
39                 $value=shift;
41                 $choices{$key}=$num;
42                 push @choices, $key;
43                 $total+=$num;
44         }
46         my $ret="";
47         foreach my $choice (@choices) {
48                 if ($open && exists $config{cgiurl}) {
49                         # use POST to avoid robots
50                         $ret.="<form method=\"POST\" action=\"$config{cgiurl}\">\n";
51                 }
52                 my $percent=$total > 0 ? int($choices{$choice} / $total * 100) : 0;
53                 $ret.="<p>\n";
54                 if ($showpercent) {
55                         $ret.="$choice ($percent%)\n";
56                 }
57                 else {
58                         $ret.="$choice ($choices{$choice})\n";
59                 }
60                 if ($open && exists $config{cgiurl}) {
61                         $ret.="<input type=\"hidden\" name=\"do\" value=\"poll\" />\n";
62                         $ret.="<input type=\"hidden\" name=\"num\" value=\"$pagenum{$params{page}}\" />\n";
63                         $ret.="<input type=\"hidden\" name=\"page\" value=\"$params{page}\" />\n";
64                         $ret.="<input type=\"hidden\" name=\"choice\" value=\"$choice\" />\n";
65                         $ret.="<input type=\"submit\" value=\"".gettext("vote")."\" />\n";
66                 }
67                 $ret.="</p>\n<hr class=poll align=left width=\"$percent%\"/>\n";
68                 if ($open && exists $config{cgiurl}) {
69                         $ret.="</form>\n";
70                 }
71         }
72         if ($showtotal) {
73                 $ret.="<span>".gettext("Total votes:")." $total</span>\n";
74         }
75         return "<div class=poll>$ret</div>";
76 } # }}}
78 sub sessioncgi ($$) { #{{{
79         my $cgi=shift;
80         my $session=shift;
81         if (defined $cgi->param('do') && $cgi->param('do') eq "poll") {
82                 my $choice=decode_utf8($cgi->param('choice'));
83                 if (! defined $choice) {
84                         error("no choice specified");
85                 }
86                 my $num=$cgi->param('num');
87                 if (! defined $num) {
88                         error("no num specified");
89                 }
90                 my $page=IkiWiki::possibly_foolish_untaint($cgi->param('page'));
91                 if (! defined $page || ! exists $pagesources{$page}) {
92                         error("bad page name");
93                 }
95                 # Did they vote before? If so, let them change their vote,
96                 # and check for dups.
97                 my $choice_param="poll_choice_${page}_$num";
98                 my $oldchoice=$session->param($choice_param);
99                 if (defined $oldchoice && $oldchoice eq $choice) {
100                         # Same vote; no-op.
101                         IkiWiki::redirect($cgi, "$config{url}/".htmlpage($page));
102                         exit;
103                 }
105                 my $prefix=$config{prefix_directives} ? "!poll" : "poll";
107                 my $content=readfile(srcfile($pagesources{$page}));
108                 # Now parse the content, find the right poll,
109                 # and find the choice within it, and increment its number.
110                 # If they voted before, decrement that one.
111                 my $edit=sub {
112                         my $escape=shift;
113                         my $params=shift;
114                         return "\\[[$prefix $params]]" if $escape;
115                         if (--$num == 0) {
116                                 $params=~s/(^|\s+)(\d+)\s+"?\Q$choice\E"?(\s+|$)/$1.($2+1)." \"$choice\"".$3/se;
117                                 if (defined $oldchoice) {
118                                         $params=~s/(^|\s+)(\d+)\s+"?\Q$oldchoice\E"?(\s+|$)/$1.($2-1 >=0 ? $2-1 : 0)." \"$oldchoice\"".$3/se;
119                                 }
120                         }
121                         return "[[$prefix $params]]";
122                 };
123                 $content =~ s{(\\?)\[\[\Q$prefix\E\s+([^]]+)\s*\]\]}{$edit->($1, $2)}seg;
125                 # Store their vote, update the page, and redirect to it.
126                 writefile($pagesources{$page}, $config{srcdir}, $content);
127                 $session->param($choice_param, $choice);
128                 IkiWiki::cgi_savesession($session);
129                 $oldchoice=$session->param($choice_param);
130                 if ($config{rcs}) {
131                         IkiWiki::disable_commit_hook();
132                         IkiWiki::rcs_commit($pagesources{$page}, "poll vote ($choice)",
133                                 IkiWiki::rcs_prepedit($pagesources{$page}),
134                                 $session->param("name"), $ENV{REMOTE_ADDR});
135                         IkiWiki::enable_commit_hook();
136                         IkiWiki::rcs_update();
137                 }
138                 require IkiWiki::Render;
139                 IkiWiki::refresh();
140                 IkiWiki::saveindex();
142                 # Need to set cookie in same http response that does the
143                 # redir.
144                 eval q{use CGI::Cookie};
145                 error($@) if $@;
146                 my $cookie = CGI::Cookie->new(-name=> $session->name, -value=> $session->id);
147                 print $cgi->redirect(-cookie => $cookie,
148                         -url => "$config{url}/".htmlpage($page));
149                 exit;
150         }
151 } #}}}