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