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