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