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