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