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