<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl
package IkiWiki::Plugin::poll;

use warnings;
use strict;
use IkiWiki 3.00;
use Encode;

sub import {
	hook(type =&gt; "getsetup", id =&gt; "poll", call =&gt; \&amp;getsetup);
	hook(type =&gt; "preprocess", id =&gt; "poll", call =&gt; \&amp;preprocess);
	hook(type =&gt; "sessioncgi", id =&gt; "poll", call =&gt; \&amp;sessioncgi);
}

sub getsetup () {
	return 
		plugin =&gt; {
			safe =&gt; 1,
			rebuild =&gt; undef,
			section =&gt; "widget",
		},
}

my %pagenum;
sub preprocess (@) {
	my %params=(open =&gt; "yes", total =&gt; "yes", percent =&gt; "yes", @_);

	my $open=IkiWiki::yesno($params{open});
	my $showtotal=IkiWiki::yesno($params{total});
	my $showpercent=IkiWiki::yesno($params{percent});
	$pagenum{$params{page}}++;

	my %choices;
	my @choices;
	my $total=0;
	while (@_) {
		my $key=shift;
		my $value=shift;

		next unless $key =~ /^\d+/;

		my $num=$key;
		$key=shift;
		$value=shift;

		$choices{$key}=$num;
		push @choices, $key;
		$total+=$num;
	}

	my $ret="";
	foreach my $choice (@choices) {
		if ($open &amp;&amp; exists $config{cgiurl}) {
			# use POST to avoid robots
			$ret.="&lt;form method=\"POST\" action=\"$config{cgiurl}\"&gt;\n";
		}
		my $percent=$total &gt; 0 ? int($choices{$choice} / $total * 100) : 0;
		$ret.="&lt;p&gt;\n";
		if ($showpercent) {
			$ret.="$choice ($percent%)\n";
		}
		else {
			$ret.="$choice ($choices{$choice})\n";
		}
		if ($open &amp;&amp; exists $config{cgiurl}) {
			$ret.="&lt;input type=\"hidden\" name=\"do\" value=\"poll\" /&gt;\n";
			$ret.="&lt;input type=\"hidden\" name=\"num\" value=\"$pagenum{$params{page}}\" /&gt;\n";
			$ret.="&lt;input type=\"hidden\" name=\"page\" value=\"$params{page}\" /&gt;\n";
			$ret.="&lt;input type=\"hidden\" name=\"choice\" value=\"$choice\" /&gt;\n";
			$ret.="&lt;input type=\"submit\" value=\"".gettext("vote")."\" /&gt;\n";
		}
		$ret.="&lt;/p&gt;\n&lt;hr class=poll align=left width=\"$percent%\"/&gt;\n";
		if ($open &amp;&amp; exists $config{cgiurl}) {
			$ret.="&lt;/form&gt;\n";
		}
	}
	if ($showtotal) {
		$ret.="&lt;span&gt;".gettext("Total votes:")." $total&lt;/span&gt;\n";
	}
	return "&lt;div class=poll&gt;$ret&lt;/div&gt;";
}

sub sessioncgi ($$) {
	my $cgi=shift;
	my $session=shift;
	if (defined $cgi-&gt;param('do') &amp;&amp; $cgi-&gt;param('do') eq "poll") {
		my $choice=decode_utf8($cgi-&gt;param('choice'));
		if (! defined $choice) {
			error("no choice specified");
		}
		my $num=$cgi-&gt;param('num');
		if (! defined $num) {
			error("no num specified");
		}
		my $page=IkiWiki::possibly_foolish_untaint($cgi-&gt;param('page'));
		if (! defined $page || ! exists $pagesources{$page}) {
			error("bad page name");
		}

		# Did they vote before? If so, let them change their vote,
		# and check for dups.
		my $choice_param="poll_choice_${page}_$num";
		my $oldchoice=$session-&gt;param($choice_param);
		if (defined $oldchoice &amp;&amp; $oldchoice eq $choice) {
			# Same vote; no-op.
			IkiWiki::redirect($cgi, urlto($page, undef, 1));
			exit;
		}

		my $prefix=$config{prefix_directives} ? "!poll" : "poll";

		my $content=readfile(srcfile($pagesources{$page}));
		# Now parse the content, find the right poll,
		# and find the choice within it, and increment its number.
		# If they voted before, decrement that one.
		my $edit=sub {
			my $escape=shift;
			my $params=shift;
			return "\\[[$prefix $params]]" if $escape;
			if (--$num == 0) {
				$params=~s/(^|\s+)(\d+)\s+"?\Q$choice\E"?(\s+|$)/$1.($2+1)." \"$choice\"".$3/se;
				if (defined $oldchoice) {
					$params=~s/(^|\s+)(\d+)\s+"?\Q$oldchoice\E"?(\s+|$)/$1.($2-1 &gt;=0 ? $2-1 : 0)." \"$oldchoice\"".$3/se;
				}
			}
			return "[[$prefix $params]]";
		};
		$content =~ s{(\\?)\[\[\Q$prefix\E\s+([^]]+)\s*\]\]}{$edit-&gt;($1, $2)}seg;

		# Store their vote, update the page, and redirect to it.
		writefile($pagesources{$page}, $config{srcdir}, $content);
		$session-&gt;param($choice_param, $choice);
		IkiWiki::cgi_savesession($session);
		$oldchoice=$session-&gt;param($choice_param);
		if ($config{rcs}) {
			IkiWiki::disable_commit_hook();
			IkiWiki::rcs_commit($pagesources{$page}, "poll vote ($choice)",
				IkiWiki::rcs_prepedit($pagesources{$page}),
				$session-&gt;param("name"), $ENV{REMOTE_ADDR});
			IkiWiki::enable_commit_hook();
			IkiWiki::rcs_update();
		}
		require IkiWiki::Render;
		IkiWiki::refresh();
		IkiWiki::saveindex();

		# Need to set cookie in same http response that does the
		# redir.
		eval q{use CGI::Cookie};
		error($@) if $@;
		my $cookie = CGI::Cookie-&gt;new(-name=&gt; $session-&gt;name, -value=&gt; $session-&gt;id);
		print $cgi-&gt;redirect(-cookie =&gt; $cookie,
			-url =&gt; urlto($page, undef, 1));
		exit;
	}
}

1
</pre></body></html>