summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/poll.pm
blob: bc1e3501e5e6b7c9c4a0d1568367e5d60bbf61cb (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::poll;
  3. use warnings;
  4. use strict;
  5. use IkiWiki 3.00;
  6. use Encode;
  7. sub import {
  8. hook(type => "getsetup", id => "poll", call => \&getsetup);
  9. hook(type => "preprocess", id => "poll", call => \&preprocess);
  10. hook(type => "sessioncgi", id => "poll", call => \&sessioncgi);
  11. }
  12. sub getsetup () {
  13. return
  14. plugin => {
  15. safe => 1,
  16. rebuild => undef,
  17. },
  18. }
  19. my %pagenum;
  20. sub preprocess (@) {
  21. my %params=(open => "yes", total => "yes", percent => "yes", @_);
  22. my $open=IkiWiki::yesno($params{open});
  23. my $showtotal=IkiWiki::yesno($params{total});
  24. my $showpercent=IkiWiki::yesno($params{percent});
  25. $pagenum{$params{page}}++;
  26. my %choices;
  27. my @choices;
  28. my $total=0;
  29. while (@_) {
  30. my $key=shift;
  31. my $value=shift;
  32. next unless $key =~ /^\d+/;
  33. my $num=$key;
  34. $key=shift;
  35. $value=shift;
  36. $choices{$key}=$num;
  37. push @choices, $key;
  38. $total+=$num;
  39. }
  40. my $ret="";
  41. foreach my $choice (@choices) {
  42. if ($open && exists $config{cgiurl}) {
  43. # use POST to avoid robots
  44. $ret.="<form method=\"POST\" action=\"$config{cgiurl}\">\n";
  45. }
  46. my $percent=$total > 0 ? int($choices{$choice} / $total * 100) : 0;
  47. $ret.="<p>\n";
  48. if ($showpercent) {
  49. $ret.="$choice ($percent%)\n";
  50. }
  51. else {
  52. $ret.="$choice ($choices{$choice})\n";
  53. }
  54. if ($open && exists $config{cgiurl}) {
  55. $ret.="<input type=\"hidden\" name=\"do\" value=\"poll\" />\n";
  56. $ret.="<input type=\"hidden\" name=\"num\" value=\"$pagenum{$params{page}}\" />\n";
  57. $ret.="<input type=\"hidden\" name=\"page\" value=\"$params{page}\" />\n";
  58. $ret.="<input type=\"hidden\" name=\"choice\" value=\"$choice\" />\n";
  59. $ret.="<input type=\"submit\" value=\"".gettext("vote")."\" />\n";
  60. }
  61. $ret.="</p>\n<hr class=poll align=left width=\"$percent%\"/>\n";
  62. if ($open && exists $config{cgiurl}) {
  63. $ret.="</form>\n";
  64. }
  65. }
  66. if ($showtotal) {
  67. $ret.="<span>".gettext("Total votes:")." $total</span>\n";
  68. }
  69. return "<div class=poll>$ret</div>";
  70. }
  71. sub sessioncgi ($$) {
  72. my $cgi=shift;
  73. my $session=shift;
  74. if (defined $cgi->param('do') && $cgi->param('do') eq "poll") {
  75. my $choice=decode_utf8($cgi->param('choice'));
  76. if (! defined $choice) {
  77. error("no choice specified");
  78. }
  79. my $num=$cgi->param('num');
  80. if (! defined $num) {
  81. error("no num specified");
  82. }
  83. my $page=IkiWiki::possibly_foolish_untaint($cgi->param('page'));
  84. if (! defined $page || ! exists $pagesources{$page}) {
  85. error("bad page name");
  86. }
  87. # Did they vote before? If so, let them change their vote,
  88. # and check for dups.
  89. my $choice_param="poll_choice_${page}_$num";
  90. my $oldchoice=$session->param($choice_param);
  91. if (defined $oldchoice && $oldchoice eq $choice) {
  92. # Same vote; no-op.
  93. IkiWiki::redirect($cgi, urlto($page, undef, 1));
  94. exit;
  95. }
  96. my $prefix=$config{prefix_directives} ? "!poll" : "poll";
  97. my $content=readfile(srcfile($pagesources{$page}));
  98. # Now parse the content, find the right poll,
  99. # and find the choice within it, and increment its number.
  100. # If they voted before, decrement that one.
  101. my $edit=sub {
  102. my $escape=shift;
  103. my $params=shift;
  104. return "\\[[$prefix $params]]" if $escape;
  105. if (--$num == 0) {
  106. $params=~s/(^|\s+)(\d+)\s+"?\Q$choice\E"?(\s+|$)/$1.($2+1)." \"$choice\"".$3/se;
  107. if (defined $oldchoice) {
  108. $params=~s/(^|\s+)(\d+)\s+"?\Q$oldchoice\E"?(\s+|$)/$1.($2-1 >=0 ? $2-1 : 0)." \"$oldchoice\"".$3/se;
  109. }
  110. }
  111. return "[[$prefix $params]]";
  112. };
  113. $content =~ s{(\\?)\[\[\Q$prefix\E\s+([^]]+)\s*\]\]}{$edit->($1, $2)}seg;
  114. # Store their vote, update the page, and redirect to it.
  115. writefile($pagesources{$page}, $config{srcdir}, $content);
  116. $session->param($choice_param, $choice);
  117. IkiWiki::cgi_savesession($session);
  118. $oldchoice=$session->param($choice_param);
  119. if ($config{rcs}) {
  120. IkiWiki::disable_commit_hook();
  121. IkiWiki::rcs_commit($pagesources{$page}, "poll vote ($choice)",
  122. IkiWiki::rcs_prepedit($pagesources{$page}),
  123. $session->param("name"), $ENV{REMOTE_ADDR});
  124. IkiWiki::enable_commit_hook();
  125. IkiWiki::rcs_update();
  126. }
  127. require IkiWiki::Render;
  128. IkiWiki::refresh();
  129. IkiWiki::saveindex();
  130. # Need to set cookie in same http response that does the
  131. # redir.
  132. eval q{use CGI::Cookie};
  133. error($@) if $@;
  134. my $cookie = CGI::Cookie->new(-name=> $session->name, -value=> $session->id);
  135. print $cgi->redirect(-cookie => $cookie,
  136. -url => urlto($page, undef, 1));
  137. exit;
  138. }
  139. }
  140. 1