]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/poll.pm
* Fix various links on the hyperestradier pages.
[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;
7 use URI;
9 sub import { #{{{
10         hook(type => "preprocess", id => "poll", call => \&preprocess);
11         hook(type => "cgi", id => "poll", call => \&cgi);
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                 my $percent=$total > 0 ? int($choices{$choice} / $total * 100) : 0;
49                 if ($showpercent) {
50                         $ret.="$choice ($percent%) ";
51                 }
52                 else {
53                         $ret.="$choice ($choices{$choice}) ";
54                 }
55                 if ($open && exists $config{cgiurl}) {
56                         my $url=URI->new($config{cgiurl});
57                         $url->query_form(
58                                 "do" => "poll",
59                                 "num" => $pagenum{$params{page}}, 
60                                 "page" => $params{page}, 
61                                 "choice" => $choice,
62                         );
63                         $ret.="<a class=pollbutton href=\"$url\">vote</a>";
64                 }
65                 $ret.="<br />\n<hr class=poll align=left width=\"$percent%\"/>\n";
66         }
67         if ($showtotal) {
68                 $ret.="<span>Total votes: $total</span>\n";
69         }
70         return "<div class=poll>$ret</div>";
71 } # }}}
73 sub cgi ($) { #{{{
74         my $cgi=shift;
75         if (defined $cgi->param('do') && $cgi->param('do') eq "poll") {
76                 my $choice=$cgi->param('choice');
77                 if (! defined $choice) {
78                         error("no choice specified");
79                 }
80                 my $num=$cgi->param('num');
81                 if (! defined $num) {
82                         error("no num specified");
83                 }
84                 my $page=IkiWiki::possibly_foolish_untaint($cgi->param('page'));
85                 if (! defined $page || ! exists $pagesources{$page}) {
86                         error("bad page name");
87                 }
89                 # Did they vote before? If so, let them change their vote,
90                 # and check for dups.
91                 my $session=IkiWiki::cgi_getsession();
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 $content=readfile(srcfile($pagesources{$page}));
101                 # Now parse the content, find the right poll,
102                 # and find the choice within it, and increment its number.
103                 # If they voted before, decrement that one.
104                 my $edit=sub {
105                         my $escape=shift;
106                         my $params=shift;
107                         return "\\[[poll $params]]" if $escape;
108                         if (--$num == 0) {
109                                 $params=~s/(^|\s+)(\d+)\s+"?\Q$choice\E"?(\s+|$)/$1.($2+1)." \"$choice\"".$3/se;
110                                 if (defined $oldchoice) {
111                                         $params=~s/(^|\s+)(\d+)\s+"?\Q$oldchoice\E"?(\s+|$)/$1.($2-1 >=0 ? $2-1 : 0)." \"$oldchoice\"".$3/se;
112                                 }
113                         }
114                         return "[[poll $params]]";
115                 };
116                 $content =~ s{(\\?)\[\[poll\s+([^]]+)\s*\]\]}{$edit->($1, $2)}seg;
118                 # Store their vote, update the page, and redirect to it.
119                 writefile($pagesources{$page}, $config{srcdir}, $content);
120                 $session->param($choice_param, $choice);
121                 IkiWiki::cgi_savesession($session);
122                 $oldchoice=$session->param($choice_param);
123                 if ($config{rcs}) {
124                         # prevent deadlock with post-commit hook
125                         IkiWiki::unlockwiki();
126                         IkiWiki::rcs_commit($pagesources{$page}, "poll vote",
127                                 IkiWiki::rcs_prepedit($pagesources{$page}),
128                                 $session->param("name"), $ENV{REMOTE_ADDR});
129                 }
130                 else {
131                         require IkiWiki::Render;
132                         IkiWiki::refresh();
133                         IkiWiki::saveindex();
134                 }
135                 # Need to set cookie in same http response that does the
136                 # redir.
137                 eval q{use CGI::Cookie};
138                 error($@) if $@;
139                 my $cookie = CGI::Cookie->new(-name=> $session->name, -value=> $session->id);
140                 print $cgi->redirect(-cookie => $cookie,
141                         -url => "$config{url}/".htmlpage($page));
142                 exit;
143         }
144 } #}}}