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