summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/comments.pm
blob: 136dc258ebe06bfbfa2d735090fdd4203ab17545 (plain)
  1. #!/usr/bin/perl
  2. # Copyright © 2006-2008 Joey Hess <joey@ikiwiki.info>
  3. # Copyright © 2008 Simon McVittie <http://smcv.pseudorandom.co.uk/>
  4. # Licensed under the GNU GPL, version 2, or any later version published by the
  5. # Free Software Foundation
  6. package IkiWiki::Plugin::comments;
  7. use warnings;
  8. use strict;
  9. use IkiWiki 2.00;
  10. use constant PREVIEW => "Preview";
  11. use constant POST_COMMENT => "Post comment";
  12. use constant CANCEL => "Cancel";
  13. sub import { #{{{
  14. hook(type => "getsetup", id => 'comments', call => \&getsetup);
  15. hook(type => "preprocess", id => 'comments', call => \&preprocess);
  16. hook(type => "sessioncgi", id => 'comment', call => \&sessioncgi);
  17. hook(type => "htmlize", id => "_comment", call => \&htmlize);
  18. IkiWiki::loadplugin("inline");
  19. IkiWiki::loadplugin("mdwn");
  20. } # }}}
  21. sub htmlize { # {{{
  22. eval q{use IkiWiki::Plugin::mdwn};
  23. error($@) if ($@);
  24. return IkiWiki::Plugin::mdwn::htmlize(@_)
  25. } # }}}
  26. sub getsetup () { #{{{
  27. return
  28. plugin => {
  29. safe => 1,
  30. rebuild => undef,
  31. },
  32. } #}}}
  33. # Somewhat based on IkiWiki::Plugin::inline blog posting support
  34. sub preprocess (@) { #{{{
  35. my %params=@_;
  36. unless (length $config{cgiurl}) {
  37. error(gettext("[[!comments plugin requires CGI enabled]]"));
  38. }
  39. my $page = $params{page};
  40. $pagestate{$page}{comments}{comments} = defined $params{closed}
  41. ? (not IkiWiki::yesno($params{closed}))
  42. : 1;
  43. $pagestate{$page}{comments}{allowdirectives} = IkiWiki::yesno($params{allowdirectives});
  44. $pagestate{$page}{comments}{commit} = defined $params{commit}
  45. ? IkiWiki::yesno($params{commit})
  46. : 1;
  47. my $formtemplate = IkiWiki::template("comments_embed.tmpl",
  48. blind_cache => 1);
  49. $formtemplate->param(cgiurl => $config{cgiurl});
  50. $formtemplate->param(page => $params{page});
  51. if (not $pagestate{$page}{comments}{comments}) {
  52. $formtemplate->param("disabled" =>
  53. gettext('comments are closed'));
  54. }
  55. elsif ($params{preview}) {
  56. $formtemplate->param("disabled" =>
  57. gettext('not available during Preview'));
  58. }
  59. debug("page $params{page} => destpage $params{destpage}");
  60. my $posts = '';
  61. unless (defined $params{inline} && !IkiWiki::yesno($params{inline})) {
  62. eval q{use IkiWiki::Plugin::inline};
  63. error($@) if ($@);
  64. my @args = (
  65. pages => "internal($params{page}/_comment_*)",
  66. template => "comments_display",
  67. show => 0,
  68. reverse => "yes",
  69. # special stuff passed through
  70. page => $params{page},
  71. destpage => $params{destpage},
  72. preview => $params{preview},
  73. );
  74. push @args, atom => $params{atom} if defined $params{atom};
  75. push @args, rss => $params{rss} if defined $params{rss};
  76. push @args, feeds => $params{feeds} if defined $params{feeds};
  77. push @args, feedshow => $params{feedshow} if defined $params{feedshow};
  78. push @args, timeformat => $params{timeformat} if defined $params{timeformat};
  79. push @args, feedonly => $params{feedonly} if defined $params{feedonly};
  80. $posts = "\n" . IkiWiki::preprocess_inline(@args);
  81. }
  82. return $formtemplate->output . $posts;
  83. } # }}}
  84. # FIXME: logic taken from editpage, should be common code?
  85. sub getcgiuser ($) { # {{{
  86. my $session = shift;
  87. my $user = $session->param('name');
  88. $user = $ENV{REMOTE_ADDR} unless defined $user;
  89. debug("getcgiuser() -> $user");
  90. return $user;
  91. } # }}}
  92. # FIXME: logic adapted from recentchanges, should be common code?
  93. sub linkuser ($) { # {{{
  94. my $user = shift;
  95. my $oiduser = eval { IkiWiki::openiduser($user) };
  96. if (defined $oiduser) {
  97. return ($user, $oiduser);
  98. }
  99. else {
  100. my $page = bestlink('', (length $config{userdir}
  101. ? "$config{userdir}/"
  102. : "").$user);
  103. return (urlto($page, undef, 1), $user);
  104. }
  105. } # }}}
  106. # Mostly cargo-culted from IkiWiki::plugin::editpage
  107. sub sessioncgi ($$) { #{{{
  108. my $cgi=shift;
  109. my $session=shift;
  110. my $do = $cgi->param('do');
  111. return unless $do eq 'comment';
  112. IkiWiki::decode_cgi_utf8($cgi);
  113. eval q{use CGI::FormBuilder};
  114. error($@) if $@;
  115. my @buttons = (POST_COMMENT, PREVIEW, CANCEL);
  116. my $form = CGI::FormBuilder->new(
  117. fields => [qw{do sid page subject body}],
  118. charset => 'utf-8',
  119. method => 'POST',
  120. required => [qw{body}],
  121. javascript => 0,
  122. params => $cgi,
  123. action => $config{cgiurl},
  124. header => 0,
  125. table => 0,
  126. template => scalar IkiWiki::template_params('comments_form.tmpl'),
  127. # wtf does this do in editpage?
  128. wikiname => $config{wikiname},
  129. );
  130. IkiWiki::decode_form_utf8($form);
  131. IkiWiki::run_hooks(formbuilder_setup => sub {
  132. shift->(title => "comment", form => $form, cgi => $cgi,
  133. session => $session, buttons => \@buttons);
  134. });
  135. IkiWiki::decode_form_utf8($form);
  136. $form->field(name => 'do', type => 'hidden');
  137. $form->field(name => 'sid', type => 'hidden', value => $session->id,
  138. force => 1);
  139. $form->field(name => 'page', type => 'hidden');
  140. $form->field(name => 'subject', type => 'text', size => 72);
  141. $form->field(name => 'body', type => 'textarea', rows => 5,
  142. cols => 80);
  143. # The untaint is OK (as in editpage) because we're about to pass
  144. # it to file_pruned anyway
  145. my $page = $form->field('page');
  146. $page = IkiWiki::possibly_foolish_untaint($page);
  147. if (!defined $page || !length $page ||
  148. IkiWiki::file_pruned($page, $config{srcdir})) {
  149. error(gettext("bad page name"));
  150. }
  151. my $allow_directives = $pagestate{$page}{comments}{allowdirectives};
  152. my $commit_comments = defined $pagestate{$page}{comments}{commit}
  153. ? $pagestate{$page}{comments}{commit}
  154. : 1;
  155. # FIXME: is this right? Or should we be using the candidate subpage
  156. # (whatever that might mean) as the base URL?
  157. my $baseurl = urlto($page, undef, 1);
  158. $form->title(sprintf(gettext("commenting on %s"),
  159. IkiWiki::pagetitle($page)));
  160. $form->tmpl_param('helponformattinglink',
  161. htmllink($page, $page, 'ikiwiki/formatting',
  162. noimageinline => 1,
  163. linktext => 'FormattingHelp'),
  164. allowdirectives => $allow_directives);
  165. if (not exists $pagesources{$page}) {
  166. error(sprintf(gettext(
  167. "page '%s' doesn't exist, so you can't comment"),
  168. $page));
  169. }
  170. if (not $pagestate{$page}{comments}{comments}) {
  171. error(sprintf(gettext(
  172. "comments are not enabled on page '%s'"),
  173. $page));
  174. }
  175. if ($form->submitted eq CANCEL) {
  176. # bounce back to the page they wanted to comment on, and exit.
  177. # CANCEL need not be considered in future
  178. IkiWiki::redirect($cgi, urlto($page, undef, 1));
  179. exit;
  180. }
  181. IkiWiki::check_canedit($page . "[postcomment]", $cgi, $session);
  182. my ($authorurl, $author) = linkuser(getcgiuser($session));
  183. my $body = $form->field('body') || '';
  184. $body =~ s/\r\n/\n/g;
  185. $body =~ s/\r/\n/g;
  186. $body .= "\n" if $body !~ /\n$/;
  187. unless ($allow_directives) {
  188. # don't allow new-style directives at all
  189. $body =~ s/(^|[^\\])\[\[!/$1\\[[!/g;
  190. # don't allow [[ unless it begins an old-style
  191. # wikilink, if prefix_directives is off
  192. $body =~ s/(^|[^\\])\[\[(?![^\n\s\]+]\]\])/$1\\[[!/g
  193. unless $config{prefix_directives};
  194. }
  195. IkiWiki::run_hooks(sanitize => sub {
  196. # $fake is a possible location for this comment. We don't
  197. # know yet what the comment number *actually* is.
  198. my $fake = "$page/_comment_1";
  199. $body=shift->(
  200. page => $fake,
  201. destpage => $fake,
  202. content => $body,
  203. );
  204. });
  205. # In this template, the [[!meta]] directives should stay at the end,
  206. # so that they will override anything the user specifies. (For
  207. # instance, [[!meta author="I can fake the author"]]...)
  208. my $content_tmpl = template('comments_comment.tmpl');
  209. $content_tmpl->param(author => $author);
  210. $content_tmpl->param(authorurl => $authorurl);
  211. $content_tmpl->param(subject => $form->field('subject'));
  212. $content_tmpl->param(body => $body);
  213. my $content = $content_tmpl->output;
  214. # This is essentially a simplified version of editpage:
  215. # - the user does not control the page that's created, only the parent
  216. # - it's always a create operation, never an edit
  217. # - this means that conflicts should never happen
  218. # - this means that if they do, rocks fall and everyone dies
  219. if ($form->submitted eq PREVIEW) {
  220. # $fake is a possible location for this comment. We don't
  221. # know yet what the comment number *actually* is.
  222. my $fake = "$page/_comment_1";
  223. my $preview = IkiWiki::htmlize($fake, $page, 'mdwn',
  224. IkiWiki::linkify($page, $page,
  225. IkiWiki::preprocess($page, $page,
  226. IkiWiki::filter($fake, $page,
  227. $content),
  228. 0, 1)));
  229. IkiWiki::run_hooks(format => sub {
  230. $preview = shift->(page => $page,
  231. content => $preview);
  232. });
  233. my $template = template("comments_display.tmpl");
  234. $template->param(content => $preview);
  235. $template->param(title => $form->field('subject'));
  236. $template->param(ctime => displaytime(time));
  237. $template->param(author => $author);
  238. $template->param(authorurl => $authorurl);
  239. $form->tmpl_param(page_preview => $template->output);
  240. }
  241. else {
  242. $form->tmpl_param(page_preview => "");
  243. }
  244. if ($form->submitted eq POST_COMMENT && $form->validate) {
  245. # Let's get posting. We don't check_canedit here because
  246. # that somewhat defeats the point of this plugin.
  247. IkiWiki::checksessionexpiry($session, $cgi->param('sid'));
  248. # FIXME: check that the wiki is locked right now, because
  249. # if it's not, there are mad race conditions!
  250. # FIXME: rather a simplistic way to make the comments...
  251. my $i = 0;
  252. my $file;
  253. do {
  254. $i++;
  255. $file = "$page/_comment_${i}._comment";
  256. } while (-e "$config{srcdir}/$file");
  257. # FIXME: could probably do some sort of graceful retry
  258. # if I could be bothered
  259. writefile($file, $config{srcdir}, $content);
  260. my $conflict;
  261. if ($config{rcs} and $commit_comments) {
  262. my $message = gettext("Added a comment");
  263. if (defined $form->field('subject') &&
  264. length $form->field('subject')) {
  265. $message .= ": ".$form->field('subject');
  266. }
  267. IkiWiki::rcs_add($file);
  268. IkiWiki::disable_commit_hook();
  269. $conflict = IkiWiki::rcs_commit_staged($message,
  270. $session->param('name'), $ENV{REMOTE_ADDR});
  271. IkiWiki::enable_commit_hook();
  272. IkiWiki::rcs_update();
  273. }
  274. # Now we need a refresh
  275. require IkiWiki::Render;
  276. IkiWiki::refresh();
  277. IkiWiki::saveindex();
  278. # this should never happen, unless a committer deliberately
  279. # breaks it or something
  280. error($conflict) if defined $conflict;
  281. # Bounce back to where we were, but defeat broken caches
  282. my $anticache = "?updated=$page/_comment_$i";
  283. IkiWiki::redirect($cgi, urlto($page, undef, 1).$anticache);
  284. }
  285. else {
  286. IkiWiki::showform ($form, \@buttons, $session, $cgi,
  287. forcebaseurl => $baseurl);
  288. }
  289. exit;
  290. } #}}}
  291. package IkiWiki::PageSpec;
  292. sub match_postcomment ($$;@) {
  293. my $page = shift;
  294. my $glob = shift;
  295. unless ($page =~ s/\[postcomment\]$//) {
  296. return IkiWiki::FailReason->new("not posting a comment");
  297. }
  298. return match_glob($page, $glob);
  299. }
  300. 1