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