summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/comments.pm
blob: 6e257d1d9b2d21d3da582639c07f5845d09f7fea (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 Encode;
  11. use POSIX qw(strftime);
  12. use constant PREVIEW => "Preview";
  13. use constant POST_COMMENT => "Post comment";
  14. use constant CANCEL => "Cancel";
  15. my $postcomment;
  16. sub import {
  17. hook(type => "checkconfig", id => 'comments', call => \&checkconfig);
  18. hook(type => "getsetup", id => 'comments', call => \&getsetup);
  19. hook(type => "preprocess", id => '_comment', call => \&preprocess);
  20. hook(type => "sessioncgi", id => 'comment', call => \&sessioncgi);
  21. hook(type => "htmlize", id => "_comment", call => \&htmlize);
  22. hook(type => "pagetemplate", id => "comments", call => \&pagetemplate);
  23. hook(type => "cgi", id => "comments", call => \&linkcgi);
  24. IkiWiki::loadplugin("inline");
  25. }
  26. sub getsetup () {
  27. return
  28. plugin => {
  29. safe => 1,
  30. rebuild => 1,
  31. },
  32. comments_pagespec => {
  33. type => 'pagespec',
  34. example => 'blog/* and *!/Discussion',
  35. description => 'PageSpec of pages where comments are allowed',
  36. link => 'ikiwiki/PageSpec',
  37. safe => 1,
  38. rebuild => 1,
  39. },
  40. comments_closed_pagespec => {
  41. type => 'pagespec',
  42. example => 'blog/controversial or blog/flamewar',
  43. description => 'PageSpec of pages where posting new comments is not allowed',
  44. link => 'ikiwiki/PageSpec',
  45. safe => 1,
  46. rebuild => 1,
  47. },
  48. comments_pagename => {
  49. type => 'string',
  50. default => 'comment_',
  51. description => 'Base name for comments, e.g. "comment_" for pages like "sandbox/comment_12"',
  52. safe => 0, # manual page moving required
  53. rebuild => undef,
  54. },
  55. comments_allowdirectives => {
  56. type => 'boolean',
  57. example => 0,
  58. description => 'Interpret directives in comments?',
  59. safe => 1,
  60. rebuild => 0,
  61. },
  62. comments_allowauthor => {
  63. type => 'boolean',
  64. example => 0,
  65. description => 'Allow anonymous commenters to set an author name?',
  66. safe => 1,
  67. rebuild => 0,
  68. },
  69. comments_commit => {
  70. type => 'boolean',
  71. example => 1,
  72. description => 'commit comments to the VCS',
  73. # old uncommitted comments are likely to cause
  74. # confusion if this is changed
  75. safe => 0,
  76. rebuild => 0,
  77. },
  78. }
  79. sub checkconfig () {
  80. $config{comments_commit} = 1
  81. unless defined $config{comments_commit};
  82. $config{comments_pagespec} = ''
  83. unless defined $config{comments_pagespec};
  84. $config{comments_closed_pagespec} = ''
  85. unless defined $config{comments_closed_pagespec};
  86. $config{comments_pagename} = 'comment_'
  87. unless defined $config{comments_pagename};
  88. }
  89. sub htmlize {
  90. my %params = @_;
  91. return $params{content};
  92. }
  93. # FIXME: copied verbatim from meta
  94. sub safeurl ($) {
  95. my $url=shift;
  96. if (exists $IkiWiki::Plugin::htmlscrubber::{safe_url_regexp} &&
  97. defined $IkiWiki::Plugin::htmlscrubber::safe_url_regexp) {
  98. return $url=~/$IkiWiki::Plugin::htmlscrubber::safe_url_regexp/;
  99. }
  100. else {
  101. return 1;
  102. }
  103. }
  104. sub preprocess {
  105. my %params = @_;
  106. my $page = $params{page};
  107. my $format = $params{format};
  108. if (defined $format && ! exists $IkiWiki::hooks{htmlize}{$format}) {
  109. error(sprintf(gettext("unsupported page format %s"), $format));
  110. }
  111. my $content = $params{content};
  112. if (! defined $content) {
  113. error(gettext("comment must have content"));
  114. }
  115. $content =~ s/\\"/"/g;
  116. $content = IkiWiki::filter($page, $params{destpage}, $content);
  117. if ($config{comments_allowdirectives}) {
  118. $content = IkiWiki::preprocess($page, $params{destpage},
  119. $content);
  120. }
  121. # no need to bother with htmlize if it's just HTML
  122. $content = IkiWiki::htmlize($page, $params{destpage}, $format,
  123. $content) if defined $format;
  124. IkiWiki::run_hooks(sanitize => sub {
  125. $content = shift->(
  126. page => $page,
  127. destpage => $params{destpage},
  128. content => $content,
  129. );
  130. });
  131. # set metadata, possibly overriding [[!meta]] directives from the
  132. # comment itself
  133. my $commentuser;
  134. my $commentip;
  135. my $commentauthor;
  136. my $commentauthorurl;
  137. if (defined $params{username}) {
  138. $commentuser = $params{username};
  139. ($commentauthorurl, $commentauthor) =
  140. linkuser($params{username});
  141. }
  142. else {
  143. if (defined $params{ip}) {
  144. $commentip = $params{ip};
  145. }
  146. $commentauthor = gettext("Anonymous");
  147. }
  148. $pagestate{$page}{comments}{commentuser} = $commentuser;
  149. $pagestate{$page}{comments}{commentip} = $commentip;
  150. $pagestate{$page}{comments}{commentauthor} = $commentauthor;
  151. $pagestate{$page}{comments}{commentauthorurl} = $commentauthorurl;
  152. if (! defined $pagestate{$page}{meta}{author}) {
  153. $pagestate{$page}{meta}{author} = $commentauthor;
  154. }
  155. if (! defined $pagestate{$page}{meta}{authorurl}) {
  156. $pagestate{$page}{meta}{authorurl} = $commentauthorurl;
  157. }
  158. if ($config{comments_allowauthor}) {
  159. if (defined $params{claimedauthor}) {
  160. $pagestate{$page}{meta}{author} = $params{claimedauthor};
  161. }
  162. if (defined $params{url} and safeurl($params{url})) {
  163. $pagestate{$page}{meta}{authorurl} = $params{url};
  164. }
  165. }
  166. else {
  167. $pagestate{$page}{meta}{author} = $commentauthor;
  168. $pagestate{$page}{meta}{authorurl} = $commentauthorurl;
  169. }
  170. if (defined $params{subject}) {
  171. $pagestate{$page}{meta}{title} = $params{subject};
  172. }
  173. my $baseurl = urlto($params{destpage}, undef, 1);
  174. my $anchor = "";
  175. if ($params{page} =~ m/\/(\Q$config{comments_pagename}\E\d+)$/) {
  176. $anchor = $1;
  177. }
  178. $pagestate{$page}{meta}{permalink} = "${baseurl}#${anchor}";
  179. eval q{use Date::Parse};
  180. if (! $@) {
  181. my $time = str2time($params{date});
  182. $IkiWiki::pagectime{$page} = $time if defined $time;
  183. }
  184. # FIXME: hard-coded HTML (although it's just to set an ID)
  185. return "<div id=\"$anchor\">$content</div>" if $anchor;
  186. return $content;
  187. }
  188. # This is exactly the same as recentchanges_link :-(
  189. sub linkcgi ($) {
  190. my $cgi=shift;
  191. if (defined $cgi->param('do') && $cgi->param('do') eq "commenter") {
  192. my $page=decode_utf8($cgi->param("page"));
  193. if (! defined $page) {
  194. error("missing page parameter");
  195. }
  196. IkiWiki::loadindex();
  197. my $link=bestlink("", $page);
  198. if (! length $link) {
  199. print "Content-type: text/html\n\n";
  200. print IkiWiki::misctemplate(gettext(gettext("missing page")),
  201. "<p>".
  202. sprintf(gettext("The page %s does not exist."),
  203. htmllink("", "", $page)).
  204. "</p>");
  205. }
  206. else {
  207. IkiWiki::redirect($cgi, urlto($link, undef, 1));
  208. }
  209. exit;
  210. }
  211. }
  212. # FIXME: basically the same logic as recentchanges
  213. # returns (author URL, pretty-printed version)
  214. sub linkuser ($) {
  215. my $user = shift;
  216. my $oiduser = eval { IkiWiki::openiduser($user) };
  217. if (defined $oiduser) {
  218. return ($user, $oiduser);
  219. }
  220. # FIXME: it'd be good to avoid having such a link for anonymous
  221. # posts
  222. else {
  223. return (IkiWiki::cgiurl(
  224. do => 'commenter',
  225. page => (length $config{userdir}
  226. ? "$config{userdir}/$user"
  227. : "$user")
  228. ), $user);
  229. }
  230. }
  231. # Mostly cargo-culted from IkiWiki::plugin::editpage
  232. sub sessioncgi ($$) {
  233. my $cgi=shift;
  234. my $session=shift;
  235. my $do = $cgi->param('do');
  236. return unless $do eq 'comment';
  237. IkiWiki::decode_cgi_utf8($cgi);
  238. eval q{use CGI::FormBuilder};
  239. error($@) if $@;
  240. my @buttons = (POST_COMMENT, PREVIEW, CANCEL);
  241. my $form = CGI::FormBuilder->new(
  242. fields => [qw{do sid page subject editcontent type author url}],
  243. charset => 'utf-8',
  244. method => 'POST',
  245. required => [qw{editcontent}],
  246. javascript => 0,
  247. params => $cgi,
  248. action => $config{cgiurl},
  249. header => 0,
  250. table => 0,
  251. template => scalar IkiWiki::template_params('comments_form.tmpl'),
  252. # wtf does this do in editpage?
  253. wikiname => $config{wikiname},
  254. );
  255. IkiWiki::decode_form_utf8($form);
  256. IkiWiki::run_hooks(formbuilder_setup => sub {
  257. shift->(title => "comment", form => $form, cgi => $cgi,
  258. session => $session, buttons => \@buttons);
  259. });
  260. IkiWiki::decode_form_utf8($form);
  261. my $type = $form->param('type');
  262. if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
  263. $type = IkiWiki::possibly_foolish_untaint($type);
  264. }
  265. else {
  266. $type = $config{default_pageext};
  267. }
  268. my @page_types;
  269. if (exists $IkiWiki::hooks{htmlize}) {
  270. @page_types = grep { ! /^_/ } keys %{$IkiWiki::hooks{htmlize}};
  271. }
  272. $form->field(name => 'do', type => 'hidden');
  273. $form->field(name => 'sid', type => 'hidden', value => $session->id,
  274. force => 1);
  275. $form->field(name => 'page', type => 'hidden');
  276. $form->field(name => 'subject', type => 'text', size => 72);
  277. $form->field(name => 'editcontent', type => 'textarea', rows => 10);
  278. $form->field(name => "type", value => $type, force => 1,
  279. type => 'select', options => \@page_types);
  280. $form->tmpl_param(username => $session->param('name'));
  281. if ($config{comments_allowauthor} and
  282. ! defined $session->param('name')) {
  283. $form->tmpl_param(allowauthor => 1);
  284. $form->field(name => 'author', type => 'text', size => '40');
  285. $form->field(name => 'url', type => 'text', size => '40');
  286. }
  287. else {
  288. $form->tmpl_param(allowauthor => 0);
  289. $form->field(name => 'author', type => 'hidden', value => '',
  290. force => 1);
  291. $form->field(name => 'url', type => 'hidden', value => '',
  292. force => 1);
  293. }
  294. # The untaint is OK (as in editpage) because we're about to pass
  295. # it to file_pruned anyway
  296. my $page = $form->field('page');
  297. $page = IkiWiki::possibly_foolish_untaint($page);
  298. if (! defined $page || ! length $page ||
  299. IkiWiki::file_pruned($page, $config{srcdir})) {
  300. error(gettext("bad page name"));
  301. }
  302. # FIXME: is this right? Or should we be using the candidate subpage
  303. # (whatever that might mean) as the base URL?
  304. my $baseurl = urlto($page, undef, 1);
  305. $form->title(sprintf(gettext("commenting on %s"),
  306. IkiWiki::pagetitle($page)));
  307. $form->tmpl_param('helponformattinglink',
  308. htmllink($page, $page, 'ikiwiki/formatting',
  309. noimageinline => 1,
  310. linktext => 'FormattingHelp'),
  311. allowdirectives => $config{allow_directives});
  312. if ($form->submitted eq CANCEL) {
  313. # bounce back to the page they wanted to comment on, and exit.
  314. # CANCEL need not be considered in future
  315. IkiWiki::redirect($cgi, urlto($page, undef, 1));
  316. exit;
  317. }
  318. if (not exists $pagesources{$page}) {
  319. error(sprintf(gettext(
  320. "page '%s' doesn't exist, so you can't comment"),
  321. $page));
  322. }
  323. if (pagespec_match($page, $config{comments_closed_pagespec},
  324. location => $page)) {
  325. error(sprintf(gettext(
  326. "comments on page '%s' are closed"),
  327. $page));
  328. }
  329. # Set a flag to indicate that we're posting a comment,
  330. # so that postcomment() can tell it should match.
  331. $postcomment=1;
  332. IkiWiki::check_canedit($page, $cgi, $session);
  333. $postcomment=0;
  334. # FIXME: rather a simplistic way to make the comments...
  335. my $i = 0;
  336. my $file;
  337. my $location;
  338. do {
  339. $i++;
  340. $location = "$page/$config{comments_pagename}$i";
  341. } while (-e "$config{srcdir}/$location._comment");
  342. my $content = "[[!_comment format=$type\n";
  343. # FIXME: handling of double quotes probably wrong?
  344. if (defined $session->param('name')) {
  345. my $username = $session->param('name');
  346. $username =~ s/"/&quot;/g;
  347. $content .= " username=\"$username\"\n";
  348. }
  349. elsif (defined $ENV{REMOTE_ADDR}) {
  350. my $ip = $ENV{REMOTE_ADDR};
  351. if ($ip =~ m/^([.0-9]+)$/) {
  352. $content .= " ip=\"$1\"\n";
  353. }
  354. }
  355. if ($config{comments_allowauthor}) {
  356. my $author = $form->field('author');
  357. if (length $author) {
  358. $author =~ s/"/&quot;/g;
  359. $content .= " claimedauthor=\"$author\"\n";
  360. }
  361. my $url = $form->field('url');
  362. if (length $url) {
  363. $url =~ s/"/&quot;/g;
  364. $content .= " url=\"$url\"\n";
  365. }
  366. }
  367. my $subject = $form->field('subject');
  368. if (length $subject) {
  369. $subject =~ s/"/&quot;/g;
  370. $content .= " subject=\"$subject\"\n";
  371. }
  372. $content .= " date=\"" . decode_utf8(strftime('%Y-%m-%dT%H:%M:%SZ', gmtime)) . "\"\n";
  373. my $editcontent = $form->field('editcontent') || '';
  374. $editcontent =~ s/\r\n/\n/g;
  375. $editcontent =~ s/\r/\n/g;
  376. $editcontent =~ s/"/\\"/g;
  377. $content .= " content=\"\"\"\n$editcontent\n\"\"\"]]\n";
  378. # This is essentially a simplified version of editpage:
  379. # - the user does not control the page that's created, only the parent
  380. # - it's always a create operation, never an edit
  381. # - this means that conflicts should never happen
  382. # - this means that if they do, rocks fall and everyone dies
  383. if ($form->submitted eq PREVIEW) {
  384. my $preview = IkiWiki::htmlize($location, $page, '_comment',
  385. IkiWiki::linkify($page, $page,
  386. IkiWiki::preprocess($page, $page,
  387. IkiWiki::filter($location,
  388. $page, $content),
  389. 0, 1)));
  390. IkiWiki::run_hooks(format => sub {
  391. $preview = shift->(page => $page,
  392. content => $preview);
  393. });
  394. my $template = template("comments_display.tmpl");
  395. $template->param(content => $preview);
  396. $template->param(title => $form->field('subject'));
  397. $template->param(ctime => displaytime(time));
  398. $form->tmpl_param(page_preview => $template->output);
  399. }
  400. else {
  401. $form->tmpl_param(page_preview => "");
  402. }
  403. if ($form->submitted eq POST_COMMENT && $form->validate) {
  404. my $file = "$location._comment";
  405. IkiWiki::checksessionexpiry($cgi, $session);
  406. # FIXME: could probably do some sort of graceful retry
  407. # on error? Would require significant unwinding though
  408. writefile($file, $config{srcdir}, $content);
  409. my $conflict;
  410. if ($config{rcs} and $config{comments_commit}) {
  411. my $message = gettext("Added a comment");
  412. if (defined $form->field('subject') &&
  413. length $form->field('subject')) {
  414. $message = sprintf(
  415. gettext("Added a comment: %s"),
  416. $form->field('subject'));
  417. }
  418. IkiWiki::rcs_add($file);
  419. IkiWiki::disable_commit_hook();
  420. $conflict = IkiWiki::rcs_commit_staged($message,
  421. $session->param('name'), $ENV{REMOTE_ADDR});
  422. IkiWiki::enable_commit_hook();
  423. IkiWiki::rcs_update();
  424. }
  425. # Now we need a refresh
  426. require IkiWiki::Render;
  427. IkiWiki::refresh();
  428. IkiWiki::saveindex();
  429. # this should never happen, unless a committer deliberately
  430. # breaks it or something
  431. error($conflict) if defined $conflict;
  432. # Bounce back to where we were, but defeat broken caches
  433. my $anticache = "?updated=$page/$config{comments_pagename}$i";
  434. IkiWiki::redirect($cgi, urlto($page, undef, 1).$anticache);
  435. }
  436. else {
  437. IkiWiki::showform ($form, \@buttons, $session, $cgi,
  438. forcebaseurl => $baseurl);
  439. }
  440. exit;
  441. }
  442. sub pagetemplate (@) {
  443. my %params = @_;
  444. my $page = $params{page};
  445. my $template = $params{template};
  446. if ($template->query(name => 'comments')) {
  447. my $comments = undef;
  448. my $open = 0;
  449. my $shown = 0;
  450. if (pagespec_match($page,
  451. $config{comments_pagespec},
  452. location => $page)) {
  453. $shown = 1;
  454. $open = length $config{cgiurl} > 0;
  455. }
  456. if (pagespec_match($page,
  457. "$config{comments_closed_pagespec} or */$config{comments_pagename}*",
  458. location => $page)) {
  459. $shown = 0;
  460. $open = 0;
  461. }
  462. if ($shown) {
  463. $comments = IkiWiki::preprocess_inline(
  464. pages => "internal($page/$config{comments_pagename}*)",
  465. template => 'comments_display',
  466. show => 0,
  467. reverse => 'yes',
  468. page => $page,
  469. destpage => $params{destpage},
  470. feedfile => 'comments',
  471. emptyfeeds => 'no',
  472. );
  473. }
  474. if (defined $comments && length $comments) {
  475. $template->param(comments => $comments);
  476. }
  477. if ($open) {
  478. my $commenturl = IkiWiki::cgiurl(do => 'comment',
  479. page => $page);
  480. $template->param(commenturl => $commenturl);
  481. }
  482. }
  483. if ($template->query(name => 'commentuser')) {
  484. $template->param(commentuser =>
  485. $pagestate{$page}{comments}{commentuser});
  486. }
  487. if ($template->query(name => 'commentip')) {
  488. $template->param(commentip =>
  489. $pagestate{$page}{comments}{commentip});
  490. }
  491. if ($template->query(name => 'commentauthor')) {
  492. $template->param(commentauthor =>
  493. $pagestate{$page}{comments}{commentauthor});
  494. }
  495. if ($template->query(name => 'commentauthorurl')) {
  496. $template->param(commentauthorurl =>
  497. $pagestate{$page}{comments}{commentauthorurl});
  498. }
  499. }
  500. package IkiWiki::PageSpec;
  501. sub match_postcomment ($$;@) {
  502. my $page = shift;
  503. my $glob = shift;
  504. if (! $postcomment) {
  505. return IkiWiki::FailReason->new("not posting a comment");
  506. }
  507. return match_glob($page, $glob);
  508. }
  509. 1