]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/poll.pm
Replace Verisign logo with one done in Inkscape
[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                         $ret.="<input type=\"submit\" value=\"".gettext("vote")."\" />\n";
73                 }
74                 $ret.="</p>\n<hr class=poll align=left width=\"$percent%\"/>\n";
75                 if ($open && exists $config{cgiurl}) {
76                         $ret.="</form>\n";
77                 }
78         }
79         
80         if ($expandable && $open && exists $config{cgiurl}) {
81                 $ret.="<p>\n";
82                 $ret.="<form method=\"POST\" action=\"".IkiWiki::cgiurl()."\">\n";
83                 $ret.="<input type=\"hidden\" name=\"do\" value=\"poll\" />\n";
84                 $ret.="<input type=\"hidden\" name=\"num\" value=\"$num\" />\n";
85                 $ret.="<input type=\"hidden\" name=\"page\" value=\"$params{page}\" />\n";
86                 $ret.=gettext("Write in").": <input name=\"choice\" size=50 />\n";
87                 $ret.="<input type=\"submit\" value=\"".gettext("vote")."\" />\n";
88                 $ret.="</form>\n";
89                 $ret.="</p>\n";
90         }
92         if ($showtotal) {
93                 $ret.="<span>".gettext("Total votes:")." $total</span>\n";
94         }
95         return "<div class=poll>$ret</div>";
96 }
98 sub sessioncgi ($$) {
99         my $cgi=shift;
100         my $session=shift;
101         if (defined $cgi->param('do') && $cgi->param('do') eq "poll") {
102                 my $choice=decode_utf8($cgi->param('choice'));
103                 if (! defined $choice || not length $choice) {
104                         error("no choice specified");
105                 }
106                 my $num=$cgi->param('num');
107                 if (! defined $num) {
108                         error("no num specified");
109                 }
110                 my $page=IkiWiki::possibly_foolish_untaint($cgi->param('page'));
111                 if (! defined $page || ! exists $pagesources{$page}) {
112                         error("bad page name");
113                 }
115                 # Did they vote before? If so, let them change their vote,
116                 # and check for dups.
117                 my $choice_param="poll_choice_${page}_$num";
118                 my $oldchoice=$session->param($choice_param);
119                 if (defined $oldchoice && $oldchoice eq $choice) {
120                         # Same vote; no-op.
121                         IkiWiki::redirect($cgi, urlto($page));
122                         exit;
123                 }
125                 my $prefix=$config{prefix_directives} ? "!poll" : "poll";
127                 my $content=readfile(srcfile($pagesources{$page}));
128                 # Now parse the content, find the right poll,
129                 # and find the choice within it, and increment its number.
130                 # If they voted before, decrement that one.
131                 my $edit=sub {
132                         my $escape=shift;
133                         my $params=shift;
134                         return "\\[[$prefix $params]]" if $escape;
135                         if (--$num == 0) {
136                                 if ($params=~s/(^|\s+)(\d+)\s+"?\Q$choice\E"?(\s+|$)/$1.($2+1)." \"$choice\"".$3/se) {
137                                 }
138                                 elsif ($params=~/expandable=(\w+)/
139                                     & &IkiWiki::yesno($1)) {
140                                         $choice=~s/["\]\n\r]//g;
141                                         $params.=" 1 \"$choice\""
142                                                 if length $choice;
143                                 }
144                                 if (defined $oldchoice) {
145                                         $params=~s/(^|\s+)(\d+)\s+"?\Q$oldchoice\E"?(\s+|$)/$1.($2-1 >=0 ? $2-1 : 0)." \"$oldchoice\"".$3/se;
146                                 }
147                         }
148                         return "[[$prefix $params]]";
149                 };
150                 $content =~ s{(\\?)\[\[\Q$prefix\E\s+([^]]+)\s*\]\]}{$edit->($1, $2)}seg;
152                 # Store their vote, update the page, and redirect to it.
153                 writefile($pagesources{$page}, $config{srcdir}, $content);
154                 $session->param($choice_param, $choice);
155                 IkiWiki::cgi_savesession($session);
156                 $oldchoice=$session->param($choice_param);
157                 if ($config{rcs}) {
158                         IkiWiki::disable_commit_hook();
159                         IkiWiki::rcs_commit(
160                                 file => $pagesources{$page},
161                                 message => "poll vote ($choice)",
162                                 token => IkiWiki::rcs_prepedit($pagesources{$page}),
163                                 session => $session,
164                         );
165                         IkiWiki::enable_commit_hook();
166                         IkiWiki::rcs_update();
167                 }
168                 require IkiWiki::Render;
169                 IkiWiki::refresh();
170                 IkiWiki::saveindex();
172                 # Need to set cookie in same http response that does the
173                 # redir.
174                 eval q{use CGI::Cookie};
175                 error($@) if $@;
176                 my $cookie = CGI::Cookie->new(-name=> $session->name, -value=> $session->id);
177                 print $cgi->redirect(-cookie => $cookie,
178                         -url => urlto($page));
179                 exit;
180         }