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