2 package IkiWiki
::Plugin
::poll
;
10 hook
(type
=> "getsetup", id
=> "poll", call
=> \
&getsetup
);
11 hook
(type
=> "preprocess", id
=> "poll", call
=> \
&preprocess
);
12 hook
(type
=> "scan", id
=> "poll", call
=> \
&scan
);
13 hook
(type
=> "sessioncgi", id
=> "poll", call
=> \
&sessioncgi
);
28 (?
>(?
:[^\
[\
]]|\
[[^\
[]|\
][^\
]])+)
33 (?
>(?
:[^\
[\
]]|\
[[^\
[]|\
][^\
]])+)
43 my $page = $params{page
};
44 my $content = $params{content
};
45 my $prefix = $config{prefix_directives
} ?
"!poll" : "poll";
46 my $type = IkiWiki
::pagetype
($pagesources{$page});
47 if (defined $type and $type eq "mdwn") {
49 while ($content =~ m{(\\?)\[\[\Q$prefix\E(\s+id="([^"]*)")?\s+($params_re)\s*\]\]}gs) {
50 my ($escape, $poll, $directive) = ($1, $3, $4);
52 $poll = '' unless defined $poll;
53 error
("poll id=`$poll' must match (|[a-z][a-z0-9_-]*) on page=`$page'")
54 unless $poll =~ m/^(|[a-z][a-z0-9_-]*)$/;
56 while ($directive =~ m/(^|\s+)(\d+)(="([^"]*)")?\s+"?([^"]*)"?/gs) {
57 my ($unknown_votes, $known_votes, $choice) = ($2, $4, $5);
58 my @known_votes = defined $known_votes ?
split(/\s+/, $known_votes) : ();
60 { unknown_votes
=> $unknown_votes
61 , known_votes
=> \
@known_votes
63 foreach my $user (@known_votes) {
64 my $userpage = linkpage
(($config{userdir
}?
$config{userdir
}.'/':'').$user);
65 add_link
($page, $userpage);
68 error
("poll id=`$poll' already exists on page=`$page'")
69 if exists $polls{$poll};
70 $polls{$poll} = \
%poll;
72 $IkiWiki::pagestate
{$page}{poll
} = \
%polls;
83 my $open=IkiWiki
::yesno
($params{open});
84 my $showtotal=IkiWiki
::yesno
($params{total
});
85 my $showpercent=IkiWiki
::yesno
($params{percent
});
86 my $expandable=IkiWiki
::yesno
($params{expandable
});
87 my $num=++$pagenum{$params{page
}}{$params{destpage
}};
93 my $unknown_votes = shift;
94 my $known_votes = shift;
96 unless $unknown_votes =~ /^\d+$/;
97 my @users = $known_votes ?
split(/\s+/, $known_votes) : ();
100 my $tot = ($unknown_votes + @users);
102 { unknown_votes
=> $unknown_votes
106 push @choices, $choice;
110 my $uri_page = URI
::Escape
::uri_escape_utf8
($params{page
}, '^A-Za-z0-9\-\._~/');
112 foreach my $choice (@choices) {
113 if ($open && exists $config{cgiurl
}) {
114 # use POST to avoid robots
115 $ret.="<form method=\"POST\" action=\"".IkiWiki
::cgiurl
()."\">\n";
117 $ret.="<dt class='choice'>";
118 my $percent = $total > 0 ?
int($choices{$choice}{total
} / $total * 100) : 0;
119 my $votes = $choices{$choice}{total
};
122 $votes .= " ($percent%)"
124 if (@
{$choices{$choice}{users
}} > 0) {
125 $votes .= " : ".join(', ', map {
126 my $userpage = linkpage
(($config{userdir
}?
$config{userdir
}.'/':'').$_);
127 htmllink
($params{page
}, $params{destpage
}, $userpage, linktext
=> pagetitle
($_))
128 } @
{$choices{$choice}{users
}});
129 $votes .= " + ".$choices{$choice}{unknown_votes
}." "
130 . ($choices{$choice}{unknown_votes
} > 1 ? gettext
("unknowns") : gettext
("unknown"))
131 if $choices{$choice}{unknown_votes
};
134 $votes .= " : ".($choices{$choice}{unknown_votes
}." ".gettext
("unknowns"))
135 if $choices{$choice}{unknown_votes
};
137 if ($open && exists $config{cgiurl
}) {
138 my $choice_escaped = URI
::Escape
::uri_escape_utf8
($choice, '^A-Za-z0-9\ \-\._~/');
139 $ret.="<input type=\"hidden\" name=\"do\" value=\"poll\" />\n";
140 $ret.="<input type=\"hidden\" name=\"num\" value=\"$num\" />\n";
141 $ret.="<input type=\"hidden\" name=\"page\" value=\"$uri_page\" />\n";
142 $ret.="<input type=\"hidden\" name=\"choice\" value=\"$choice_escaped\" />\n";
143 $ret.="<input type=\"submit\" value=\"".gettext
("vote")."\" />\n";
145 $ret.="<span class='description'>$choice</span>";
147 $ret.="<dd class='votes'>";
149 $ret.="<hr class='poll' align=left width=\"$percent%\"/>\n";
150 if ($open && exists $config{cgiurl
}) {
156 if ($expandable && $open && exists $config{cgiurl
}) {
158 $ret.="<form method=\"POST\" action=\"".IkiWiki
::cgiurl
()."\">\n";
159 $ret.="<input type=\"hidden\" name=\"do\" value=\"poll\" />\n";
160 $ret.="<input type=\"hidden\" name=\"num\" value=\"$num\" />\n";
161 $ret.="<input type=\"hidden\" name=\"page\" value=\"$uri_page\" />\n";
162 $ret.=gettext
("Write in").": <input name=\"choice\" size=50 />\n";
163 $ret.="<input type=\"submit\" value=\"".gettext
("vote")."\" />\n";
170 return "<dl class='poll'>$ret</dl>";
172 sub sessioncgi
($$) {
175 if (defined $cgi->param('do') && $cgi->param('do') eq "poll") {
176 my $choice = Encode
::decode_utf8
(URI
::Escape
::uri_unescape
(IkiWiki
::possibly_foolish_untaint
($cgi->param('choice'))));
178 if (! defined $choice || not length $choice) {
179 error
("no choice specified");
181 my $num=$cgi->param('num');
182 if (! defined $num) {
183 error
("no num specified");
185 my $page=Encode
::decode_utf8
(URI
::Escape
::uri_unescape
(IkiWiki
::possibly_foolish_untaint
($cgi->param('page'))));
186 if (! defined $page || ! exists $pagesources{$page}) {
188 error
("bad page name");
190 &IkiWiki
::check_canedit
($page, $cgi, $session);
192 # Did they vote before? If so, let them change their vote,
193 # and check for dups.
194 my $choice_param="poll_choice_${page}_$num";
195 my $oldchoice=$session->param($choice_param);
196 #if (defined $oldchoice && $oldchoice eq $choice) {
197 # # Same vote; no-op.
198 # IkiWiki::redirect($cgi, urlto($page));
201 my $prefix=$config{prefix_directives
} ?
"!poll" : "poll";
202 my $content=readfile
(srcfile
($pagesources{$page}));
203 # Now parse the content, find the right poll,
204 # and find the choice within it, and increment its number.
205 # If they voted before, decrement that one.
213 my ($action, $unknown_votes, $known_votes) = @_;
214 my $user = $session->param("name");
216 foreach (split(/\s+/, $known_votes)) {
219 if ($action eq 'add') {
221 if (exists $users{$user} or (defined $oldchoice and $oldchoice eq $choice)) {
222 delete $users{$user};
223 $known_votes = join(' ', sort {lc $a <=> lc $b} (keys %users));
226 $known_votes = join(' ', sort {lc $a <=> lc $b} ($user, keys %users));
233 elsif ($action eq 'del') {
235 if (exists $users{$user}) {
236 delete $users{$user};
237 $known_votes = join(' ', sort {lc $a <=> lc $b} (keys %users));
241 $unknown_votes = ($unknown_votes-1 >=0 ?
$unknown_votes-1 : 0);
244 return $unknown_votes.($known_votes?
"=\"$known_votes\"":"")
246 if ($params=~s/(^|\s+)(\d+)(="([^"]*)")?(\s+)"?\Q$choice\E"?(\s+|$)/$1.$vote->('add', $2, $4)."$5\"$choice\"".$6/es) {
248 elsif ($params=~/expandable=(\w+)/
249 & &IkiWiki
::yesno
($1)) {
250 $choice=~s/["\]\n\r]//g;
251 $params.=" 1 \"$choice\""
254 if (defined $oldchoice and not ($oldchoice eq $choice)) {
255 $params=~s/(^|\s+)(\d+)(="([^"]*)")?(\s+)"?\Q$oldchoice\E"?(\s+|$)/$1.$vote->('del', $2, $4)."$5\"$oldchoice\"".$6/es;
265 (?
:(?
<id_space
>\s
+)id
="(?<id>[^"]*)")?
267 (?<params>$params_re)
274 .($+{id} eq ''?'':$+{id_space}.'id="'.$+{id}.'"')
276 .$edit->($+{escape}, $+{params})
281 # Store their vote, update the page, and redirect to it.
282 writefile($pagesources{$page}, $config{srcdir}, $content);
283 if (defined $oldchoice and $choice eq $oldchoice) {
284 $session->param($choice_param, undef);
285 # TOTRY: $session->clear($choice_param);
288 $session->param($choice_param, $choice);
290 IkiWiki::cgi_savesession($session);
292 IkiWiki::disable_commit_hook();
294 ( file => $pagesources{$page}
295 , message => "poll vote
: id
=$id: $choice"
296 , session => $session
297 , token => IkiWiki::rcs_prepedit($pagesources{$page})
299 IkiWiki::enable_commit_hook();
300 IkiWiki::rcs_update();
302 require IkiWiki::Render;
304 IkiWiki::saveindex();
305 # Need to set cookie in same http response that does the redir.
306 eval q{use CGI::Cookie};
308 my $cookie = CGI
::Cookie
->new
309 ( -name
=> $session->name
310 , -value
=> $session->id );
313 , -url
=> urlto
($page) );
317 package IkiWiki
::PageSpec
;
318 sub match_poll
($$;@
) {
319 my ($page, $match, %params) = @_;
320 my $polls = $IkiWiki::pagestate
{$page}{poll
};
321 if (defined $polls and %$polls) {
322 my ($match_id, $match_user, $match_choice) = $match =~ m/^id=(.*?) user=(.*?) choice=(.*?)$/;
323 my $match_id_re = IkiWiki
::glob2re
($match_id?
$match_id:'*');
324 my @polls = grep {$_ =~ $match_id_re} (keys %$polls);
325 return IkiWiki
::FailReason
->new("no poll match id=`$match_id'", $page => $IkiWiki::DEPEND_CONTENT
)
327 foreach my $poll (@polls) {
328 my %poll = %{$polls->{$poll}};
329 my $match_user_re = IkiWiki
::glob2re
($match_user?
$match_user:'*');
330 my $match_choice_re = IkiWiki
::glob2re
($match_choice?
$match_choice:'*');
331 while (my ($choice, $data) = each %poll) {
332 next unless $choice =~ $match_choice_re;
333 if ($match_user eq '') {
334 if ($data->{unknown_votes
} > 0) {
335 return IkiWiki
::SuccessReason
->new("unkown user has voted for choice=`$choice'", $page => $IkiWiki::DEPEND_CONTENT
);
338 return IkiWiki
::FailReason
->new("no unkown user has voted for choice=`$choice'", $page => $IkiWiki::DEPEND_CONTENT
);
342 foreach my $user (@
{$data->{known_votes
}}) {
343 next unless $user =~ $match_user_re;
344 return IkiWiki
::SuccessReason
->new("user=`$user' has voted for choice=`$choice'", $page => $IkiWiki::DEPEND_CONTENT
);
349 return IkiWiki
::FailReason
->new("no user=`$match_user' has voted for choice=`$match_choice'", $page => $IkiWiki::DEPEND_CONTENT
);
352 return IkiWiki
::FailReason
->new("no poll", $page => $IkiWiki::DEPEND_CONTENT
);