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