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