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