summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/comments.pm
blob: 6bd18a5cf2be10765a5dbbf5bf83fbd1a88c80bf (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. elsif (defined $params{ip}) {
  78. $commentip = $params{ip};
  79. $commentauthor = sprintf(
  80. gettext("Anonymous (IP: %s)"), $params{ip});
  81. }
  82. else {
  83. $commentauthor = gettext("Anonymous");
  84. }
  85. $pagestate{$page}{comments}{commentuser} = $commentuser;
  86. $pagestate{$page}{comments}{commentip} = $commentip;
  87. $pagestate{$page}{comments}{commentauthor} = $commentauthor;
  88. $pagestate{$page}{comments}{commentauthorurl} = $commentauthorurl;
  89. if (!defined $pagestate{$page}{meta}{author}) {
  90. $pagestate{$page}{meta}{author} = $commentauthor;
  91. }
  92. if (!defined $pagestate{$page}{meta}{authorurl}) {
  93. $pagestate{$page}{meta}{authorurl} = $commentauthorurl;
  94. }
  95. if ($config{comments_allowauthor}) {
  96. if (defined $params{claimedauthor}) {
  97. $pagestate{$page}{meta}{author} = $params{claimedauthor};
  98. }
  99. if (defined $params{url} and safeurl($params{url})) {
  100. $pagestate{$page}{meta}{authorurl} = $params{url};
  101. }
  102. }
  103. else {
  104. $pagestate{$page}{meta}{author} = $commentauthor;
  105. $pagestate{$page}{meta}{authorurl} = $commentauthorurl;
  106. }
  107. if (defined $params{subject}) {
  108. $pagestate{$page}{meta}{title} = $params{subject};
  109. }
  110. my $baseurl = urlto($params{destpage}, undef, 1);
  111. my $anchor = "";
  112. my $comments_pagename = $config{comments_pagename};
  113. if ($params{page} =~ m/\/(\Q${comments_pagename}\E\d+)$/) {
  114. $anchor = $1;
  115. }
  116. $pagestate{$page}{meta}{permalink} = "${baseurl}#${anchor}";
  117. eval q{use Date::Parse};
  118. if (! $@) {
  119. my $time = str2time($params{date});
  120. $IkiWiki::pagectime{$page} = $time if defined $time;
  121. }
  122. # FIXME: hard-coded HTML (although it's just to set an ID)
  123. return "<div id=\"$anchor\">$content</div>" if $anchor;
  124. return $content;
  125. } # }}}
  126. sub getsetup () { #{{{
  127. return
  128. plugin => {
  129. safe => 1,
  130. rebuild => 1,
  131. },
  132. # Pages where comments are shown, but new comments are not
  133. # allowed, will show "Comments are closed".
  134. comments_shown_pagespec => {
  135. type => 'pagespec',
  136. example => 'blog/*',
  137. default => '',
  138. description => 'PageSpec for pages where comments will be shown inline',
  139. link => 'ikiwiki/PageSpec',
  140. safe => 1,
  141. rebuild => 1,
  142. },
  143. comments_open_pagespec => {
  144. type => 'pagespec',
  145. example => 'blog/* and created_after(close_old_comments)',
  146. default => '',
  147. description => 'PageSpec for pages where new comments can be posted',
  148. link => 'ikiwiki/PageSpec',
  149. safe => 1,
  150. rebuild => 1,
  151. },
  152. comments_pagename => {
  153. type => 'string',
  154. example => 'comment_',
  155. default => 'comment_',
  156. description => 'Base name for comments, e.g. "comment_" for pages like "sandbox/comment_12"',
  157. safe => 0, # manual page moving will required
  158. rebuild => undef,
  159. },
  160. comments_allowdirectives => {
  161. type => 'boolean',
  162. default => 0,
  163. example => 0,
  164. description => 'Interpret directives in comments?',
  165. safe => 1,
  166. rebuild => 0,
  167. },
  168. comments_allowauthor => {
  169. type => 'boolean',
  170. default => 0,
  171. example => 0,
  172. description => 'Allow anonymous commenters to set an author name?',
  173. safe => 1,
  174. rebuild => 0,
  175. },
  176. comments_commit => {
  177. type => 'boolean',
  178. example => 1,
  179. default => 1,
  180. description => 'commit comments to the VCS',
  181. # old uncommitted comments are likely to cause
  182. # confusion if this is changed
  183. safe => 0,
  184. rebuild => 0,
  185. },
  186. } #}}}
  187. sub checkconfig () { #{{{
  188. $config{comments_commit} = 1 unless defined $config{comments_commit};
  189. $config{comments_pagename} = 'comment_'
  190. unless defined $config{comments_pagename};
  191. } #}}}
  192. # This is exactly the same as recentchanges_link :-(
  193. sub linkcgi ($) { #{{{
  194. my $cgi=shift;
  195. if (defined $cgi->param('do') && $cgi->param('do') eq "commenter") {
  196. my $page=decode_utf8($cgi->param("page"));
  197. if (!defined $page) {
  198. error("missing page parameter");
  199. }
  200. IkiWiki::loadindex();
  201. my $link=bestlink("", $page);
  202. if (! length $link) {
  203. print "Content-type: text/html\n\n";
  204. print IkiWiki::misctemplate(gettext(gettext("missing page")),
  205. "<p>".
  206. sprintf(gettext("The page %s does not exist."),
  207. htmllink("", "", $page)).
  208. "</p>");
  209. }
  210. else {
  211. IkiWiki::redirect($cgi, urlto($link, undef, 1));
  212. }
  213. exit;
  214. }
  215. }
  216. # FIXME: basically the same logic as recentchanges
  217. # returns (author URL, pretty-printed version)
  218. sub linkuser ($) { # {{{
  219. my $user = shift;
  220. my $oiduser = eval { IkiWiki::openiduser($user) };
  221. if (defined $oiduser) {
  222. return ($user, $oiduser);
  223. }
  224. # FIXME: it'd be good to avoid having such a link for anonymous
  225. # posts
  226. else {
  227. return (IkiWiki::cgiurl(
  228. do => 'commenter',
  229. page => (length $config{userdir}
  230. ? "$config{userdir}/$user"
  231. : "$user")
  232. ), $user);
  233. }
  234. } # }}}
  235. # Mostly cargo-culted from IkiWiki::plugin::editpage
  236. sub sessioncgi ($$) { #{{{
  237. my $cgi=shift;
  238. my $session=shift;
  239. my $do = $cgi->param('do');
  240. return unless $do eq 'comment';
  241. IkiWiki::decode_cgi_utf8($cgi);
  242. eval q{use CGI::FormBuilder};
  243. error($@) if $@;
  244. my @buttons = (POST_COMMENT, PREVIEW, CANCEL);
  245. my $form = CGI::FormBuilder->new(
  246. fields => [qw{do sid page subject editcontent type author url}],
  247. charset => 'utf-8',
  248. method => 'POST',
  249. required => [qw{editcontent}],
  250. javascript => 0,
  251. params => $cgi,
  252. action => $config{cgiurl},
  253. header => 0,
  254. table => 0,
  255. template => scalar IkiWiki::template_params('comments_form.tmpl'),
  256. # wtf does this do in editpage?
  257. wikiname => $config{wikiname},
  258. );
  259. IkiWiki::decode_form_utf8($form);
  260. IkiWiki::run_hooks(formbuilder_setup => sub {
  261. shift->(title => "comment", form => $form, cgi => $cgi,
  262. session => $session, buttons => \@buttons);
  263. });
  264. IkiWiki::decode_form_utf8($form);
  265. my $type = $form->param('type');
  266. if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
  267. $type = possibly_foolish_untaint($type);
  268. }
  269. else {
  270. $type = $config{default_pageext};
  271. }
  272. my @page_types;
  273. if (exists $IkiWiki::hooks{htmlize}) {
  274. @page_types = grep { !/^_/ } keys %{$IkiWiki::hooks{htmlize}};
  275. }
  276. my $allow_author = $config{comments_allowauthor};
  277. $form->field(name => 'do', type => 'hidden');
  278. $form->field(name => 'sid', type => 'hidden', value => $session->id,
  279. force => 1);
  280. $form->field(name => 'page', type => 'hidden');
  281. $form->field(name => 'subject', type => 'text', size => 72);
  282. $form->field(name => 'editcontent', type => 'textarea', rows => 10);
  283. $form->field(name => "type", value => $type, force => 1,
  284. type => 'select', options => \@page_types);
  285. $form->tmpl_param(username => $session->param('name'));
  286. if ($allow_author and !defined $session->param('name')) {
  287. $form->tmpl_param(allowauthor => 1);
  288. $form->field(name => 'author', type => 'text', size => '40');
  289. $form->field(name => 'url', type => 'text', size => '40');
  290. }
  291. else {
  292. $form->tmpl_param(allowauthor => 0);
  293. $form->field(name => 'author', type => 'hidden', value => '',
  294. force => 1);
  295. $form->field(name => 'url', type => 'hidden', value => '',
  296. force => 1);
  297. }
  298. # The untaint is OK (as in editpage) because we're about to pass
  299. # it to file_pruned anyway
  300. my $page = $form->field('page');
  301. $page = IkiWiki::possibly_foolish_untaint($page);
  302. if (!defined $page || !length $page ||
  303. IkiWiki::file_pruned($page, $config{srcdir})) {
  304. error(gettext("bad page name"));
  305. }
  306. my $allow_directives = $config{comments_allowdirectives};
  307. my $commit_comments = $config{comments_commit};
  308. my $comments_pagename = $config{comments_pagename};
  309. # FIXME: is this right? Or should we be using the candidate subpage
  310. # (whatever that might mean) as the base URL?
  311. my $baseurl = urlto($page, undef, 1);
  312. $form->title(sprintf(gettext("commenting on %s"),
  313. IkiWiki::pagetitle($page)));
  314. $form->tmpl_param('helponformattinglink',
  315. htmllink($page, $page, 'ikiwiki/formatting',
  316. noimageinline => 1,
  317. linktext => 'FormattingHelp'),
  318. allowdirectives => $allow_directives);
  319. if ($form->submitted eq CANCEL) {
  320. # bounce back to the page they wanted to comment on, and exit.
  321. # CANCEL need not be considered in future
  322. IkiWiki::redirect($cgi, urlto($page, undef, 1));
  323. exit;
  324. }
  325. if (not exists $pagesources{$page}) {
  326. error(sprintf(gettext(
  327. "page '%s' doesn't exist, so you can't comment"),
  328. $page));
  329. }
  330. if (not pagespec_match($page, $config{comments_open_pagespec},
  331. location => $page)) {
  332. error(sprintf(gettext(
  333. "comments on page '%s' are closed"),
  334. $page));
  335. }
  336. IkiWiki::check_canedit($page . "[postcomment]", $cgi, $session);
  337. my $editcontent = $form->field('editcontent') || '';
  338. $editcontent =~ s/\r\n/\n/g;
  339. $editcontent =~ s/\r/\n/g;
  340. # FIXME: check that the wiki is locked right now, because
  341. # if it's not, there are mad race conditions!
  342. # FIXME: rather a simplistic way to make the comments...
  343. my $i = 0;
  344. my $file;
  345. my $location;
  346. do {
  347. $i++;
  348. $location = "$page/${comments_pagename}${i}";
  349. } while (-e "$config{srcdir}/$location._comment");
  350. my $anchor = "${comments_pagename}${i}";
  351. $editcontent =~ s/"/\\"/g;
  352. my $content = "[[!comment format=$type\n";
  353. # FIXME: handling of double quotes probably wrong?
  354. if (defined $session->param('name')) {
  355. my $username = $session->param('name');
  356. $username =~ s/"/&quot;/g;
  357. $content .= " username=\"$username\"\n";
  358. }
  359. elsif (defined $ENV{REMOTE_ADDR}) {
  360. my $ip = $ENV{REMOTE_ADDR};
  361. if ($ip =~ m/^([.0-9]+)$/) {
  362. $content .= " ip=\"$1\"\n";
  363. }
  364. }
  365. if ($allow_author) {
  366. my $author = $form->field('author');
  367. if (length $author) {
  368. $author =~ s/"/&quot;/g;
  369. $content .= " claimedauthor=\"$author\"\n";
  370. }
  371. my $url = $form->field('url');
  372. if (length $url) {
  373. $url =~ s/"/&quot;/g;
  374. $content .= " url=\"$url\"\n";
  375. }
  376. }
  377. my $subject = $form->field('subject');
  378. $subject =~ s/"/&quot;/g;
  379. $content .= " subject=\"$subject\"\n";
  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. );
  478. $comments = IkiWiki::preprocess_inline(@args);
  479. }
  480. if (defined $comments && length $comments) {
  481. $template->param(comments => $comments);
  482. }
  483. if ($open) {
  484. my $commenturl = IkiWiki::cgiurl(do => 'comment',
  485. page => $page);
  486. $template->param(commenturl => $commenturl);
  487. }
  488. }
  489. if ($template->query(name => 'commentuser')) {
  490. $template->param(commentuser =>
  491. $pagestate{$page}{comments}{commentuser});
  492. }
  493. if ($template->query(name => 'commentip')) {
  494. $template->param(commentip =>
  495. $pagestate{$page}{comments}{commentip});
  496. }
  497. if ($template->query(name => 'commentauthor')) {
  498. $template->param(commentauthor =>
  499. $pagestate{$page}{comments}{commentauthor});
  500. }
  501. if ($template->query(name => 'commentauthorurl')) {
  502. $template->param(commentauthorurl =>
  503. $pagestate{$page}{comments}{commentauthorurl});
  504. }
  505. } # }}}
  506. package IkiWiki::PageSpec;
  507. sub match_postcomment ($$;@) {
  508. my $page = shift;
  509. my $glob = shift;
  510. unless ($page =~ s/\[postcomment\]$//) {
  511. return IkiWiki::FailReason->new("not posting a comment");
  512. }
  513. return match_glob($page, $glob);
  514. }
  515. 1