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