summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/comments.pm
blob: c545a1335d39d385692f9d38e8abc7dd4dbc5bfc (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. IkiWiki::run_hooks(sanitize => sub {
  215. # $fake is a possible location for this comment. We don't
  216. # know yet what the comment number *actually* is.
  217. my $fake = "$page/_comment_1";
  218. $body=shift->(
  219. page => $fake,
  220. destpage => $fake,
  221. content => $body,
  222. );
  223. });
  224. # In this template, the [[!meta]] directives should stay at the end,
  225. # so that they will override anything the user specifies. (For
  226. # instance, [[!meta author="I can fake the author"]]...)
  227. my $content_tmpl = template('comments_comment.tmpl');
  228. $content_tmpl->param(author => $author);
  229. $content_tmpl->param(authorurl => $authorurl);
  230. $content_tmpl->param(subject => $form->field('subject'));
  231. $content_tmpl->param(body => $body);
  232. my $content = $content_tmpl->output;
  233. # This is essentially a simplified version of editpage:
  234. # - the user does not control the page that's created, only the parent
  235. # - it's always a create operation, never an edit
  236. # - this means that conflicts should never happen
  237. # - this means that if they do, rocks fall and everyone dies
  238. if ($form->submitted eq PREVIEW) {
  239. # $fake is a possible location for this comment. We don't
  240. # know yet what the comment number *actually* is.
  241. my $fake = "$page/_comment_1";
  242. my $preview = IkiWiki::htmlize($fake, $page, 'mdwn',
  243. IkiWiki::linkify($page, $page,
  244. IkiWiki::preprocess($page, $page,
  245. IkiWiki::filter($fake, $page,
  246. $content),
  247. 0, 1)));
  248. IkiWiki::run_hooks(format => sub {
  249. $preview = shift->(page => $page,
  250. content => $preview);
  251. });
  252. my $template = template("comments_display.tmpl");
  253. $template->param(content => $preview);
  254. $template->param(title => $form->field('subject'));
  255. $template->param(ctime => displaytime(time));
  256. $template->param(author => $author);
  257. $template->param(authorurl => $authorurl);
  258. $form->tmpl_param(page_preview => $template->output);
  259. }
  260. else {
  261. $form->tmpl_param(page_preview => "");
  262. }
  263. if ($form->submitted eq POST_COMMENT && $form->validate) {
  264. # Let's get posting. We don't check_canedit here because
  265. # that somewhat defeats the point of this plugin.
  266. checksessionexpiry($session, $cgi->param('sid'));
  267. # FIXME: check that the wiki is locked right now, because
  268. # if it's not, there are mad race conditions!
  269. # FIXME: rather a simplistic way to make the comments...
  270. my $i = 0;
  271. my $file;
  272. do {
  273. $i++;
  274. $file = "$page/_comment_${i}._comment";
  275. } while (-e "$config{srcdir}/$file");
  276. # FIXME: could probably do some sort of graceful retry
  277. # if I could be bothered
  278. writefile($file, $config{srcdir}, $content);
  279. my $conflict;
  280. if ($config{rcs} and $commit_comments) {
  281. my $message = gettext("Added a comment");
  282. if (defined $form->field('subject') &&
  283. length $form->field('subject')) {
  284. $message .= ": ".$form->field('subject');
  285. }
  286. IkiWiki::rcs_add($file);
  287. IkiWiki::disable_commit_hook();
  288. $conflict = IkiWiki::rcs_commit_staged($message,
  289. $session->param('name'), $ENV{REMOTE_ADDR});
  290. IkiWiki::enable_commit_hook();
  291. IkiWiki::rcs_update();
  292. }
  293. # Now we need a refresh
  294. require IkiWiki::Render;
  295. IkiWiki::refresh();
  296. IkiWiki::saveindex();
  297. # this should never happen, unless a committer deliberately
  298. # breaks it or something
  299. error($conflict) if defined $conflict;
  300. # Bounce back to where we were, but defeat broken caches
  301. my $anticache = "?updated=$page/_comment_$i";
  302. IkiWiki::redirect($cgi, urlto($page, undef, 1).$anticache);
  303. }
  304. else {
  305. IkiWiki::showform ($form, \@buttons, $session, $cgi,
  306. forcebaseurl => $baseurl);
  307. }
  308. exit;
  309. } #}}}
  310. package IkiWiki::PageSpec;
  311. sub match_postcomment ($$;@) {
  312. my $page = shift;
  313. my $glob = shift;
  314. unless ($page =~ s/\[postcomment\]$//) {
  315. return IkiWiki::FailReason->new("not posting a comment");
  316. }
  317. return match_glob($page, $glob);
  318. }
  319. 1