summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/poll.pm
blob: 82e862c59b92b8162e1684cd6ff4badbd8d37486 (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 => "sessioncgi", id => "poll", call => \&sessioncgi);
  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 sessioncgi ($$) { #{{{
  67. my $cgi=shift;
  68. my $session=shift;
  69. if (defined $cgi->param('do') && $cgi->param('do') eq "poll") {
  70. my $choice=$cgi->param('choice');
  71. if (! defined $choice) {
  72. error("no choice specified");
  73. }
  74. my $num=$cgi->param('num');
  75. if (! defined $num) {
  76. error("no num specified");
  77. }
  78. my $page=IkiWiki::possibly_foolish_untaint($cgi->param('page'));
  79. if (! defined $page || ! exists $pagesources{$page}) {
  80. error("bad page name");
  81. }
  82. # Did they vote before? If so, let them change their vote,
  83. # and check for dups.
  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 $prefix=$config{prefix_directives} ? "!poll" : "poll";
  92. my $content=readfile(srcfile($pagesources{$page}));
  93. # Now parse the content, find the right poll,
  94. # and find the choice within it, and increment its number.
  95. # If they voted before, decrement that one.
  96. my $edit=sub {
  97. my $escape=shift;
  98. my $params=shift;
  99. return "\\[[$prefix $params]]" if $escape;
  100. if (--$num == 0) {
  101. $params=~s/(^|\s+)(\d+)\s+"?\Q$choice\E"?(\s+|$)/$1.($2+1)." \"$choice\"".$3/se;
  102. if (defined $oldchoice) {
  103. $params=~s/(^|\s+)(\d+)\s+"?\Q$oldchoice\E"?(\s+|$)/$1.($2-1 >=0 ? $2-1 : 0)." \"$oldchoice\"".$3/se;
  104. }
  105. }
  106. return "[[$prefix $params]]";
  107. };
  108. $content =~ s{(\\?)\[\[\Q$prefix\E\s+([^]]+)\s*\]\]}{$edit->($1, $2)}seg;
  109. # Store their vote, update the page, and redirect to it.
  110. writefile($pagesources{$page}, $config{srcdir}, $content);
  111. $session->param($choice_param, $choice);
  112. IkiWiki::cgi_savesession($session);
  113. $oldchoice=$session->param($choice_param);
  114. if ($config{rcs}) {
  115. IkiWiki::disable_commit_hook();
  116. IkiWiki::rcs_commit($pagesources{$page}, "poll vote ($choice)",
  117. IkiWiki::rcs_prepedit($pagesources{$page}),
  118. $session->param("name"), $ENV{REMOTE_ADDR});
  119. IkiWiki::enable_commit_hook();
  120. IkiWiki::rcs_update();
  121. }
  122. require IkiWiki::Render;
  123. IkiWiki::refresh();
  124. IkiWiki::saveindex();
  125. # Need to set cookie in same http response that does the
  126. # redir.
  127. eval q{use CGI::Cookie};
  128. error($@) if $@;
  129. my $cookie = CGI::Cookie->new(-name=> $session->name, -value=> $session->id);
  130. print $cgi->redirect(-cookie => $cookie,
  131. -url => "$config{url}/".htmlpage($page));
  132. exit;
  133. }
  134. } #}}}
  135. 1