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 $content = $params{content
};
44 my $prefix = $config{prefix_directives
} ?
"!poll" : "poll";
45 my $type = IkiWiki
::pagetype
($pagesources{$params{page
}});
46 if (defined $type and $type eq "mdwn") {
48 while ($content =~ m{(\\?)\[\[\Q$prefix\E(\s+id="([^"]*)")?\s+($params_re)\s*\]\]}gs) {
49 my ($escape, $poll, $directive) = ($1, $3, $4);
51 $poll = '' unless defined $poll;
52 error
("poll id=`$poll' must match (|[a-z][a-z0-9_-]*) on page=`$params{page}'")
53 unless $poll =~ m/^(|[a-z][a-z0-9_-]*)$/;
55 my $userdir = defined $config{userdir
} ?
$config{userdir
}.'/' : '';
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 ?
grep {length $_ > 0} (split(/\s+/, $known_votes)) : ();
60 { unknown_votes
=> $unknown_votes
61 , known_votes
=> \
@known_votes
63 foreach my $user (@known_votes) {
64 my $userpage_best = bestlink
($params{page
}, $user);
65 $userpage_best = (length $userpage_best > 0 ?
$userpage_best : bestlink
($userdir, $user));
66 $userpage_best = (length $userpage_best > 0 ?
$userpage_best : ($user =~ m{/} ?
'/' : $userdir).$user);
67 add_link
($params{page
}, $userpage_best);
70 error
("poll id=`$poll' already exists on page=`$params{page}'")
71 if exists $polls{$poll};
72 $polls{$poll} = \
%poll;
74 $IkiWiki::pagestate
{$params{page
}}{poll
} = \
%polls;
85 my $open=IkiWiki
::yesno
($params{open});
86 my $showtotal=IkiWiki
::yesno
($params{total
});
87 my $showpercent=IkiWiki
::yesno
($params{percent
});
88 my $expandable=IkiWiki
::yesno
($params{expandable
});
89 my $num=++$pagenum{$params{page
}}{$params{destpage
}};
95 my $unknown_votes = shift;
96 my $known_votes = shift;
98 unless $unknown_votes =~ /^\d+$/;
99 my @users = $known_votes ?
grep {length $_ > 0} (split(/\s+/, $known_votes)) : ();
102 my $tot = ($unknown_votes + @users);
104 { unknown_votes
=> $unknown_votes
108 push @choices, $choice;
112 my $uri_page = URI
::Escape
::uri_escape_utf8
($params{page
}, '^A-Za-z0-9\-\._~/');
113 my $userdir = defined $config{userdir
} ?
$config{userdir
}.'/' : '';
115 foreach my $choice (@choices) {
116 if ($open && exists $config{cgiurl
}) {
117 # use POST to avoid robots
118 $ret.="<form method=\"POST\" action=\"".IkiWiki
::cgiurl
()."\">\n";
120 $ret.="<dt class='choice'>";
121 my $percent = $total > 0 ?
int($choices{$choice}{total
} / $total * 100) : 0;
122 my $votes = $choices{$choice}{total
};
125 $votes .= " ($percent%)"
127 if (@
{$choices{$choice}{users
}} > 0) {
128 $votes .= " : ".join(', ', map {
130 #my $userpage = linkpage(($config{userdir}?$config{userdir}.'/':'').$_);
131 #htmllink($params{page}, $params{destpage}, $userpage, linktext => pagetitle($_))
132 my $userpage_best = bestlink
($userdir, $user);
133 $userpage_best = (length $userpage_best > 0 ?
$userpage_best : bestlink
($userdir, $user));
134 $userpage_best = (length $userpage_best > 0 ?
$userpage_best : ($user =~ m{/} ?
'/' : $userdir).$user);
139 , noimageinline
=> 1 )
140 } @
{$choices{$choice}{users
}});
141 $votes .= " + ".$choices{$choice}{unknown_votes
}." "
142 . ($choices{$choice}{unknown_votes
} > 1 ? gettext
("unknowns") : gettext
("unknown"))
143 if $choices{$choice}{unknown_votes
};
146 $votes .= " : ".($choices{$choice}{unknown_votes
}." ".gettext
("unknowns"))
147 if $choices{$choice}{unknown_votes
};
149 if ($open && exists $config{cgiurl
}) {
150 my $choice_escaped = URI
::Escape
::uri_escape_utf8
($choice, '^A-Za-z0-9\ \-\._~/');
151 $ret.="<input type=\"hidden\" name=\"do\" value=\"poll\" />\n";
152 $ret.="<input type=\"hidden\" name=\"num\" value=\"$num\" />\n";
153 $ret.="<input type=\"hidden\" name=\"page\" value=\"$uri_page\" />\n";
154 $ret.="<input type=\"hidden\" name=\"choice\" value=\"$choice_escaped\" />\n";
155 $ret.="<input type=\"submit\" value=\"".gettext
("vote")."\" />\n";
157 $ret.="<span class='description'>$choice</span>";
159 $ret.="<dd class='votes'>";
161 $ret.="<hr class='poll' align=left width=\"$percent%\"/>\n";
162 if ($open && exists $config{cgiurl
}) {
168 if ($expandable && $open && exists $config{cgiurl
}) {
170 $ret.="<form method=\"POST\" action=\"".IkiWiki
::cgiurl
()."\">\n";
171 $ret.="<input type=\"hidden\" name=\"do\" value=\"poll\" />\n";
172 $ret.="<input type=\"hidden\" name=\"num\" value=\"$num\" />\n";
173 $ret.="<input type=\"hidden\" name=\"page\" value=\"$uri_page\" />\n";
174 $ret.=gettext
("Write in").": <input name=\"choice\" size=50 />\n";
175 $ret.="<input type=\"submit\" value=\"".gettext
("vote")."\" />\n";
182 return "<dl class='poll'>$ret</dl>";
184 sub sessioncgi
($$) {
187 if (defined $cgi->param('do') && $cgi->param('do') eq "poll") {
188 my $choice = Encode
::decode_utf8
(URI
::Escape
::uri_unescape
(IkiWiki
::possibly_foolish_untaint
($cgi->param('choice'))));
190 if (! defined $choice || not length $choice) {
191 error
("no choice specified");
193 my $num=$cgi->param('num');
194 if (! defined $num) {
195 error
("no num specified");
197 my $page=Encode
::decode_utf8
(URI
::Escape
::uri_unescape
(IkiWiki
::possibly_foolish_untaint
($cgi->param('page'))));
198 if (! defined $page || ! exists $pagesources{$page}) {
200 error
("bad page name");
202 &IkiWiki
::check_canedit
($page, $cgi, $session);
204 # Did they vote before? If so, let them change their vote,
205 # and check for dups.
206 my $choice_param="poll_choice_${page}_$num";
207 my $oldchoice=$session->param($choice_param);
208 #if (defined $oldchoice && $oldchoice eq $choice) {
209 # # Same vote; no-op.
210 # IkiWiki::redirect($cgi, urlto($page));
213 my $prefix=$config{prefix_directives
} ?
"!poll" : "poll";
214 my $content=readfile
(srcfile
($pagesources{$page}));
215 # Now parse the content, find the right poll,
216 # and find the choice within it, and increment its number.
217 # If they voted before, decrement that one.
225 my ($action, $unknown_votes, $known_votes) = @_;
226 my $user = $session->param("name");
228 foreach (split(/\s+/, $known_votes)) {
232 if ($action eq 'add') {
234 if (exists $users{$user} or (defined $oldchoice and $oldchoice eq $choice)) {
235 delete $users{$user};
236 $known_votes = join(' ', sort {lc $a <=> lc $b} (keys %users));
239 $known_votes = join(' ', sort {lc $a <=> lc $b} ($user, keys %users));
246 elsif ($action eq 'del') {
248 if (exists $users{$user}) {
249 delete $users{$user};
250 $known_votes = join(' ', sort {lc $a <=> lc $b} (keys %users));
254 $unknown_votes = ($unknown_votes-1 >=0 ?
$unknown_votes-1 : 0);
257 return $unknown_votes.($known_votes?
"=\"$known_votes\"":"")
259 if ($params=~s/(^|\s+)(\d+)(="([^"]*)")?(\s+)"?\Q$choice\E"?(\s+|$)/$1.$vote->('add', $2, $4)."$5\"$choice\"".$6/es) {
261 elsif ($params=~/expandable=(\w+)/
262 & &IkiWiki
::yesno
($1)) {
263 $choice=~s/["\]\n\r]//g;
264 $params.=" 1 \"$choice\""
267 if (defined $oldchoice and not ($oldchoice eq $choice)
268 and not ($params=~/multiple=(\w+)/ & &IkiWiki
::yesno
($1))) {
269 $params=~s/(^|\s+)(\d+)(="([^"]*)")?(\s+)"?\Q$oldchoice\E"?(\s+|$)/$1.$vote->('del', $2, $4)."$5\"$oldchoice\"".$6/es;
279 (?
:(?
<id_space
>\s
+)id
="(?<id>[^"]*)")?
281 (?<params>$params_re)
288 .($+{id} eq ''?'':$+{id_space}.'id="'.$+{id}.'"')
290 .$edit->($+{escape}, $+{params})
295 # Store their vote, update the page, and redirect to it.
296 writefile($pagesources{$page}, $config{srcdir}, $content);
297 if (defined $oldchoice and $choice eq $oldchoice) {
298 $session->param($choice_param, undef);
299 # TOTRY: $session->clear($choice_param);
302 $session->param($choice_param, $choice);
304 IkiWiki::cgi_savesession($session);
306 IkiWiki::disable_commit_hook();
308 ( file => $pagesources{$page}
309 , message => "poll vote
: id
=$id: $choice"
310 , session => $session
311 , token => IkiWiki::rcs_prepedit($pagesources{$page})
313 IkiWiki::enable_commit_hook();
314 IkiWiki::rcs_update();
316 require IkiWiki::Render;
318 IkiWiki::saveindex();
319 # Need to set cookie in same http response that does the redir.
320 eval q{use CGI::Cookie};
322 my $cookie = CGI
::Cookie
->new
323 ( -name
=> $session->name
324 , -value
=> $session->id );
327 , -url
=> urlto
($page) );
331 package IkiWiki
::PageSpec
;
332 sub match_poll
($$;@
) {
333 my ($page, $match, %params) = @_;
334 my $polls = $IkiWiki::pagestate
{$page}{poll
};
335 if (defined $polls and %$polls) {
336 my ($match_id, $match_user, $match_choice) = $match =~ m/^id=(.*?) user=(.*?) choice=(.*?)$/;
337 my $match_id_re = IkiWiki
::glob2re
($match_id?
$match_id:'*');
338 my @polls = grep {$_ =~ $match_id_re} (keys %$polls);
339 return IkiWiki
::FailReason
->new("no poll match id=`$match_id'", $page => $IkiWiki::DEPEND_CONTENT
)
341 foreach my $poll (@polls) {
342 my %poll = %{$polls->{$poll}};
343 my $match_user_re = IkiWiki
::glob2re
($match_user?
$match_user:'*');
344 my $match_choice_re = IkiWiki
::glob2re
($match_choice?
$match_choice:'*');
345 while (my ($choice, $data) = each %poll) {
346 next unless $choice =~ $match_choice_re;
347 if ($match_user eq '') {
348 if ($data->{unknown_votes
} > 0) {
349 return IkiWiki
::SuccessReason
->new("unkown user has voted for choice=`$choice'", $page => $IkiWiki::DEPEND_CONTENT
);
352 return IkiWiki
::FailReason
->new("no unkown user has voted for choice=`$choice'", $page => $IkiWiki::DEPEND_CONTENT
);
356 foreach my $user (@
{$data->{known_votes
}}) {
357 next unless $user =~ $match_user_re;
358 return IkiWiki
::SuccessReason
->new("user=`$user' has voted for choice=`$choice'", $page => $IkiWiki::DEPEND_CONTENT
);
363 return IkiWiki
::FailReason
->new("no user=`$match_user' has voted for choice=`$match_choice'", $page => $IkiWiki::DEPEND_CONTENT
);
366 return IkiWiki
::FailReason
->new("no poll", $page => $IkiWiki::DEPEND_CONTENT
);